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 incorrect renames of headers #83

Merged
merged 1 commit into from
Oct 28, 2022
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
5 changes: 5 additions & 0 deletions src/languageFeatures/fileRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ export class MdFileRenameProvider extends Disposable {
if (link.href.kind !== HrefKind.Internal) {
return false;
}

if (link.source.hrefText.startsWith('#')) {
// No rewrite needed as we are referencing the current doc implicitly
return false;
}

if (link.source.hrefText.startsWith('/')) {
// We likely don't need to update anything since an absolute path is used
Expand Down
37 changes: 37 additions & 0 deletions src/test/fileRename.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,9 @@ suite('File Rename', () => {
`[abc](../old)`,
`[abc](/old)`,
`[abc](/old/)`,
``, // Own header links should not get rewritten here
`# header`,
`[text](#header)`,
));

const workspace = store.add(new InMemoryWorkspace([
Expand All @@ -516,4 +519,38 @@ suite('File Rename', () => {
]
});
}));

test('Should not rewrite fragment links to self', withStore(async (store) => {
// Create workspace state after the rename
const newDocUri = workspacePath('sub', 'newReadme.md');
const newDoc = new InMemoryDocument(newDocUri, joinLines(
`# Header`,
`[abc](#header)`, // No change
`[abc](oldReadme.md#header)`, // Needs rewrite
`[abc](./oldReadme.md#header)`, // Needs rewrite
``,
`[def1]: #header`, // No change
`[def2]: oldReadme.md#header`, // Needs rewrite
`[def3]: ./oldReadme.md#header`, // Needs rewrite
));

const workspace = store.add(new InMemoryWorkspace([
newDoc,
]));

const response = await getFileRenameEdits(store, [
{ oldUri: workspacePath('oldReadme.md'), newUri: newDocUri },
], workspace);

assertEditsEqual(response!.edit, {
// Here we need to be using the new path to 'doc'
uri: newDocUri, edits: [
lsp.TextEdit.replace(makeRange(2, 6, 2, 18), 'newReadme.md'),
lsp.TextEdit.replace(makeRange(3, 6, 3, 20), './newReadme.md'),

lsp.TextEdit.replace(makeRange(6, 8, 6, 20), 'newReadme.md'),
lsp.TextEdit.replace(makeRange(7, 8, 7, 22), './newReadme.md'),
]
});
}));
});