Skip to content

Commit 6717d92

Browse files
morahanclaudemaria-rcks
authored
fix(web): Tab completes folder names in the add-project browser (pingdotgg#28243)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 77c885e commit 6717d92

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
createBrowseNavigationCoordinator,
3333
filterFilesystemBrowseEntries,
3434
getFilesystemBrowsePath,
35+
getFilesystemBrowseTabCompletion,
3536
} from "@t3tools/client-runtime/state/filesystem";
3637
import {
3738
isAtomCommandInterrupted,
@@ -2492,6 +2493,30 @@ function OpenCommandPaletteDialog(props: {
24922493
}
24932494

24942495
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) return;
2496+
2497+
if (event.key === "Tab" && isBrowsing && !relativePathNeedsActiveProject) {
2498+
if (event.nativeEvent.isComposing || event.keyCode === 229) return;
2499+
(event as typeof event & { preventBaseUIHandler?: () => void }).preventBaseUIHandler?.();
2500+
event.preventDefault();
2501+
if (highlightedItemValue === "browse:up") {
2502+
void browseUp();
2503+
return;
2504+
}
2505+
const highlightedEntry = visibleBrowseEntries.find(
2506+
(entry) => `browse:${entry.fullPath}` === highlightedItemValue,
2507+
);
2508+
const completion = highlightedEntry
2509+
? { name: highlightedEntry.name, isComplete: true }
2510+
: getFilesystemBrowseTabCompletion(visibleBrowseEntries, browsePath.filterQuery);
2511+
if (completion?.isComplete) {
2512+
void browseTo(completion.name);
2513+
} else if (completion && !pinnedCloneDirectoryName) {
2514+
setHighlightedItemValue(null);
2515+
setQuery(`${getBrowseDirectoryPath(query)}${completion.name}`);
2516+
}
2517+
return;
2518+
}
2519+
24952520
const firstItemValue = autoHighlightsFirstRow ? resultRows.itemValues[0] : undefined;
24962521
if (
24972522
event.key === "ArrowDown" &&

‎packages/client-runtime/src/state/filesystem.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { AuthFilesystemReadScope, AuthOrchestrationReadScope } from "@t3tools/co
44
import {
55
createBrowseNavigationCoordinator,
66
getFilesystemBrowsePath,
7+
getFilesystemBrowseTabCompletion,
78
resolveFilesystemReadAccess,
89
} from "./filesystem.ts";
910

@@ -79,6 +80,33 @@ describe("filesystem browse model", () => {
7980
expect(getFilesystemBrowsePath("C:\\Users\\test", "MacIntel").isBrowsing).toBe(false);
8081
expect(getFilesystemBrowsePath("~/projects/", "", false).isBrowsing).toBe(false);
8182
});
83+
84+
it("completes Tab like a shell", () => {
85+
const entry = (name: string) => ({ name, fullPath: `/Users/me/${name}` });
86+
expect(getFilesystemBrowseTabCompletion([entry("Creations")], "Creation")).toEqual({
87+
name: "Creations",
88+
isComplete: true,
89+
});
90+
expect(getFilesystemBrowseTabCompletion([entry("OpenSourced"), entry("OpenAI")], "op")).toEqual(
91+
{ name: "Open", isComplete: false },
92+
);
93+
expect(getFilesystemBrowseTabCompletion([entry("Open"), entry("Opal")], "Op")).toBeNull();
94+
expect(getFilesystemBrowseTabCompletion([entry("openAI"), entry("OpenSource")], "o")).toEqual({
95+
name: "open",
96+
isComplete: false,
97+
});
98+
expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😁two")], "")).toBeNull();
99+
expect(getFilesystemBrowseTabCompletion([entry("ΟΣα"), entry("οσβ")], "ο")).toBeNull();
100+
expect(getFilesystemBrowseTabCompletion([entry("İfoo"), entry("İfar")], "i\u0307")).toEqual({
101+
name: "İf",
102+
isComplete: false,
103+
});
104+
expect(getFilesystemBrowseTabCompletion([entry("😀one"), entry("😀other")], "😀")).toEqual({
105+
name: "😀o",
106+
isComplete: false,
107+
});
108+
expect(getFilesystemBrowseTabCompletion([], "x")).toBeNull();
109+
});
82110
});
83111

84112
describe("browse navigation", () => {

‎packages/client-runtime/src/state/filesystem.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ export function filterFilesystemBrowseEntries(
8787
return { visibleEntries, exactEntry };
8888
}
8989

90+
/** Shell-style Tab: a single match enters the directory, several extend to their common prefix. */
91+
export function getFilesystemBrowseTabCompletion(
92+
visibleEntries: ReadonlyArray<FilesystemBrowseEntry>,
93+
filterQuery: string,
94+
): { readonly name: string; readonly isComplete: boolean } | null {
95+
const [first, ...rest] = visibleEntries;
96+
if (first === undefined) return null;
97+
if (rest.length === 0) return { name: first.name, isComplete: true };
98+
let prefix = Array.from(first.name);
99+
for (const entry of rest) {
100+
const characters = Array.from(entry.name);
101+
let length = 0;
102+
while (
103+
length < prefix.length &&
104+
prefix[length]?.toLowerCase() === characters[length]?.toLowerCase()
105+
)
106+
length += 1;
107+
prefix = prefix.slice(0, length);
108+
}
109+
const name = prefix.join("");
110+
const lowerName = name.toLowerCase();
111+
return lowerName.length > filterQuery.toLowerCase().length &&
112+
visibleEntries.every((entry) => entry.name.toLowerCase().startsWith(lowerName))
113+
? { name, isComplete: false }
114+
: null;
115+
}
116+
90117
export function createBrowseNavigationCoordinator() {
91118
let generation = 0;
92119

0 commit comments

Comments
 (0)