Update lsp
This commit is contained in:
107
parser.ts
107
parser.ts
@@ -1,7 +1,8 @@
|
||||
import fs from 'fs';
|
||||
import * as fs from 'fs';
|
||||
|
||||
import process from 'process';
|
||||
import { Severity, type Dialog } from './lsp';
|
||||
import { createSourceFile } from 'typescript';
|
||||
|
||||
|
||||
type ParseResultConversion = {
|
||||
@@ -30,7 +31,8 @@ function parseComment(text: string, curPos: number): number {
|
||||
return text.length - curPos;
|
||||
}
|
||||
|
||||
function createPartition(text: string, startPos: number, curPos: number, result: string): [ParseResultConversion[], string] | null {
|
||||
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*$/)) {
|
||||
return null;
|
||||
}
|
||||
@@ -74,7 +76,7 @@ function createPartition(text: string, startPos: number, curPos: number, result:
|
||||
}
|
||||
|
||||
function isChar(charCode: number): boolean {
|
||||
return (charCode >= 92 && charCode <= 122) || (charCode >= 65 && charCode <= 90);
|
||||
return (charCode >= 92 && charCode <= 122) || (charCode >= 65 && charCode <= 90) || charCode == 42;
|
||||
}
|
||||
function readBalanced(endChar: string, startChar: string, text: string, curPos: number): number {
|
||||
let bal = 1;
|
||||
@@ -104,19 +106,26 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
}
|
||||
|
||||
if (text[curPos + 1] === '\\') {
|
||||
return [1, {
|
||||
return [2, {
|
||||
length: 1,
|
||||
position: result.length,
|
||||
original_position: curPos + 1,
|
||||
type: 'text'
|
||||
}, '\\'];
|
||||
} else if (text[curPos + 1] === '%') {
|
||||
return [1, {
|
||||
return [2, {
|
||||
length: 1,
|
||||
position: result.length,
|
||||
original_position: curPos + 1,
|
||||
type: 'text'
|
||||
}, '%'];
|
||||
} else if (text[curPos + 1] === '_') {
|
||||
return [2, {
|
||||
length: 1,
|
||||
position: result.length,
|
||||
original_position: curPos + 1,
|
||||
type: 'text'
|
||||
}, '_'];
|
||||
}
|
||||
|
||||
let commandName = "";
|
||||
@@ -166,7 +175,7 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
}
|
||||
|
||||
console.log(text.substring(i - 20, i + 20));
|
||||
console.log('Char:', char.charCodeAt(0));
|
||||
console.log('Char:' + char.charCodeAt(0));
|
||||
throw new Error("TODO handle not char chars in the parse command function");
|
||||
}
|
||||
}
|
||||
@@ -175,6 +184,9 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
throw new Error("Could not end of the command");
|
||||
}
|
||||
|
||||
//console.log("Parsed '" + text.substring(curPos, len) + "'")
|
||||
//console.log("Ranged '" + text.substring(curPos - 5 , len + 5) + "'")
|
||||
|
||||
len = len - curPos;
|
||||
|
||||
switch (commandName) {
|
||||
@@ -195,11 +207,13 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
case 'includegraphics':
|
||||
case 'appendix':
|
||||
case 'printbibliography':
|
||||
case 'subsection*':
|
||||
case 'section*':
|
||||
return [len];
|
||||
case 'title':
|
||||
case 'author':
|
||||
case 'end':
|
||||
console.log("TODO: add way to check the", commandName)
|
||||
console.log("TODO: add way to check the " + commandName)
|
||||
return [len];
|
||||
|
||||
case 'cite':
|
||||
@@ -207,15 +221,49 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
return [len];
|
||||
|
||||
case 'begin':
|
||||
case 'item':
|
||||
console.log("TODO handle", commandName, ":", args)
|
||||
|
||||
switch (args[0]) {
|
||||
|
||||
case 'verbatim':
|
||||
|
||||
const find = '\end{verbatim}';
|
||||
|
||||
let endPos = text.indexOf(find, curPos) + find.length;
|
||||
|
||||
len = endPos - curPos;
|
||||
break
|
||||
default:
|
||||
console.log("Do not know how to handle " + args[0])
|
||||
}
|
||||
|
||||
|
||||
return [len];
|
||||
|
||||
case 'item':
|
||||
|
||||
if (args[0]) {
|
||||
return [len, {
|
||||
length: 2 + args[0].length + 1,
|
||||
original_length: len,
|
||||
original_position: curPos + commandName.length,
|
||||
position: result.length,
|
||||
type: 'text'
|
||||
}, "— " + args[0] + '\n'];
|
||||
}
|
||||
|
||||
return [len, {
|
||||
length: 2,
|
||||
original_length: len,
|
||||
original_position: curPos,
|
||||
position: result.length,
|
||||
type: 'text'
|
||||
}, "— "];
|
||||
|
||||
case 'section':
|
||||
return [len, {
|
||||
length: args[0].length + 1,
|
||||
original_length: len,
|
||||
original_position: curPos - commandName.length,
|
||||
original_position: curPos + 2 + commandName.length,
|
||||
position: result.length,
|
||||
type: 'h1',
|
||||
}, args[0] + '\n']
|
||||
@@ -224,13 +272,13 @@ function parseCommand(text: string, curPos: number, result: string): [number, Pa
|
||||
return [len, {
|
||||
length: args[0].length + 1,
|
||||
original_length: len,
|
||||
original_position: curPos - commandName.length,
|
||||
original_position: curPos + 2 + commandName.length,
|
||||
position: result.length,
|
||||
type: 'h2',
|
||||
}, args[0] + '\n']
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -294,7 +342,6 @@ export function parseLsp(text: string): ParseResult {
|
||||
const [len] = res;
|
||||
i += len;
|
||||
} else {
|
||||
|
||||
const [len, conv, toAdd] = res;
|
||||
|
||||
result.conversions.push(conv);
|
||||
@@ -309,7 +356,7 @@ export function parseLsp(text: string): ParseResult {
|
||||
throw new Error("Handle double math expression");
|
||||
}
|
||||
|
||||
const possiblePartition = createPartition(text, conversionStartPosition, i - 1, result.text);
|
||||
const possiblePartition = createPartition(text, conversionStartPosition, i, result.text);
|
||||
|
||||
if (possiblePartition) {
|
||||
const [conv, toAdd] = possiblePartition;
|
||||
@@ -319,6 +366,18 @@ export function parseLsp(text: string): ParseResult {
|
||||
|
||||
const len = readUntil(text, '$', i + 1);
|
||||
|
||||
let to_add = 'mathexpr' + (text[i + len + 1 + 1] === ' ' ? ' ' : '');
|
||||
|
||||
result.conversions = result.conversions.concat(
|
||||
[{
|
||||
length: to_add.length,
|
||||
position: result.text.length,
|
||||
original_position: i,
|
||||
type: 'text',
|
||||
}]
|
||||
);
|
||||
result.text += to_add;
|
||||
|
||||
i += len + 1;
|
||||
|
||||
conversionStartPosition = i + 1;
|
||||
@@ -328,6 +387,8 @@ export function parseLsp(text: string): ParseResult {
|
||||
|
||||
}
|
||||
|
||||
result.text = result.text.replace(/``/g, "''");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -418,7 +479,7 @@ type Match = {
|
||||
}
|
||||
|
||||
|
||||
export async function diagnosticsRequests(res: ParseResult): Promise < Match[] > {
|
||||
export async function diagnosticsRequests(res: ParseResult): Promise<Match[]> {
|
||||
const formData = new URLSearchParams();
|
||||
|
||||
formData.set('text', res.text);
|
||||
@@ -436,6 +497,7 @@ export async function diagnosticsRequests(res: ParseResult): Promise < Match[] >
|
||||
});
|
||||
|
||||
if (rawRes.status !== 200) {
|
||||
console.log("Error:" + (await (await rawRes.blob()).text()))
|
||||
process.exit(2);
|
||||
}
|
||||
const body = await rawRes.json();
|
||||
@@ -443,10 +505,14 @@ export async function diagnosticsRequests(res: ParseResult): Promise < Match[] >
|
||||
return body.matches;
|
||||
}
|
||||
|
||||
export async function getDiagnostics(file: string): Promise<(Dialog & {replacements: string[]})[]> {
|
||||
export type GetDiagnosticsReturn = (Dialog & { replacements: string[], rule_id: string, word?: string });
|
||||
|
||||
export async function getDiagnostics(file: string): Promise<GetDiagnosticsReturn[]> {
|
||||
|
||||
const res = parseLsp(file);
|
||||
|
||||
fs.writeFileSync('/tmp/latex-lsp-res', res.text)
|
||||
|
||||
const matches = await diagnosticsRequests(res);
|
||||
|
||||
const lineIndex = buildLineIndex(file);
|
||||
@@ -460,6 +526,13 @@ export async function getDiagnostics(file: string): Promise<(Dialog & {replaceme
|
||||
console.log("Could not find the original position")
|
||||
continue;
|
||||
}
|
||||
|
||||
let word: string | undefined = undefined;
|
||||
|
||||
if (match.rule.id.startsWith("MORFOLOGIK_RULE")) {
|
||||
word = file.substring(original_position, original_position + match.length);
|
||||
}
|
||||
|
||||
const [startLine, startChar] = getLineAndChar(lineIndex, original_position);
|
||||
const [endLine, endChar] = getLineAndChar(lineIndex, original_position + match.length);
|
||||
|
||||
@@ -473,6 +546,8 @@ export async function getDiagnostics(file: string): Promise<(Dialog & {replaceme
|
||||
severity: Severity.Error,
|
||||
message: match.message,
|
||||
replacements: match.replacements.map(a => a.value),
|
||||
rule_id: match.rule.id,
|
||||
word,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user