Skip to content

Commit 0210fd2

Browse files
committed
Initial SendLogNotification
1 parent ddc23b0 commit 0210fd2

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@
212212
],
213213
"default": null,
214214
"description": "Path to the directory where platform-specific ReScript binaries are. You can use it if you haven't or don't want to use the installed ReScript from node_modules in your project."
215+
},
216+
"rescript.settings.logLevel": {
217+
"type": "string",
218+
"enum": ["error", "warning", "info", "log"],
219+
"default": "info",
220+
"description": "Controls the log level of the language server. Logs below this level will be filtered out."
215221
}
216222
}
217223
},

server/src/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface extensionConfiguration {
2626
enable?: boolean;
2727
};
2828
};
29+
logLevel: "error" | "warning" | "info" | "log"
2930
}
3031

3132
// All values here are temporary, and will be overridden as the server is
@@ -55,6 +56,7 @@ let config: { extensionConfiguration: extensionConfiguration } = {
5556
enable: true,
5657
},
5758
},
59+
logLevel: "info"
5860
},
5961
};
6062

server/src/server.ts

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,38 @@ import * as ic from "./incrementalCompilation";
2828
import config, { extensionConfiguration } from "./config";
2929
import { projectsFiles } from "./projectFiles";
3030

31+
const notificationLevelMap = new Map([
32+
["error", p.MessageType.Error],
33+
["warning", p.MessageType.Warning],
34+
["info", p.MessageType.Info],
35+
["log", p.MessageType.Log]
36+
])
37+
38+
/**
39+
* Sends an LSP log notification that will appear in the ReScript Language Server Output window in VSCode.
40+
* Other LSP clients will also receive this notification,
41+
* as we utilize LanguageClient in the VSCode extension, providing this functionality at no extra cost.
42+
*/
43+
function sendLogNotification(level: p.MessageType, message: string) {
44+
const currentLogLevel = notificationLevelMap.get(config.extensionConfiguration.logLevel) || p.MessageType.Info;
45+
46+
if (currentLogLevel >= level) {
47+
const logMessageParams: p.LogMessageParams = {
48+
type: level,
49+
message
50+
}
51+
const notificationMessage: p.NotificationMessage = {
52+
method: "window/logMessage",
53+
jsonrpc: c.jsonrpcVersion,
54+
params: logMessageParams
55+
}
56+
57+
if (send) {
58+
send(notificationMessage);
59+
}
60+
}
61+
}
62+
3163
// This holds client capabilities specific to our extension, and not necessarily
3264
// related to the LS protocol. It's for enabling/disabling features that might
3365
// work in one client, like VSCode, but perhaps not in others, like vim.
@@ -55,18 +87,18 @@ let stupidFileContentCache: Map<string, string> = new Map();
5587
let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
5688

5789
// will be properly defined later depending on the mode (stdio/node-rpc)
58-
let send: (msg: p.Message) => void = (_) => {};
90+
let send: (msg: p.Message) => void = (_) => { };
5991

6092
let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
6193
config.extensionConfiguration.binaryPath == null
6294
? lookup.findFilePathFromProjectRoot(
63-
projectRootPath,
64-
path.join(c.nodeModulesBinDir, c.rescriptBinName)
65-
)
95+
projectRootPath,
96+
path.join(c.nodeModulesBinDir, c.rescriptBinName)
97+
)
6698
: utils.findBinary(
67-
config.extensionConfiguration.binaryPath,
68-
c.rescriptBinName
69-
);
99+
config.extensionConfiguration.binaryPath,
100+
c.rescriptBinName
101+
);
70102

71103
let createInterfaceRequest = new v.RequestType<
72104
p.TextDocumentIdentifier,
@@ -333,9 +365,9 @@ let openedFile = (fileUri: string, fileContent: string) => {
333365
message:
334366
config.extensionConfiguration.binaryPath == null
335367
? `Can't find ReScript binary in ${path.join(
336-
projectRootPath,
337-
c.nodeModulesBinDir
338-
)} or parent directories. Did you install it? It's required to use "rescript" > 9.1`
368+
projectRootPath,
369+
c.nodeModulesBinDir
370+
)} or parent directories. Did you install it? It's required to use "rescript" > 9.1`
339371
: `Can't find ReScript binary in the directory ${config.extensionConfiguration.binaryPath}`,
340372
},
341373
};
@@ -419,6 +451,7 @@ export default function listen(useStdio = false) {
419451
send = (msg: p.Message) => process.send!(msg);
420452
process.on("message", onMessage);
421453
}
454+
utils.setSendLogNotification(sendLogNotification);
422455
}
423456

424457
function hover(msg: p.RequestMessage) {
@@ -1183,15 +1216,15 @@ function onMessage(msg: p.Message) {
11831216
inlayHintProvider: config.extensionConfiguration.inlayHints?.enable,
11841217
codeLensProvider: config.extensionConfiguration.codeLens
11851218
? {
1186-
workDoneProgress: false,
1187-
}
1219+
workDoneProgress: false,
1220+
}
11881221
: undefined,
11891222
signatureHelpProvider: config.extensionConfiguration.signatureHelp
11901223
?.enabled
11911224
? {
1192-
triggerCharacters: ["("],
1193-
retriggerCharacters: ["=", ","],
1194-
}
1225+
triggerCharacters: ["("],
1226+
retriggerCharacters: ["=", ","],
1227+
}
11951228
: undefined,
11961229
},
11971230
};
@@ -1202,6 +1235,8 @@ function onMessage(msg: p.Message) {
12021235
};
12031236
initialized = true;
12041237

1238+
sendLogNotification(p.MessageType.Info, `LSP Server started!`)
1239+
12051240
// Periodically pull configuration from the client.
12061241
pullConfigurationPeriodically = setInterval(() => {
12071242
askForAllCurrentConfiguration();

0 commit comments

Comments
 (0)