Skip to content

Commit f8d3e5d

Browse files
committed
Fix mixed-case folder prefixes in CLI tab completion
Found this while walking through the CLI code a lil bit. Matching ignores case but sorting did not, so a + Tab with Alpha, Azure and alps returned Alp and dropped a valid choice. Sort using the same lowercase comparison and add two regressions for mixed-case directory names.
1 parent 65bca0d commit f8d3e5d

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎cli/src/__tests__/path-completion.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ describe('getPathCompletion', () => {
122122
})
123123
})
124124

125+
describe('mixed-case common prefixes', () => {
126+
test('does not extend past a directory between the sorted endpoints', () => {
127+
for (const name of ['Alpha', 'Azure', 'alps']) {
128+
mkdirSync(path.join(tempDir, name))
129+
}
130+
131+
expect(getPathCompletion(path.join(tempDir, 'a'))).toBeNull()
132+
})
133+
134+
test('extends only to the prefix shared by every mixed-case match', () => {
135+
for (const name of ['ProjectAlpha', 'ProjectBeta', 'projectAlps']) {
136+
mkdirSync(path.join(tempDir, name))
137+
}
138+
139+
expect(getPathCompletion(path.join(tempDir, 'pro'))).toBe(
140+
path.join(tempDir, 'Project'),
141+
)
142+
})
143+
})
144+
125145
describe('hidden directories', () => {
126146
test('skips hidden directories by default', () => {
127147
mkdirSync(path.join(tempDir, '.hidden'))

‎cli/src/utils/path-completion.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ export function getPathCompletion(inputPath: string): string | null {
8080
}
8181

8282
// Multiple matches - find common prefix
83-
const sortedMatches = matches.sort()
83+
const sortedMatches = matches.sort((a, b) => {
84+
const lowerA = a.toLowerCase()
85+
const lowerB = b.toLowerCase()
86+
return lowerA < lowerB ? -1 : lowerA > lowerB ? 1 : 0
87+
})
8488
const first = sortedMatches[0].toLowerCase()
8589
const last = sortedMatches[sortedMatches.length - 1].toLowerCase()
8690
let commonLength = partial.length

0 commit comments

Comments
 (0)