Try automatically adding .md file extension to new paths on rename

This commit is contained in:
Matt Bierner 2022-04-20 16:46:30 -07:00
parent 0ac39e800d
commit 0610f195fb
No known key found for this signature in database
GPG key ID: 099C331567E11888
2 changed files with 31 additions and 1 deletions

View file

@ -10,6 +10,7 @@ import { resolveDocumentLink } from '../util/openDocumentLink';
import { MdWorkspaceContents, SkinnyTextDocument } from '../workspaceContents';
import { InternalHref } from './documentLinkProvider';
import { MdHeaderReference, MdLinkReference, MdReference, MdReferencesProvider, tryFindMdDocumentForLink } from './references';
import * as URI from 'vscode-uri';
const localize = nls.loadMessageBundle();
@ -145,7 +146,17 @@ export class MdRenameProvider extends Disposable implements vscode.RenameProvide
const targetDoc = await tryFindMdDocumentForLink(triggerHref, this.workspaceContents);
const targetUri = targetDoc?.uri ?? triggerHref.path;
const newFilePath = resolveDocumentLink(newName, triggerHref.path);
let newFilePath = resolveDocumentLink(newName, triggerHref.path);
if (!URI.Utils.extname(newFilePath)) {
// If the newly entered path doesn't have a file extension but the original file did
// tack on a .md file extension
if (URI.Utils.extname(targetUri)) {
newFilePath = newFilePath.with({
path: newFilePath.path + '.md'
});
}
}
// First rename the file
fileRenames.push({ from: targetUri.toString(), to: newFilePath.toString() });

View file

@ -463,6 +463,25 @@ suite('markdown: rename', () => {
});
});
test('Path rename should use .md extension on extension-less link', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(
`[text](/doc#header)`,
`[ref]: /doc#other`,
));
const edit = await getRenameEdits(doc, new vscode.Position(0, 10), '/new File', new InMemoryWorkspaceMarkdownDocuments([doc]));
assertEditsEqual(edit!, {
originalUri: uri,
newUri: workspacePath('new File.md'),
}, {
uri: uri, edits: [
new vscode.TextEdit(new vscode.Range(0, 7, 0, 11), '/new%20File'),
new vscode.TextEdit(new vscode.Range(1, 7, 1, 11), '/new%20File'),
]
});
});
test('Rename on link should use header text as placeholder', async () => {
const uri = workspacePath('doc.md');
const doc = new InMemoryDocument(uri, joinLines(