@: directories complete, not just files

walk({includeDirs}) yields src/ and src/ui/ with trailing slash, only
on the @ path. matchPaths ranks dirs before files for the same prefix
so @src/ surfaces the directory itself. Insertion keeps the slash.
This commit is contained in:
asepharyana
2026-09-09 13:55:02 +07:00
parent 5028ea6973
commit 4b4ddd0585
5 changed files with 96 additions and 10 deletions
+64
View File
@@ -0,0 +1,64 @@
import { expect, test } from 'bun:test';
import { mkdirSync, mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { walk } from '../src/ignore';
import { matchPaths, completePath, pathToken } from '../src/complete';
test('walk includeDirs yields directories with trailing slash', async () => {
const dir = mkdtempSync(join(tmpdir(), 'shiro-dirs-'));
const orig = process.cwd();
try {
process.chdir(dir);
await Bun.write('a.ts', '');
mkdirSync('src/ui', { recursive: true });
await Bun.write('src/ui/App.tsx', '');
await Bun.write('src/session.ts', '');
const out: string[] = [];
for await (const p of walk({ includeDirs: true })) out.push(p);
expect(out).toContain('src/');
expect(out).toContain('src/ui/');
expect(out).toContain('src/session.ts');
expect(out).toContain('a.ts');
} finally {
process.chdir(orig);
rmSync(dir, { recursive: true, force: true });
}
});
test('walk without includeDirs still file-only (existing behaviour)', async () => {
const dir = mkdtempSync(join(tmpdir(), 'shiro-nodirs-'));
const orig = process.cwd();
try {
process.chdir(dir);
mkdirSync('src', { recursive: true });
await Bun.write('src/a.ts', '');
const out: string[] = [];
for await (const p of walk()) out.push(p);
expect(out).not.toContain('src/');
expect(out).toContain('src/a.ts');
} finally {
process.chdir(orig);
rmSync(dir, { recursive: true, force: true });
}
});
test('matchPaths surfaces dirs for a directory prefix and keeps trailing slash through completion', () => {
const paths = ['src/', 'src/ui/', 'src/ui/App.tsx', 'src/session.ts', 'README.md'];
const hits = matchPaths(paths, 'src/');
expect(hits).toContain('src/');
expect(hits).toContain('src/ui/');
// dirs rank before files for the same prefix
expect(hits.indexOf('src/ui/')).toBeLessThan(hits.indexOf('src/ui/App.tsx'));
// inserted dir keeps trailing slash and trailing space
const tok = pathToken('@src/u', 6)!;
const { value } = completePath('@src/u', tok, 'src/ui/');
expect(value).toBe('src/ui/ ');
});
test('matchPaths with empty query prefers dirs', () => {
const hits = matchPaths(['src/', 'a.ts', 'src/ui/', 'b.ts'], '', 3);
expect(hits[0]).toBe('src/');
});