Skip to content

Commit 6d9bcee

Browse files
authored
Fixes invalid ranges from getContentRangeInLine (#5)
1 parent 1466230 commit 6d9bcee

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

server/plugins/link.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class LinkProvider {
3333
if (!relative) continue;
3434

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

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

4444
const range = utils.getContentRangeInLine(node.lines[0], doc, file);
45-
links.push({ target: Scanner.fullPath(file), range });
45+
if (range) links.push({ target: Scanner.fullPath(file), range });
4646
}
4747
}
4848

server/plugins/linkedEdit.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export default class LinkedEditProvider {
2626
const ast = this.services.Documents.ast(params.textDocument.uri);
2727
if (!ast || !doc) return;
2828

29-
for (const { start, end, tag } of utils.getBlockRanges(ast))
29+
for (const { start, end, tag } of utils.getBlockRanges(ast)) {
3030
if (tag && [start, end].includes(params.position.line)) {
3131
const open = utils.getContentRangeInLine(start, doc, tag);
3232
const close = utils.getContentRangeInLine(end, doc, tag);
33-
return { ranges: [open, close] };
33+
if (open && close) return { ranges: [open, close] };
3434
}
35+
}
3536
}
3637
}

server/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ export function getContentRangeInLine(
2424
doc: TextDocument,
2525
text: string
2626
) {
27+
if (typeof line !== "number" || line < 0) return null;
2728
const lineContent = doc.getText(LSP.Range.create(line, 0, line + 1, 0));
2829
const startOffset = lineContent.indexOf(text);
2930
const endOffset = startOffset + text.length;
30-
return LSP.Range.create(line, startOffset, line, endOffset);
31+
return startOffset < 0
32+
? null
33+
: LSP.Range.create(line, startOffset, line, endOffset);
3134
}
3235

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

0 commit comments

Comments
 (0)