Skip to content

Commit 7de9585

Browse files
committed
Initial SendLogNotification
1 parent 79992f1 commit 7de9585

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
@@ -206,6 +206,12 @@
206206
],
207207
"default": null,
208208
"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."
209+
},
210+
"rescript.settings.logLevel": {
211+
"type": "string",
212+
"enum": ["error", "warning", "info", "log"],
213+
"default": "info",
214+
"description": "Controls the log level of the language server. Logs below this level will be filtered out."
209215
}
210216
}
211217
},

server/src/config.ts

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

3031
// All values here are temporary, and will be overridden as the server is
@@ -53,6 +54,7 @@ let config: { extensionConfiguration: extensionConfiguration } = {
5354
enable: true,
5455
},
5556
},
57+
logLevel: "info"
5658
},
5759
};
5860

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.
@@ -54,18 +86,18 @@ let stupidFileContentCache: Map<string, string> = new Map();
5486
let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
5587

5688
// will be properly defined later depending on the mode (stdio/node-rpc)
57-
let send: (msg: p.Message) => void = (_) => {};
89+
let send: (msg: p.Message) => void = (_) => { };
5890

5991
let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
6092
config.extensionConfiguration.binaryPath == null
6193
? lookup.findFilePathFromProjectRoot(
62-
projectRootPath,
63-
path.join(c.nodeModulesBinDir, c.rescriptBinName)
64-
)
94+
projectRootPath,
95+
path.join(c.nodeModulesBinDir, c.rescriptBinName)
96+
)
6597
: utils.findBinary(
66-
config.extensionConfiguration.binaryPath,
67-
c.rescriptBinName
68-
);
98+
config.extensionConfiguration.binaryPath,
99+
c.rescriptBinName
100+
);
69101

70102
let createInterfaceRequest = new v.RequestType<
71103
p.TextDocumentIdentifier,
@@ -332,9 +364,9 @@ let openedFile = (fileUri: string, fileContent: string) => {
332364
message:
333365
config.extensionConfiguration.binaryPath == null
334366
? `Can't find ReScript binary in ${path.join(
335-
projectRootPath,
336-
c.nodeModulesBinDir
337-
)} or parent directories. Did you install it? It's required to use "rescript" > 9.1`
367+
projectRootPath,
368+
c.nodeModulesBinDir
369+
)} or parent directories. Did you install it? It's required to use "rescript" > 9.1`
338370
: `Can't find ReScript binary in the directory ${config.extensionConfiguration.binaryPath}`,
339371
},
340372
};
@@ -418,6 +450,7 @@ export default function listen(useStdio = false) {
418450
send = (msg: p.Message) => process.send!(msg);
419451
process.on("message", onMessage);
420452
}
453+
utils.setSendLogNotification(sendLogNotification);
421454
}
422455

423456
function hover(msg: p.RequestMessage) {
@@ -1158,15 +1191,15 @@ function onMessage(msg: p.Message) {
11581191
inlayHintProvider: config.extensionConfiguration.inlayHints?.enable,
11591192
codeLensProvider: config.extensionConfiguration.codeLens
11601193
? {
1161-
workDoneProgress: false,
1162-
}
1194+
workDoneProgress: false,
1195+
}
11631196
: undefined,
11641197
signatureHelpProvider: config.extensionConfiguration.signatureHelp
11651198
?.enabled
11661199
? {
1167-
triggerCharacters: ["("],
1168-
retriggerCharacters: ["=", ","],
1169-
}
1200+
triggerCharacters: ["("],
1201+
retriggerCharacters: ["=", ","],
1202+
}
11701203
: undefined,
11711204
},
11721205
};
@@ -1177,6 +1210,8 @@ function onMessage(msg: p.Message) {
11771210
};
11781211
initialized = true;
11791212

1213+
sendLogNotification(p.MessageType.Info, `LSP Server started!`)
1214+
11801215
// Periodically pull configuration from the client.
11811216
pullConfigurationPeriodically = setInterval(() => {
11821217
askForAllCurrentConfiguration();

0 commit comments

Comments
 (0)