More work on the lsp

This commit is contained in:
Andre Henriques 2023-12-26 20:01:14 +00:00
parent 57682ed72b
commit 11e7c303e3
3 changed files with 80 additions and 36 deletions

View File

@ -1,6 +1,6 @@
import fs from 'fs'; import fs from 'fs';
import process from 'process'; import process from 'process';
import { getDiagnostics, parseLsp } from './parser'; import { diagnosticsRequests, getDiagnostics, parseLsp } from './parser';
if (process.argv.length !== 3) { if (process.argv.length !== 3) {
console.log("Please provide only one pass"); console.log("Please provide only one pass");
@ -13,4 +13,4 @@ const res = parseLsp(file);
fs.writeFileSync('./res', res.text); fs.writeFileSync('./res', res.text);
console.log(await getDiagnostics(file)); console.log((await diagnosticsRequests(res)).map(a => a.replacements));

41
lsp.ts
View File

@ -81,6 +81,16 @@ type TextDocumentDidOpen = RPCRequest & {
} }
} }
type TextDocumentCodeAction = RPCRequest & {
method: 'textDocument/codeAction',
params: {
range: Range,
textDocument: {
uri: string,
}
},
};
type Position = { type Position = {
line: number, line: number,
character: number character: number
@ -104,6 +114,8 @@ export enum Severity {
Hint = 4, Hint = 4,
} }
const saveRes: Record<string, (Dialog & {replacements: string[]})[]> = { };
async function handleJSON(req: RPCRequest) { async function handleJSON(req: RPCRequest) {
log(`New Message entered: method: ${req.method}`); log(`New Message entered: method: ${req.method}`);
@ -119,15 +131,37 @@ async function handleJSON(req: RPCRequest) {
return; return;
} }
const diags = await getDiagnostics(reqO.params.textDocument.text);
saveRes[reqO.params.textDocument.uri] = diags;
const params = { const params = {
uri: reqO.params.textDocument.uri, uri: reqO.params.textDocument.uri,
diagnostics: (await getDiagnostics(reqO.params.textDocument.text)), diagnostics: diags.map(a => ({
message: a.message,
range: a.range,
severity: a.severity,
}) as Dialog),
} }
notifyRPC('textDocument/publishDiagnostics', params); notifyRPC('textDocument/publishDiagnostics', params);
//error(`TODO ${req.method}`); //error(`TODO ${req.method}`);
} else if (req.method === 'textDocument/codeAction') {
const reqO: TextDocumentCodeAction = req as any;
const diagRecord = saveRes[reqO.params.textDocument.uri];
if (!diagRecord) {
sendRPCMessage(req, [{
title: "No diagnostics found in this file"
}]);
return;
}
log(JSON.stringify(req.params));
error("TODO handle " + req.method);
} else { } else {
error(`Handle: ${req.method} after init`); error(`Handle: ${req.method} after init`);
} }
@ -145,6 +179,9 @@ async function handleJSON(req: RPCRequest) {
// TODO change in the future when the server also checks the ib file as well // TODO change in the future when the server also checks the ib file as well
interFileDependencies: false, interFileDependencies: false,
workspaceDiagnostics: false, workspaceDiagnostics: false,
},
codeActionProvider: {
codeActionKinds: [ "quickfix" ]
} }
}, },
serverInfo: { serverInfo: {

View File

@ -388,11 +388,37 @@ function buildLineIndex(file: string) {
} }
return lineIndex; return lineIndex;
} }
type Match = {
message: string,
shortMessage: string,
offset: number,
length: number,
replacements: {
value: string
}[],
context: {
text: string,
offset: number,
length: number
},
sentence: string,
rule: {
id: string,
subId: string,
description: string,
urls: {
value: string
}[],
issueType: string,
category: {
id: string,
name: string
}
}
}
export async function getDiagnostics(file: string): Promise<Dialog[]> {
const res = parseLsp(file);
export async function diagnosticsRequests(res: ParseResult): Promise < Match[] > {
const formData = new URLSearchParams(); const formData = new URLSearchParams();
formData.set('text', res.text); formData.set('text', res.text);
@ -414,40 +440,20 @@ export async function getDiagnostics(file: string): Promise<Dialog[]> {
} }
const body = await rawRes.json(); const body = await rawRes.json();
type Match = { return body.matches;
message: string, }
shortMessage: string,
offset: number, export async function getDiagnostics(file: string): Promise<(Dialog & {replacements: string[]})[]> {
length: number,
replacements: { const res = parseLsp(file);
value: string
}[], const matches = await diagnosticsRequests(res);
context: {
text: string,
offset: number,
length: number
},
sentence: string,
rule: {
id: string,
subId: string,
description: string,
urls: {
value: string
}[],
issueType: string,
category: {
id: string,
name: string
}
}
}
const lineIndex = buildLineIndex(file); const lineIndex = buildLineIndex(file);
const diags = []; const diags = [];
for (const i of body.matches) { for (const i of matches) {
const match: Match = i; const match: Match = i;
const original_position = getOriginalPostion(res, match.offset); const original_position = getOriginalPostion(res, match.offset);
if (original_position == -1) { if (original_position == -1) {
@ -466,6 +472,7 @@ export async function getDiagnostics(file: string): Promise<Dialog[]> {
range, range,
severity: Severity.Error, severity: Severity.Error,
message: match.message, message: match.message,
replacements: match.replacements.map(a => a.value),
}) })
} }