|
| 1 | +// Copyright 2026, Command Line Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { DefaultMockFilesystem } from "./mockfilesystem"; |
| 5 | + |
| 6 | +const MaxMockSuggestions = 50; |
| 7 | + |
| 8 | +type ResolvedMockFileQuery = { |
| 9 | + baseDir: string; |
| 10 | + queryPrefix: string; |
| 11 | + searchTerm: string; |
| 12 | +}; |
| 13 | + |
| 14 | +function ensureTrailingSlash(path: string): string { |
| 15 | + if (path === "" || path.endsWith("/")) { |
| 16 | + return path; |
| 17 | + } |
| 18 | + return path + "/"; |
| 19 | +} |
| 20 | + |
| 21 | +function trimTrailingSlash(path: string): string { |
| 22 | + if (path === "/") { |
| 23 | + return path; |
| 24 | + } |
| 25 | + return path.replace(/\/+$/, ""); |
| 26 | +} |
| 27 | + |
| 28 | +function getDirName(path: string): string { |
| 29 | + const trimmedPath = trimTrailingSlash(path); |
| 30 | + if (trimmedPath === "/") { |
| 31 | + return "/"; |
| 32 | + } |
| 33 | + const idx = trimmedPath.lastIndexOf("/"); |
| 34 | + if (idx <= 0) { |
| 35 | + return "/"; |
| 36 | + } |
| 37 | + return trimmedPath.slice(0, idx); |
| 38 | +} |
| 39 | + |
| 40 | +function getBaseName(path: string): string { |
| 41 | + const trimmedPath = trimTrailingSlash(path); |
| 42 | + if (trimmedPath === "/") { |
| 43 | + return "/"; |
| 44 | + } |
| 45 | + const idx = trimmedPath.lastIndexOf("/"); |
| 46 | + return idx < 0 ? trimmedPath : trimmedPath.slice(idx + 1); |
| 47 | +} |
| 48 | + |
| 49 | +function expandMockHome(path: string): string { |
| 50 | + if (path === "~") { |
| 51 | + return DefaultMockFilesystem.homePath; |
| 52 | + } |
| 53 | + if (path.startsWith("~/")) { |
| 54 | + return DefaultMockFilesystem.homePath + path.slice(1); |
| 55 | + } |
| 56 | + return path; |
| 57 | +} |
| 58 | + |
| 59 | +function normalizeMockPath(path: string, basePath = DefaultMockFilesystem.homePath): string { |
| 60 | + if (path == null || path === "") { |
| 61 | + return basePath; |
| 62 | + } |
| 63 | + path = expandMockHome(path); |
| 64 | + if (!path.startsWith("/")) { |
| 65 | + path = `${basePath}/${path}`; |
| 66 | + } |
| 67 | + const resolvedParts: string[] = []; |
| 68 | + for (const part of path.split("/")) { |
| 69 | + if (part === "" || part === ".") { |
| 70 | + continue; |
| 71 | + } |
| 72 | + if (part === "..") { |
| 73 | + resolvedParts.pop(); |
| 74 | + continue; |
| 75 | + } |
| 76 | + resolvedParts.push(part); |
| 77 | + } |
| 78 | + return "/" + resolvedParts.join("/"); |
| 79 | +} |
| 80 | + |
| 81 | +function resolveMockFileQuery(cwd: string, query: string): ResolvedMockFileQuery { |
| 82 | + const resolvedCwd = normalizeMockPath(cwd || "~", "/"); |
| 83 | + if (query == null || query === "") { |
| 84 | + return { baseDir: resolvedCwd, queryPrefix: "", searchTerm: "" }; |
| 85 | + } |
| 86 | + if (query === "~" || query === "~/") { |
| 87 | + return { baseDir: DefaultMockFilesystem.homePath, queryPrefix: "~/", searchTerm: "" }; |
| 88 | + } |
| 89 | + const expandedQuery = expandMockHome(query); |
| 90 | + if (expandedQuery.startsWith("/")) { |
| 91 | + if (query.endsWith("/")) { |
| 92 | + return { |
| 93 | + baseDir: normalizeMockPath(expandedQuery, "/"), |
| 94 | + queryPrefix: query, |
| 95 | + searchTerm: "", |
| 96 | + }; |
| 97 | + } |
| 98 | + if (expandedQuery === "/") { |
| 99 | + return { baseDir: "/", queryPrefix: "/", searchTerm: "" }; |
| 100 | + } |
| 101 | + return { |
| 102 | + baseDir: getDirName(expandedQuery), |
| 103 | + queryPrefix: ensureTrailingSlash(getDirName(query)), |
| 104 | + searchTerm: getBaseName(expandedQuery), |
| 105 | + }; |
| 106 | + } |
| 107 | + if (query.endsWith("/")) { |
| 108 | + return { |
| 109 | + baseDir: normalizeMockPath(query, resolvedCwd), |
| 110 | + queryPrefix: query, |
| 111 | + searchTerm: "", |
| 112 | + }; |
| 113 | + } |
| 114 | + const slashIdx = query.lastIndexOf("/"); |
| 115 | + if (slashIdx !== -1) { |
| 116 | + const dirPart = query.slice(0, slashIdx); |
| 117 | + return { |
| 118 | + baseDir: normalizeMockPath(dirPart, resolvedCwd), |
| 119 | + queryPrefix: ensureTrailingSlash(dirPart), |
| 120 | + searchTerm: query.slice(slashIdx + 1), |
| 121 | + }; |
| 122 | + } |
| 123 | + return { baseDir: resolvedCwd, queryPrefix: "", searchTerm: query }; |
| 124 | +} |
| 125 | + |
| 126 | +function findMatchPositions(value: string, searchTerm: string): number[] { |
| 127 | + const lowerValue = value.toLowerCase(); |
| 128 | + const lowerSearchTerm = searchTerm.toLowerCase(); |
| 129 | + const positions: number[] = []; |
| 130 | + let searchIdx = 0; |
| 131 | + for (let idx = 0; idx < lowerValue.length; idx++) { |
| 132 | + if (lowerValue[idx] !== lowerSearchTerm[searchIdx]) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + positions.push(idx); |
| 136 | + searchIdx++; |
| 137 | + if (searchIdx >= lowerSearchTerm.length) { |
| 138 | + return positions; |
| 139 | + } |
| 140 | + } |
| 141 | + return null; |
| 142 | +} |
| 143 | + |
| 144 | +function scoreSuggestion(value: string, positions: number[], fallbackIndex: number): number { |
| 145 | + if (positions.length === 0) { |
| 146 | + return MaxMockSuggestions - fallbackIndex; |
| 147 | + } |
| 148 | + let score = 1000 - value.length; |
| 149 | + if (positions[0] === 0) { |
| 150 | + score += 500; |
| 151 | + } |
| 152 | + for (let idx = 1; idx < positions.length; idx++) { |
| 153 | + if (positions[idx] === positions[idx - 1] + 1) { |
| 154 | + score += 25; |
| 155 | + } |
| 156 | + } |
| 157 | + return score; |
| 158 | +} |
| 159 | + |
| 160 | +export async function fetchMockSuggestions(data: FetchSuggestionsData): Promise<FetchSuggestionsResponse> { |
| 161 | + if (data?.suggestiontype !== "file") { |
| 162 | + return { reqnum: data?.reqnum ?? 0, suggestions: [] }; |
| 163 | + } |
| 164 | + const { baseDir, queryPrefix, searchTerm } = resolveMockFileQuery(data?.["file:cwd"], data?.query ?? ""); |
| 165 | + const fileInfos = await DefaultMockFilesystem.fileList({ |
| 166 | + path: baseDir, |
| 167 | + opts: { all: true, limit: MaxMockSuggestions * 4 }, |
| 168 | + }); |
| 169 | + const suggestions = fileInfos |
| 170 | + .map((fileInfo, idx) => { |
| 171 | + if (data?.["file:dironly"] && !fileInfo.isdir) { |
| 172 | + return null; |
| 173 | + } |
| 174 | + const suggestionName = `${queryPrefix}${fileInfo.name}`; |
| 175 | + const matchpos = searchTerm === "" ? [] : findMatchPositions(suggestionName, searchTerm); |
| 176 | + if (searchTerm !== "" && matchpos == null) { |
| 177 | + return null; |
| 178 | + } |
| 179 | + return { |
| 180 | + type: "file", |
| 181 | + suggestionid: fileInfo.path, |
| 182 | + display: suggestionName, |
| 183 | + "file:path": fileInfo.path, |
| 184 | + "file:name": suggestionName, |
| 185 | + "file:mimetype": fileInfo.mimetype, |
| 186 | + matchpos, |
| 187 | + score: scoreSuggestion(suggestionName, matchpos ?? [], idx), |
| 188 | + } satisfies SuggestionType; |
| 189 | + }) |
| 190 | + .filter((suggestion): suggestion is SuggestionType => suggestion != null); |
| 191 | + suggestions.sort((a, b) => { |
| 192 | + if ((a.score ?? 0) !== (b.score ?? 0)) { |
| 193 | + return (b.score ?? 0) - (a.score ?? 0); |
| 194 | + } |
| 195 | + return a.display.length - b.display.length; |
| 196 | + }); |
| 197 | + return { |
| 198 | + reqnum: data?.reqnum ?? 0, |
| 199 | + suggestions: suggestions.slice(0, MaxMockSuggestions), |
| 200 | + }; |
| 201 | +} |
0 commit comments