From fdb1f4d43d9e73052bed1c3b3aff57d819e7d405 Mon Sep 17 00:00:00 2001 From: Thomas Vigouroux Date: Tue, 13 Jun 2023 17:17:11 +0200 Subject: [PATCH] feat: use azy instead of fzy-lua-native to perform matching --- README.md | 2 +- lua/complementree/comparators.lua | 40 ++++++++++++++----------------- lua/complementree/defaults.lua | 2 +- lua/complementree/init.lua | 2 +- teal/complementree/comparators.tl | 40 ++++++++++++++----------------- teal/complementree/defaults.tl | 2 +- teal/complementree/filters.tl | 2 +- teal/complementree/sources.tl | 4 ++-- types/fzy.d.tl | 17 +++++++++++++ types/types.d.tl | 1 + 10 files changed, 61 insertions(+), 51 deletions(-) create mode 100644 types/fzy.d.tl diff --git a/README.md b/README.md index 71637d5..ac6185e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Features and design goals: ## Installation ```lua -use {'https://git.sr.ht/~vigoux/complementree.nvim', requires = {'L3MON4D3/LuaSnip', 'nvim-treesitter/nvim-treesitter'} } +use {'https://git.sr.ht/~vigoux/complementree.nvim', requires = {'L3MON4D3/LuaSnip', 'https://git.sr.ht/~vigoux/azy.nvim'} } ``` ## Setting up diff --git a/lua/complementree/comparators.lua b/lua/complementree/comparators.lua index 6f537e6..d2573a0 100644 --- a/lua/complementree/comparators.lua +++ b/lua/complementree/comparators.lua @@ -36,34 +36,30 @@ Comparators.length = mk_comparator(function(a, b) return #a < #b end) -local ok_fzy, fzy = pcall(require, 'fzy-lua-native') +local ok_fzy, fzy = pcall(require, 'fzy') if ok_fzy then - local function mk_fzy(is_case_sensitive) - return function(msource) - return function(ltc, lnum) - local orig, prefix = msource(ltc, lnum) - local scores = {} - local matching = {} - if prefix ~= "" then - for _, a in ipairs(orig) do - local s = fzy.score(prefix, utils.cword(a), is_case_sensitive) - if math.abs(s) ~= math.huge or prefix == utils.cword(a) then - scores[a] = s - table.insert(matching, a) - end + function Comparators.fzy(msource) + return function(ltc, lnum) + local orig, prefix = msource(ltc, lnum) + local scores = {} + local matching = {} + if prefix ~= "" then + for _, a in ipairs(orig) do + local s, _ = fzy.match(prefix, utils.cword(a)) + if math.abs(s or math.huge) ~= math.huge or prefix == utils.cword(a) then + scores[a] = s + table.insert(matching, a) end - table.sort(matching, function(a, b) - return scores[a] > scores[b] - end) - return matching, prefix - else - return orig, prefix end + table.sort(matching, function(a, b) + return scores[a] > scores[b] + end) + return matching, prefix + else + return orig, prefix end end end - Comparators.fzy = mk_fzy(false) - Comparators.ifzy = mk_fzy(true) end return Comparators diff --git a/lua/complementree/defaults.lua b/lua/complementree/defaults.lua index 94231a0..619a4bf 100644 --- a/lua/complementree/defaults.lua +++ b/lua/complementree/defaults.lua @@ -21,7 +21,7 @@ function Defaults.ins_completion(mode) end end -function Defaults.dummy() +function Defaults.dummy(_, _) end diff --git a/lua/complementree/init.lua b/lua/complementree/init.lua index 9aeff0f..36e0084 100644 --- a/lua/complementree/init.lua +++ b/lua/complementree/init.lua @@ -128,7 +128,7 @@ function M.complete() local func = get_completion(ft, line_to_cursor, lnum, pref_start) - if not (type(func) == "nil") then + if not (func == nil) then if func(line_to_cursor, lnum) then return true end diff --git a/teal/complementree/comparators.tl b/teal/complementree/comparators.tl index dd92e80..32f186e 100644 --- a/teal/complementree/comparators.tl +++ b/teal/complementree/comparators.tl @@ -36,34 +36,30 @@ Comparators.length = mk_comparator(function(a: string, b: string): boolean return #a < #b end) -local ok_fzy, fzy = pcall(require, 'fzy-lua-native') +local ok_fzy, fzy = pcall(require, 'fzy') if ok_fzy then - local function mk_fzy(is_case_sensitive: boolean): Pipe - return function(msource: Source): Source - return function(ltc: string, lnum: integer): {CompleteItem}, string - local orig, prefix = msource(ltc, lnum) - local scores: {CompleteItem:integer} = {} - local matching: {CompleteItem} = {} - if prefix ~= "" then - for _, a in ipairs(orig) do - local s = fzy.score(prefix, utils.cword(a), is_case_sensitive) - if math.abs(s) ~= math.huge or prefix == utils.cword(a) then - scores[a] = s - table.insert(matching, a) - end + function Comparators.fzy(msource: Source): Source + return function (ltc: string, lnum: integer): {CompleteItem}, string + local orig, prefix = msource(ltc, lnum) + local scores: {CompleteItem:number} = {} + local matching: {CompleteItem} = {} + if prefix ~= "" then + for _, a in ipairs(orig) do + local s, _ = fzy.match(prefix, utils.cword(a)) + if math.abs(s or math.huge) ~= math.huge or prefix == utils.cword(a) then + scores[a] = s + table.insert(matching, a) end - table.sort(matching, function(a: CompleteItem, b: CompleteItem): boolean - return scores[a] > scores[b] - end) - return matching, prefix - else - return orig, prefix end + table.sort(matching, function(a: CompleteItem, b: CompleteItem): boolean + return scores[a] > scores[b] + end) + return matching, prefix + else + return orig, prefix end end end - Comparators.fzy = mk_fzy(false) - Comparators.ifzy = mk_fzy(true) end return Comparators diff --git a/teal/complementree/defaults.tl b/teal/complementree/defaults.tl index 427b251..6114260 100644 --- a/teal/complementree/defaults.tl +++ b/teal/complementree/defaults.tl @@ -21,7 +21,7 @@ function Defaults.ins_completion(mode: string): Completor end end -function Defaults.dummy(): boolean +function Defaults.dummy(_: string, _: integer): boolean -- Does nothing end diff --git a/teal/complementree/filters.tl b/teal/complementree/filters.tl index b5b7854..75cd141 100644 --- a/teal/complementree/filters.tl +++ b/teal/complementree/filters.tl @@ -1,7 +1,7 @@ local utils = require 'complementree.utils' local record Filters - amount: Pipe + amount: function(integer): Pipe prefix: Pipe strict_prefix: Pipe substr: Pipe diff --git a/teal/complementree/sources.tl b/teal/complementree/sources.tl index 7b5cd5f..06980e8 100644 --- a/teal/complementree/sources.tl +++ b/teal/complementree/sources.tl @@ -27,7 +27,7 @@ end local record Sources lsp_matches: function(LspOptions): Source luasnip_matches: function(LuasnipOptions): Source - ctags_matches: function(LuasnipOptions): Source + ctags_matches: function(CtagsOptions): Source filepath_matches: function(FilepathOptions): Source treesitter_matches: function(TreesitterOptions): Source @@ -303,7 +303,7 @@ function Sources.filepath_matches(opts: FilepathOptions): Source }, opts) local function iter_files(): function(): string|nil - local path_stack = vim.fn.reverse(config.root_dirs or { '.' }) + local path_stack: {string} = vim.fn.reverse(config.root_dirs or { '.' }) local iter_stack = {} for _, p in ipairs(path_stack) do table.insert(iter_stack, vim.loop.fs_scandir(p)) diff --git a/types/fzy.d.tl b/types/fzy.d.tl new file mode 100644 index 0000000..b26f6f6 --- /dev/null +++ b/types/fzy.d.tl @@ -0,0 +1,17 @@ +local record Fzy + type Choices = record + add_incremental: function(Choices, {M}) + available: function(Choices): integer + elements: function(Choices): {{M, integer}} + search: function(Choices, string) + selected: function(Choices): M, integer + next: function(Choices): M, integer + prev: function(Choices): M, integer + get: function(Choices, integer): M + end + + create: function({M}): Choices + match: function(string, string): number, {integer} +end + +return Fzy diff --git a/types/types.d.tl b/types/types.d.tl index 79cd09a..3038e44 100644 --- a/types/types.d.tl +++ b/types/types.d.tl @@ -154,6 +154,7 @@ global record vim notify: function(string) pretty_print: function(...: any) inspect: function(...: any): string + print: function(...: any) end global record jit -- 2.45.2