Skip to content

Commit

Permalink
Fix LSP error from converting between lines/cols and positions
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Sep 29, 2024
1 parent c1fdc9c commit 52ed352
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/brioche-core/runtime/dist/index.js

Large diffs are not rendered by default.

63 changes: 50 additions & 13 deletions crates/brioche-core/runtime/src/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ class Lsp {

completion(params: lsp.TextDocumentPositionParams): lsp.CompletionItem[] {
const fileName = params.textDocument.uri;
const position = ts.getPositionOfLineAndCharacter(
const position = tryGetPositionOfLineAndCharacter(
this.host.getSourceFile(fileName),
params.position.line,
params.position.character
);
if (position == null) {
return [];
}

const completions = this.languageService.getCompletionsAtPosition(brioche.toTsUrl(fileName), position, {});

Expand Down Expand Up @@ -137,12 +140,13 @@ class Lsp {
return [];
}

const start = sourceFile.getLineAndCharacterOfPosition(diagnostic.start);
const end = sourceFile.getLineAndCharacterOfPosition(diagnostic.start + diagnostic.length);

return [{
range: {
start: sourceFile.getLineAndCharacterOfPosition(diagnostic.start),
end: sourceFile.getLineAndCharacterOfPosition(
diagnostic.start + diagnostic.length
),
start,
end,
},
message: ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n"),
}];
Expand All @@ -166,12 +170,12 @@ class Lsp {
return [{
range: {
start: {
line: startLine - 1,
character: startColumn - 1,
line: Math.max(0, startLine - 1),
character: Math.max(0, startColumn - 1),
},
end: {
line: endLine - 1,
character: endColumn - 1,
line: Math.max(0, endLine - 1),
character: Math.max(0, endColumn - 1),
},
},
message: diagnostic.message,
Expand All @@ -189,11 +193,15 @@ class Lsp {
return null;
}

const position = ts.getPositionOfLineAndCharacter(
const position = tryGetPositionOfLineAndCharacter(
sourceFile,
params.position.line,
params.position.character,
);
if (position == null) {
return null;
}

const definition = this.languageService.getDefinitionAtPosition(
brioche.toTsUrl(fileName),
position,
Expand Down Expand Up @@ -224,11 +232,15 @@ class Lsp {
return null;
}

const position = ts.getPositionOfLineAndCharacter(
const position = tryGetPositionOfLineAndCharacter(
sourceFile,
params.position.line,
params.position.character,
);
if (position == null) {
return null;
}

const info = this.languageService.getQuickInfoAtPosition(brioche.toTsUrl(fileName), position);
if (!info) {
return null;
Expand All @@ -249,11 +261,15 @@ class Lsp {
return null;
}

const position = ts.getPositionOfLineAndCharacter(
const position = tryGetPositionOfLineAndCharacter(
sourceFile,
params.position.line,
params.position.character,
);
if (position == null) {
return null;
}

const references = this.languageService.getReferencesAtPosition(brioche.toTsUrl(fileName), position);
if (!references) {
return null;
Expand Down Expand Up @@ -286,6 +302,10 @@ class Lsp {
params.position.line,
params.position.character,
);
if (position == null) {
return null;
}

const searchFilenames = new Set([...this.host.getScriptFileNames(), brioche.toTsUrl(fileName)]);
const highlights = this.languageService.getDocumentHighlights(brioche.toTsUrl(fileName), position, Array.from(searchFilenames));
if (!highlights) {
Expand Down Expand Up @@ -316,11 +336,15 @@ class Lsp {
return null;
}

const position = ts.getPositionOfLineAndCharacter(
const position = tryGetPositionOfLineAndCharacter(
sourceFile,
params.position.line,
params.position.character,
);
if (position == null) {
return null;
}

const rename = this.languageService.getRenameInfo(brioche.toTsUrl(fileName), position, {});
if (rename == null || !rename.canRename) {
return null;
Expand Down Expand Up @@ -386,3 +410,16 @@ function lspSeverityFromEslint(severity: eslint.Linter.Severity): lsp.Diagnostic
return lsp.DiagnosticSeverity.Error;
}
}

function tryGetPositionOfLineAndCharacter(
sourceFile: ts.SourceFile,
line: number,
character: number
): number | null {
try {
return ts.getPositionOfLineAndCharacter(sourceFile, line, character);
} catch (error) {
console.warn("error getting position of line and character", { line, character }, error);
return null;
}
}

0 comments on commit 52ed352

Please sign in to comment.