Skip to content

Commit

Permalink
とりあえず直そーとしてみた
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed Jun 6, 2024
1 parent 31e1800 commit 944cdf3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
24 changes: 19 additions & 5 deletions denops/@ddc-filters/sorter_fzf.ts
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[] = [];
Expand All @@ -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,
Expand All @@ -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,
});
Expand All @@ -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,
}));
Expand Down
16 changes: 16 additions & 0 deletions denops/@ddc-filters/sorter_fzf/fzf_type.ts
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>[];
}

0 comments on commit 944cdf3

Please sign in to comment.