Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix link autocompletion with tags #885

Merged
merged 2 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions packages/foam-vscode/src/features/link-completion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,40 +88,48 @@ describe('Link Completion', () => {
});

it('should return notes with unique identifiers, and placeholders', async () => {
const { uri } = await createFile('[[file]] [[');
const { doc } = await showInEditor(uri);
const provider = new CompletionProvider(ws, graph);

const links = await provider.provideCompletionItems(
doc,
new vscode.Position(0, 11)
);

expect(links.items.length).toEqual(5);
expect(new Set(links.items.map(i => i.insertText))).toEqual(
new Set([
'to/file',
'another/file',
'File name with spaces',
'file-name',
'placeholder text',
])
);
for (const text of ['[[', '[[file]] [[', '[[file]] #tag [[']) {
const { uri } = await createFile(text);
const { doc } = await showInEditor(uri);
const provider = new CompletionProvider(ws, graph);

const links = await provider.provideCompletionItems(
doc,
new vscode.Position(0, text.length)
);

expect(links.items.length).toEqual(5);
expect(new Set(links.items.map(i => i.insertText))).toEqual(
new Set([
'to/file',
'another/file',
'File name with spaces',
'file-name',
'placeholder text',
])
);
}
});

it('should return sections for other notes', async () => {
const { uri } = await createFile('[[file-name#');
const { doc } = await showInEditor(uri);
const provider = new SectionCompletionProvider(ws);

const links = await provider.provideCompletionItems(
doc,
new vscode.Position(0, 12)
);

expect(new Set(links.items.map(i => i.label))).toEqual(
new Set(['Section One', 'Section Two'])
);
for (const text of [
'[[file-name#',
'[[file]] [[file-name#',
'[[file]] #tag [[file-name#',
]) {
const { uri } = await createFile(text);
const { doc } = await showInEditor(uri);
const provider = new SectionCompletionProvider(ws);

const links = await provider.provideCompletionItems(
doc,
new vscode.Position(0, text.length)
);

expect(new Set(links.items.map(i => i.label))).toEqual(
new Set(['Section One', 'Section Two'])
);
}
});

it('should return sections within the note', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/foam-vscode/src/features/link-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class CompletionProvider
// eslint-disable-next-line no-useless-escape
const requiresAutocomplete = cursorPrefix.match(WIKILINK_REGEX);

if (!requiresAutocomplete || cursorPrefix.indexOf('#') >= 0) {
if (!requiresAutocomplete || requiresAutocomplete[0].indexOf('#') >= 0) {
return null;
}

Expand Down