Skip to content

Commit

Permalink
feat: Support links.format = absolute on file rename
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Oct 31, 2020
1 parent a85520c commit fe6b569
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/features/completionProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('provideCompletionItems()', () => {
]);
});

describe('with links.format === absolutePathInWorkspace', () => {
describe('with links.format = absolutePathInWorkspace', () => {
it('should provide only long links', async () => {
const name0 = `a-${rndName()}`;
const name1 = `b-${rndName()}`;
Expand Down
27 changes: 27 additions & 0 deletions src/features/fsWatcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
closeEditorsAndCleanWorkspace,
cacheWorkspace,
getWorkspaceCache,
updateMemoConfigProperty,
waitForExpect,
} from '../test/testUtils';

Expand Down Expand Up @@ -195,6 +196,32 @@ describe('fsWatcher feature', () => {

await waitForExpect(() => expect(doc.getText()).toBe(`[[${nextName}.gif]]`));
});

describe('with links.format = absolutePathInWorkspace', () => {
it('should update long ref with long ref on file rename', async () => {
const noteName0 = rndName();
const noteName1 = rndName();
const nextNoteName1 = rndName();

await updateMemoConfigProperty('links.format', 'absolutePathInWorkspace');

await createFile(`${noteName0}.md`, `[[folder1/${noteName1}]]`, false);
await createFile(`${noteName1}.md`, '', false);
await createFile(`/folder1/${noteName1}.md`, '', false);

const edit = new WorkspaceEdit();
edit.renameFile(
Uri.file(`${getWorkspaceFolder()}/folder1/${noteName1}.md`),
Uri.file(`${getWorkspaceFolder()}/folder1/${nextNoteName1}.md`),
);

await workspace.applyEdit(edit);

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

await waitForExpect(() => expect(doc.getText()).toBe(`[[folder1/${nextNoteName1}]]`));
});
});
});

it('should sync workspace cache on file create', async () => {
Expand Down
17 changes: 9 additions & 8 deletions src/features/fsWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
cacheUris,
addCachedRefs,
removeCachedRefs,
getMemoConfigProperty,
} from '../utils';

const getBasename = (pathParam: string) => path.basename(pathParam).toLowerCase();
Expand Down Expand Up @@ -72,6 +73,8 @@ export const activate = (
);
}

const linksFormat = getMemoConfigProperty('links.format', 'shortestPathWhenPossible');

const oldFsPaths = files.map(({ oldUri }) => oldUri.fsPath);

const oldUrisGroupedByBasename = groupBy(
Expand Down Expand Up @@ -140,14 +143,12 @@ export const activate = (
basePath: workspaceFolder,
keepExt: preserveNewExtension,
});
const oldUriIsShortRef = isFirstUriInGroup(
oldUri.fsPath,
oldUrisGroupedByBasename[getBasename(oldUri.fsPath)],
);
const newUriIsShortRef = isFirstUriInGroup(
newUri.fsPath,
newUrisGroupedByBasename[getBasename(newUri.fsPath)],
);
const oldUriIsShortRef =
linksFormat !== 'absolutePathInWorkspace' &&
isFirstUriInGroup(oldUri.fsPath, oldUrisGroupedByBasename[getBasename(oldUri.fsPath)]);
const newUriIsShortRef =
linksFormat !== 'absolutePathInWorkspace' &&
isFirstUriInGroup(newUri.fsPath, newUrisGroupedByBasename[getBasename(newUri.fsPath)]);

if (!oldShortRef || !newShortRef || !oldLongRef || !newLongRef) {
return;
Expand Down

0 comments on commit fe6b569

Please sign in to comment.