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 process from 'process';
import { getDiagnostics, parseLsp } from './parser';
import { diagnosticsRequests, getDiagnostics, parseLsp } from './parser';
if (process.argv.length !== 3) {
console.log("Please provide only one pass");
@ -13,4 +13,4 @@ const res = parseLsp(file);
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 = {
line: number,
character: number
@ -104,6 +114,8 @@ export enum Severity {
Hint = 4,
}
const saveRes: Record<string, (Dialog & {replacements: string[]})[]> = { };
async function handleJSON(req: RPCRequest) {
log(`New Message entered: method: ${req.method}`);
@ -119,15 +131,37 @@ async function handleJSON(req: RPCRequest) {
return;
}
const diags = await getDiagnostics(reqO.params.textDocument.text);
saveRes[reqO.params.textDocument.uri] = diags;
const params = {
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);
//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 {
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
interFileDependencies: false,
workspaceDiagnostics: false,
},
codeActionProvider: {
codeActionKinds: [ "quickfix" ]
}
},
serverInfo: {

View File

@ -388,33 +388,7 @@ function buildLineIndex(file: string) {
}
return lineIndex;
}
export async function getDiagnostics(file: string): Promise<Dialog[]> {
const res = parseLsp(file);
const formData = new URLSearchParams();
formData.set('text', res.text);
formData.set('language', 'en-GB');
formData.set('username', process.env.USERNAME ?? '');
formData.set('apiKey', process.env.APIKEY ?? '');
formData.set('level', 'picky');
const rawRes = await fetch('https://api.languagetoolplus.com/v2/check', {
method: 'POST',
headers: {
'Accept': 'application/json',
},
body: formData,
});
if (rawRes.status !== 200) {
process.exit(2);
}
const body = await rawRes.json();
type Match = {
type Match = {
message: string,
shortMessage: string,
offset: number,
@ -441,13 +415,45 @@ export async function getDiagnostics(file: string): Promise<Dialog[]> {
name: string
}
}
}
export async function diagnosticsRequests(res: ParseResult): Promise < Match[] > {
const formData = new URLSearchParams();
formData.set('text', res.text);
formData.set('language', 'en-GB');
formData.set('username', process.env.USERNAME ?? '');
formData.set('apiKey', process.env.APIKEY ?? '');
formData.set('level', 'picky');
const rawRes = await fetch('https://api.languagetoolplus.com/v2/check', {
method: 'POST',
headers: {
'Accept': 'application/json',
},
body: formData,
});
if (rawRes.status !== 200) {
process.exit(2);
}
const body = await rawRes.json();
return body.matches;
}
export async function getDiagnostics(file: string): Promise<(Dialog & {replacements: string[]})[]> {
const res = parseLsp(file);
const matches = await diagnosticsRequests(res);
const lineIndex = buildLineIndex(file);
const diags = [];
for (const i of body.matches) {
for (const i of matches) {
const match: Match = i;
const original_position = getOriginalPostion(res, match.offset);
if (original_position == -1) {
@ -466,6 +472,7 @@ export async function getDiagnostics(file: string): Promise<Dialog[]> {
range,
severity: Severity.Error,
message: match.message,
replacements: match.replacements.map(a => a.value),
})
}