├── LICENSE ├── Makefile ├── README.md ├── denops └── @ddc-filters │ ├── matcher_full_fuzzy.ts │ ├── matcher_fuzzy.ts │ └── tests │ ├── full_fuzzy_test.ts │ └── fuzzy_test.ts └── doc └── ddc-matcher_fuzzy.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Haruki Matsui 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TARGETS := $$(find . \( -name '*.ts' -or -name '*.md' \) -not -path './.deno/*') 2 | 3 | .DEFAULT_GOAL := help 4 | 5 | help: 6 | @cat $(MAKEFILE_LIST) | \ 7 | perl -ne 'print if /^\w+.*##/;' | \ 8 | perl -pe 's/(.*):.*##\s*/sprintf("%-20s",$$1)/eg;' 9 | 10 | fmt: FORCE ## Format code 11 | @deno fmt 12 | 13 | fmt-check: FORCE ## Format check 14 | @deno fmt --check 15 | 16 | lint: FORCE ## Lint code 17 | @deno lint 18 | 19 | type-check: FORCE ## Type check 20 | @deno test --unstable --no-run ${TARGETS} 21 | 22 | test: FORCE ## Test 23 | @deno test --unstable -A --no-check --jobs 24 | 25 | deps: FORCE ## Update dependencies 26 | @deno run -A https://deno.land/x/udd@0.8.1/main.ts ${TARGETS} 27 | @make fmt 28 | 29 | FORCE: 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ddc-matcher_fuzzy 2 | 3 | Fuzzy matcher for ddc.vim 4 | 5 | For better support such as sorting candidates by match distance and pum.vim's 6 | highlighting support, use [ddc-fuzzy](https://github.com/tani/ddc-fuzzy) 7 | instead. 8 | 9 | ## Required 10 | 11 | ### denops.vim 12 | 13 | https://github.com/vim-denops/denops.vim 14 | 15 | ### ddc.vim 16 | 17 | https://github.com/Shougo/ddc.vim 18 | 19 | ## Configuration 20 | 21 | For details, please see [help](doc/ddc-matcher_fuzzy.txt). 22 | 23 | ### examples 24 | 25 | ```vim 26 | call ddc#custom#patch_global('sourceOptions', { 27 | \ '_': { 28 | \ 'matchers': ['matcher_fuzzy'], 29 | \ }) 30 | 31 | call ddc#custom#patch_global('filterParams', { 32 | \ 'matcher_fuzzy': {'camelcase': v:true}, 33 | \ }) 34 | ``` 35 | -------------------------------------------------------------------------------- /denops/@ddc-filters/matcher_full_fuzzy.ts: -------------------------------------------------------------------------------- 1 | import { BaseFilter, Item } from "https://deno.land/x/ddc_vim@v3.3.0/types.ts"; 2 | import { 3 | FilterArguments, 4 | } from "https://deno.land/x/ddc_vim@v3.3.0/base/filter.ts"; 5 | import { fuzzyEscape } from "./matcher_fuzzy.ts"; 6 | 7 | export type Params = { 8 | camelcase: boolean; 9 | }; 10 | 11 | export class Filter extends BaseFilter { 12 | filter({ 13 | sourceOptions, 14 | filterParams, 15 | completeStr, 16 | items, 17 | }: FilterArguments): Promise { 18 | if (sourceOptions.ignoreCase) { 19 | completeStr = completeStr.toLowerCase(); 20 | } 21 | const pattern = new RegExp( 22 | fuzzyEscape(completeStr, filterParams.camelcase as boolean), 23 | ); 24 | if (sourceOptions.ignoreCase) { 25 | return Promise.resolve(items.filter( 26 | (candidate) => candidate.word.toLowerCase().match(pattern), 27 | )); 28 | } else { 29 | return Promise.resolve(items.filter( 30 | (candidate) => candidate.word.match(pattern), 31 | )); 32 | } 33 | } 34 | 35 | params(): Params { 36 | return { 37 | camelcase: false, 38 | }; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /denops/@ddc-filters/matcher_fuzzy.ts: -------------------------------------------------------------------------------- 1 | import { BaseFilter, Item } from "https://deno.land/x/ddc_vim@v3.3.0/types.ts"; 2 | import { 3 | FilterArguments, 4 | } from "https://deno.land/x/ddc_vim@v3.3.0/base/filter.ts"; 5 | 6 | export function fuzzyEscape(str: string, camelcase: boolean): string { 7 | // escape special letters 8 | let p = str.replaceAll(/[.*+?^=!:${}()|[\]\/\\]/g, "\\$&"); 9 | p = p.replaceAll(/([a-zA-Z0-9])/g, "$1.*"); 10 | if (camelcase && str.search(/[A-Z]/) != -1) { 11 | p = p.replaceAll( 12 | /([a-z])/g, 13 | (pat) => `[${pat.concat(pat.toUpperCase())}]`, 14 | ); 15 | } 16 | p = p.replaceAll(/([a-zA-Z0-9_])\.\*/g, "$1[^$1]*"); 17 | return p; 18 | } 19 | 20 | export type Params = { 21 | camelcase: boolean; 22 | }; 23 | 24 | export class Filter extends BaseFilter { 25 | filter({ 26 | sourceOptions, 27 | filterParams, 28 | completeStr, 29 | items, 30 | }: FilterArguments): Promise { 31 | if (!completeStr) { 32 | return Promise.resolve(items); 33 | } 34 | if (sourceOptions.ignoreCase) { 35 | completeStr = completeStr.toLowerCase(); 36 | } 37 | const pattern = new RegExp( 38 | "^" + fuzzyEscape(completeStr, filterParams.camelcase as boolean), 39 | ); 40 | 41 | if (sourceOptions.ignoreCase) { 42 | return Promise.resolve(items.filter( 43 | (candidate) => candidate.word.toLowerCase().search(pattern) != -1, 44 | )); 45 | } else { 46 | return Promise.resolve(items.filter( 47 | (candidate) => candidate.word.search(pattern) != -1, 48 | )); 49 | } 50 | } 51 | 52 | params(): Params { 53 | return { 54 | camelcase: false, 55 | }; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /denops/@ddc-filters/tests/full_fuzzy_test.ts: -------------------------------------------------------------------------------- 1 | import { Item } from "https://deno.land/x/ddc_vim@v3.3.0/types.ts"; 2 | import { 3 | FilterArguments, 4 | } from "https://deno.land/x/ddc_vim@v3.3.0/base/filter.ts"; 5 | import { assertEquals } from "https://deno.land/std@0.166.0/testing/asserts.ts"; 6 | import { Filter, Params } from "../matcher_full_fuzzy.ts"; 7 | 8 | export function filterWrapper( 9 | completeStr: string, 10 | items: Item[], 11 | ignoreCase = true, 12 | camelcase = true, 13 | ): Promise { 14 | const filter = new Filter(); 15 | return filter.filter({ 16 | sourceOptions: { ignoreCase }, 17 | filterParams: { camelcase }, 18 | completeStr, 19 | items, 20 | } as FilterArguments); 21 | } 22 | 23 | Deno.test("fuzzy filter", async () => { 24 | const testCandidates = [ 25 | { "word": "foobar" }, 26 | { "word": "afoobar" }, 27 | { "word": "fooBar" }, 28 | { "word": "afooBar" }, 29 | { "word": "Foobar" }, 30 | { "word": "aFoobar" }, 31 | { "word": "FooBar" }, 32 | { "word": "aFooBar" }, 33 | ]; 34 | 35 | assertEquals(await filterWrapper("", testCandidates), [ 36 | { "word": "foobar" }, 37 | { "word": "afoobar" }, 38 | { "word": "fooBar" }, 39 | { "word": "afooBar" }, 40 | { "word": "Foobar" }, 41 | { "word": "aFoobar" }, 42 | { "word": "FooBar" }, 43 | { "word": "aFooBar" }, 44 | ]); 45 | assertEquals(await filterWrapper("FOBR", testCandidates), [ 46 | { "word": "foobar" }, 47 | { "word": "afoobar" }, 48 | { "word": "fooBar" }, 49 | { "word": "afooBar" }, 50 | { "word": "Foobar" }, 51 | { "word": "aFoobar" }, 52 | { "word": "FooBar" }, 53 | { "word": "aFooBar" }, 54 | ]); 55 | assertEquals(await filterWrapper("foBr", testCandidates, false, true), [ 56 | { "word": "fooBar" }, 57 | { "word": "afooBar" }, 58 | { "word": "FooBar" }, 59 | { "word": "aFooBar" }, 60 | ]); 61 | assertEquals(await filterWrapper("fobr", testCandidates, true, false), [ 62 | { "word": "foobar" }, 63 | { "word": "afoobar" }, 64 | { "word": "fooBar" }, 65 | { "word": "afooBar" }, 66 | { "word": "Foobar" }, 67 | { "word": "aFoobar" }, 68 | { "word": "FooBar" }, 69 | { "word": "aFooBar" }, 70 | ]); 71 | assertEquals(await filterWrapper("fobr", testCandidates, false, false), [ 72 | { "word": "foobar" }, 73 | { "word": "afoobar" }, 74 | ]); 75 | 76 | await filterWrapper("foo+=", testCandidates, false, false); 77 | }); 78 | -------------------------------------------------------------------------------- /denops/@ddc-filters/tests/fuzzy_test.ts: -------------------------------------------------------------------------------- 1 | import { Item } from "https://deno.land/x/ddc_vim@v3.3.0/types.ts"; 2 | import { 3 | FilterArguments, 4 | } from "https://deno.land/x/ddc_vim@v3.3.0/base/filter.ts"; 5 | import { assertEquals } from "https://deno.land/std@0.166.0/testing/asserts.ts"; 6 | import { Filter, Params } from "../matcher_fuzzy.ts"; 7 | 8 | export function filterWrapper( 9 | completeStr: string, 10 | items: Item[], 11 | ignoreCase = true, 12 | camelcase = true, 13 | ): Promise { 14 | const filter = new Filter(); 15 | return filter.filter({ 16 | sourceOptions: { ignoreCase }, 17 | filterParams: { camelcase }, 18 | completeStr, 19 | items, 20 | } as FilterArguments); 21 | } 22 | 23 | Deno.test("fuzzy filter", async () => { 24 | const testCandidates = [ 25 | { "word": "foobar" }, 26 | { "word": "afoobar" }, 27 | { "word": "fooBar" }, 28 | { "word": "afooBar" }, 29 | { "word": "Foobar" }, 30 | { "word": "aFoobar" }, 31 | { "word": "FooBar" }, 32 | { "word": "aFooBar" }, 33 | ]; 34 | 35 | assertEquals(await filterWrapper("", testCandidates), [ 36 | { "word": "foobar" }, 37 | { "word": "afoobar" }, 38 | { "word": "fooBar" }, 39 | { "word": "afooBar" }, 40 | { "word": "Foobar" }, 41 | { "word": "aFoobar" }, 42 | { "word": "FooBar" }, 43 | { "word": "aFooBar" }, 44 | ]); 45 | assertEquals(await filterWrapper("FOBR", testCandidates), [ 46 | { "word": "foobar" }, 47 | { "word": "fooBar" }, 48 | { "word": "Foobar" }, 49 | { "word": "FooBar" }, 50 | ]); 51 | assertEquals(await filterWrapper("foBr", testCandidates, false, false), [ 52 | { "word": "fooBar" }, 53 | ]); 54 | assertEquals(await filterWrapper("foBr", testCandidates, false, true), [ 55 | { "word": "fooBar" }, 56 | { "word": "FooBar" }, 57 | ]); 58 | assertEquals(await filterWrapper("fobr", testCandidates, true, false), [ 59 | { "word": "foobar" }, 60 | { "word": "fooBar" }, 61 | { "word": "Foobar" }, 62 | { "word": "FooBar" }, 63 | ]); 64 | assertEquals(await filterWrapper("fobr", testCandidates, true, true), [ 65 | { "word": "foobar" }, 66 | { "word": "fooBar" }, 67 | { "word": "Foobar" }, 68 | { "word": "FooBar" }, 69 | ]); 70 | assertEquals(await filterWrapper("fobr", testCandidates, false, false), [ 71 | { "word": "foobar" }, 72 | ]); 73 | 74 | await filterWrapper("foo+=", testCandidates, false, false); 75 | assertEquals(await filterWrapper("hg", [{ "word": "Hoge" }], true, false), [ 76 | { "word": "Hoge" }, 77 | ]); 78 | }); 79 | -------------------------------------------------------------------------------- /doc/ddc-matcher_fuzzy.txt: -------------------------------------------------------------------------------- 1 | *ddc-matcher_fuzzy.txt* Fuzzy matcher for ddc.vim 2 | 3 | Author: matsui54 4 | License: MIT license 5 | 6 | CONTENTS *ddc-matcher_fuzzy-contents* 7 | 8 | Introduction |ddc-matcher_fuzzy-introduction| 9 | Install |ddc-matcher_fuzzy-install| 10 | Examples |ddc-matcher_fuzzy-examples| 11 | Matcher |ddc-matcher_fuzzy-matchers| 12 | Params |ddc-matcher_fuzzy-params| 13 | 14 | 15 | ============================================================================== 16 | INTRODUCTION *ddc-matcher_fuzzy-introduction* 17 | 18 | This matcher filters candidates which matches fuzzily with user input. 19 | 20 | For example, candidates like "foobar" matches "fb", "foor" and so on. 21 | 22 | ============================================================================== 23 | INSTALL *ddc-matcher_fuzzy-install* 24 | 25 | Please install both "ddc.vim" and "denops.vim". 26 | 27 | https://github.com/Shougo/ddc.vim 28 | https://github.com/vim-denops/denops.vim 29 | 30 | 31 | ============================================================================== 32 | EXAMPLES *ddc-matcher_fuzzy-examples* 33 | 34 | > 35 | call ddc#custom#patch_global('sourceOptions', { 36 | \ '_': { 37 | \ 'matchers': ['matcher_fuzzy'], 38 | \ }) 39 | 40 | call ddc#custom#patch_global('filterParams', { 41 | \ 'matcher_fuzzy': {'camelcase': v:true}, 42 | \ }) 43 | < 44 | 45 | ============================================================================== 46 | MATCHERS *ddc-matcher_fuzzy-matchers* 47 | 48 | *ddc-matcher_fuzzy-matchers-matcher_fuzzy* 49 | matcher_fuzzy Fuzzy matcher, but the first character must be the same. 50 | 51 | *ddc-matcher_fuzzy-matchers-matcher_full_fuzzy* 52 | matcher_full_fuzzy Fuzzy matcher, first character doesn't need to be the same. 53 | 54 | 55 | ============================================================================== 56 | PARAMS *ddc-matcher_fuzzy-params* 57 | 58 | *ddc-matcher_fuzzy-param-camelcase* 59 | camelcase (boolean) 60 | If it is true, when user input includes uppercase character, 61 | lowercase characters in input match with uppercase characters 62 | of candidates. 63 | Ex: "foB" is matched with "FooBar" but not with "foobar". 64 | 65 | Default: v:true 66 | 67 | 68 | ============================================================================== 69 | vim:tw=78:ts=8:ft=help:norl:noet:fen:noet: 70 | --------------------------------------------------------------------------------