@greppleai/grip-tui 0.0.0-stage → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +818 -2
- package/dist/autocomplete.d.ts +48 -0
- package/dist/autocomplete.d.ts.map +1 -0
- package/dist/autocomplete.js +566 -0
- package/dist/autocomplete.js.map +1 -0
- package/dist/components/alt-screen-flash.d.ts +13 -0
- package/dist/components/alt-screen-flash.d.ts.map +1 -0
- package/dist/components/alt-screen-flash.js +37 -0
- package/dist/components/alt-screen-flash.js.map +1 -0
- package/dist/components/box.d.ts +22 -0
- package/dist/components/box.d.ts.map +1 -0
- package/dist/components/box.js +104 -0
- package/dist/components/box.js.map +1 -0
- package/dist/components/cancellable-loader.d.ts +22 -0
- package/dist/components/cancellable-loader.d.ts.map +1 -0
- package/dist/components/cancellable-loader.js +35 -0
- package/dist/components/cancellable-loader.js.map +1 -0
- package/dist/components/editor.d.ts +292 -0
- package/dist/components/editor.d.ts.map +1 -0
- package/dist/components/editor.js +1887 -0
- package/dist/components/editor.js.map +1 -0
- package/dist/components/h-stack.d.ts +7 -0
- package/dist/components/h-stack.d.ts.map +1 -0
- package/dist/components/h-stack.js +43 -0
- package/dist/components/h-stack.js.map +1 -0
- package/dist/components/image.d.ts +32 -0
- package/dist/components/image.d.ts.map +1 -0
- package/dist/components/image.js +76 -0
- package/dist/components/image.js.map +1 -0
- package/dist/components/input.d.ts +46 -0
- package/dist/components/input.d.ts.map +1 -0
- package/dist/components/input.js +352 -0
- package/dist/components/input.js.map +1 -0
- package/dist/components/loader.d.ts +32 -0
- package/dist/components/loader.d.ts.map +1 -0
- package/dist/components/loader.js +80 -0
- package/dist/components/loader.js.map +1 -0
- package/dist/components/markdown.d.ts +132 -0
- package/dist/components/markdown.d.ts.map +1 -0
- package/dist/components/markdown.js +717 -0
- package/dist/components/markdown.js.map +1 -0
- package/dist/components/scroll-view.d.ts +53 -0
- package/dist/components/scroll-view.d.ts.map +1 -0
- package/dist/components/scroll-view.js +173 -0
- package/dist/components/scroll-view.js.map +1 -0
- package/dist/components/select-list.d.ts +50 -0
- package/dist/components/select-list.d.ts.map +1 -0
- package/dist/components/select-list.js +159 -0
- package/dist/components/select-list.js.map +1 -0
- package/dist/components/settings-list.d.ts +52 -0
- package/dist/components/settings-list.d.ts.map +1 -0
- package/dist/components/settings-list.js +187 -0
- package/dist/components/settings-list.js.map +1 -0
- package/dist/components/spacer.d.ts +12 -0
- package/dist/components/spacer.d.ts.map +1 -0
- package/dist/components/spacer.js +23 -0
- package/dist/components/spacer.js.map +1 -0
- package/dist/components/stack.d.ts +32 -0
- package/dist/components/stack.d.ts.map +1 -0
- package/dist/components/stack.js +122 -0
- package/dist/components/stack.js.map +1 -0
- package/dist/components/text.d.ts +19 -0
- package/dist/components/text.d.ts.map +1 -0
- package/dist/components/text.js +89 -0
- package/dist/components/text.js.map +1 -0
- package/dist/components/truncated-text.d.ts +13 -0
- package/dist/components/truncated-text.d.ts.map +1 -0
- package/dist/components/truncated-text.js +51 -0
- package/dist/components/truncated-text.js.map +1 -0
- package/dist/components/v-stack.d.ts +8 -0
- package/dist/components/v-stack.d.ts.map +1 -0
- package/dist/components/v-stack.js +27 -0
- package/dist/components/v-stack.js.map +1 -0
- package/dist/editor-component.d.ts +39 -0
- package/dist/editor-component.d.ts.map +1 -0
- package/dist/editor-component.js +2 -0
- package/dist/editor-component.js.map +1 -0
- package/dist/fuzzy.d.ts +16 -0
- package/dist/fuzzy.d.ts.map +1 -0
- package/dist/fuzzy.js +101 -0
- package/dist/fuzzy.js.map +1 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/keybindings.d.ts +243 -0
- package/dist/keybindings.d.ts.map +1 -0
- package/dist/keybindings.js +209 -0
- package/dist/keybindings.js.map +1 -0
- package/dist/keys.d.ts +184 -0
- package/dist/keys.d.ts.map +1 -0
- package/dist/keys.js +1053 -0
- package/dist/keys.js.map +1 -0
- package/dist/kill-ring.d.ts +30 -0
- package/dist/kill-ring.d.ts.map +1 -0
- package/dist/kill-ring.js +37 -0
- package/dist/kill-ring.js.map +1 -0
- package/dist/latex.d.ts +10 -0
- package/dist/latex.d.ts.map +1 -0
- package/dist/latex.js +1406 -0
- package/dist/latex.js.map +1 -0
- package/dist/layout-node.d.ts +43 -0
- package/dist/layout-node.d.ts.map +1 -0
- package/dist/layout-node.js +6 -0
- package/dist/layout-node.js.map +1 -0
- package/dist/layout.d.ts +40 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +386 -0
- package/dist/layout.js.map +1 -0
- package/dist/native-modifiers.d.ts +3 -0
- package/dist/native-modifiers.d.ts.map +1 -0
- package/dist/native-modifiers.js +53 -0
- package/dist/native-modifiers.js.map +1 -0
- package/dist/stdin-buffer.d.ts +58 -0
- package/dist/stdin-buffer.d.ts.map +1 -0
- package/dist/stdin-buffer.js +318 -0
- package/dist/stdin-buffer.js.map +1 -0
- package/dist/terminal-control.d.ts +5 -0
- package/dist/terminal-control.d.ts.map +1 -0
- package/dist/terminal-control.js +26 -0
- package/dist/terminal-control.js.map +1 -0
- package/dist/terminal-image.d.ts +112 -0
- package/dist/terminal-image.d.ts.map +1 -0
- package/dist/terminal-image.js +508 -0
- package/dist/terminal-image.js.map +1 -0
- package/dist/terminal.d.ts +117 -0
- package/dist/terminal.d.ts.map +1 -0
- package/dist/terminal.js +490 -0
- package/dist/terminal.js.map +1 -0
- package/dist/tui-alt-screen.d.ts +114 -0
- package/dist/tui-alt-screen.d.ts.map +1 -0
- package/dist/tui-alt-screen.js +950 -0
- package/dist/tui-alt-screen.js.map +1 -0
- package/dist/tui-main-screen.d.ts +52 -0
- package/dist/tui-main-screen.d.ts.map +1 -0
- package/dist/tui-main-screen.js +514 -0
- package/dist/tui-main-screen.js.map +1 -0
- package/dist/tui.d.ts +295 -0
- package/dist/tui.d.ts.map +1 -0
- package/dist/tui.js +868 -0
- package/dist/tui.js.map +1 -0
- package/dist/undo-stack.d.ts +17 -0
- package/dist/undo-stack.d.ts.map +1 -0
- package/dist/undo-stack.js +25 -0
- package/dist/undo-stack.js.map +1 -0
- package/dist/utils.d.ts +96 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1106 -0
- package/dist/utils.js.map +1 -0
- package/dist/word-navigation.d.ts +25 -0
- package/dist/word-navigation.d.ts.map +1 -0
- package/dist/word-navigation.js +94 -0
- package/dist/word-navigation.js.map +1 -0
- package/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
- package/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
- package/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
- package/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
- package/package.json +47 -5
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { SelectItem } from "./components/select-list.ts";
|
|
2
|
+
export interface AutocompleteSuggestionRequest {
|
|
3
|
+
signal: AbortSignal;
|
|
4
|
+
force?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export interface AutocompleteCompletion {
|
|
7
|
+
lines: string[];
|
|
8
|
+
cursorLine: number;
|
|
9
|
+
cursorCol: number;
|
|
10
|
+
}
|
|
11
|
+
export type AutocompleteItem = SelectItem;
|
|
12
|
+
type Awaitable<T> = T | Promise<T>;
|
|
13
|
+
export interface SlashCommand {
|
|
14
|
+
name: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
argumentHint?: string;
|
|
17
|
+
getArgumentCompletions?(argumentPrefix: string): Awaitable<AutocompleteItem[] | null>;
|
|
18
|
+
}
|
|
19
|
+
export interface AutocompleteSuggestions {
|
|
20
|
+
items: AutocompleteItem[];
|
|
21
|
+
prefix: string;
|
|
22
|
+
}
|
|
23
|
+
export interface AutocompleteProvider {
|
|
24
|
+
getSuggestions(lines: string[], cursorLine: number, cursorCol: number, options: AutocompleteSuggestionRequest): Promise<AutocompleteSuggestions | null>;
|
|
25
|
+
applyCompletion(lines: string[], cursorLine: number, cursorCol: number, item: AutocompleteItem, prefix: string): AutocompleteCompletion;
|
|
26
|
+
shouldTriggerFileCompletion?(lines: string[], cursorLine: number, cursorCol: number): boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare class CombinedAutocompleteProvider implements AutocompleteProvider {
|
|
29
|
+
private commands;
|
|
30
|
+
private basePath;
|
|
31
|
+
private fdPath;
|
|
32
|
+
constructor(commands: (SelectItem | SlashCommand)[] | undefined, basePath: string, fdPath?: string | null);
|
|
33
|
+
getSuggestions(lines: string[], cursorLine: number, cursorCol: number, options: AutocompleteSuggestionRequest): Promise<AutocompleteSuggestions | null>;
|
|
34
|
+
private getSlashCommandSuggestions;
|
|
35
|
+
applyCompletion(lines: string[], cursorLine: number, cursorCol: number, item: AutocompleteItem, prefix: string): AutocompleteCompletion;
|
|
36
|
+
private extractAtPrefix;
|
|
37
|
+
private extractPathPrefix;
|
|
38
|
+
private expandHomePath;
|
|
39
|
+
private resolveScopedFuzzyQuery;
|
|
40
|
+
private scopedPathForDisplay;
|
|
41
|
+
private resolveFileSuggestionSearch;
|
|
42
|
+
private getFileSuggestions;
|
|
43
|
+
private scoreEntry;
|
|
44
|
+
private getFuzzyFileSuggestions;
|
|
45
|
+
shouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=autocomplete.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"autocomplete.d.ts","sourceRoot":"","sources":["../src/autocomplete.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAoB9D,MAAM,WAAW,6BAA6B;IAC7C,MAAM,EAAE,WAAW,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,sBAAsB;IACtC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CAClB;AAyPD,MAAM,MAAM,gBAAgB,GAAG,UAAU,CAAC;AAE1C,KAAK,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEnC,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IAGtB,sBAAsB,CAAC,CAAC,cAAc,EAAE,MAAM,GAAG,SAAS,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAAC;CACtF;AA6BD,MAAM,WAAW,uBAAuB;IACvC,KAAK,EAAE,gBAAgB,EAAE,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,oBAAoB;IAGpC,cAAc,CACb,KAAK,EAAE,MAAM,EAAE,EACf,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,6BAA6B,GACpC,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC,CAAC;IAI3C,eAAe,CACd,KAAK,EAAE,MAAM,EAAE,EACf,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,gBAAgB,EACtB,MAAM,EAAE,MAAM,GACZ,sBAAsB,CAAC;IAG1B,2BAA2B,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC;CAC9F;AAGD,qBAAa,4BAA6B,YAAW,oBAAoB;IACxE,OAAO,CAAC,QAAQ,CAAsC;IACtD,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,MAAM,CAAgB;IAE9B,YAAY,QAAQ,2CAA0C,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAE,MAAM,GAAG,IAAW,EAI7G;IAEK,cAAc,CACnB,KAAK,EAAE,MAAM,EAAE,EACf,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,6BAA6B,GACpC,OAAO,CAAC,uBAAuB,GAAG,IAAI,CAAC,CAmCzC;YAEa,0BAA0B;IAkCxC,eAAe,CACd,KAAK,EAAE,MAAM,EAAE,EACf,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,gBAAgB,EACtB,MAAM,EAAE,MAAM,GACZ,sBAAsB,CAwDxB;IAGD,OAAO,CAAC,eAAe;IAiBvB,OAAO,CAAC,iBAAiB;IA8BzB,OAAO,CAAC,cAAc;IAWtB,OAAO,CAAC,uBAAuB;IA8B/B,OAAO,CAAC,oBAAoB;IAQ5B,OAAO,CAAC,2BAA2B;IAwBnC,OAAO,CAAC,kBAAkB;IAsC1B,OAAO,CAAC,UAAU;YAuBJ,uBAAuB;IAuDrC,2BAA2B,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAU3F;CACD","sourcesContent":["import { spawn } from \"child_process\";\nimport { type Dirent, readdirSync, statSync } from \"fs\";\nimport { homedir } from \"os\";\nimport { basename, dirname, join } from \"path\";\nimport type { SelectItem } from \"./components/select-list.ts\";\nimport { fuzzyFilter } from \"./fuzzy.ts\";\n\ninterface ParsedPathPrefix {\n\trawPrefix: string;\n\tisAtPrefix: boolean;\n\tisQuotedPrefix: boolean;\n}\n\ninterface CompletionValueOptions {\n\tisDirectory: boolean;\n\tisAtPrefix: boolean;\n\tisQuotedPrefix: boolean;\n}\n\ninterface FileSearchEntry {\n\tpath: string;\n\tisDirectory: boolean;\n}\n\nexport interface AutocompleteSuggestionRequest {\n\tsignal: AbortSignal;\n\tforce?: boolean;\n}\n\nexport interface AutocompleteCompletion {\n\tlines: string[];\n\tcursorLine: number;\n\tcursorCol: number;\n}\n\ninterface ScopedFuzzyQuery {\n\tbaseDir: string;\n\tquery: string;\n\tdisplayBase: string;\n}\n\ninterface FuzzyFileSuggestionOptions {\n\tisQuotedPrefix: boolean;\n\tsignal: AbortSignal;\n}\n\ninterface FileSuggestionSearch {\n\trawPrefix: string;\n\tisAtPrefix: boolean;\n\tisQuotedPrefix: boolean;\n\tsearchDir: string;\n\tsearchPrefix: string;\n}\n\nconst PATH_DELIMITERS = new Set([\" \", \"\\t\", '\"', \"'\", \"=\"]);\nconst ROOT_PATH_PREFIXES = new Set([\"\", \"./\", \"../\", \"~\", \"~/\", \"/\"]);\n\nfunction isFileSuggestionDirectory(searchDir: string, entry: Dirent): boolean {\n\tif (entry.isDirectory()) return true;\n\tif (!entry.isSymbolicLink()) return false;\n\ttry {\n\t\treturn statSync(join(searchDir, entry.name)).isDirectory();\n\t} catch {\n\t\treturn false;\n\t}\n}\n\nfunction buildRelativeSuggestionPath(displayPrefix: string, name: string): string {\n\tif (displayPrefix.endsWith(\"/\")) return displayPrefix + name;\n\tconst hasDirectoryPrefix = displayPrefix.includes(\"/\") || displayPrefix.includes(\"\\\\\");\n\tif (!hasDirectoryPrefix) return displayPrefix.startsWith(\"~\") ? `~/${name}` : name;\n\tif (displayPrefix.startsWith(\"~/\")) {\n\t\tconst dir = dirname(displayPrefix.slice(2));\n\t\treturn `~/${dir === \".\" ? name : join(dir, name)}`;\n\t}\n\tif (displayPrefix.startsWith(\"/\")) {\n\t\tconst dir = dirname(displayPrefix);\n\t\treturn dir === \"/\" ? `/${name}` : `${dir}/${name}`;\n\t}\n\tconst relativePath = join(dirname(displayPrefix), name);\n\treturn displayPrefix.startsWith(\"./\") && !relativePath.startsWith(\"./\") ? `./${relativePath}` : relativePath;\n}\n\nfunction toDisplayPath(value: string): string {\n\treturn value.replace(/\\\\/g, \"/\");\n}\n\nfunction escapeRegex(value: string): string {\n\treturn value.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction buildFdPathQuery(query: string): string {\n\tconst normalized = toDisplayPath(query);\n\tif (!normalized.includes(\"/\")) {\n\t\treturn normalized;\n\t}\n\n\tconst hasTrailingSeparator = normalized.endsWith(\"/\");\n\tconst trimmed = normalized.replace(/^\\/+|\\/+$/g, \"\");\n\tif (!trimmed) {\n\t\treturn normalized;\n\t}\n\n\tconst separatorPattern = \"[\\\\\\\\/]\";\n\tconst segments = trimmed\n\t\t.split(\"/\")\n\t\t.filter(Boolean)\n\t\t.map((segment) => escapeRegex(segment));\n\tif (segments.length === 0) {\n\t\treturn normalized;\n\t}\n\n\tlet pattern = segments.join(separatorPattern);\n\tif (hasTrailingSeparator) {\n\t\tpattern += separatorPattern;\n\t}\n\treturn pattern;\n}\n\nfunction findLastDelimiter(text: string): number {\n\tfor (let i = text.length - 1; i >= 0; i -= 1) {\n\t\tif (PATH_DELIMITERS.has(text[i] ?? \"\")) {\n\t\t\treturn i;\n\t\t}\n\t}\n\treturn -1;\n}\n\nfunction findUnclosedQuoteStart(text: string): number | null {\n\tlet inQuotes = false;\n\tlet quoteStart = -1;\n\n\tfor (let i = 0; i < text.length; i += 1) {\n\t\tif (text[i] === '\"') {\n\t\t\tinQuotes = !inQuotes;\n\t\t\tif (inQuotes) {\n\t\t\t\tquoteStart = i;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn inQuotes ? quoteStart : null;\n}\n\nfunction isTokenStart(text: string, index: number): boolean {\n\treturn index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? \"\");\n}\n\nfunction extractQuotedPrefix(text: string): string | null {\n\tconst quoteStart = findUnclosedQuoteStart(text);\n\tif (quoteStart === null) {\n\t\treturn null;\n\t}\n\n\tif (quoteStart > 0 && text[quoteStart - 1] === \"@\") {\n\t\tif (!isTokenStart(text, quoteStart - 1)) {\n\t\t\treturn null;\n\t\t}\n\t\treturn text.slice(quoteStart - 1);\n\t}\n\n\tif (!isTokenStart(text, quoteStart)) {\n\t\treturn null;\n\t}\n\n\treturn text.slice(quoteStart);\n}\n\nfunction parsePathPrefix(prefix: string): ParsedPathPrefix {\n\tif (prefix.startsWith('@\"')) {\n\t\treturn { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true };\n\t}\n\tif (prefix.startsWith('\"')) {\n\t\treturn { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true };\n\t}\n\tif (prefix.startsWith(\"@\")) {\n\t\treturn { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false };\n\t}\n\treturn { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false };\n}\n\nfunction buildCompletionValue(path: string, options: CompletionValueOptions): string {\n\tconst needsQuotes = options.isQuotedPrefix || path.includes(\" \");\n\tconst prefix = options.isAtPrefix ? \"@\" : \"\";\n\n\tif (!needsQuotes) {\n\t\treturn `${prefix}${path}`;\n\t}\n\n\tconst openQuote = `${prefix}\"`;\n\tconst closeQuote = '\"';\n\treturn `${openQuote}${path}${closeQuote}`;\n}\n\nfunction parseFdSearchOutput(stdout: string, exitCode: number | null, aborted: boolean): FileSearchEntry[] {\n\tif (aborted || exitCode !== 0 || !stdout) return [];\n\tconst results: FileSearchEntry[] = [];\n\tfor (const line of stdout.trim().split(\"\\n\").filter(Boolean)) {\n\t\tconst displayLine = toDisplayPath(line);\n\t\tconst hasTrailingSeparator = displayLine.endsWith(\"/\");\n\t\tconst normalizedPath = hasTrailingSeparator ? displayLine.slice(0, -1) : displayLine;\n\t\tif (normalizedPath === \".git\" || normalizedPath.startsWith(\".git/\") || normalizedPath.includes(\"/.git/\")) {\n\t\t\tcontinue;\n\t\t}\n\t\tresults.push({ path: displayLine, isDirectory: hasTrailingSeparator });\n\t}\n\treturn results;\n}\n\nasync function walkDirectoryWithFd(\n\tbaseDir: string,\n\tfdPath: string,\n\tquery: string,\n\tmaxResults: number,\n\tsignal: AbortSignal,\n): Promise<FileSearchEntry[]> {\n\tconst args = [\n\t\t\"--base-directory\",\n\t\tbaseDir,\n\t\t\"--max-results\",\n\t\tString(maxResults),\n\t\t\"--type\",\n\t\t\"f\",\n\t\t\"--type\",\n\t\t\"d\",\n\t\t\"--follow\",\n\t\t\"--hidden\",\n\t\t\"--exclude\",\n\t\t\".git\",\n\t\t\"--exclude\",\n\t\t\".git/*\",\n\t\t\"--exclude\",\n\t\t\".git/**\",\n\t];\n\n\tif (toDisplayPath(query).includes(\"/\")) {\n\t\targs.push(\"--full-path\");\n\t}\n\n\tif (query) {\n\t\targs.push(buildFdPathQuery(query));\n\t}\n\n\treturn await new Promise((resolve) => {\n\t\tif (signal.aborted) {\n\t\t\tresolve([]);\n\t\t\treturn;\n\t\t}\n\n\t\tconst child = spawn(fdPath, args, {\n\t\t\tstdio: [\"ignore\", \"pipe\", \"pipe\"],\n\t\t});\n\t\tlet stdout = \"\";\n\t\tlet resolved = false;\n\n\t\tconst finish = (results: FileSearchEntry[]) => {\n\t\t\tif (resolved) return;\n\t\t\tresolved = true;\n\t\t\tsignal.removeEventListener(\"abort\", onAbort);\n\t\t\tresolve(results);\n\t\t};\n\n\t\tconst onAbort = () => {\n\t\t\tif (child.exitCode === null) {\n\t\t\t\tchild.kill(\"SIGKILL\");\n\t\t\t}\n\t\t};\n\n\t\tsignal.addEventListener(\"abort\", onAbort, { once: true });\n\t\tchild.stdout.setEncoding(\"utf-8\");\n\t\tchild.stdout.on(\"data\", (chunk: string) => {\n\t\t\tstdout += chunk;\n\t\t});\n\t\tchild.on(\"error\", () => {\n\t\t\tfinish([]);\n\t\t});\n\t\tchild.on(\"close\", (code) => {\n\t\t\tfinish(parseFdSearchOutput(stdout, code, signal.aborted));\n\t\t});\n\t});\n}\n\nexport type AutocompleteItem = SelectItem;\n\ntype Awaitable<T> = T | Promise<T>;\n\nexport interface SlashCommand {\n\tname: string;\n\tdescription?: string;\n\targumentHint?: string;\n\t// Function to get argument completions for this command\n\t// Returns null if no argument completion is available\n\tgetArgumentCompletions?(argumentPrefix: string): Awaitable<AutocompleteItem[] | null>;\n}\n\ninterface CommandSuggestionItem {\n\tname: string;\n\tlabel: string;\n\tdescription?: string;\n}\n\nfunction toCommandSuggestionItem(command: SlashCommand | AutocompleteItem): CommandSuggestionItem {\n\tconst name = \"name\" in command ? command.name : command.value;\n\tconst hint = \"argumentHint\" in command && command.argumentHint ? command.argumentHint : undefined;\n\tconst description = command.description ?? \"\";\n\tconst fullDescription = hint ? (description ? `${hint} — ${description}` : hint) : description;\n\treturn {\n\t\tname,\n\t\tlabel: name,\n\t\tdescription: fullDescription || undefined,\n\t};\n}\n\nfunction isSlashCommandCompletion(prefix: string, beforePrefix: string): boolean {\n\treturn prefix.startsWith(\"/\") && beforePrefix.trim() === \"\" && !prefix.slice(1).includes(\"/\");\n}\n\nfunction completionCursorOffset(item: AutocompleteItem): number {\n\tconst trailingQuoteOffset = item.label.endsWith(\"/\") && item.value.endsWith('\"') ? 1 : 0;\n\treturn item.value.length - trailingQuoteOffset;\n}\n\nexport interface AutocompleteSuggestions {\n\titems: AutocompleteItem[];\n\tprefix: string; // What we're matching against (e.g., \"/\" or \"src/\")\n}\n\nexport interface AutocompleteProvider {\n\t// Get autocomplete suggestions for current text/cursor position\n\t// Returns null if no suggestions available\n\tgetSuggestions(\n\t\tlines: string[],\n\t\tcursorLine: number,\n\t\tcursorCol: number,\n\t\toptions: AutocompleteSuggestionRequest,\n\t): Promise<AutocompleteSuggestions | null>;\n\n\t// Apply the selected item\n\t// Returns the new text and cursor position\n\tapplyCompletion(\n\t\tlines: string[],\n\t\tcursorLine: number,\n\t\tcursorCol: number,\n\t\titem: AutocompleteItem,\n\t\tprefix: string,\n\t): AutocompleteCompletion;\n\n\t// Check if file completion should trigger for explicit Tab completion\n\tshouldTriggerFileCompletion?(lines: string[], cursorLine: number, cursorCol: number): boolean;\n}\n\n// Combined provider that handles both slash commands and file paths\nexport class CombinedAutocompleteProvider implements AutocompleteProvider {\n\tprivate commands: (SlashCommand | AutocompleteItem)[];\n\tprivate basePath: string;\n\tprivate fdPath: string | null;\n\n\tconstructor(commands: (SlashCommand | AutocompleteItem)[] = [], basePath: string, fdPath: string | null = null) {\n\t\tthis.commands = commands;\n\t\tthis.basePath = basePath;\n\t\tthis.fdPath = fdPath;\n\t}\n\n\tasync getSuggestions(\n\t\tlines: string[],\n\t\tcursorLine: number,\n\t\tcursorCol: number,\n\t\toptions: AutocompleteSuggestionRequest,\n\t): Promise<AutocompleteSuggestions | null> {\n\t\tconst currentLine = lines[cursorLine] || \"\";\n\t\tconst textBeforeCursor = currentLine.slice(0, cursorCol);\n\n\t\tconst atPrefix = this.extractAtPrefix(textBeforeCursor);\n\t\tif (atPrefix) {\n\t\t\tconst { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix);\n\t\t\tconst suggestions = await this.getFuzzyFileSuggestions(rawPrefix, {\n\t\t\t\tisQuotedPrefix,\n\t\t\t\tsignal: options.signal,\n\t\t\t});\n\t\t\tif (suggestions.length === 0) return null;\n\n\t\t\treturn {\n\t\t\t\titems: suggestions,\n\t\t\t\tprefix: atPrefix,\n\t\t\t};\n\t\t}\n\n\t\tif (!options.force && textBeforeCursor.startsWith(\"/\")) {\n\t\t\treturn await this.getSlashCommandSuggestions(textBeforeCursor);\n\t\t}\n\n\t\tconst pathMatch = this.extractPathPrefix(textBeforeCursor, options.force ?? false);\n\t\tif (pathMatch === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst suggestions = this.getFileSuggestions(pathMatch);\n\t\tif (suggestions.length === 0) return null;\n\n\t\treturn {\n\t\t\titems: suggestions,\n\t\t\tprefix: pathMatch,\n\t\t};\n\t}\n\n\tprivate async getSlashCommandSuggestions(textBeforeCursor: string): Promise<AutocompleteSuggestions | null> {\n\t\tconst spaceIndex = textBeforeCursor.indexOf(\" \");\n\t\tif (spaceIndex === -1) {\n\t\t\tconst prefix = textBeforeCursor.slice(1);\n\t\t\tconst commandItems = this.commands.map(toCommandSuggestionItem);\n\n\t\t\tconst filtered = fuzzyFilter(commandItems, prefix, (item) => item.name).map((item) => ({\n\t\t\t\tvalue: item.name,\n\t\t\t\tlabel: item.label,\n\t\t\t\t...(item.description && { description: item.description }),\n\t\t\t}));\n\t\t\tif (filtered.length === 0) return null;\n\n\t\t\treturn { items: filtered, prefix: textBeforeCursor };\n\t\t}\n\n\t\tconst commandName = textBeforeCursor.slice(1, spaceIndex);\n\t\tconst argumentText = textBeforeCursor.slice(spaceIndex + 1);\n\t\tconst command = this.commands.find((candidate) => {\n\t\t\tconst name = \"name\" in candidate ? candidate.name : candidate.value;\n\t\t\treturn name === commandName;\n\t\t});\n\t\tif (!command || !(\"getArgumentCompletions\" in command) || !command.getArgumentCompletions) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst argumentSuggestions = await command.getArgumentCompletions(argumentText);\n\t\tif (!Array.isArray(argumentSuggestions) || argumentSuggestions.length === 0) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn { items: argumentSuggestions, prefix: argumentText };\n\t}\n\n\tapplyCompletion(\n\t\tlines: string[],\n\t\tcursorLine: number,\n\t\tcursorCol: number,\n\t\titem: AutocompleteItem,\n\t\tprefix: string,\n\t): AutocompleteCompletion {\n\t\tconst currentLine = lines[cursorLine] || \"\";\n\t\tconst beforePrefix = currentLine.slice(0, cursorCol - prefix.length);\n\t\tconst afterCursor = currentLine.slice(cursorCol);\n\t\tconst isQuotedPrefix = prefix.startsWith('\"') || prefix.startsWith('@\"');\n\t\tconst hasLeadingQuoteAfterCursor = afterCursor.startsWith('\"');\n\t\tconst hasTrailingQuoteInItem = item.value.endsWith('\"');\n\t\tconst adjustedAfterCursor =\n\t\t\tisQuotedPrefix && hasTrailingQuoteInItem && hasLeadingQuoteAfterCursor ? afterCursor.slice(1) : afterCursor;\n\n\t\t// Check if we're completing a slash command (prefix starts with \"/\" but NOT a file path)\n\t\t// Slash commands are at the start of the line and don't contain path separators after the first /.\n\t\tif (isSlashCommandCompletion(prefix, beforePrefix)) {\n\t\t\t// This is a command name completion\n\t\t\tconst newLine = `${beforePrefix}/${item.value} ${adjustedAfterCursor}`;\n\t\t\tconst newLines = [...lines];\n\t\t\tnewLines[cursorLine] = newLine;\n\n\t\t\treturn {\n\t\t\t\tlines: newLines,\n\t\t\t\tcursorLine,\n\t\t\t\tcursorCol: beforePrefix.length + item.value.length + 2, // +2 for \"/\" and space\n\t\t\t};\n\t\t}\n\n\t\t// Check if we're completing a file attachment (prefix starts with \"@\")\n\t\tif (prefix.startsWith(\"@\")) {\n\t\t\t// This is a file attachment completion\n\t\t\t// Don't add space after directories so user can continue autocompleting\n\t\t\tconst isDirectory = item.label.endsWith(\"/\");\n\t\t\tconst suffix = isDirectory ? \"\" : \" \";\n\t\t\tconst newLine = `${beforePrefix + item.value}${suffix}${adjustedAfterCursor}`;\n\t\t\tconst newLines = [...lines];\n\t\t\tnewLines[cursorLine] = newLine;\n\n\t\t\tconst cursorOffset = completionCursorOffset(item);\n\n\t\t\treturn {\n\t\t\t\tlines: newLines,\n\t\t\t\tcursorLine,\n\t\t\t\tcursorCol: beforePrefix.length + cursorOffset + suffix.length,\n\t\t\t};\n\t\t}\n\n\t\t// For file paths, complete the path\n\t\tconst newLine = beforePrefix + item.value + adjustedAfterCursor;\n\t\tconst newLines = [...lines];\n\t\tnewLines[cursorLine] = newLine;\n\n\t\tconst cursorOffset = completionCursorOffset(item);\n\n\t\treturn {\n\t\t\tlines: newLines,\n\t\t\tcursorLine,\n\t\t\tcursorCol: beforePrefix.length + cursorOffset,\n\t\t};\n\t}\n\n\t// Extract @ prefix for fuzzy file suggestions\n\tprivate extractAtPrefix(text: string): string | null {\n\t\tconst quotedPrefix = extractQuotedPrefix(text);\n\t\tif (quotedPrefix?.startsWith('@\"')) {\n\t\t\treturn quotedPrefix;\n\t\t}\n\n\t\tconst lastDelimiterIndex = findLastDelimiter(text);\n\t\tconst tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1;\n\n\t\tif (text[tokenStart] === \"@\") {\n\t\t\treturn text.slice(tokenStart);\n\t\t}\n\n\t\treturn null;\n\t}\n\n\t// Extract a path-like prefix from the text before cursor\n\tprivate extractPathPrefix(text: string, forceExtract: boolean = false): string | null {\n\t\tconst quotedPrefix = extractQuotedPrefix(text);\n\t\tif (quotedPrefix) {\n\t\t\treturn quotedPrefix;\n\t\t}\n\n\t\tconst lastDelimiterIndex = findLastDelimiter(text);\n\t\tconst pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);\n\n\t\t// For forced extraction (Tab key), always return something\n\t\tif (forceExtract) {\n\t\t\treturn pathPrefix;\n\t\t}\n\n\t\t// For natural triggers, return if it looks like a path, ends with /, starts with ~/, .\n\t\t// Only return empty string if the text looks like it's starting a path context\n\t\tif (pathPrefix.includes(\"/\") || pathPrefix.startsWith(\".\") || pathPrefix.startsWith(\"~/\")) {\n\t\t\treturn pathPrefix;\n\t\t}\n\n\t\t// Return empty string only after a space (not for completely empty text)\n\t\t// Empty text should not trigger file suggestions - that's for forced Tab completion\n\t\tif (pathPrefix === \"\" && text.endsWith(\" \")) {\n\t\t\treturn pathPrefix;\n\t\t}\n\n\t\treturn null;\n\t}\n\n\t// Expand home directory (~/) to actual home path\n\tprivate expandHomePath(path: string): string {\n\t\tif (path.startsWith(\"~/\")) {\n\t\t\tconst expandedPath = join(homedir(), path.slice(2));\n\t\t\t// Preserve trailing slash if original path had one\n\t\t\treturn path.endsWith(\"/\") && !expandedPath.endsWith(\"/\") ? `${expandedPath}/` : expandedPath;\n\t\t} else if (path === \"~\") {\n\t\t\treturn homedir();\n\t\t}\n\t\treturn path;\n\t}\n\n\tprivate resolveScopedFuzzyQuery(rawQuery: string): ScopedFuzzyQuery | null {\n\t\tconst normalizedQuery = toDisplayPath(rawQuery);\n\t\tconst slashIndex = normalizedQuery.lastIndexOf(\"/\");\n\t\tif (slashIndex === -1) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst displayBase = normalizedQuery.slice(0, slashIndex + 1);\n\t\tconst query = normalizedQuery.slice(slashIndex + 1);\n\n\t\tlet baseDir: string;\n\t\tif (displayBase.startsWith(\"~/\")) {\n\t\t\tbaseDir = this.expandHomePath(displayBase);\n\t\t} else if (displayBase.startsWith(\"/\")) {\n\t\t\tbaseDir = displayBase;\n\t\t} else {\n\t\t\tbaseDir = join(this.basePath, displayBase);\n\t\t}\n\n\t\ttry {\n\t\t\tif (!statSync(baseDir).isDirectory()) {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn { baseDir, query, displayBase };\n\t}\n\n\tprivate scopedPathForDisplay(displayBase: string, relativePath: string): string {\n\t\tconst normalizedRelativePath = toDisplayPath(relativePath);\n\t\tif (displayBase === \"/\") {\n\t\t\treturn `/${normalizedRelativePath}`;\n\t\t}\n\t\treturn `${toDisplayPath(displayBase)}${normalizedRelativePath}`;\n\t}\n\n\tprivate resolveFileSuggestionSearch(prefix: string): FileSuggestionSearch {\n\t\tconst { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix);\n\t\tconst expandedPrefix = rawPrefix.startsWith(\"~\") ? this.expandHomePath(rawPrefix) : rawPrefix;\n\t\tconst useExpandedDirectory = rawPrefix.startsWith(\"~\") || expandedPrefix.startsWith(\"/\");\n\t\tif (ROOT_PATH_PREFIXES.has(rawPrefix) || rawPrefix.endsWith(\"/\")) {\n\t\t\treturn {\n\t\t\t\trawPrefix,\n\t\t\t\tisAtPrefix,\n\t\t\t\tisQuotedPrefix,\n\t\t\t\tsearchDir: useExpandedDirectory ? expandedPrefix : join(this.basePath, expandedPrefix),\n\t\t\t\tsearchPrefix: \"\",\n\t\t\t};\n\t\t}\n\t\tconst directory = dirname(expandedPrefix);\n\t\treturn {\n\t\t\trawPrefix,\n\t\t\tisAtPrefix,\n\t\t\tisQuotedPrefix,\n\t\t\tsearchDir: useExpandedDirectory ? directory : join(this.basePath, directory),\n\t\t\tsearchPrefix: basename(expandedPrefix),\n\t\t};\n\t}\n\n\t// Get file/directory suggestions for a given path prefix\n\tprivate getFileSuggestions(prefix: string): AutocompleteItem[] {\n\t\ttry {\n\t\t\tconst search = this.resolveFileSuggestionSearch(prefix);\n\t\t\tconst entries = readdirSync(search.searchDir, { withFileTypes: true });\n\t\t\tconst suggestions: AutocompleteItem[] = [];\n\t\t\tconst normalizedSearchPrefix = search.searchPrefix.toLowerCase();\n\n\t\t\tfor (const entry of entries) {\n\t\t\t\tif (!entry.name.toLowerCase().startsWith(normalizedSearchPrefix)) continue;\n\t\t\t\tconst isDirectory = isFileSuggestionDirectory(search.searchDir, entry);\n\t\t\t\tconst relativePath = toDisplayPath(buildRelativeSuggestionPath(search.rawPrefix, entry.name));\n\t\t\t\tconst pathValue = isDirectory ? `${relativePath}/` : relativePath;\n\t\t\t\tconst value = buildCompletionValue(pathValue, {\n\t\t\t\t\tisDirectory,\n\t\t\t\t\tisAtPrefix: search.isAtPrefix,\n\t\t\t\t\tisQuotedPrefix: search.isQuotedPrefix,\n\t\t\t\t});\n\t\t\t\tsuggestions.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tlabel: entry.name + (isDirectory ? \"/\" : \"\"),\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tsuggestions.sort((a, b) => {\n\t\t\t\tconst aIsDir = a.value.endsWith(\"/\");\n\t\t\t\tconst bIsDir = b.value.endsWith(\"/\");\n\t\t\t\tif (aIsDir && !bIsDir) return -1;\n\t\t\t\tif (!aIsDir && bIsDir) return 1;\n\t\t\t\treturn a.label.localeCompare(b.label);\n\t\t\t});\n\t\t\treturn suggestions;\n\t\t} catch (_e) {\n\t\t\treturn [];\n\t\t}\n\t}\n\n\t// Score an entry against the query (higher = better match)\n\t// isDirectory adds bonus to prioritize folders\n\tprivate scoreEntry(filePath: string, query: string, isDirectory: boolean): number {\n\t\tconst fileName = basename(filePath);\n\t\tconst lowerFileName = fileName.toLowerCase();\n\t\tconst lowerQuery = query.toLowerCase();\n\n\t\tlet score = 0;\n\n\t\t// Exact filename match (highest)\n\t\tif (lowerFileName === lowerQuery) score = 100;\n\t\t// Filename starts with query\n\t\telse if (lowerFileName.startsWith(lowerQuery)) score = 80;\n\t\t// Substring match in filename\n\t\telse if (lowerFileName.includes(lowerQuery)) score = 50;\n\t\t// Substring match in full path\n\t\telse if (filePath.toLowerCase().includes(lowerQuery)) score = 30;\n\n\t\t// Directories get a bonus to appear first\n\t\tif (isDirectory && score > 0) score += 10;\n\n\t\treturn score;\n\t}\n\n\t// Fuzzy file search using fd (fast, respects .gitignore)\n\tprivate async getFuzzyFileSuggestions(\n\t\tquery: string,\n\t\toptions: FuzzyFileSuggestionOptions,\n\t): Promise<AutocompleteItem[]> {\n\t\tif (!this.fdPath || options.signal.aborted) {\n\t\t\treturn [];\n\t\t}\n\n\t\ttry {\n\t\t\tconst scopedQuery = this.resolveScopedFuzzyQuery(query);\n\t\t\tconst fdBaseDir = scopedQuery?.baseDir ?? this.basePath;\n\t\t\tconst fdQuery = scopedQuery?.query ?? query;\n\t\t\tconst entries = await walkDirectoryWithFd(fdBaseDir, this.fdPath, fdQuery, 100, options.signal);\n\t\t\tif (options.signal.aborted) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\tconst scoredEntries = entries\n\t\t\t\t.map((entry) => ({\n\t\t\t\t\t...entry,\n\t\t\t\t\tscore: fdQuery ? this.scoreEntry(entry.path, fdQuery, entry.isDirectory) : 1,\n\t\t\t\t}))\n\t\t\t\t.filter((entry) => entry.score > 0);\n\n\t\t\tscoredEntries.sort((a, b) => b.score - a.score);\n\t\t\tconst topEntries = scoredEntries.slice(0, 20);\n\n\t\t\tconst suggestions: AutocompleteItem[] = [];\n\t\t\tfor (const { path: entryPath, isDirectory } of topEntries) {\n\t\t\t\tconst pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;\n\t\t\t\tconst displayPath = scopedQuery\n\t\t\t\t\t? this.scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash)\n\t\t\t\t\t: pathWithoutSlash;\n\t\t\t\tconst entryName = basename(pathWithoutSlash);\n\t\t\t\tconst completionPath = isDirectory ? `${displayPath}/` : displayPath;\n\t\t\t\tconst value = buildCompletionValue(completionPath, {\n\t\t\t\t\tisDirectory,\n\t\t\t\t\tisAtPrefix: true,\n\t\t\t\t\tisQuotedPrefix: options.isQuotedPrefix,\n\t\t\t\t});\n\n\t\t\t\tsuggestions.push({\n\t\t\t\t\tvalue,\n\t\t\t\t\tlabel: entryName + (isDirectory ? \"/\" : \"\"),\n\t\t\t\t\tdescription: displayPath,\n\t\t\t\t});\n\t\t\t}\n\n\t\t\treturn suggestions;\n\t\t} catch {\n\t\t\treturn [];\n\t\t}\n\t}\n\n\t// Check if we should trigger file completion (called on Tab key)\n\tshouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean {\n\t\tconst currentLine = lines[cursorLine] || \"\";\n\t\tconst textBeforeCursor = currentLine.slice(0, cursorCol);\n\n\t\t// Don't trigger if we're typing a slash command at the start of the line\n\t\tif (textBeforeCursor.trim().startsWith(\"/\") && !textBeforeCursor.trim().includes(\" \")) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn true;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import { readdirSync, statSync } from "fs";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
import { basename, dirname, join } from "path";
|
|
5
|
+
import { fuzzyFilter } from "./fuzzy.js";
|
|
6
|
+
const PATH_DELIMITERS = new Set([" ", "\t", '"', "'", "="]);
|
|
7
|
+
const ROOT_PATH_PREFIXES = new Set(["", "./", "../", "~", "~/", "/"]);
|
|
8
|
+
function isFileSuggestionDirectory(searchDir, entry) {
|
|
9
|
+
if (entry.isDirectory())
|
|
10
|
+
return true;
|
|
11
|
+
if (!entry.isSymbolicLink())
|
|
12
|
+
return false;
|
|
13
|
+
try {
|
|
14
|
+
return statSync(join(searchDir, entry.name)).isDirectory();
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function buildRelativeSuggestionPath(displayPrefix, name) {
|
|
21
|
+
if (displayPrefix.endsWith("/"))
|
|
22
|
+
return displayPrefix + name;
|
|
23
|
+
const hasDirectoryPrefix = displayPrefix.includes("/") || displayPrefix.includes("\\");
|
|
24
|
+
if (!hasDirectoryPrefix)
|
|
25
|
+
return displayPrefix.startsWith("~") ? `~/${name}` : name;
|
|
26
|
+
if (displayPrefix.startsWith("~/")) {
|
|
27
|
+
const dir = dirname(displayPrefix.slice(2));
|
|
28
|
+
return `~/${dir === "." ? name : join(dir, name)}`;
|
|
29
|
+
}
|
|
30
|
+
if (displayPrefix.startsWith("/")) {
|
|
31
|
+
const dir = dirname(displayPrefix);
|
|
32
|
+
return dir === "/" ? `/${name}` : `${dir}/${name}`;
|
|
33
|
+
}
|
|
34
|
+
const relativePath = join(dirname(displayPrefix), name);
|
|
35
|
+
return displayPrefix.startsWith("./") && !relativePath.startsWith("./") ? `./${relativePath}` : relativePath;
|
|
36
|
+
}
|
|
37
|
+
function toDisplayPath(value) {
|
|
38
|
+
return value.replace(/\\/g, "/");
|
|
39
|
+
}
|
|
40
|
+
function escapeRegex(value) {
|
|
41
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
42
|
+
}
|
|
43
|
+
function buildFdPathQuery(query) {
|
|
44
|
+
const normalized = toDisplayPath(query);
|
|
45
|
+
if (!normalized.includes("/")) {
|
|
46
|
+
return normalized;
|
|
47
|
+
}
|
|
48
|
+
const hasTrailingSeparator = normalized.endsWith("/");
|
|
49
|
+
const trimmed = normalized.replace(/^\/+|\/+$/g, "");
|
|
50
|
+
if (!trimmed) {
|
|
51
|
+
return normalized;
|
|
52
|
+
}
|
|
53
|
+
const separatorPattern = "[\\\\/]";
|
|
54
|
+
const segments = trimmed
|
|
55
|
+
.split("/")
|
|
56
|
+
.filter(Boolean)
|
|
57
|
+
.map((segment) => escapeRegex(segment));
|
|
58
|
+
if (segments.length === 0) {
|
|
59
|
+
return normalized;
|
|
60
|
+
}
|
|
61
|
+
let pattern = segments.join(separatorPattern);
|
|
62
|
+
if (hasTrailingSeparator) {
|
|
63
|
+
pattern += separatorPattern;
|
|
64
|
+
}
|
|
65
|
+
return pattern;
|
|
66
|
+
}
|
|
67
|
+
function findLastDelimiter(text) {
|
|
68
|
+
for (let i = text.length - 1; i >= 0; i -= 1) {
|
|
69
|
+
if (PATH_DELIMITERS.has(text[i] ?? "")) {
|
|
70
|
+
return i;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return -1;
|
|
74
|
+
}
|
|
75
|
+
function findUnclosedQuoteStart(text) {
|
|
76
|
+
let inQuotes = false;
|
|
77
|
+
let quoteStart = -1;
|
|
78
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
79
|
+
if (text[i] === '"') {
|
|
80
|
+
inQuotes = !inQuotes;
|
|
81
|
+
if (inQuotes) {
|
|
82
|
+
quoteStart = i;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return inQuotes ? quoteStart : null;
|
|
87
|
+
}
|
|
88
|
+
function isTokenStart(text, index) {
|
|
89
|
+
return index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? "");
|
|
90
|
+
}
|
|
91
|
+
function extractQuotedPrefix(text) {
|
|
92
|
+
const quoteStart = findUnclosedQuoteStart(text);
|
|
93
|
+
if (quoteStart === null) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (quoteStart > 0 && text[quoteStart - 1] === "@") {
|
|
97
|
+
if (!isTokenStart(text, quoteStart - 1)) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return text.slice(quoteStart - 1);
|
|
101
|
+
}
|
|
102
|
+
if (!isTokenStart(text, quoteStart)) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return text.slice(quoteStart);
|
|
106
|
+
}
|
|
107
|
+
function parsePathPrefix(prefix) {
|
|
108
|
+
if (prefix.startsWith('@"')) {
|
|
109
|
+
return { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true };
|
|
110
|
+
}
|
|
111
|
+
if (prefix.startsWith('"')) {
|
|
112
|
+
return { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true };
|
|
113
|
+
}
|
|
114
|
+
if (prefix.startsWith("@")) {
|
|
115
|
+
return { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false };
|
|
116
|
+
}
|
|
117
|
+
return { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false };
|
|
118
|
+
}
|
|
119
|
+
function buildCompletionValue(path, options) {
|
|
120
|
+
const needsQuotes = options.isQuotedPrefix || path.includes(" ");
|
|
121
|
+
const prefix = options.isAtPrefix ? "@" : "";
|
|
122
|
+
if (!needsQuotes) {
|
|
123
|
+
return `${prefix}${path}`;
|
|
124
|
+
}
|
|
125
|
+
const openQuote = `${prefix}"`;
|
|
126
|
+
const closeQuote = '"';
|
|
127
|
+
return `${openQuote}${path}${closeQuote}`;
|
|
128
|
+
}
|
|
129
|
+
function parseFdSearchOutput(stdout, exitCode, aborted) {
|
|
130
|
+
if (aborted || exitCode !== 0 || !stdout)
|
|
131
|
+
return [];
|
|
132
|
+
const results = [];
|
|
133
|
+
for (const line of stdout.trim().split("\n").filter(Boolean)) {
|
|
134
|
+
const displayLine = toDisplayPath(line);
|
|
135
|
+
const hasTrailingSeparator = displayLine.endsWith("/");
|
|
136
|
+
const normalizedPath = hasTrailingSeparator ? displayLine.slice(0, -1) : displayLine;
|
|
137
|
+
if (normalizedPath === ".git" || normalizedPath.startsWith(".git/") || normalizedPath.includes("/.git/")) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
results.push({ path: displayLine, isDirectory: hasTrailingSeparator });
|
|
141
|
+
}
|
|
142
|
+
return results;
|
|
143
|
+
}
|
|
144
|
+
async function walkDirectoryWithFd(baseDir, fdPath, query, maxResults, signal) {
|
|
145
|
+
const args = [
|
|
146
|
+
"--base-directory",
|
|
147
|
+
baseDir,
|
|
148
|
+
"--max-results",
|
|
149
|
+
String(maxResults),
|
|
150
|
+
"--type",
|
|
151
|
+
"f",
|
|
152
|
+
"--type",
|
|
153
|
+
"d",
|
|
154
|
+
"--follow",
|
|
155
|
+
"--hidden",
|
|
156
|
+
"--exclude",
|
|
157
|
+
".git",
|
|
158
|
+
"--exclude",
|
|
159
|
+
".git/*",
|
|
160
|
+
"--exclude",
|
|
161
|
+
".git/**",
|
|
162
|
+
];
|
|
163
|
+
if (toDisplayPath(query).includes("/")) {
|
|
164
|
+
args.push("--full-path");
|
|
165
|
+
}
|
|
166
|
+
if (query) {
|
|
167
|
+
args.push(buildFdPathQuery(query));
|
|
168
|
+
}
|
|
169
|
+
return await new Promise((resolve) => {
|
|
170
|
+
if (signal.aborted) {
|
|
171
|
+
resolve([]);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const child = spawn(fdPath, args, {
|
|
175
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
176
|
+
});
|
|
177
|
+
let stdout = "";
|
|
178
|
+
let resolved = false;
|
|
179
|
+
const finish = (results) => {
|
|
180
|
+
if (resolved)
|
|
181
|
+
return;
|
|
182
|
+
resolved = true;
|
|
183
|
+
signal.removeEventListener("abort", onAbort);
|
|
184
|
+
resolve(results);
|
|
185
|
+
};
|
|
186
|
+
const onAbort = () => {
|
|
187
|
+
if (child.exitCode === null) {
|
|
188
|
+
child.kill("SIGKILL");
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
192
|
+
child.stdout.setEncoding("utf-8");
|
|
193
|
+
child.stdout.on("data", (chunk) => {
|
|
194
|
+
stdout += chunk;
|
|
195
|
+
});
|
|
196
|
+
child.on("error", () => {
|
|
197
|
+
finish([]);
|
|
198
|
+
});
|
|
199
|
+
child.on("close", (code) => {
|
|
200
|
+
finish(parseFdSearchOutput(stdout, code, signal.aborted));
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
function toCommandSuggestionItem(command) {
|
|
205
|
+
const name = "name" in command ? command.name : command.value;
|
|
206
|
+
const hint = "argumentHint" in command && command.argumentHint ? command.argumentHint : undefined;
|
|
207
|
+
const description = command.description ?? "";
|
|
208
|
+
const fullDescription = hint ? (description ? `${hint} — ${description}` : hint) : description;
|
|
209
|
+
return {
|
|
210
|
+
name,
|
|
211
|
+
label: name,
|
|
212
|
+
description: fullDescription || undefined,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function isSlashCommandCompletion(prefix, beforePrefix) {
|
|
216
|
+
return prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/");
|
|
217
|
+
}
|
|
218
|
+
function completionCursorOffset(item) {
|
|
219
|
+
const trailingQuoteOffset = item.label.endsWith("/") && item.value.endsWith('"') ? 1 : 0;
|
|
220
|
+
return item.value.length - trailingQuoteOffset;
|
|
221
|
+
}
|
|
222
|
+
// Combined provider that handles both slash commands and file paths
|
|
223
|
+
export class CombinedAutocompleteProvider {
|
|
224
|
+
commands;
|
|
225
|
+
basePath;
|
|
226
|
+
fdPath;
|
|
227
|
+
constructor(commands = [], basePath, fdPath = null) {
|
|
228
|
+
this.commands = commands;
|
|
229
|
+
this.basePath = basePath;
|
|
230
|
+
this.fdPath = fdPath;
|
|
231
|
+
}
|
|
232
|
+
async getSuggestions(lines, cursorLine, cursorCol, options) {
|
|
233
|
+
const currentLine = lines[cursorLine] || "";
|
|
234
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
235
|
+
const atPrefix = this.extractAtPrefix(textBeforeCursor);
|
|
236
|
+
if (atPrefix) {
|
|
237
|
+
const { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix);
|
|
238
|
+
const suggestions = await this.getFuzzyFileSuggestions(rawPrefix, {
|
|
239
|
+
isQuotedPrefix,
|
|
240
|
+
signal: options.signal,
|
|
241
|
+
});
|
|
242
|
+
if (suggestions.length === 0)
|
|
243
|
+
return null;
|
|
244
|
+
return {
|
|
245
|
+
items: suggestions,
|
|
246
|
+
prefix: atPrefix,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
if (!options.force && textBeforeCursor.startsWith("/")) {
|
|
250
|
+
return await this.getSlashCommandSuggestions(textBeforeCursor);
|
|
251
|
+
}
|
|
252
|
+
const pathMatch = this.extractPathPrefix(textBeforeCursor, options.force ?? false);
|
|
253
|
+
if (pathMatch === null) {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
const suggestions = this.getFileSuggestions(pathMatch);
|
|
257
|
+
if (suggestions.length === 0)
|
|
258
|
+
return null;
|
|
259
|
+
return {
|
|
260
|
+
items: suggestions,
|
|
261
|
+
prefix: pathMatch,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
async getSlashCommandSuggestions(textBeforeCursor) {
|
|
265
|
+
const spaceIndex = textBeforeCursor.indexOf(" ");
|
|
266
|
+
if (spaceIndex === -1) {
|
|
267
|
+
const prefix = textBeforeCursor.slice(1);
|
|
268
|
+
const commandItems = this.commands.map(toCommandSuggestionItem);
|
|
269
|
+
const filtered = fuzzyFilter(commandItems, prefix, (item) => item.name).map((item) => ({
|
|
270
|
+
value: item.name,
|
|
271
|
+
label: item.label,
|
|
272
|
+
...(item.description && { description: item.description }),
|
|
273
|
+
}));
|
|
274
|
+
if (filtered.length === 0)
|
|
275
|
+
return null;
|
|
276
|
+
return { items: filtered, prefix: textBeforeCursor };
|
|
277
|
+
}
|
|
278
|
+
const commandName = textBeforeCursor.slice(1, spaceIndex);
|
|
279
|
+
const argumentText = textBeforeCursor.slice(spaceIndex + 1);
|
|
280
|
+
const command = this.commands.find((candidate) => {
|
|
281
|
+
const name = "name" in candidate ? candidate.name : candidate.value;
|
|
282
|
+
return name === commandName;
|
|
283
|
+
});
|
|
284
|
+
if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
const argumentSuggestions = await command.getArgumentCompletions(argumentText);
|
|
288
|
+
if (!Array.isArray(argumentSuggestions) || argumentSuggestions.length === 0) {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
return { items: argumentSuggestions, prefix: argumentText };
|
|
292
|
+
}
|
|
293
|
+
applyCompletion(lines, cursorLine, cursorCol, item, prefix) {
|
|
294
|
+
const currentLine = lines[cursorLine] || "";
|
|
295
|
+
const beforePrefix = currentLine.slice(0, cursorCol - prefix.length);
|
|
296
|
+
const afterCursor = currentLine.slice(cursorCol);
|
|
297
|
+
const isQuotedPrefix = prefix.startsWith('"') || prefix.startsWith('@"');
|
|
298
|
+
const hasLeadingQuoteAfterCursor = afterCursor.startsWith('"');
|
|
299
|
+
const hasTrailingQuoteInItem = item.value.endsWith('"');
|
|
300
|
+
const adjustedAfterCursor = isQuotedPrefix && hasTrailingQuoteInItem && hasLeadingQuoteAfterCursor ? afterCursor.slice(1) : afterCursor;
|
|
301
|
+
// Check if we're completing a slash command (prefix starts with "/" but NOT a file path)
|
|
302
|
+
// Slash commands are at the start of the line and don't contain path separators after the first /.
|
|
303
|
+
if (isSlashCommandCompletion(prefix, beforePrefix)) {
|
|
304
|
+
// This is a command name completion
|
|
305
|
+
const newLine = `${beforePrefix}/${item.value} ${adjustedAfterCursor}`;
|
|
306
|
+
const newLines = [...lines];
|
|
307
|
+
newLines[cursorLine] = newLine;
|
|
308
|
+
return {
|
|
309
|
+
lines: newLines,
|
|
310
|
+
cursorLine,
|
|
311
|
+
cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// Check if we're completing a file attachment (prefix starts with "@")
|
|
315
|
+
if (prefix.startsWith("@")) {
|
|
316
|
+
// This is a file attachment completion
|
|
317
|
+
// Don't add space after directories so user can continue autocompleting
|
|
318
|
+
const isDirectory = item.label.endsWith("/");
|
|
319
|
+
const suffix = isDirectory ? "" : " ";
|
|
320
|
+
const newLine = `${beforePrefix + item.value}${suffix}${adjustedAfterCursor}`;
|
|
321
|
+
const newLines = [...lines];
|
|
322
|
+
newLines[cursorLine] = newLine;
|
|
323
|
+
const cursorOffset = completionCursorOffset(item);
|
|
324
|
+
return {
|
|
325
|
+
lines: newLines,
|
|
326
|
+
cursorLine,
|
|
327
|
+
cursorCol: beforePrefix.length + cursorOffset + suffix.length,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
// For file paths, complete the path
|
|
331
|
+
const newLine = beforePrefix + item.value + adjustedAfterCursor;
|
|
332
|
+
const newLines = [...lines];
|
|
333
|
+
newLines[cursorLine] = newLine;
|
|
334
|
+
const cursorOffset = completionCursorOffset(item);
|
|
335
|
+
return {
|
|
336
|
+
lines: newLines,
|
|
337
|
+
cursorLine,
|
|
338
|
+
cursorCol: beforePrefix.length + cursorOffset,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
// Extract @ prefix for fuzzy file suggestions
|
|
342
|
+
extractAtPrefix(text) {
|
|
343
|
+
const quotedPrefix = extractQuotedPrefix(text);
|
|
344
|
+
if (quotedPrefix?.startsWith('@"')) {
|
|
345
|
+
return quotedPrefix;
|
|
346
|
+
}
|
|
347
|
+
const lastDelimiterIndex = findLastDelimiter(text);
|
|
348
|
+
const tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1;
|
|
349
|
+
if (text[tokenStart] === "@") {
|
|
350
|
+
return text.slice(tokenStart);
|
|
351
|
+
}
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
// Extract a path-like prefix from the text before cursor
|
|
355
|
+
extractPathPrefix(text, forceExtract = false) {
|
|
356
|
+
const quotedPrefix = extractQuotedPrefix(text);
|
|
357
|
+
if (quotedPrefix) {
|
|
358
|
+
return quotedPrefix;
|
|
359
|
+
}
|
|
360
|
+
const lastDelimiterIndex = findLastDelimiter(text);
|
|
361
|
+
const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);
|
|
362
|
+
// For forced extraction (Tab key), always return something
|
|
363
|
+
if (forceExtract) {
|
|
364
|
+
return pathPrefix;
|
|
365
|
+
}
|
|
366
|
+
// For natural triggers, return if it looks like a path, ends with /, starts with ~/, .
|
|
367
|
+
// Only return empty string if the text looks like it's starting a path context
|
|
368
|
+
if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) {
|
|
369
|
+
return pathPrefix;
|
|
370
|
+
}
|
|
371
|
+
// Return empty string only after a space (not for completely empty text)
|
|
372
|
+
// Empty text should not trigger file suggestions - that's for forced Tab completion
|
|
373
|
+
if (pathPrefix === "" && text.endsWith(" ")) {
|
|
374
|
+
return pathPrefix;
|
|
375
|
+
}
|
|
376
|
+
return null;
|
|
377
|
+
}
|
|
378
|
+
// Expand home directory (~/) to actual home path
|
|
379
|
+
expandHomePath(path) {
|
|
380
|
+
if (path.startsWith("~/")) {
|
|
381
|
+
const expandedPath = join(homedir(), path.slice(2));
|
|
382
|
+
// Preserve trailing slash if original path had one
|
|
383
|
+
return path.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath;
|
|
384
|
+
}
|
|
385
|
+
else if (path === "~") {
|
|
386
|
+
return homedir();
|
|
387
|
+
}
|
|
388
|
+
return path;
|
|
389
|
+
}
|
|
390
|
+
resolveScopedFuzzyQuery(rawQuery) {
|
|
391
|
+
const normalizedQuery = toDisplayPath(rawQuery);
|
|
392
|
+
const slashIndex = normalizedQuery.lastIndexOf("/");
|
|
393
|
+
if (slashIndex === -1) {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
const displayBase = normalizedQuery.slice(0, slashIndex + 1);
|
|
397
|
+
const query = normalizedQuery.slice(slashIndex + 1);
|
|
398
|
+
let baseDir;
|
|
399
|
+
if (displayBase.startsWith("~/")) {
|
|
400
|
+
baseDir = this.expandHomePath(displayBase);
|
|
401
|
+
}
|
|
402
|
+
else if (displayBase.startsWith("/")) {
|
|
403
|
+
baseDir = displayBase;
|
|
404
|
+
}
|
|
405
|
+
else {
|
|
406
|
+
baseDir = join(this.basePath, displayBase);
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
if (!statSync(baseDir).isDirectory()) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
catch {
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
return { baseDir, query, displayBase };
|
|
417
|
+
}
|
|
418
|
+
scopedPathForDisplay(displayBase, relativePath) {
|
|
419
|
+
const normalizedRelativePath = toDisplayPath(relativePath);
|
|
420
|
+
if (displayBase === "/") {
|
|
421
|
+
return `/${normalizedRelativePath}`;
|
|
422
|
+
}
|
|
423
|
+
return `${toDisplayPath(displayBase)}${normalizedRelativePath}`;
|
|
424
|
+
}
|
|
425
|
+
resolveFileSuggestionSearch(prefix) {
|
|
426
|
+
const { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix);
|
|
427
|
+
const expandedPrefix = rawPrefix.startsWith("~") ? this.expandHomePath(rawPrefix) : rawPrefix;
|
|
428
|
+
const useExpandedDirectory = rawPrefix.startsWith("~") || expandedPrefix.startsWith("/");
|
|
429
|
+
if (ROOT_PATH_PREFIXES.has(rawPrefix) || rawPrefix.endsWith("/")) {
|
|
430
|
+
return {
|
|
431
|
+
rawPrefix,
|
|
432
|
+
isAtPrefix,
|
|
433
|
+
isQuotedPrefix,
|
|
434
|
+
searchDir: useExpandedDirectory ? expandedPrefix : join(this.basePath, expandedPrefix),
|
|
435
|
+
searchPrefix: "",
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
const directory = dirname(expandedPrefix);
|
|
439
|
+
return {
|
|
440
|
+
rawPrefix,
|
|
441
|
+
isAtPrefix,
|
|
442
|
+
isQuotedPrefix,
|
|
443
|
+
searchDir: useExpandedDirectory ? directory : join(this.basePath, directory),
|
|
444
|
+
searchPrefix: basename(expandedPrefix),
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
// Get file/directory suggestions for a given path prefix
|
|
448
|
+
getFileSuggestions(prefix) {
|
|
449
|
+
try {
|
|
450
|
+
const search = this.resolveFileSuggestionSearch(prefix);
|
|
451
|
+
const entries = readdirSync(search.searchDir, { withFileTypes: true });
|
|
452
|
+
const suggestions = [];
|
|
453
|
+
const normalizedSearchPrefix = search.searchPrefix.toLowerCase();
|
|
454
|
+
for (const entry of entries) {
|
|
455
|
+
if (!entry.name.toLowerCase().startsWith(normalizedSearchPrefix))
|
|
456
|
+
continue;
|
|
457
|
+
const isDirectory = isFileSuggestionDirectory(search.searchDir, entry);
|
|
458
|
+
const relativePath = toDisplayPath(buildRelativeSuggestionPath(search.rawPrefix, entry.name));
|
|
459
|
+
const pathValue = isDirectory ? `${relativePath}/` : relativePath;
|
|
460
|
+
const value = buildCompletionValue(pathValue, {
|
|
461
|
+
isDirectory,
|
|
462
|
+
isAtPrefix: search.isAtPrefix,
|
|
463
|
+
isQuotedPrefix: search.isQuotedPrefix,
|
|
464
|
+
});
|
|
465
|
+
suggestions.push({
|
|
466
|
+
value,
|
|
467
|
+
label: entry.name + (isDirectory ? "/" : ""),
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
suggestions.sort((a, b) => {
|
|
471
|
+
const aIsDir = a.value.endsWith("/");
|
|
472
|
+
const bIsDir = b.value.endsWith("/");
|
|
473
|
+
if (aIsDir && !bIsDir)
|
|
474
|
+
return -1;
|
|
475
|
+
if (!aIsDir && bIsDir)
|
|
476
|
+
return 1;
|
|
477
|
+
return a.label.localeCompare(b.label);
|
|
478
|
+
});
|
|
479
|
+
return suggestions;
|
|
480
|
+
}
|
|
481
|
+
catch (_e) {
|
|
482
|
+
return [];
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
// Score an entry against the query (higher = better match)
|
|
486
|
+
// isDirectory adds bonus to prioritize folders
|
|
487
|
+
scoreEntry(filePath, query, isDirectory) {
|
|
488
|
+
const fileName = basename(filePath);
|
|
489
|
+
const lowerFileName = fileName.toLowerCase();
|
|
490
|
+
const lowerQuery = query.toLowerCase();
|
|
491
|
+
let score = 0;
|
|
492
|
+
// Exact filename match (highest)
|
|
493
|
+
if (lowerFileName === lowerQuery)
|
|
494
|
+
score = 100;
|
|
495
|
+
// Filename starts with query
|
|
496
|
+
else if (lowerFileName.startsWith(lowerQuery))
|
|
497
|
+
score = 80;
|
|
498
|
+
// Substring match in filename
|
|
499
|
+
else if (lowerFileName.includes(lowerQuery))
|
|
500
|
+
score = 50;
|
|
501
|
+
// Substring match in full path
|
|
502
|
+
else if (filePath.toLowerCase().includes(lowerQuery))
|
|
503
|
+
score = 30;
|
|
504
|
+
// Directories get a bonus to appear first
|
|
505
|
+
if (isDirectory && score > 0)
|
|
506
|
+
score += 10;
|
|
507
|
+
return score;
|
|
508
|
+
}
|
|
509
|
+
// Fuzzy file search using fd (fast, respects .gitignore)
|
|
510
|
+
async getFuzzyFileSuggestions(query, options) {
|
|
511
|
+
if (!this.fdPath || options.signal.aborted) {
|
|
512
|
+
return [];
|
|
513
|
+
}
|
|
514
|
+
try {
|
|
515
|
+
const scopedQuery = this.resolveScopedFuzzyQuery(query);
|
|
516
|
+
const fdBaseDir = scopedQuery?.baseDir ?? this.basePath;
|
|
517
|
+
const fdQuery = scopedQuery?.query ?? query;
|
|
518
|
+
const entries = await walkDirectoryWithFd(fdBaseDir, this.fdPath, fdQuery, 100, options.signal);
|
|
519
|
+
if (options.signal.aborted) {
|
|
520
|
+
return [];
|
|
521
|
+
}
|
|
522
|
+
const scoredEntries = entries
|
|
523
|
+
.map((entry) => ({
|
|
524
|
+
...entry,
|
|
525
|
+
score: fdQuery ? this.scoreEntry(entry.path, fdQuery, entry.isDirectory) : 1,
|
|
526
|
+
}))
|
|
527
|
+
.filter((entry) => entry.score > 0);
|
|
528
|
+
scoredEntries.sort((a, b) => b.score - a.score);
|
|
529
|
+
const topEntries = scoredEntries.slice(0, 20);
|
|
530
|
+
const suggestions = [];
|
|
531
|
+
for (const { path: entryPath, isDirectory } of topEntries) {
|
|
532
|
+
const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;
|
|
533
|
+
const displayPath = scopedQuery
|
|
534
|
+
? this.scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash)
|
|
535
|
+
: pathWithoutSlash;
|
|
536
|
+
const entryName = basename(pathWithoutSlash);
|
|
537
|
+
const completionPath = isDirectory ? `${displayPath}/` : displayPath;
|
|
538
|
+
const value = buildCompletionValue(completionPath, {
|
|
539
|
+
isDirectory,
|
|
540
|
+
isAtPrefix: true,
|
|
541
|
+
isQuotedPrefix: options.isQuotedPrefix,
|
|
542
|
+
});
|
|
543
|
+
suggestions.push({
|
|
544
|
+
value,
|
|
545
|
+
label: entryName + (isDirectory ? "/" : ""),
|
|
546
|
+
description: displayPath,
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
return suggestions;
|
|
550
|
+
}
|
|
551
|
+
catch {
|
|
552
|
+
return [];
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
// Check if we should trigger file completion (called on Tab key)
|
|
556
|
+
shouldTriggerFileCompletion(lines, cursorLine, cursorCol) {
|
|
557
|
+
const currentLine = lines[cursorLine] || "";
|
|
558
|
+
const textBeforeCursor = currentLine.slice(0, cursorCol);
|
|
559
|
+
// Don't trigger if we're typing a slash command at the start of the line
|
|
560
|
+
if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
|
|
561
|
+
return false;
|
|
562
|
+
}
|
|
563
|
+
return true;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
//# sourceMappingURL=autocomplete.js.map
|