Skip to content
31 changes: 30 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let projectsFiles: Map<
let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};

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

interface CreateInterfaceRequestParams {
uri: string;
Expand Down Expand Up @@ -588,6 +588,33 @@ function format(msg: p.RequestMessage): Array<p.Message> {
}
}

const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => {
const filePath = fileURLToPath(fileUri);
const tmpname = utils.createFileInTempDir();
fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" });

const items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck(
filePath,
[
"diagnosticSyntax",
tmpname
],
);

const notification: p.NotificationMessage = {
jsonrpc: c.jsonrpcVersion,
method: "textDocument/publishDiagnostics",
params: {
uri: fileUri,
diagnostics: items ?? []
}
}

fs.unlink(tmpname, () => null);

send(notification)
}

function createInterface(msg: p.RequestMessage): p.Message {
let params = msg.params as CreateInterfaceRequestParams;
let extension = path.extname(params.uri);
Expand Down Expand Up @@ -774,6 +801,7 @@ function onMessage(msg: p.Message) {
} else if (msg.method === DidOpenTextDocumentNotification.method) {
let params = msg.params as p.DidOpenTextDocumentParams;
openedFile(params.textDocument.uri, params.textDocument.text);
updateDiagnosticSyntax(params.textDocument.uri, params.textDocument.text);
} else if (msg.method === DidChangeTextDocumentNotification.method) {
let params = msg.params as p.DidChangeTextDocumentParams;
let extName = path.extname(params.textDocument.uri);
Expand All @@ -787,6 +815,7 @@ function onMessage(msg: p.Message) {
params.textDocument.uri,
changes[changes.length - 1].text
);
updateDiagnosticSyntax(params.textDocument.uri, changes[changes.length - 1].text);
}
}
} else if (msg.method === DidCloseTextDocumentNotification.method) {
Expand Down
8 changes: 4 additions & 4 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,9 @@ export let parseCompilerLogOutput = (
// 10 ┆
} else if (line.startsWith(" ")) {
// part of the actual diagnostics message
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
line.slice(2)
);
parsedDiagnostics[parsedDiagnostics.length - 1].content.push(
line.slice(2)
);
} else if (line.trim() != "") {
// We'll assume that everything else is also part of the diagnostics too.
// Most of these should have been indented 2 spaces; sadly, some of them
Expand Down Expand Up @@ -635,7 +635,7 @@ export let parseCompilerLogOutput = (
range,
source: "ReScript",
// remove start and end whitespaces/newlines
message: diagnosticMessage.join("\n").trim() + "\n",
message: diagnosticMessage.join("\n").trim(),
};

// Check for potential code actions
Expand Down