Skip to content

Commit 8d6000d

Browse files
zthcristianoc
authored andcommitted
basics for pulling config from the client
1 parent deac6bb commit 8d6000d

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

Diff for: package.json

+5-16
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,11 @@
121121
"type": "object",
122122
"title": "ReScript",
123123
"properties": {
124-
"languageServerExample.maxNumberOfProblems": {
125-
"scope": "resource",
126-
"type": "number",
127-
"default": 100,
128-
"description": "Controls the maximum number of problems produced by the server."
129-
},
130-
"languageServerExample.trace.server": {
131-
"scope": "window",
132-
"type": "string",
133-
"enum": [
134-
"off",
135-
"messages",
136-
"verbose"
137-
],
138-
"default": "off",
139-
"description": "Traces the communication between VS Code and the language server."
124+
"rescript.settings.askToStartBuild": {
125+
"scope": "language-overridable",
126+
"type": "boolean",
127+
"default": true,
128+
"description": "Whether you want the extension to prompt for autostarting a ReScript build if a project is opened with no build running."
140129
}
141130
}
142131
},

Diff for: server/src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ export let startBuildAction = "Start Build";
4949
// bsconfig defaults according configuration schema (https://rescript-lang.org/docs/manual/latest/build-configuration-schema)
5050
export let bsconfigModuleDefault = "commonjs";
5151
export let bsconfigSuffixDefault = ".js";
52+
53+
export let configurationRequestId = "rescript_configuration_request";

Diff for: server/src/server.ts

+63-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DidOpenTextDocumentNotification,
1111
DidChangeTextDocumentNotification,
1212
DidCloseTextDocumentNotification,
13+
DidChangeConfigurationNotification,
1314
} from "vscode-languageserver-protocol";
1415
import * as utils from "./utils";
1516
import * as codeActions from "./codeActions";
@@ -20,6 +21,14 @@ import { fileURLToPath } from "url";
2021
import { ChildProcess } from "child_process";
2122
import { WorkspaceEdit } from "vscode-languageserver";
2223

