Fix explicit references to own file

This commit is contained in:
Matt Bierner 2022-04-01 00:03:40 -07:00
parent a7ba4439ae
commit 114b340f7a
No known key found for this signature in database
GPG key ID: 099C331567E11888
4 changed files with 79 additions and 5 deletions

View file

@ -218,8 +218,8 @@ export class MdReferencesProvider extends Disposable implements vscode.Reference
}
} else { // Triggered on a link without a fragment so we only require matching the file and ignore fragments
// But exclude cases where the file is referencing itself
if (link.sourceResource.fsPath !== targetDoc.uri.fsPath) {
// But exclude cases where the file is implicitly referencing itself
if (!link.sourceText.startsWith('#') || link.sourceResource.fsPath !== targetDoc.uri.fsPath) {
references.push({
kind: 'link',
isTriggerLocation,

View file

@ -56,7 +56,10 @@ export class MdRenameProvider extends Disposable implements vscode.RenameProvide
return triggerRef.link.sourceHrefRange;
}
} else {
return triggerRef.fragmentLocation?.range ?? triggerRef.location.range;
if (triggerRef.fragmentLocation) {
return triggerRef.fragmentLocation.range;
}
throw new Error(localize('renameNoFiles', "Renaming files is currently not supported"));
}
}
}

View file

@ -312,7 +312,7 @@ suite('markdown: find all references', () => {
const otherUri = workspacePath('sub', 'other.md');
const doc = new InMemoryDocument(docUri, joinLines(
`[other](./sub/other)`,
`[other](./sub/other)`, // trigger here
));
const refs = await getReferences(doc, new vscode.Position(0, 15), new InMemoryWorkspaceMarkdownDocuments([
@ -328,6 +328,22 @@ suite('markdown: find all references', () => {
);
});
test('Should find explicit references to own file ', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
`[bare](doc.md)`, // trigger here
`[rel](./doc.md)`,
`[abs](/doc.md)`,
));
const refs = await getReferences(doc, new vscode.Position(0, 12), new InMemoryWorkspaceMarkdownDocuments([doc]));
assertReferencesEqual(refs!,
{ uri, line: 0 },
{ uri, line: 1 },
{ uri, line: 2 },
);
});
suite('Reference links', () => {
test('Should find reference links within file from link', async () => {
const docUri = workspacePath('doc.md');

View file

@ -208,6 +208,52 @@ suite('markdown: rename', () => {
});
});
test('Rename on link in other file should pick up all refs', async () => {
const uri = workspacePath('doc.md');
const otherUri = workspacePath('other.md');
const doc = new InMemoryDocument(uri, joinLines(
`### A b C`,
`[text](#a-b-c)`,
));
const otherDoc = new InMemoryDocument(otherUri, joinLines(
`[text](#a-b-c)`,
`[text](./doc.md#a-b-c)`,
`[text](./doc#a-b-c)`
));
const expectedEdits = [
{
uri: uri, edits: [
new vscode.TextEdit(new vscode.Range(0, 4, 0, 9), 'New Header'),
new vscode.TextEdit(new vscode.Range(1, 8, 1, 13), 'new-header'),
]
}, {
uri: otherUri, edits: [
new vscode.TextEdit(new vscode.Range(1, 16, 1, 21), 'new-header'),
new vscode.TextEdit(new vscode.Range(2, 13, 2, 18), 'new-header'),
]
}
];
{
// Rename on header with file extension
const edit = await getRenameEdits(otherDoc, new vscode.Position(1, 17), "New Header", new InMemoryWorkspaceMarkdownDocuments([
doc,
otherDoc
]));
assertEditsEqual(edit!, ...expectedEdits);
}
{
// Rename on header without extension
const edit = await getRenameEdits(otherDoc, new vscode.Position(2, 15), "New Header", new InMemoryWorkspaceMarkdownDocuments([
doc,
otherDoc
]));
assertEditsEqual(edit!, ...expectedEdits);
}
});
test('Rename on ref should rename refs and def', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
@ -246,7 +292,6 @@ suite('markdown: rename', () => {
});
});
test('Rename should not be supported on link text', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
@ -256,4 +301,14 @@ suite('markdown: rename', () => {
await assert.rejects(getRenameRange(doc, new vscode.Position(1, 2), new InMemoryWorkspaceMarkdownDocuments([doc])));
});
test('Rename should not be supported on bare file references', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
`[text](./doc.md)`,
`[other](./doc.md)`,
));
await assert.rejects(getRenameRange(doc, new vscode.Position(0, 10), new InMemoryWorkspaceMarkdownDocuments([doc])));
});
});