chore: more work on the lsp

This commit is contained in:
Andre Henriques 2024-02-29 15:39:13 +00:00
parent 7b1e9e063e
commit d2d5a279ad
5 changed files with 511 additions and 434 deletions

View File

@ -15,6 +15,6 @@ const res = parseLsp(file);
fs.writeFileSync('./res', res.text); fs.writeFileSync('./res', res.text);
//const diag = await diagnosticsRequests(res); //const diag = await diagnosticsRequests(res);
const diag = await getDiagnostics(file); //const diag = await getDiagnostics(file);
//add_personal(['AutoML']); //add_personal(['AutoML']);

View File

@ -6,7 +6,7 @@ export async function add_personal(words: string[]): Promise<void> {
method: 'POST', method: 'POST',
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',
'authorization': process.env.AUTHORIZATION ?? '', 'authorization': process.env.AUTHORIZATION_MY_LTEX ?? '',
'content-type': 'application/json', 'content-type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({

14
lsp.ts
View File

@ -187,11 +187,19 @@ async function handleJSON(req: RPCRequest) {
const character = req.params.range.start.character; const character = req.params.range.start.character;
const line = req.params.range.start.line; const line = req.params.range.start.line;
if (!req.params.context?.diagnostics[0]) {
sendRPCMessage(req, [{
title: "No diagnostics found in this file"
}]);
return;
}
let item: GetDiagnosticsReturn | undefined = undefined; let item: GetDiagnosticsReturn | undefined = undefined;
console.log(JSON.stringify(req.params)); console.log(JSON.stringify(req.params) + "\n\n\n");
console.log(JSON.stringify(req.params.context.diagnostics[0]) + "\n\n\n");
if (!req.params.context?.diagnostics || !req.params.context?.diagnostics[0] || typeof req.params.context?.diagnostics[0].data != 'number') { if (typeof req.params.context?.diagnostics[0].data != 'number') {
for (const _item of saved) { for (const _item of saved) {
if (_item.range.end.line >= line && _item.range.start.line <= line) { if (_item.range.end.line >= line && _item.range.start.line <= line) {
if (_item.range.end.character >= character && _item.range.start.character <= line) { if (_item.range.end.character >= character && _item.range.start.character <= line) {
@ -236,7 +244,7 @@ async function handleJSON(req: RPCRequest) {
changes: { changes: {
[reqO.params.textDocument.uri]: [ [reqO.params.textDocument.uri]: [
{ {
range: item!.range, range: reqO.params.context.diagnostics[0].range,
newText: value newText: value
} }
] ]

View File

@ -2,12 +2,24 @@ import { error, handleData, log } from "./lsp";
console.log = log; console.log = log;
log('Started the server logging');
try {
log('Listening for Input')
process.stdin.resume(); process.stdin.resume();
} catch (e: any) {
log('failed to listen to the stdin');
log(e.toString());
throw e;
}
process.stdin.on('data', function(data) { process.stdin.on('data', function(data) {
log('Got the data function');
try { try {
handleData(data); handleData(data);
} catch (e) { } catch (e) {
log("got here")
error("TRY GOT ERROR:" + e) error("TRY GOT ERROR:" + e)
} }
}) })

View File

@ -2,7 +2,7 @@ import * as fs from 'fs';
import process from 'process'; import process from 'process';
import { Severity, type Dialog } from './lsp'; import { Severity, type Dialog } from './lsp';
import { createSourceFile } from 'typescript'; import { OperationCanceledException } from 'typescript';
type ParseResultConversion = { type ParseResultConversion = {
@ -33,13 +33,13 @@ function parseComment(text: string, curPos: number): number {
function createPartition(text: string, startPos: number, curPos: number, result: string, ignoreLast: number = 0): [ParseResultConversion[], string] | null { function createPartition(text: string, startPos: number, curPos: number, result: string, ignoreLast: number = 0): [ParseResultConversion[], string] | null {
curPos = curPos - ignoreLast; curPos = curPos - ignoreLast;
if (startPos >= curPos || text.substring(startPos, curPos).match(/^\s*$/)) { if (startPos >= curPos || text.substring(startPos, curPos + 1).match(/^\s*$/)) {
return null; return null;
} }
var t = text.substring(startPos, curPos); var t = text.substring(startPos, curPos + 1);
if (t.indexOf('\n') == -1) { if (!t.includes('\n')) {
return [[{ return [[{
length: curPos - startPos, length: curPos - startPos,
position: result.length, position: result.length,
@ -207,18 +207,36 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
case 'includegraphics': case 'includegraphics':
case 'appendix': case 'appendix':
case 'printbibliography': case 'printbibliography':
case 'subsection*': case 'vspace*':
case 'section*': case 'pagebreak':
case 'today':
case 'label':
return [len]; return [len];
case 'title': case 'title':
case 'author': case 'author':
case 'end': case 'end':
case 'ref':
case 'caption':
case 'footnote':
console.log("TODO: add way to check the " + commandName) console.log("TODO: add way to check the " + commandName)
return [len]; return [len];
case 'cite': case 'cite':
console.log("TODO check if it exists on the bibliography"); console.log("TODO check if it exists on the bibliography");
return [len]; console.log(`Find cite for '${args[0]}'`);
let toAdd = "[0]"
return [len, {
length: toAdd.length,
original_length: len,
original_position: curPos,
position: result.length,
type: 'text'
}, toAdd];
// return [len];
case 'begin': case 'begin':
@ -259,6 +277,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
type: 'text' type: 'text'
}, "— "]; }, "— "];
case 'section*':
case 'section': case 'section':
return [len, { return [len, {
length: args[0].length + 1, length: args[0].length + 1,
@ -268,6 +287,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
type: 'h1', type: 'h1',
}, args[0] + '\n'] }, args[0] + '\n']
case 'subsection*':
case 'subsection': case 'subsection':
return [len, { return [len, {
length: args[0].length + 1, length: args[0].length + 1,
@ -279,7 +299,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
default: default:
console.log("Command name: " + commandName + " options: " + options + " args: " + args); console.log("Command name: " + commandName + " options: " + options + " args: " + args);
throw new Error("TODO handle this case"); throw new Error("TODO handle this case " + commandName);
} }
@ -330,6 +350,8 @@ export function parseLsp(text: string): ParseResult {
const possiblePartition = createPartition(text, conversionStartPosition, i - 1, result.text); const possiblePartition = createPartition(text, conversionStartPosition, i - 1, result.text);
console.log(possiblePartition)
if (possiblePartition) { if (possiblePartition) {
const [conv, toAdd] = possiblePartition; const [conv, toAdd] = possiblePartition;
result.conversions = result.conversions.concat(conv); result.conversions = result.conversions.concat(conv);
@ -356,7 +378,7 @@ export function parseLsp(text: string): ParseResult {
throw new Error("Handle double math expression"); throw new Error("Handle double math expression");
} }
const possiblePartition = createPartition(text, conversionStartPosition, i, result.text); const possiblePartition = createPartition(text, conversionStartPosition, i - 1, result.text);
if (possiblePartition) { if (possiblePartition) {
const [conv, toAdd] = possiblePartition; const [conv, toAdd] = possiblePartition;
@ -366,7 +388,7 @@ export function parseLsp(text: string): ParseResult {
const len = readUntil(text, '$', i + 1); const len = readUntil(text, '$', i + 1);
let to_add = 'mathexpr' + (text[i + len + 1 + 1] === ' ' ? ' ' : ''); let to_add = 'mathexpr' + (text[i + len + 1] === ' ' ? ' ' : '');
result.conversions = result.conversions.concat( result.conversions = result.conversions.concat(
[{ [{
@ -380,6 +402,41 @@ export function parseLsp(text: string): ParseResult {
i += len + 1; i += len + 1;
conversionStartPosition = i + 1;
} else if (char == '`' || char == "'") {
console.log('Found coutes')
if (text[i + 1] !== char) {
continue;
}
const possiblePartition = createPartition(text, conversionStartPosition, i - 1, result.text);
if (possiblePartition) {
const [conv, toAdd] = possiblePartition;
result.conversions = result.conversions.concat(conv);
result.text += toAdd;
}
let to_add = '"';
if (char == '`') {
to_add = "“"
} else if (char == "'") {
to_add = "”"
}
result.conversions = result.conversions.concat(
[{
length: to_add.length,
position: result.text.length,
original_position: i,
type: 'text',
}]
);
result.text += to_add;
i += 1;
conversionStartPosition = i + 1; conversionStartPosition = i + 1;
} else { } else {
//console.log(char); //console.log(char);
@ -484,8 +541,8 @@ export async function diagnosticsRequests(res: ParseResult): Promise<Match[]> {
formData.set('text', res.text); formData.set('text', res.text);
formData.set('language', 'en-GB'); formData.set('language', 'en-GB');
formData.set('username', process.env.USERNAME ?? ''); formData.set('username', process.env.USERNAME_MY_LTEX ?? '');
formData.set('apiKey', process.env.APIKEY ?? ''); formData.set('apiKey', process.env.APIKEY_MY_LTEX ?? '');
formData.set('level', 'picky'); formData.set('level', 'picky');
const rawRes = await fetch('https://api.languagetoolplus.com/v2/check', { const rawRes = await fetch('https://api.languagetoolplus.com/v2/check', {