Skip to content

Commit

Permalink
refactor: Allow "finding all references" in an active text editor as …
Browse files Browse the repository at this point in the history
…well
  • Loading branch information
svsool committed Aug 7, 2020
1 parent 6c5c4b6 commit 02ba2b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
71 changes: 67 additions & 4 deletions src/features/ReferenceProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ describe('ReferenceProvider', () => {
afterEach(closeEditorsAndCleanWorkspace);

it('should provide references', async () => {
const note0 = rndName();
const note1 = rndName();
const note0 = `a-${rndName()}`;
const note1 = `b-${rndName()}`;

await createFile(`${note0}.md`, `[[${note0}]]`);
await createFile(`${note1}.md`, `[[${note0}]]`);

const doc = await openTextDocument(`${note1}.md`);
const doc = await openTextDocument(`${note0}.md`);

const referenceProvider = new ReferenceProvider();

Expand All @@ -40,7 +40,23 @@ describe('ReferenceProvider', () => {
},
],
uri: {
path: expect.stringContaining(`${note0}.md`),
path: expect.toEndWith(`${note0}.md`),
scheme: 'file',
},
},
{
range: [
{
character: expect.any(Number),
line: 0,
},
{
character: expect.any(Number),
line: 0,
},
],
uri: {
path: expect.toEndWith(`${note1}.md`),
scheme: 'file',
},
},
Expand Down Expand Up @@ -87,4 +103,51 @@ describe('ReferenceProvider', () => {

expect(links).toHaveLength(0);
});

it('should provide references from a file itself', async () => {
const note = rndName();

await createFile(`${note}.md`, `[[ref1]] [[ref1]] [[ref2]]`);

const doc = await openTextDocument(`${note}.md`);

const referenceProvider = new ReferenceProvider();

const links = await referenceProvider.provideReferences(doc, new vscode.Position(0, 2));

expect(toPlainObject(links)).toMatchObject([
{
range: [
{
character: 2,
line: 0,
},
{
character: 6,
line: 0,
},
],
uri: {
path: expect.toEndWith(`${note}.md`),
scheme: 'file',
},
},
{
range: [
{
character: 11,
line: 0,
},
{
character: 15,
line: 0,
},
],
uri: {
path: expect.toEndWith(`${note}.md`),
scheme: 'file',
},
},
]);
});
});
4 changes: 1 addition & 3 deletions src/features/ReferenceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export default class ReferenceProvider implements vscode.ReferenceProvider {
public async provideReferences(document: vscode.TextDocument, position: vscode.Position) {
const refAtPos = getReferenceAtPosition(document, position);

return refAtPos
? (await findReferences(refAtPos.ref, [document.uri.fsPath])).map(({ location }) => location)
: [];
return refAtPos ? (await findReferences(refAtPos.ref)).map(({ location }) => location) : [];
}
}

0 comments on commit 02ba2b0

Please sign in to comment.