Skip to content

Commit 8b7086a

Browse files
committed
Fixing false positive ref link being detected
1 parent c39d09a commit 8b7086a

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

extensions/markdown-language-features/src/languageFeatures/documentLinkProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const linkPattern = /(\[((!\[[^\]]*?\]\(\s*)([^\s\(\)]+?)\s*\)\]|(?:\\\]|[^\]])*
157157
/**
158158
* Matches `[text][ref]`
159159
*/
160-
const referenceLinkPattern = /(?:(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]|\[\s*?([^\s\]]*?)\])(?!\:)/g;
160+
const referenceLinkPattern = /(?:(\[((?:\\\]|[^\]])+)\]\[\s*?)([^\s\]]*?)\]|\[\s*?([^\s\]]*?)\])(?![\:\(])/g;
161161

162162
/**
163163
* Matches `[text]: link`

extensions/markdown-language-features/src/languageFeatures/references.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,11 @@ export class MdReferencesProvider extends Disposable implements vscode.Reference
106106

107107
const references: MdReference[] = [];
108108

109-
const line = document.lineAt(header.line);
110109
references.push({
111110
kind: 'header',
112111
isTriggerLocation: true,
113112
isDefinition: true,
114-
location: new vscode.Location(document.uri, new vscode.Range(header.line, 0, header.line, line.text.length)),
113+
location: header.headerLocation,
115114
headerTextLocation: header.headerTextLocation
116115
});
117116

extensions/markdown-language-features/src/test/references.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ suite('markdown: find all references', () => {
7171
);
7272
});
7373

74+
test('Should not return references when on link text', async () => {
75+
const doc = new InMemoryDocument(workspacePath('doc.md'), joinLines(
76+
`[ref](#abc)`,
77+
`[ref]: http://example.com`,
78+
));
79+
80+
const refs = await getReferences(doc, new vscode.Position(0, 1), new InMemoryWorkspaceMarkdownDocuments([doc]));
81+
assert.deepStrictEqual(refs, []);
82+
});
83+
7484
test('Should find references using normalized slug', async () => {
7585
const doc = new InMemoryDocument(workspacePath('doc.md'), joinLines(
7686
`# a B c`,

extensions/markdown-language-features/src/test/rename.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,15 @@ suite('markdown: rename', () => {
245245
]
246246
});
247247
});
248+
249+
250+
test('Rename should not be supported on link text', async () => {
251+
const uri = workspacePath('doc.md');
252+
const doc = new InMemoryDocument(uri, joinLines(
253+
`# Header`,
254+
`[text](#header)`,
255+
));
256+
257+
await assert.rejects(getRenameRange(doc, new vscode.Position(1, 2), new InMemoryWorkspaceMarkdownDocuments([doc])));
258+
});
248259
});

0 commit comments

Comments
 (0)