Skip to content

Commit

Permalink
Fixes invalid ranges from getContentRangeInLine (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpaul-stripe authored Jun 15, 2023
1 parent 1466230 commit 6d9bcee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 2 additions & 2 deletions server/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class LinkProvider {
if (!relative) continue;

const range = utils.getContentRangeInLine(node.lines[0], doc, href);
links.push({ target: Scanner.fullPath(relative), range });
if (range) links.push({ target: Scanner.fullPath(relative), range });
continue;
}

Expand All @@ -42,7 +42,7 @@ export default class LinkProvider {
if (typeof file !== "string") continue;

const range = utils.getContentRangeInLine(node.lines[0], doc, file);
links.push({ target: Scanner.fullPath(file), range });
if (range) links.push({ target: Scanner.fullPath(file), range });
}
}

Expand Down
5 changes: 3 additions & 2 deletions server/plugins/linkedEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ export default class LinkedEditProvider {
const ast = this.services.Documents.ast(params.textDocument.uri);
if (!ast || !doc) return;

for (const { start, end, tag } of utils.getBlockRanges(ast))
for (const { start, end, tag } of utils.getBlockRanges(ast)) {
if (tag && [start, end].includes(params.position.line)) {
const open = utils.getContentRangeInLine(start, doc, tag);
const close = utils.getContentRangeInLine(end, doc, tag);
return { ranges: [open, close] };
if (open && close) return { ranges: [open, close] };
}
}
}
}
5 changes: 4 additions & 1 deletion server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ export function getContentRangeInLine(
doc: TextDocument,
text: string
) {
if (typeof line !== "number" || line < 0) return null;
const lineContent = doc.getText(LSP.Range.create(line, 0, line + 1, 0));
const startOffset = lineContent.indexOf(text);
const endOffset = startOffset + text.length;
return LSP.Range.create(line, startOffset, line, endOffset);
return startOffset < 0
? null
: LSP.Range.create(line, startOffset, line, endOffset);
}

export function* getBlockRanges(ast: Markdoc.Node) {
Expand Down

0 comments on commit 6d9bcee

Please sign in to comment.