Skip to content

Commit 3369afe

Browse files
committed
Cleanup
1 parent 49ef881 commit 3369afe

File tree

3 files changed

+58
-48
lines changed

3 files changed

+58
-48
lines changed

client/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ export function activate(context: ExtensionContext) {
9393

9494
// Create the language client and start the client.
9595
client = new LanguageClient(
96-
'languageServerExample',
97-
'Language Server Example',
96+
'ReScriptLSP',
97+
'ReScript Language Server',
9898
serverOptions,
9999
clientOptions
100100
);

server/src/server.ts

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import process, { allowedNodeEnvironmentFlags } from "process";
1+
import process from "process";
22
import * as p from "vscode-languageserver-protocol";
33
import * as t from "vscode-languageserver-types";
44
import * as j from "vscode-jsonrpc";
55
import * as m from "vscode-jsonrpc/lib/messages";
66
import * as v from "vscode-languageserver";
77
import * as path from 'path';
88
import fs from 'fs';
9-
// TODO: check DidChangeWatchedFilesNotification. Check DidChangeTextDocumentNotification. Do they fire on uninitialized files?
9+
// TODO: check DidChangeWatchedFilesNotification.
1010
import { DidOpenTextDocumentNotification, DidChangeTextDocumentNotification, DidCloseTextDocumentNotification, DidChangeWatchedFilesNotification, CompletionResolveRequest } from 'vscode-languageserver-protocol';
1111
import * as utils from './utils';
1212
import * as c from './constants';
1313
import * as chokidar from 'chokidar'
1414
import { assert } from 'console';
15-
// TODO: what's this?
1615
import { fileURLToPath } from 'url';
1716

1817
// https://microsoft.github.io/language-server-protocol/specification#initialize
@@ -106,7 +105,7 @@ let openedFile = (fileUri: string, fileContent: string) => {
106105

107106
stupidFileContentCache.set(filePath, fileContent)
108107

109-
let projectRootPath = utils.findDirOfFileNearFile(c.bsconfigPartialPath, filePath)
108+
let projectRootPath = utils.findProjectRootOfFile(filePath)
110109
if (projectRootPath != null) {
111110
if (!projectsFiles.has(projectRootPath)) {
112111
projectsFiles.set(projectRootPath, { openFiles: new Set(), filesWithDiagnostics: new Set() })
@@ -123,7 +122,7 @@ let closedFile = (fileUri: string) => {
123122

124123
stupidFileContentCache.delete(filePath)
125124

126-
let projectRootPath = utils.findDirOfFileNearFile(c.bsconfigPartialPath, filePath)
125+
let projectRootPath = utils.findProjectRootOfFile(filePath)
127126
if (projectRootPath != null) {
128127
let root = projectsFiles.get(projectRootPath)
129128
if (root != null) {
@@ -155,7 +154,6 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
155154
if (!initialized && aa.method !== 'exit') {
156155
// From spec: "Notifications should be dropped, except for the exit notification. This will allow the exit of a server without an initialize request"
157156
// For us: do nothing. We don't have anything we need to clean up right now
158-
// TODO: think of fs watcher
159157
} else if (aa.method === 'exit') {
160158
// The server should exit with success code 0 if the shutdown request has been received before; otherwise with error code 1
161159
if (shutdownRequestAlreadyReceived) {
@@ -200,7 +198,6 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
200198
};
201199
process.send!(response);
202200
} else if (aa.method === 'initialize') {
203-
// startWatchingCompilerLog(process)
204201
// send the list of things we support
205202
let result: p.InitializeResult = {
206203
capabilities: {
@@ -263,58 +260,71 @@ process.on('message', (a: (m.RequestMessage | m.NotificationMessage)) => {
263260
};
264261
process.send!(response);
265262
} else {
266-
let nodeModulesParentPath = utils.findDirOfFileNearFile(c.bscPartialPath, filePath)
267-
if (nodeModulesParentPath == null) {
263+
let projectRootPath = utils.findProjectRootOfFile(filePath)
264+
if (projectRootPath == null) {
268265
let response: m.ResponseMessage = {
269266
jsonrpc: c.jsonrpcVersion,
270267
id: aa.id,
271268
error: {
272269
code: m.ErrorCodes.InvalidRequest,
273-
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for formatting.`,
270+
message: `Cannot find a nearby ${c.bsconfigPartialPath}. It's needed for determining the project's root.`,
274271
}
275272
};
276273
process.send!(response);
277274
} else {
278-
// code will always be defined here, even though technically it can be undefined
279-
let code = getOpenedFileContent(params.textDocument.uri)
280-
let formattedResult = utils.formatUsingValidBscPath(
281-
code,
282-
path.join(nodeModulesParentPath, c.bscPartialPath),
283-
extension === c.resiExt,
284-
);
285-
if (formattedResult.kind === 'success') {
286-
let result: p.TextEdit[] = [{
287-
range: {
288-
start: { line: 0, character: 0 },
289-
end: { line: Number.MAX_VALUE, character: Number.MAX_VALUE }
290-
},
291-
newText: formattedResult.result,
292-
}]
275+
let bscPath = path.join(projectRootPath, c.bscPartialPath)
276+
if (!fs.existsSync(bscPath)) {
293277
let response: m.ResponseMessage = {
294278
jsonrpc: c.jsonrpcVersion,
295279
id: aa.id,
296-
result: result,
280+
error: {
281+
code: m.ErrorCodes.InvalidRequest,
282+
message: `Cannot find a nearby ${c.bscPartialPath}. It's needed for formatting.`,
283+
}
297284
};
298285
process.send!(response);
299286
} else {
300-
let response: m.ResponseMessage = {
301-
jsonrpc: c.jsonrpcVersion,
302-
id: aa.id,
303-
result: [],
304-
// technically a formatting failure should return the error but
305-
// since this is LSP... the idiom seems to be to silently return
306-
// nothing (to avoid an alert window each time on bad formatting)
307-
// while sending a diagnostic about the error afterward. We won't
308-
// send an extra diagnostic because the .compiler.log watcher
309-
// should have reported th won't send an extra diagnostic because
310-
// theiler.log watcher should have reported them.
287+
// code will always be defined here, even though technically it can be undefined
288+
let code = getOpenedFileContent(params.textDocument.uri)
289+
let formattedResult = utils.formatUsingValidBscPath(
290+
code,
291+
bscPath,
292+
extension === c.resiExt,
293+
);
294+
if (formattedResult.kind === 'success') {
295+
let result: p.TextEdit[] = [{
296+
range: {
297+
start: { line: 0, character: 0 },
298+
end: { line: Number.MAX_VALUE, character: Number.MAX_VALUE }
299+
},
300+
newText: formattedResult.result,
301+
}]
302+
let response: m.ResponseMessage = {
303+
jsonrpc: c.jsonrpcVersion,
304+
id: aa.id,
305+
result: result,
306+
};
307+
process.send!(response);
308+
} else {
309+
let response: m.ResponseMessage = {
310+
jsonrpc: c.jsonrpcVersion,
311+
id: aa.id,
312+
result: [],
313+
// technically a formatting failure should return the error but
314+
// since this is LSP... the idiom seems to be to silently return
315+
// nothing (to avoid an alert window each time on bad formatting)
316+
// while sending a diagnostic about the error afterward. We won't
317+
// send an extra diagnostic because the .compiler.log watcher
318+
// should have reported th won't send an extra diagnostic because
319+
// theiler.log watcher should have reported them.
311320

312-
// error: {
313-
// code: m.ErrorCodes.ParseError,
314-
// message: formattedResult.error,
315-
// }
316-
};
317-
process.send!(response);
321+
// error: {
322+
// code: m.ErrorCodes.ParseError,
323+
// message: formattedResult.error,
324+
// }
325+
};
326+
process.send!(response);
327+
}
318328
}
319329
}
320330
}

server/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import fs from 'fs';
99

1010
// TODO: races here
1111
// TODO: this doesn't handle file:/// scheme
12-
export let findDirOfFileNearFile = (fileToFind: p.DocumentUri, source: p.DocumentUri): null | p.DocumentUri => {
12+
export let findProjectRootOfFile = (source: p.DocumentUri): null | p.DocumentUri => {
1313
let dir = path.dirname(source)
14-
if (fs.existsSync(path.join(dir, fileToFind))) {
14+
if (fs.existsSync(path.join(dir, c.bsconfigPartialPath))) {
1515
return dir
1616
} else {
1717
if (dir === source) {
1818
// reached top
1919
return null
2020
} else {
21-
return findDirOfFileNearFile(fileToFind, dir)
21+
return findProjectRootOfFile(dir)
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)