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

add . and ../ completions #237836

Merged
merged 41 commits into from
Jan 15, 2025
Merged
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1879f85
fix #234351
meganrogge Jan 13, 2025
a1c66f4
tweak
meganrogge Jan 13, 2025
90413df
consolidate
meganrogge Jan 13, 2025
789a154
move . and .. to top
meganrogge Jan 13, 2025
8b6dbad
Push WIP for fixing edge cases
Tyriar Jan 13, 2025
68151b2
update test expectations, get tests to pass
meganrogge Jan 14, 2025
b7e0f32
use provider, not builtin
meganrogge Jan 14, 2025
48613cd
provide paths as details
meganrogge Jan 14, 2025
4624f36
add test case for and fix bug
meganrogge Jan 14, 2025
c26f1b5
fix tests, some bugs
meganrogge Jan 14, 2025
8e798af
Refactor, clean up the code
meganrogge Jan 14, 2025
99de7b6
further simplify
meganrogge Jan 14, 2025
8bb2b4a
add a comment
meganrogge Jan 14, 2025
2dc39b9
better name
meganrogge Jan 14, 2025
7a93217
sort by label alphabetically
meganrogge Jan 14, 2025
2f4dee8
Add comment
meganrogge Jan 14, 2025
6f77cab
refactor tests
meganrogge Jan 14, 2025
f29b6b8
move score part into conditional to not override it
meganrogge Jan 14, 2025
a84759c
add test for alphabetical order
meganrogge Jan 14, 2025
76aa350
Add more tests
meganrogge Jan 14, 2025
db1757f
add more tests
meganrogge Jan 14, 2025
5c0724b
rm commas
meganrogge Jan 14, 2025
9fb7a4a
rename
meganrogge Jan 14, 2025
3389bdb
also assert last item
meganrogge Jan 15, 2025
1bd065c
add isWindows and opposite test case
meganrogge Jan 15, 2025
2086f15
normalize prefix
meganrogge Jan 15, 2025
74ff875
pass in should normalize prefix
meganrogge Jan 15, 2025
d4e0228
check if absolute path before adding children
meganrogge Jan 15, 2025
9010261
fix a test
meganrogge Jan 15, 2025
2932776
rm a test
meganrogge Jan 15, 2025
b7a74a7
alter test
meganrogge Jan 15, 2025
33ca570
add test for absolute paths on windows
meganrogge Jan 15, 2025
9b95e66
Fix filling of children dirs on Windows, fix unit tests on Windows
Tyriar Jan 15, 2025
c9b2a97
Remove most recent completion feature
Tyriar Jan 15, 2025
013e59c
Show absolute path in completion details
Tyriar Jan 15, 2025
0f33b3d
fix test expectations
meganrogge Jan 15, 2025
c1ab0e7
fix test expectations
meganrogge Jan 15, 2025
44d5bbd
don't provide parent dir for absolute path
meganrogge Jan 15, 2025
4450eab
set as var
meganrogge Jan 15, 2025
a031ef3
use path sep in else
meganrogge Jan 15, 2025
6237400
add comment, todo
meganrogge Jan 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CancellationToken } from '../../../../../base/common/cancellation.js';
import { Disposable, IDisposable, toDisposable } from '../../../../../base/common/lifecycle.js';
import { Schemas } from '../../../../../base/common/network.js';
import { basename } from '../../../../../base/common/path.js';
import { isWindows } from '../../../../../base/common/platform.js';
import { URI } from '../../../../../base/common/uri.js';
import { IConfigurationService } from '../../../../../platform/configuration/common/configuration.js';
import { IFileService } from '../../../../../platform/files/common/files.js';
Expand Down Expand Up @@ -200,8 +201,33 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
const endsWithSpace = cursorPrefix.endsWith(' ');
const lastWord = endsWithSpace ? '' : cursorPrefix.split(' ').at(-1) ?? '';

// Get the nearest folder path from the prefix. This ignores everything after the `/` as
// they are what triggers changes in the directory.
let lastSlashIndex: number;
if (isWindows) {
// TODO: This support is very basic, ideally the slashes supported would depend upon the
// shell type. For example git bash under Windows does not allow using \ as a path
// separator.
lastSlashIndex = Math.max(lastWord.lastIndexOf('\\'), lastWord.lastIndexOf('/'));
} else {
lastSlashIndex = lastWord.lastIndexOf('/');
}

