fuzzy-match + recency-rank the command palette
Completes the command-palette acceptance: the filter is now a subsequence fuzzy match (was substring), and recently-invoked commands float to the top and break score ties. The subsequence matcher is extracted to a shared fuzzy helper so the palette and quick-open file finder use one implementation instead of a private copy each. Pinned commands and cross-session recency persistence are left as a follow-up (they need a pin affordance + settings storage). Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -57,5 +57,33 @@ void main() {
|
||||
expect(palette.isOpen, false);
|
||||
expect(palette.filter, '');
|
||||
});
|
||||
|
||||
test('fuzzy subsequence match — non-contiguous chars (not substring)', () {
|
||||
// "gc" / "gp" aren't substrings of the titles, but are subsequences.
|
||||
palette.setFilter('gc');
|
||||
expect(palette.filtered().map((c) => c.command), contains('git.commit'));
|
||||
palette.setFilter('gp');
|
||||
expect(palette.filtered().map((c) => c.command), contains('git.push'));
|
||||
// Garbage that isn't a subsequence matches nothing.
|
||||
palette.setFilter('zzz');
|
||||
expect(palette.filtered(), isEmpty);
|
||||
});
|
||||
|
||||
test('recency floats invoked commands to the top on an empty filter', () async {
|
||||
expect(palette.filtered().first.command, 'git.commit'); // registry order
|
||||
await palette.invoke('theme.pick');
|
||||
expect(palette.filtered().first.command, 'theme.pick');
|
||||
await palette.invoke('git.push');
|
||||
expect(palette.filtered().map((c) => c.command).take(2).toList(), ['git.push', 'theme.pick']);
|
||||
});
|
||||
|
||||
test('recency breaks fuzzy-score ties', () async {
|
||||
// "git" matches both git.* titles with an equal score; invoking push
|
||||
// floats it above commit among the equal matches.
|
||||
await palette.invoke('git.push');
|
||||
palette.setFilter('git');
|
||||
final order = palette.filtered().map((c) => c.command).toList();
|
||||
expect(order.indexOf('git.push'), lessThan(order.indexOf('git.commit')));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import 'package:clide/kernel/src/fuzzy.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('fuzzyScore', () {
|
||||
test('empty query scores 0 (matches anything)', () {
|
||||
expect(fuzzyScore('anything', ''), 0);
|
||||
});
|
||||
|
||||
test('returns null when query is not a subsequence', () {
|
||||
expect(fuzzyScore('git commit', 'xyz'), isNull);
|
||||
expect(fuzzyScore('abc', 'abcd'), isNull); // query longer / extra char
|
||||
});
|
||||
|
||||
test('matches non-contiguous subsequences', () {
|
||||
expect(fuzzyScore('git commit', 'gc'), isNotNull);
|
||||
expect(fuzzyScore('theme pick', 'tp'), isNotNull);
|
||||
});
|
||||
|
||||
test('lower score is better: contiguous + early beats gappy + late', () {
|
||||
final contiguousEarly = fuzzyScore('abcxxxx', 'abc')!; // match at 0,1,2
|
||||
final gappyLate = fuzzyScore('xxxxabc', 'abc')!; // starts at 4
|
||||
expect(contiguousEarly, lessThan(gappyLate));
|
||||
|
||||
final tight = fuzzyScore('ab', 'ab')!; // adjacent
|
||||
final spread = fuzzyScore('axb', 'ab')!; // a gap between a and b
|
||||
expect(tight, lessThan(spread));
|
||||
});
|
||||
|
||||
test('case sensitivity is the caller\'s responsibility', () {
|
||||
expect(fuzzyScore('GIT', 'git'), isNull); // differing case → no match
|
||||
expect(fuzzyScore('GIT'.toLowerCase(), 'git'), isNotNull);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user