Skip to content

Commit 4f6dd74

Browse files
authored
Sort file search results shortest to longest (#1849)
1 parent dca4563 commit 4f6dd74

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

src/services/search/file-search.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path"
33
import * as fs from "fs"
44
import * as childProcess from "child_process"
55
import * as readline from "readline"
6-
import { Fzf } from "fzf"
6+
import { byLengthAsc, Fzf } from "fzf"
77
import { getBinPath } from "../ripgrep"
88

99
async function executeRipgrepForFiles(
@@ -123,22 +123,16 @@ export async function searchWorkspaceFiles(
123123
// Run fzf search on all items
124124
const fzf = new Fzf(searchItems, {
125125
selector: (item) => item.searchStr,
126+
tiebreakers: [byLengthAsc],
127+
limit: limit,
126128
})
127129

128130
// Get all matching results from fzf
129-
const fzfResults = fzf.find(query)
130-
131-
// First, sort all results by path length (shortest first)
132-
fzfResults.sort((a, b) => {
133-
return a.item.original.path.length - b.item.original.path.length
134-
})
135-
136-
// Take the top N (limit) shortest results
137-
const shortestResults = fzfResults.slice(0, limit).map((result) => result.item.original)
131+
const fzfResults = fzf.find(query).map((result) => result.item.original)
138132

139133
// Verify types of the shortest results
140134
const verifiedResults = await Promise.all(
141-
shortestResults.map(async (result) => {
135+
fzfResults.map(async (result) => {
142136
const fullPath = path.join(workspacePath, result.path)
143137
// Verify if the path exists and is actually a directory
144138
if (fs.existsSync(fullPath)) {
@@ -153,17 +147,6 @@ export async function searchWorkspaceFiles(
153147
}),
154148
)
155149

156-
// Final sort to put directories first within the shortest results
157-
verifiedResults.sort((a, b) => {
158-
if (a.type === "folder" && b.type !== "folder") {
159-
return -1
160-
}
161-
if (a.type !== "folder" && b.type === "folder") {
162-
return 1
163-
}
164-
return 0 // Keep original sorting by path length
165-
})
166-
167150
return verifiedResults
168151
} catch (error) {
169152
console.error("Error in searchWorkspaceFiles:", error)

0 commit comments

Comments
 (0)