Make sure we decode path components for md path completions

Fixes #145447
pull/145430/head
Matt Bierner 2022-03-22 16:58:04 -07:00
parent 5a2acb7773
commit 82c70007a3
No known key found for this signature in database
GPG Key ID: 099C331567E11888
3 changed files with 27 additions and 2 deletions

View File

@ -63,6 +63,14 @@ interface CompletionContext {
readonly anchorInfo?: AnchorContext;
}
function tryDecodeUriComponent(str: string): string {
try {
return decodeURIComponent(str);
} catch {
return str;
}
}
export class PathCompletionProvider implements vscode.CompletionItemProvider {
public static register(selector: vscode.DocumentSelector, engine: MarkdownEngine): vscode.Disposable {
@ -157,7 +165,7 @@ export class PathCompletionProvider implements vscode.CompletionItemProvider {
const suffix = lineSuffixText.match(/^[^\)\s]*/);
return {
kind: CompletionContextKind.Link,
linkPrefix: prefix,
linkPrefix: tryDecodeUriComponent(prefix),
linkTextStartPosition: position.translate({ characterDelta: -prefix.length }),
linkSuffix: suffix ? suffix[0] : '',
anchorInfo: this.getAnchorContext(prefix),
@ -174,7 +182,7 @@ export class PathCompletionProvider implements vscode.CompletionItemProvider {
const suffix = lineSuffixText.match(/^[^\s]*/);
return {
kind: CompletionContextKind.LinkDefinition,
linkPrefix: prefix,
linkPrefix: tryDecodeUriComponent(prefix),
linkTextStartPosition: position.translate({ characterDelta: -prefix.length }),
linkSuffix: suffix ? suffix[0] : '',
anchorInfo: this.getAnchorContext(prefix),

View File

@ -151,4 +151,20 @@ suite('Markdown path completion provider', () => {
assert.ok(completions.some(x => x.insertText === 'file%20with%20space.md'), 'Has encoded path completion');
});
test('Should complete paths for path with encoded spaces', async () => {
const completions = await getCompletionsAtCursor(workspaceFile('new.md'), joinLines(
`[](./sub%20with%20space/${CURSOR})`
));
assert.ok(completions.some(x => x.insertText === 'file.md'), 'Has file from space');
});
test('Should complete definition path for path with encoded spaces', async () => {
const completions = await getCompletionsAtCursor(workspaceFile('new.md'), joinLines(
`[def]: ./sub%20with%20space/${CURSOR}`
));
assert.ok(completions.some(x => x.insertText === 'file.md'), 'Has file from space');
});
});