diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index f9427c3..cc9eb14 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -671,8 +671,6 @@ pickers.entry_manager = function(max_results, set_entry) return setmetatable({ add_entry = function(self, score, entry) - assert(type(entry) == "table", "entry must be a table by the time it reaches here") - score = score or 0 for index, item in ipairs(entry_state) do @@ -727,6 +725,10 @@ pickers.entry_manager = function(max_results, set_entry) return (entry_state[index] or {}).entry end, + get_score = function(_, index) + return (entry_state[index] or {}).score + end, + find_entry = function(_, entry) if entry == nil then return nil diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index 5931eb0..6371bbe 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -55,7 +55,6 @@ sorters.get_ngram_sorter = function() return score end) - print(prompt, line, result) return ok and result or 1 end } @@ -209,13 +208,6 @@ sorters.get_fuzzy_file = function(opts) } end -sorters.get_norcalli_sorter = function() - vim.api.nvim_err_writeln( - "get_norcalli_sorter() is changed to get_generic_fuzzy_sorter()" - ) - return sorters.get_generic_fuzzy_sorter() -end - sorters.get_generic_fuzzy_sorter = function() local ngramlen = 2 diff --git a/lua/tests/telescope_spec.lua b/lua/tests/automated/telescope_spec.lua similarity index 83% rename from lua/tests/telescope_spec.lua rename to lua/tests/automated/telescope_spec.lua index 4579902..8decb50 100644 --- a/lua/tests/telescope_spec.lua +++ b/lua/tests/automated/telescope_spec.lua @@ -1,5 +1,7 @@ require('plenary.test_harness'):setup_busted() +local assert = require('luassert') + local log = require('telescope.log') log.level = 'info' -- log.use_console = false @@ -24,7 +26,7 @@ describe('Picker', function() manager:add_entry(1, "hello") - assert.are.same(1, manager:_get_state()[1].score) + assert.are.same(1, manager:get_score(1)) end) it('works with two entries', function() @@ -33,8 +35,8 @@ describe('Picker', function() manager:add_entry(1, "hello") manager:add_entry(2, "later") - assert.are.same("hello", manager:get_ordinal(1)) - assert.are.same("later", manager:get_ordinal(2)) + assert.are.same("hello", manager:get_entry(1)) + assert.are.same("later", manager:get_entry(2)) end) it('calls functions when inserting', function() @@ -62,8 +64,8 @@ describe('Picker', function() manager:add_entry(5, "worse result") manager:add_entry(2, "better result") - assert.are.same("better result", manager:get_ordinal(1)) - assert.are.same("worse result", manager:get_ordinal(2)) + assert.are.same("better result", manager:get_entry(1)) + assert.are.same("worse result", manager:get_entry(2)) -- once to insert "worse" -- once to insert "better" @@ -77,7 +79,7 @@ describe('Picker', function() manager:add_entry(2, "better result") manager:add_entry(5, "worse result") - assert.are.same("better result", manager:get_ordinal(1)) + assert.are.same("better result", manager:get_entry(1)) -- once to insert "worse" -- once to insert "better" @@ -210,21 +212,21 @@ describe('Picker', function() end) describe('Sorters', function() - describe('norcalli_sorter', function() + describe('generic_fuzzy_sorter', function() it('sort matches well', function() - local sorter = require('telescope.sorters').get_norcalli_sorter() + local sorter = require('telescope.sorters').get_generic_fuzzy_sorter() local exact_match = sorter:score('hello', 'hello') local no_match = sorter:score('abcdef', 'ghijkl') local ok_match = sorter:score('abcdef', 'ab') - assert(exact_match < no_match) - assert(exact_match < ok_match) - assert(ok_match < no_match) + assert(exact_match < no_match, "exact match better than no match") + assert(exact_match < ok_match, "exact match better than ok match") + assert(ok_match < no_match, "ok match better than no match") end) it('sorts multiple finds better', function() - local sorter = require('telescope.sorters').get_norcalli_sorter() + local sorter = require('telescope.sorters').get_generic_fuzzy_sorter() local multi_match = sorter:score('generics', 'exercises/generics/generics2.rs') local one_match = sorter:score('abcdef', 'exercises/generics/README.md') @@ -245,27 +247,30 @@ describe('Sorters', function() exact_match < no_match, string.format("Exact match better than no match: %s %s", exact_match, no_match) ) - assert(exact_match < ok_match, "Exact match better than OK match") + assert( + exact_match < ok_match, + string.format("Exact match better than OK match: %s %s", exact_match, ok_match) + ) assert(ok_match < no_match, "OK match better than no match") end) it('sorts matches after last os sep better', function() local sorter = require('telescope.sorters').get_fuzzy_file() - local exact_match = sorter:score('aaa/bbb', 'aaa') - local ok_match = sorter:score('bbb/aaa', 'aaa') + local better_match = sorter:score('aaa', 'bbb/aaa') + local worse_match = sorter:score('aaa', 'aaa/bbb') - assert(exact_match < ok_match, "Exact match better than OK match") + assert(better_match < worse_match, "Final match should be stronger") end) - -- it('sorts multiple finds better', function() - -- local sorter = require('telescope.sorters').get_fuzzy_file() + it('sorts multiple finds better', function() + local sorter = require('telescope.sorters').get_fuzzy_file() - -- local multi_match = sorter:score('generics', 'exercises/generics/generics2.rs') - -- local one_match = sorter:score('abcdef', 'exercises/generics/README.md') + local multi_match = sorter:score('generics', 'exercises/generics/generics2.rs') + local one_match = sorter:score('abcdef', 'exercises/generics/README.md') - -- assert(multi_match < one_match) - -- end) + assert(multi_match < one_match) + end) end) end)