Skip to content

Commit 7519dce

Browse files
committed
feat(server): syntax error as you type
1 parent f9de5e0 commit 7519dce

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

server/src/server.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ let projectsFiles: Map<
5050
let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
5151

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

5555
interface CreateInterfaceRequestParams {
5656
uri: string;
@@ -588,6 +588,33 @@ function format(msg: p.RequestMessage): Array<p.Message> {
588588
}
589589
}
590590

591+
const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => {
592+
const filePath = fileURLToPath(fileUri);
593+
const tmpname = utils.createFileInTempDir();
594+
fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" });
595+
596+
const items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck(
597+
filePath,
598+
[
599+
"diagnosticSyntax",
600+
tmpname
601+
],
602+
);
603+
604+
const notification: p.NotificationMessage = {
605+
jsonrpc: c.jsonrpcVersion,
606+
method: "textDocument/publishDiagnostics",
607+
params: {
608+
uri: fileUri,
609+
diagnostics: items ?? []
610+
}
611+
}
612+
613+
fs.unlink(tmpname, () => null);
614+
615+
send(notification)
616+
}
617+
591618
function createInterface(msg: p.RequestMessage): p.Message {
592619
let params = msg.params as CreateInterfaceRequestParams;
593620
let extension = path.extname(params.uri);
@@ -774,6 +801,7 @@ function onMessage(msg: p.Message) {
774801
} else if (msg.method === DidOpenTextDocumentNotification.method) {
775802
let params = msg.params as p.DidOpenTextDocumentParams;
776803
openedFile(params.textDocument.uri, params.textDocument.text);
804+
updateDiagnosticSyntax(params.textDocument.uri, params.textDocument.text);
777805
} else if (msg.method === DidChangeTextDocumentNotification.method) {
778806
let params = msg.params as p.DidChangeTextDocumentParams;
779807
let extName = path.extname(params.textDocument.uri);
@@ -787,6 +815,7 @@ function onMessage(msg: p.Message) {
787815
params.textDocument.uri,
788816
changes[changes.length - 1].text
789817
);
818+
updateDiagnosticSyntax(params.textDocument.uri, changes[changes.length - 1].text);
790819
}
791820
}
792821
} else if (msg.method === DidCloseTextDocumentNotification.method) {

server/src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,9 @@ export let parseCompilerLogOutput = (
603603
// 10 ┆
604604
} else if (line.startsWith(" ")) {
605605
// part of the actual diagnostics message
606-
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
607-
line.slice(2)
608-
);
606+
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
607+
line.slice(2)
608+
);
609609
} else if (line.trim() != "") {
610610
// We'll assume that everything else is also part of the diagnostics too.
611611
// Most of these should have been indented 2 spaces; sadly, some of them
@@ -635,7 +635,7 @@ export let parseCompilerLogOutput = (
635635
range,
636636
source: "ReScript",
637637
// remove start and end whitespaces/newlines
638-
message: diagnosticMessage.join("\n").trim() + "\n",
638+
message: diagnosticMessage.join("\n").trim(),
639639
};
640640

641641
// Check for potential code actions

0 commit comments

Comments
 (0)