24+
interface extensionConfiguration {
25+
askToStartBuild: boolean;
26+
}
27+
let extensionConfiguration: extensionConfiguration = {
28+
askToStartBuild: true,
29+
};
30+
let pullConfigurationInterval: NodeJS.Timeout | null = null;
31+
2332
// https://microsoft.github.io/language-server-protocol/specification#initialize
2433
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
2534
let initialized = false;
@@ -183,7 +192,11 @@ let openedFile = (fileUri: string, fileContent: string) => {
183192
// check if .bsb.lock is still there. If not, start a bsb -w ourselves
184193
// because otherwise the diagnostics info we'll display might be stale
185194
let bsbLockPath = path.join(projectRootPath, c.bsbLock);
186-
if (firstOpenFileOfProject && !fs.existsSync(bsbLockPath)) {
195+
if (
196+
extensionConfiguration.askToStartBuild === true &&
197+
firstOpenFileOfProject &&
198+
!fs.existsSync(bsbLockPath)
199+
) {
187200
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
188201
// stale. Use that logic
189202
// TODO: close watcher when lang-server shuts down
@@ -268,6 +281,8 @@ if (process.argv.includes("--stdio")) {
268281
process.on("message", onMessage);
269282
}
270283

284+
askForAllCurrentConfiguration();
285+
271286
function hover(msg: p.RequestMessage) {
272287
let params = msg.params as p.HoverParams;
273288
let filePath = fileURLToPath(params.textDocument.uri);
@@ -412,6 +427,24 @@ function documentSymbol(msg: p.RequestMessage) {
412427
return response;
413428
}
414429

430+
function askForAllCurrentConfiguration() {
431+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_configuration
432+
let params: p.ConfigurationParams = {
433+
items: [
434+
{
435+
section: "rescript.settings",
436+
},
437+
],
438+
};
439+
let req: p.RequestMessage = {
440+
jsonrpc: c.jsonrpcVersion,
441+
id: c.configurationRequestId,
442+
method: p.ConfigurationRequest.type.method,
443+
params,
444+
};
445+
send(req);
446+
}
447+
415448
function semanticTokens(msg: p.RequestMessage) {
416449
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_semanticTokens
417450
let params = msg.params as p.SemanticTokensParams;
@@ -434,7 +467,6 @@ function completion(msg: p.RequestMessage) {
434467
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
435468
let params = msg.params as p.ReferenceParams;
436469
let filePath = fileURLToPath(params.textDocument.uri);
437-
let extension = path.extname(params.textDocument.uri);
438470
let code = getOpenedFileContent(params.textDocument.uri);
439471
let tmpname = utils.createFileInTempDir();
440472
fs.writeFileSync(tmpname, code, { encoding: "utf-8" });
@@ -743,7 +775,6 @@ function onMessage(msg: m.Message) {
743775
}
744776
} else if (msg.method === DidOpenTextDocumentNotification.method) {
745777
let params = msg.params as p.DidOpenTextDocumentParams;
746-
let extName = path.extname(params.textDocument.uri);
747778
openedFile(params.textDocument.uri, params.textDocument.text);
748779
} else if (msg.method === DidChangeTextDocumentNotification.method) {
749780
let params = msg.params as p.DidChangeTextDocumentParams;
@@ -763,6 +794,9 @@ function onMessage(msg: m.Message) {
763794
} else if (msg.method === DidCloseTextDocumentNotification.method) {
764795
let params = msg.params as p.DidCloseTextDocumentParams;
765796
closedFile(params.textDocument.uri);
797+
} else if (msg.method === DidChangeConfigurationNotification.type.method) {
798+
// Can't seem to get this notification to trigger, but if it does this will be here and ensure we're synced up at the server.
799+
askForAllCurrentConfiguration();
766800
}
767801
} else if (m.isRequestMessage(msg)) {
768802
// request message, aka client sent request and waits for our mandatory reply
@@ -820,6 +854,15 @@ function onMessage(msg: m.Message) {
820854
result: result,
821855
};
822856
initialized = true;
857+
858+
// Periodically pull configuration from the client.
859+
pullConfigurationInterval = setInterval(() => {
860+
askForAllCurrentConfiguration();
861+
}, 10_000);
862+
863+
// Pull config right away as we've initied.
864+
askForAllCurrentConfiguration();
865+
823866
send(response);
824867
} else if (msg.method === "initialized") {
825868
// sent from client after initialize. Nothing to do for now
@@ -847,6 +890,10 @@ function onMessage(msg: m.Message) {
847890
stopWatchingCompilerLog();
848891
// TODO: delete bsb watchers
849892

893+
if (pullConfigurationInterval != null) {
894+
clearInterval(pullConfigurationInterval);
895+
}
896+
850897
let response: m.ResponseMessage = {
851898
jsonrpc: c.jsonrpcVersion,
852899
id: msg.id,
@@ -893,11 +940,19 @@ function onMessage(msg: m.Message) {
893940
send(response);
894941
}
895942
} else if (m.isResponseMessage(msg)) {
896-
// response message. Currently the client should have only sent a response
897-
// for asking us to start the build (see window/showMessageRequest in this
898-
// file)
899-
900-
if (
943+
if (msg.id === c.configurationRequestId) {
944+
if (msg.result != null) {
945+
// This is a response from a request to get updated configuration. Note
946+
// that it seems to return the configuration in a way that lets the
947+
// current workspace settings override the user settings. This is good
948+
// as we get started, but _might_ be problematic further down the line
949+
// if we want to support having several projects open at the same time
950+
// without their settings overriding eachother. Not a problem now though
951+
// as we'll likely only have "global" settings starting out.
952+
let [configuration] = msg.result as [extensionConfiguration];
953+
extensionConfiguration = configuration;
954+
}
955+
} else if (
901956
msg.result != null &&
902957
// @ts-ignore
903958
msg.result.title != null &&

0 commit comments

Comments
 (0)