let lastSep = lastWord.lastIndexOf('/');
if (isWindows) {
const otherLastSep = lastWord.lastIndexOf('\\');
lastSep = Math.max(lastSep, otherLastSep);
}
let lastWordRelativeFolder = lastSlashIndex === -1 ? '' : lastWord.slice(0, lastSlashIndex + 1);
if (isWindows) {
lastWordRelativeFolder = lastWordRelativeFolder.replaceAll('/', '\\');
}
console.log('cwd', cwd.fsPath);
console.log('lastWord', lastWord);
console.log('lastWordRelativeFolder', lastWordRelativeFolder);

if (foldersRequested) {
if (!lastWord.trim()) {
if (!lastWordRelativeFolder.trim()) {
resourceCompletions.push({
label: '.',
provider: 'builtin',
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -212,10 +238,22 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
replacementIndex: cursorPosition - lastWord.length,
replacementLength: lastWord.length
});
} else {
resourceCompletions.push({
label: lastWordRelativeFolder,
provider: 'builtin',
kind: TerminalCompletionItemKind.Folder,
isDirectory: true,
isFile: false,
detail: 'Source folder',
replacementIndex: cursorPosition - lastWord.length,
replacementLength: lastWord.length
});
}
if (lastWord.endsWith('..' + resourceRequestConfig.pathSeparator)) {
// TODO: Refine cases where ..\ shows. For example it looks strange to offer `.\..\`
if (isWindows ? lastWordRelativeFolder.match(/[\\\/]/) : lastWordRelativeFolder.includes(resourceRequestConfig.pathSeparator)) {
resourceCompletions.push({
label: lastWord + '..' + resourceRequestConfig.pathSeparator,
label: lastWordRelativeFolder + '..' + resourceRequestConfig.pathSeparator,
provider: 'builtin',
kind: TerminalCompletionItemKind.Folder,
isDirectory: true,
Expand Down Expand Up @@ -252,23 +290,31 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
const fileName = basename(stat.resource.fsPath);

let label;
if (!lastWord.startsWith('.' + resourceRequestConfig.pathSeparator) && !lastWord.startsWith('..' + resourceRequestConfig.pathSeparator)) {
// add a dot to the beginning of the label if it doesn't already have one
label = `.${resourceRequestConfig.pathSeparator}${fileName}`;
} else {
if (lastWord.endsWith(resourceRequestConfig.pathSeparator)) {
label = `${lastWord}${fileName}`;
} else {
label = `${lastWord}${resourceRequestConfig.pathSeparator}${fileName}`;
}
if (lastWord.length && lastWord.at(-1) !== resourceRequestConfig.pathSeparator && lastWord.at(-1) !== '.') {
label = `.${resourceRequestConfig.pathSeparator}${fileName}`;
}
}
// TODO: This doesn't do the windows path equivalence check
// if (!lastWord.startsWith('.' + resourceRequestConfig.pathSeparator) && !lastWord.startsWith('..' + resourceRequestConfig.pathSeparator)) {
meganrogge marked this conversation as resolved.
Show resolved Hide resolved
// // add a dot to the beginning of the label if it doesn't already have one
// label = `.${resourceRequestConfig.pathSeparator}${fileName}`;
// } else {
label = `${lastWordRelativeFolder}${fileName}`;
// if (lastWord.endsWith(resourceRequestConfig.pathSeparator)) {
// label = `${lastWord}${fileName}`;
// } else {
// label = `${lastWord}${resourceRequestConfig.pathSeparator}${fileName}`;
// }
// if (lastWord.length && lastWord.at(-1) !== resourceRequestConfig.pathSeparator && lastWord.at(-1) !== '.') {
// label = `.${resourceRequestConfig.pathSeparator}${fileName}`;
// }
// }
if (isDirectory && !label.endsWith(resourceRequestConfig.pathSeparator)) {
label = label + resourceRequestConfig.pathSeparator;
}

// Normalize path separator to `\` on Windows. It should act the exact same as `/` but
// suggestions should all use `\`
if (isWindows) {
label = label.replaceAll('/', '\\');
}

resourceCompletions.push({
label,
provider,
Expand All @@ -280,6 +326,7 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
});
}

console.log('resourceCompletions', resourceCompletions);

return resourceCompletions.length ? resourceCompletions : undefined;
}
Expand Down
Loading