Skip to content

Commit

Permalink
Merge pull request #3 from Milly/options
Browse files Browse the repository at this point in the history
feat: `smartCase` and `minInputLength` options
  • Loading branch information
Milly authored Aug 1, 2024
2 parents 3f900df + 8279ed8 commit 5099acd
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions denops/@ddu-filters/matcher_kensaku.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import {
} from "jsr:@shougo/ddu-vim@^5.0.0/filter";
import type {
DduItem,
FilterOptions,
ItemHighlight,
SourceOptions,
} from "jsr:@shougo/ddu-vim@^5.0.0/types";

const MATCHED_HIGHLIGHT_NAME = "matched";
const MATCHED_HIGHLIGHT_NAME = "ddu-filter-matcher_kensaku-matched";

type Params = {
/**
* The highlight group of matched text.
* If empty, this feature will be disabled.
*
* @default {""}
*/
highlightMatched: string;
};
Expand All @@ -29,36 +32,56 @@ export class Filter extends BaseFilter<Params> {
}

override async filter(
{ denops, input, items, sourceOptions, filterParams }: FilterArguments<
Params
>,
args: FilterArguments<Params>,
): Promise<DduItem[]> {
const matchers = await this.#getMatchers(denops, input, sourceOptions);
const {
denops,
input,
items,
sourceOptions,
filterOptions,
filterParams,
} = args;

const matchers = await this.#getMatchers(
denops,
input,
sourceOptions,
filterOptions,
);
if (matchers.length === 0) return items;

items = this.#extractMatches(items, matchers);
let filteredItems = this.#extractMatches(items, matchers);

if (filterParams.highlightMatched !== "") {
items = this.#updateHighlights(items, matchers, filterParams);
filteredItems = this.#updateHighlights(
filteredItems,
matchers,
filterParams,
);
}

return items;
return filteredItems;
}

async #getMatchers(
denops: Denops,
input: string,
{ ignoreCase }: SourceOptions,
{ ignoreCase, smartCase }: SourceOptions,
{ minInputLength }: FilterOptions,
): Promise<RegExp[]> {
input = input.trim();
if (input === "") return [];
if (ignoreCase) {
input = input.toLowerCase();
}
// No global flag, because it only needs to match once to extract
const patternFlags = ignoreCase ? "i" : "";
const inputParts = input.split(/\s+/).filter((part) =>
part.length >= minInputLength
);
return await Promise.all(
input.split(/\s+/).map(async (text) => {
inputParts.map(async (text) => {
let patternFlags = "";
if (ignoreCase && (!smartCase || !/\p{Lu}/v.test(text))) {
text = text.toLowerCase();
patternFlags = "i";
}
const pattern = await kensakuQuery(denops, text);
return new RegExp(pattern, patternFlags);
}),
Expand Down Expand Up @@ -110,12 +133,13 @@ export class Filter extends BaseFilter<Params> {
(item) => {
const display = item.display ?? item.word;
const ranges = getMatchedRanges(display);
const matchedHighlights = ranges.map(({ col, width }) => ({
name: MATCHED_HIGHLIGHT_NAME,
"hl_group": highlightMatched,
col,
width,
}));
const matchedHighlights = ranges
.map(({ col, width }): ItemHighlight => ({
name: MATCHED_HIGHLIGHT_NAME,
"hl_group": highlightMatched,
col,
width,
}));
const highlightsWithoutMatched =
item.highlights?.filter(({ name }) =>
name !== MATCHED_HIGHLIGHT_NAME
Expand Down

0 comments on commit 5099acd

Please sign in to comment.