(
pattern: string,
options: SearchOptions = {},
)
| 110 | } |
| 111 | |
| 112 | async search( |
| 113 | pattern: string, |
| 114 | options: SearchOptions = {}, |
| 115 | ): Promise<string[]> { |
| 116 | if (!this.resultCache || !this.fzf || !this.ignore) { |
| 117 | throw new Error('Engine not initialized. Call initialize() first.'); |
| 118 | } |
| 119 | |
| 120 | pattern = pattern || '*'; |
| 121 | |
| 122 | let filteredCandidates; |
| 123 | const { files: candidates, isExactMatch } = |
| 124 | await this.resultCache!.get(pattern); |
| 125 | |
| 126 | if (isExactMatch) { |
| 127 | // Use the cached result. |
| 128 | filteredCandidates = candidates; |
| 129 | } else { |
| 130 | let shouldCache = true; |
| 131 | if (pattern.includes('*')) { |
| 132 | filteredCandidates = await filter(candidates, pattern, options.signal); |
| 133 | } else { |
| 134 | filteredCandidates = await this.fzf |
| 135 | .find(pattern) |
| 136 | .then((results: Array<FzfResultItem<string>>) => |
| 137 | results.map((entry: FzfResultItem<string>) => entry.item), |
| 138 | ) |
| 139 | .catch(() => { |
| 140 | shouldCache = false; |
| 141 | return []; |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | if (shouldCache) { |
| 146 | this.resultCache!.set(pattern, filteredCandidates); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | const fileFilter = this.ignore.getFileFilter(); |
| 151 | const results: string[] = []; |
| 152 | for (const [i, candidate] of filteredCandidates.entries()) { |
| 153 | if (i % 1000 === 0) { |
| 154 | await new Promise((resolve) => setImmediate(resolve)); |
| 155 | if (options.signal?.aborted) { |
| 156 | throw new AbortError(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (results.length >= (options.maxResults ?? Infinity)) { |
| 161 | break; |
| 162 | } |
| 163 | if (candidate === '.') { |
| 164 | continue; |
| 165 | } |
| 166 | if (!fileFilter(candidate)) { |
| 167 | results.push(candidate); |
| 168 | } |
| 169 | } |
nothing calls this directly
no test coverage detected