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);
//const diag = await diagnosticsRequests(res);
const diag = await getDiagnostics(file);
//const diag = await getDiagnostics(file);
//add_personal(['AutoML']);

View File

@ -6,7 +6,7 @@ export async function add_personal(words: string[]): Promise<void> {
method: 'POST',
headers: {
'Accept': 'application/json',
'authorization': process.env.AUTHORIZATION ?? '',
'authorization': process.env.AUTHORIZATION_MY_LTEX ?? '',
'content-type': 'application/json',
},
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 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;
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) {
if (_item.range.end.line >= line && _item.range.start.line <= line) {
if (_item.range.end.character >= character && _item.range.start.character <= line) {
@ -236,7 +244,7 @@ async function handleJSON(req: RPCRequest) {
changes: {
[reqO.params.textDocument.uri]: [
{
range: item!.range,
range: reqO.params.context.diagnostics[0].range,
newText: value
}
]

View File

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

View File

@ -2,7 +2,7 @@ import * as fs from 'fs';
import process from 'process';
import { Severity, type Dialog } from './lsp';
import { createSourceFile } from 'typescript';
import { OperationCanceledException } from 'typescript';
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 {
curPos = curPos - ignoreLast;
if (startPos >= curPos || text.substring(startPos, curPos).match(/^\s*$/)) {
if (startPos >= curPos || text.substring(startPos, curPos + 1).match(/^\s*$/)) {
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 [[{
length: curPos - startPos,
position: result.length,
@ -207,18 +207,36 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
case 'includegraphics':
case 'appendix':
case 'printbibliography':
case 'subsection*':
case 'section*':
case 'vspace*':
case 'pagebreak':
case 'today':
case 'label':
return [len];
case 'title':
case 'author':
case 'end':
case 'ref':
case 'caption':
case 'footnote':
console.log("TODO: add way to check the " + commandName)
return [len];
case 'cite':
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':
@ -259,6 +277,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
type: 'text'
}, "— "];
case 'section*':
case 'section':
return [len, {
length: args[0].length + 1,
@ -268,6 +287,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
type: 'h1',
}, args[0] + '\n']
case 'subsection*':
case 'subsection':
return [len, {
length: args[0].length + 1,
@ -279,7 +299,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
default:
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);
console.log(possiblePartition)
if (possiblePartition) {
const [conv, toAdd] = possiblePartition;
result.conversions = result.conversions.concat(conv);
@ -356,7 +378,7 @@ export function parseLsp(text: string): ParseResult {
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) {
const [conv, toAdd] = possiblePartition;
@ -366,7 +388,7 @@ export function parseLsp(text: string): ParseResult {
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(
[{
@ -380,6 +402,41 @@ export function parseLsp(text: string): ParseResult {
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;
} else {
//console.log(char);
@ -484,8 +541,8 @@ export async function diagnosticsRequests(res: ParseResult): Promise<Match[]> {
formData.set('text', res.text);
formData.set('language', 'en-GB');
formData.set('username', process.env.USERNAME ?? '');
formData.set('apiKey', process.env.APIKEY ?? '');
formData.set('username', process.env.USERNAME_MY_LTEX ?? '');
formData.set('apiKey', process.env.APIKEY_MY_LTEX ?? '');
formData.set('level', 'picky');
const rawRes = await fetch('https://api.languagetoolplus.com/v2/check', {