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 path completions for file names with % #146

Merged
merged 1 commit into from
Sep 1, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.4.0-alpha.6 — WIP
- Fix path completions when file name contains a literal `%`. In these cases the `%` needs to be encoded to prevent it from being incorrectly decoded on link click

## 0.4.0-alpha.5 — June 5, 2023
- Make rename and path completions escape angle brackets when inside of angle bracket links.
- On rename, try removing angle brackets from links if the link no longer requires it.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-markdown-languageservice",
"description": "Markdown language service",
"version": "0.4.0-alpha.5",
"version": "0.4.0-alpha.6",
"author": "Microsoft Corporation",
"license": "MIT",
"engines": {
Expand Down
4 changes: 4 additions & 0 deletions src/languageFeatures/pathCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ export class MdPathCompletionProvider {
}

#getPathInsertText(context: PathCompletionContext, name: string): string {
// If the path has a literal `%` in it, we need encode it to prevent
// it being incorrectly decoded
name = name.replaceAll('%', '%25');

if (context.kind === CompletionContextKind.HtmlAttribute) {
return name
.replaceAll(`"`, '"')
Expand Down
22 changes: 22 additions & 0 deletions src/test/diagnostic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,28 @@ suite('Diagnostic Computer', () => {
const diagnostics = await getComputedDiagnostics(store, doc, workspace, { });
assertDiagnosticsEqual(diagnostics, []);
}));

test(`Should handle file names with '%' in the name`, withStore(async (store) => {
const doc1 = new InMemoryDocument(workspacePath('doc1.md'), joinLines(
`[i](/a%20b.md)`, // These should fail since the file will be resolved to 'a b.md'
`[i](a%20b.md)`,
`[i](./a%20b.md)`,
`[i](/a%2520b.md)`, // These should be resolved
`[i](a%2520b.md)`,
`[i](./a%2520b.md)`,
`[i](<a b.md>)`, // This should also fail due since space should not resolve to a file name '%20'
));
const doc2 = new InMemoryDocument(workspacePath('a%20b.md'), joinLines(''));
const workspace = store.add(new InMemoryWorkspace([doc1, doc2]));

const diagnostics = await getComputedDiagnostics(store, doc1, workspace, { });
assertDiagnosticsEqual(diagnostics, [
makeRange(0, 4, 0, 13),
makeRange(1, 4, 1, 12),
makeRange(2, 4, 2, 14),
makeRange(6, 5, 6, 11),
]);
}));
});


Expand Down
28 changes: 28 additions & 0 deletions src/test/pathCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,20 @@ suite('Path completions', () => {
]);
}));

test(`Should escape '%' used in file name`, withStore(async (store) => {
const workspace = store.add(new InMemoryWorkspace([
new InMemoryDocument(workspacePath('a%b.md'), ''),
]));

const completions = await getCompletionsAtCursorForFileContents(store, workspacePath('new.md'), joinLines(
`[](./${CURSOR}`,
), workspace);

assertCompletionsEqual(completions, [
{ label: 'a%b.md', insertText: 'a%25b.md' },
]);
}));

suite('Cross file header completions', () => {

test('Should return completions for headers in current doc', withStore(async (store) => {
Expand Down Expand Up @@ -648,6 +662,20 @@ suite('Path completions', () => {
{ label: '#header', insertText: 'テ%20ス%20ト.md#header' },
]);
}));

test(`Should escape '%' used in file name`, withStore(async (store) => {
const workspace = store.add(new InMemoryWorkspace([
new InMemoryDocument(workspacePath('a%b.md'), '# Header'),
]));

const completions = await getCompletionsAtCursorForFileContents(store, workspacePath('new.md'), joinLines(
`[](##${CURSOR}`,
), workspace, undefined, {includeWorkspaceHeaderCompletions: IncludeWorkspaceHeaderCompletions.onDoubleHash});

assertCompletionsEqual(completions, [
{ label: '#header', insertText: 'a%25b.md#header' },
]);
}));
});

suite('Html attribute path completions', () => {
Expand Down