-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
35 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { BaseFilter, FilterArguments, Item } from "../@deps/ddc.ts"; | ||
import { extendedMatch, Fzf } from "https://esm.sh/[email protected]"; | ||
// @deno-types=./sorter_fzf/fzf_type.ts | ||
import { Fzf } from "npm:[email protected]"; | ||
|
||
type Never = Record<PropertyKey, never>; | ||
|
||
export function commonString(haystack: string, needle: string): string { | ||
function commonString(haystack: string, needle: string): string { | ||
haystack = haystack.toLowerCase(); | ||
needle = needle.toLowerCase(); | ||
const matches: string[] = []; | ||
|
@@ -18,10 +19,24 @@ export function commonString(haystack: string, needle: string): string { | |
return matches.join(""); | ||
} | ||
|
||
function commonString2(haystack: string, needle: string): string { | ||
const found = []; | ||
for (let offset = 0; offset < needle.length; offset++) { | ||
const n = needle.slice(offset); | ||
const matches = commonString(haystack, n); | ||
found.push(matches); | ||
if (matches.length == n.length) { | ||
break; | ||
} | ||
} | ||
found.sort((a, b) => b.length - a.length); | ||
return found[0] ?? ""; | ||
} | ||
|
||
export class Filter extends BaseFilter<Never> { | ||
filter(args: FilterArguments<Never>): Item[] { | ||
const a = args.items.map((item, index) => { | ||
const common = commonString(item.word, args.completeStr); | ||
const common = commonString2(item.word, args.completeStr); | ||
return { | ||
common, | ||
index, | ||
|
@@ -31,7 +46,6 @@ export class Filter extends BaseFilter<Never> { | |
const b = Map.groupBy(a, (e) => e.common); | ||
const c = [...b.entries()].map(([key, es]) => { | ||
const fzf = new Fzf(es, { | ||
match: extendedMatch, | ||
selector: (e) => e.item.word, | ||
sort: false, | ||
}); | ||
|
@@ -55,7 +69,7 @@ export class Filter extends BaseFilter<Never> { | |
.map((p) => ({ | ||
name: "ddc-filter-sorter_fzf", | ||
type: "abbr", | ||
hl_group: "String", | ||
hl_group: "PumHighlight", | ||
col: p + 1, | ||
width: 1, | ||
})); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Note: Currently can't use type declare in npm import at `deno lsp` | ||
type FzfResult<T> = { | ||
item: T; | ||
positions: Set<number>; | ||
score: number; | ||
}; | ||
|
||
type FzfOptions<T> = { | ||
selector?: (item: T) => string; | ||
sort?: boolean; | ||
}; | ||
|
||
export declare class Fzf<T> { | ||
constructor(items: T[], options: FzfOptions<T>); | ||
find(key: string): FzfResult<T>[]; | ||
} |