Skip to content

Commit f4f0f0f

Browse files
committed
feat: make fzf behave like fzf cli
1 parent 1ca518c commit f4f0f0f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

builtin/matcher/fzf.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { asyncExtendedMatch, AsyncFzf } from "fzf";
1+
import {
2+
asyncExtendedMatch,
3+
AsyncFzf,
4+
type AsyncFzfOptions,
5+
type Tiebreaker,
6+
} from "fzf";
27

3-
import type { IdItem } from "../../item.ts";
8+
import type { Detail, IdItem } from "../../item.ts";
49
import { defineMatcher, type Matcher } from "../../matcher.ts";
510

611
/**
@@ -10,13 +15,9 @@ import { defineMatcher, type Matcher } from "../../matcher.ts";
1015
* - `normalize`: Normalizes characters for comparison (e.g., ignoring accents).
1116
* - `sort`: Enables sorting of results.
1217
* - `forward`: Controls the search direction (forward or backward).
18+
* - `match`: Custom matching function for FZF.
1319
*/
14-
export type FzfOptions = {
15-
casing?: "smart-case" | "case-sensitive" | "case-insensitive";
16-
normalize?: boolean;
17-
sort?: boolean;
18-
forward?: boolean;
19-
};
20+
export type FzfOptions = Omit<AsyncFzfOptions<IdItem<Detail>>, "selector">;
2021

2122
/**
2223
* Creates an FZF-based matcher that filters items based on user query terms.
@@ -33,6 +34,7 @@ export function fzf(options: FzfOptions = {}): Matcher {
3334
const normalize = options.normalize ?? true;
3435
const sort = options.sort ?? true;
3536
const forward = options.forward ?? true;
37+
const match = options.match ?? asyncExtendedMatch;
3638

3739
return defineMatcher(async function* (_denops, { items, query }, { signal }) {
3840
if (query.trim() === "") {
@@ -51,7 +53,8 @@ export function fzf(options: FzfOptions = {}): Matcher {
5153
normalize,
5254
sort,
5355
forward,
54-
match: asyncExtendedMatch,
56+
match,
57+
tiebreakers: [byTrimmedLengthAsc],
5558
});
5659

5760
// Perform the FZF search
@@ -83,3 +86,8 @@ export function fzf(options: FzfOptions = {}): Matcher {
8386
yield* items;
8487
});
8588
}
89+
90+
// deno-lint-ignore no-explicit-any
91+
const byTrimmedLengthAsc: Tiebreaker<IdItem<any>> = (a, b, selector) => {
92+
return selector(a.item).trim().length - selector(b.item).trim().length;
93+
};

0 commit comments

Comments
 (0)