Skip to content

Commit

Permalink
Allow images autocomplete in regular links (not for embedding)
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jul 11, 2020
1 parent 3f0c04a commit 4c03763
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/extensions/completionProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ describe('provideCompletionItems()', () => {

afterEach(closeEditorsAndCleanWorkspace);

it('should provide only note links', async () => {
it('should provide links to notes and images', async () => {
const name0 = `a-${rndName()}`;
const name1 = `b-${rndName()}`;
const name2 = `c-${rndName()}`;

await createFile(`${name0}.md`);
await createFile(`${name1}.md`);
await createFile(`${rndName()}.png`);
await createFile(`${name2}.png`);

await cacheWorkspace();

Expand All @@ -35,18 +36,20 @@ describe('provideCompletionItems()', () => {
expect(completionItems).toEqual([
expect.objectContaining({ insertText: name0, label: name0 }),
expect.objectContaining({ insertText: name1, label: name1 }),
expect.objectContaining({ insertText: `${name2}.png`, label: `${name2}.png` }),
]);
});

it('should provide short and long links on name clash', async () => {
const name0 = `a-${rndName()}`;
const name1 = `b-${rndName()}`;
const name2 = `c-${rndName()}`;

await createFile(`${name0}.md`);
await createFile(`${name1}.md`);
await createFile(`/folder1/${name1}.md`);
await createFile(`/folder1/subfolder1/${name1}.md`);
await createFile(`${rndName()}.png`);
await createFile(`${name2}.png`);

await cacheWorkspace();

Expand All @@ -66,6 +69,11 @@ describe('provideCompletionItems()', () => {
insertText: `folder1/subfolder1/${name1}`,
label: `folder1/subfolder1/${name1}`,
}),
// images expected to come after notes in autocomplete due to sortPaths logic
expect.objectContaining({
insertText: `${name2}.png`,
label: `${name2}.png`,
}),
]);
});

Expand Down
11 changes: 9 additions & 2 deletions src/extensions/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import groupBy from 'lodash.groupby';

import { getWorkspaceCache, extractLongRef, extractShortRef, containsImageExt } from '../utils';

const padWithZero = (n: number): string => (n < 10 ? '0' + n : String(n));

export const provideCompletionItems = (document: TextDocument, position: Position) => {
const linePrefix = document.lineAt(position).text.substr(0, position.character);

Expand All @@ -28,12 +30,14 @@ export const provideCompletionItems = (document: TextDocument, position: Positio
...(isResourceAutocomplete
? [...getWorkspaceCache().imageUris, ...getWorkspaceCache().markdownUris]
: []),
...(!isResourceAutocomplete ? getWorkspaceCache().markdownUris : []),
...(!isResourceAutocomplete
? [...getWorkspaceCache().markdownUris, ...getWorkspaceCache().imageUris]
: []),
];

const urisByPathBasename = groupBy(uris, ({ fsPath }) => path.basename(fsPath).toLowerCase());

uris.forEach((uri) => {
uris.forEach((uri, index) => {
const workspaceFolder = workspace.getWorkspaceFolder(uri);

if (!workspaceFolder) {
Expand Down Expand Up @@ -61,6 +65,9 @@ export const provideCompletionItems = (document: TextDocument, position: Positio

item.insertText = isFirstUriInGroup ? shortRef.ref : longRef.ref;

// prepend index with 0, so a lexicographic sort doesn't mess things up
item.sortText = padWithZero(index);

completionItems.push(item);
});

Expand Down

0 comments on commit 4c03763

Please sign in to comment.