Fix range of reference links (#154819)

Fixes #150921
This commit is contained in:
Matt Bierner 2022-07-11 08:46:15 -07:00 committed by GitHub
parent 30b6e1a9b7
commit e02c71e3c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View file

@ -400,7 +400,8 @@ export class MdLinkComputer {
private *getReferenceLinks(document: ITextDocument, noLinkRanges: NoLinkRanges): Iterable<MdLink> {
const text = document.getText();
for (const match of text.matchAll(referenceLinkPattern)) {
const linkStart = document.positionAt(match.index ?? 0);
const linkStartOffset = (match.index ?? 0) + match[1].length;
const linkStart = document.positionAt(linkStartOffset);
if (noLinkRanges.contains(linkStart)) {
continue;
}
@ -410,17 +411,17 @@ export class MdLinkComputer {
let reference = match[4];
if (reference === '') { // [ref][],
reference = match[3];
const offset = ((match.index ?? 0) + match[1].length) + 1;
const offset = linkStartOffset + 1;
hrefStart = document.positionAt(offset);
hrefEnd = document.positionAt(offset + reference.length);
} else if (reference) { // [text][ref]
const pre = match[2];
const offset = ((match.index ?? 0) + match[1].length) + pre.length;
const offset = linkStartOffset + pre.length;
hrefStart = document.positionAt(offset);
hrefEnd = document.positionAt(offset + reference.length);
} else if (match[5]) { // [ref]
reference = match[5];
const offset = ((match.index ?? 0) + match[1].length) + 1;
const offset = linkStartOffset + 1;
hrefStart = document.positionAt(offset);
const line = getLine(document, hrefStart.line);
// See if link looks like a checkbox
@ -433,7 +434,7 @@ export class MdLinkComputer {
continue;
}
const linkEnd = linkStart.translate(0, match[0].length);
const linkEnd = linkStart.translate(0, match[0].length - match[1].length);
yield {
kind: 'link',
source: {

View file

@ -128,6 +128,15 @@ suite('Markdown: MdLinkComputer', () => {
new vscode.Range(0, 35, 0, 39),
]);
}
{
const links = await getLinksForFile(joinLines(
`# h`,
`[[a]](http://example.com)`,
));
assertLinksEqual(links, [
new vscode.Range(1, 6, 1, 24),
]);
}
});
test('Should handle two links without space', async () => {