feat: Action improvements (#472)
* feat: replace_map * feat: Add action_set and action_state * fix: Move all actions.get_ to action_state.get_ * fix: replace all internal references of _goto_file_selection_edit * feat: add some docs * fix: lint * feat: actions.select * remove mentions and usage of goto_file_selection APIs * feat: special case attach_mappings to be overridable and defaultable * Having goto_file_selection mappings will cause a error as well as replacing deprecated goto_file_selection methodes For config and replacing use this instead: - actions.select_default - actions.select_horizonal - actions.select_vertical - actions.select_tab Only replacing: - actions.set.edit -- for replacing all select functions * adds actions.state.select_key_to_edit_key Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
local actions = require('telescope.actions')
|
||||
local action_set = require('telescope.actions.set')
|
||||
|
||||
local transform_mod = require('telescope.actions.mt').transform_mod
|
||||
|
||||
local eq = function(a, b)
|
||||
@@ -51,6 +54,69 @@ describe('actions', function()
|
||||
eq("x", a.x())
|
||||
end)
|
||||
|
||||
it('allows overriding an action only in specific cases with if', function()
|
||||
local a = transform_mod {
|
||||
x = function(e) return e * 10 end,
|
||||
y = function() return "y" end,
|
||||
}
|
||||
|
||||
-- actions.file_goto_selection_edit:replace(...)
|
||||
a.x:replace_if(
|
||||
function(e) return e > 0 end,
|
||||
function(e) return (e / 10) end
|
||||
)
|
||||
eq(-100, a.x(-10))
|
||||
eq(10, a.x(100))
|
||||
eq(1, a.x(10))
|
||||
|
||||
a._clear()
|
||||
eq(100, a.x(10))
|
||||
end)
|
||||
|
||||
it('allows overriding an action only in specific cases with mod', function()
|
||||
local a = transform_mod {
|
||||
x = function(e) return e * 10 end,
|
||||
y = function() return "y" end,
|
||||
}
|
||||
|
||||
-- actions.file_goto_selection_edit:replace(...)
|
||||
a.x:replace_map {
|
||||
[function(e) return e > 0 end] = function(e) return (e / 10) end,
|
||||
[function(e) return e == 0 end] = function(e) return (e + 10) end,
|
||||
}
|
||||
|
||||
eq(-100, a.x(-10))
|
||||
eq(10, a.x(100))
|
||||
eq(1, a.x(10))
|
||||
eq(10, a.x(0))
|
||||
|
||||
a._clear()
|
||||
eq(100, a.x(10))
|
||||
end)
|
||||
|
||||
it('continuous replacement', function()
|
||||
local a = transform_mod {
|
||||
x = function() return "cleared" end,
|
||||
y = function() return "y" end,
|
||||
}
|
||||
|
||||
-- Replace original, which becomes new fallback
|
||||
a.x:replace(function() return "negative" end)
|
||||
|
||||
-- actions.file_goto_selection_edit:replace(...)
|
||||
a.x:replace_map {
|
||||
[function(e) return e > 0 end] = function(e) return "positive" end,
|
||||
[function(e) return e == 0 end] = function(e) return "zero" end,
|
||||
}
|
||||
|
||||
eq("positive", a.x(10))
|
||||
eq("zero" , a.x(0))
|
||||
eq("negative", a.x(-10))
|
||||
|
||||
a._clear()
|
||||
eq("cleared", a.x(10))
|
||||
end)
|
||||
|
||||
it('enhance.pre', function()
|
||||
local a = transform_mod {
|
||||
x = function() return "x" end,
|
||||
@@ -159,4 +225,27 @@ describe('actions', function()
|
||||
a.x:replace(function(bufnr) return string.format("modified: %s", bufnr) end)
|
||||
eq("modified: 5", a.x(5))
|
||||
end)
|
||||
|
||||
describe('action_set', function()
|
||||
it('can replace `action_set.edit`', function()
|
||||
action_set.edit:replace(function(_, arg) return "replaced:" .. arg end)
|
||||
eq("replaced:edit", actions.file_edit())
|
||||
eq("replaced:vnew", actions.file_vsplit())
|
||||
end)
|
||||
|
||||
it('handles backwards compat with select and edit files', function()
|
||||
-- Reproduce steps:
|
||||
-- In config, we have { ["<CR>"] = actions.select, ... }
|
||||
-- In caller, we have actions._goto:replace(...)
|
||||
-- Person calls `select`, does not see update
|
||||
action_set.edit:replace(function(_, arg) return "default_to_edit:" .. arg end)
|
||||
eq("default_to_edit:edit", actions.select_default())
|
||||
|
||||
action_set.select:replace(function(_, arg) return "override_with_select:" .. arg end)
|
||||
eq("override_with_select:default", actions.select_default())
|
||||
|
||||
-- Sometimes you might want to change the default selection...
|
||||
-- but you don't want to prohibit the ability to edit the code...
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
@@ -1,202 +1,205 @@
|
||||
local assert = require('luassert')
|
||||
local picker = require('telescope.pickers')
|
||||
|
||||
local log = require('telescope.log')
|
||||
log.level = 'info'
|
||||
-- log.use_console = false
|
||||
local eq = assert.are.same
|
||||
|
||||
--[[
|
||||
lua RELOAD('plenary'); require("plenary.test_harness"):test_directory("busted", "./tests/automated")
|
||||
--]]
|
||||
describe('telescope', function()
|
||||
describe('Picker', function()
|
||||
describe('window_dimensions', function()
|
||||
it('', function()
|
||||
assert(true)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Picker', function()
|
||||
describe('window_dimensions', function()
|
||||
it('', function()
|
||||
assert(true)
|
||||
describe('attach_mappings', function()
|
||||
it('should allow for passing in a function', function()
|
||||
local p = picker.new({}, { attach_mappings = function() return 1 end })
|
||||
eq(1, p.attach_mappings())
|
||||
end)
|
||||
|
||||
it('should override an attach mappings passed in by opts', function()
|
||||
local called_order = {}
|
||||
local p = picker.new({
|
||||
attach_mappings = function()
|
||||
table.insert(called_order, 'opts')
|
||||
end,
|
||||
}, {
|
||||
attach_mappings = function()
|
||||
table.insert(called_order, 'default')
|
||||
end
|
||||
})
|
||||
|
||||
p.attach_mappings()
|
||||
|
||||
eq({'default', 'opts'}, called_order)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Sorters', function()
|
||||
describe('generic_fuzzy_sorter', function()
|
||||
it('sort matches well', function()
|
||||
local sorter = require('telescope.sorters').get_generic_fuzzy_sorter()
|
||||
|
||||
local exact_match = sorter:score('hello', {ordinal = 'hello'})
|
||||
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||
|
||||
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_generic_fuzzy_sorter()
|
||||
|
||||
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)
|
||||
end)
|
||||
|
||||
describe('fuzzy_file', function()
|
||||
it('sort matches well', function()
|
||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||
|
||||
local exact_match = sorter:score('abcdef', {ordinal = 'abcdef'})
|
||||
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||
|
||||
assert(
|
||||
exact_match < no_match,
|
||||
string.format("Exact match better than no match: %s %s", exact_match, no_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 better_match = sorter:score('aaa', {ordinal = 'bbb/aaa'})
|
||||
local worse_match = sorter:score('aaa', {ordinal = 'aaa/bbb'})
|
||||
|
||||
assert(better_match < worse_match, "Final match should be stronger")
|
||||
end)
|
||||
|
||||
pending('sorts multiple finds better', function()
|
||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||
|
||||
local multi_match = sorter:score('generics', {ordinal = 'exercises/generics/generics2.rs'})
|
||||
local one_match = sorter:score('abcdef', {ordinal = 'exercises/generics/README.md'})
|
||||
|
||||
assert(multi_match < one_match)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('fzy', function()
|
||||
local sorter = require'telescope.sorters'.get_fzy_sorter()
|
||||
local function score(prompt, line)
|
||||
return sorter:score(prompt, {ordinal = line})
|
||||
end
|
||||
|
||||
describe("matches", function()
|
||||
it("exact matches", function()
|
||||
assert.True(score("a", "a") >= 0)
|
||||
assert.True(score("a.bb", "a.bb") >= 0)
|
||||
end)
|
||||
it("ignore case", function()
|
||||
assert.True(score("AbB", "abb") >= 0)
|
||||
assert.True(score("abb", "ABB") >= 0)
|
||||
end)
|
||||
it("partial matches", function()
|
||||
assert.True(score("a", "ab") >= 0)
|
||||
assert.True(score("a", "ba") >= 0)
|
||||
assert.True(score("aba", "baabbaab") >= 0)
|
||||
end)
|
||||
it("with delimiters between", function()
|
||||
assert.True(score("abc", "a|b|c") >= 0)
|
||||
end)
|
||||
it("with empty query", function()
|
||||
assert.True(score("", "") >= 0)
|
||||
assert.True(score("", "a") >= 0)
|
||||
end)
|
||||
it("rejects non-matches", function()
|
||||
assert.True(score("a", "") < 0)
|
||||
assert.True(score("a", "b") < 0)
|
||||
assert.True(score("aa", "a") < 0)
|
||||
assert.True(score("ba", "a") < 0)
|
||||
assert.True(score("ab", "a") < 0)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("scoring", function()
|
||||
it("prefers beginnings of words", function()
|
||||
assert.True(score("amor", "app/models/order") < score("amor", "app/models/zrder"))
|
||||
end)
|
||||
it("prefers consecutive letters", function()
|
||||
assert.True(score("amo", "app/models/foo") < score("amo", "app/m/foo"))
|
||||
assert.True(score("erf", "perfect") < score("erf", "terrific"))
|
||||
end)
|
||||
it("prefers contiguous over letter following period", function()
|
||||
assert.True(score("gemfil", "Gemfile") < score("gemfil", "Gemfile.lock"))
|
||||
end)
|
||||
it("prefers shorter matches", function()
|
||||
assert.True(score("abce", "abcdef") < score("abce", "abc de"));
|
||||
assert.True(score("abc", " a b c ") < score("abc", " a b c "));
|
||||
assert.True(score("abc", " a b c ") < score("abc", " a b c "));
|
||||
end)
|
||||
it("prefers shorter candidates", function()
|
||||
assert.True(score("test", "tests") < score("test", "testing"))
|
||||
end)
|
||||
it("prefers matches at the beginning", function()
|
||||
assert.True(score("ab", "abbb") < score("ab", "babb"))
|
||||
assert.True(score("test", "testing") < score("test", "/testing"))
|
||||
end)
|
||||
it("prefers matches at some locations", function()
|
||||
assert.True(score("a", "/a") < score("a", "ba"))
|
||||
assert.True(score("a", "bA") < score("a", "ba"))
|
||||
assert.True(score("a", ".a") < score("a", "ba"))
|
||||
end)
|
||||
end)
|
||||
|
||||
local function positions(prompt, line)
|
||||
return sorter:highlighter(prompt, line)
|
||||
end
|
||||
|
||||
describe("positioning", function()
|
||||
it("favors consecutive positions", function()
|
||||
assert.same({1, 5, 6}, positions("amo", "app/models/foo"))
|
||||
end)
|
||||
it("favors word beginnings", function()
|
||||
assert.same({1, 5, 12, 13}, positions("amor", "app/models/order"))
|
||||
end)
|
||||
it("works when there are no bonuses", function()
|
||||
assert.same({2, 4}, positions("as", "tags"))
|
||||
assert.same({3, 8}, positions("as", "examples.txt"))
|
||||
end)
|
||||
it("favors smaller groupings of positions", function()
|
||||
assert.same({3, 5, 7}, positions("abc", "a/a/b/c/c"))
|
||||
assert.same({3, 5}, positions("ab", "caacbbc"))
|
||||
end)
|
||||
it("handles exact matches", function()
|
||||
assert.same({1, 2, 3}, positions("foo", "foo"))
|
||||
end)
|
||||
it("ignores empty requests", function()
|
||||
assert.same({}, positions("", ""))
|
||||
assert.same({}, positions("", "foo"))
|
||||
assert.same({}, positions("foo", ""))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('layout_strategies', function()
|
||||
describe('center', function()
|
||||
it('should handle large terminals', function()
|
||||
-- TODO: This could call layout_strategies.center w/ some weird edge case.
|
||||
-- and then assert stuff about the dimensions.
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('Sorters', function()
|
||||
describe('generic_fuzzy_sorter', function()
|
||||
it('sort matches well', function()
|
||||
local sorter = require('telescope.sorters').get_generic_fuzzy_sorter()
|
||||
|
||||
local exact_match = sorter:score('hello', {ordinal = 'hello'})
|
||||
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||
|
||||
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_generic_fuzzy_sorter()
|
||||
|
||||
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)
|
||||
end)
|
||||
|
||||
describe('fuzzy_file', function()
|
||||
it('sort matches well', function()
|
||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||
|
||||
local exact_match = sorter:score('abcdef', {ordinal = 'abcdef'})
|
||||
local no_match = sorter:score('abcdef', {ordinal = 'ghijkl'})
|
||||
local ok_match = sorter:score('abcdef', {ordinal = 'ab'})
|
||||
|
||||
assert(
|
||||
exact_match < no_match,
|
||||
string.format("Exact match better than no match: %s %s", exact_match, no_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 better_match = sorter:score('aaa', {ordinal = 'bbb/aaa'})
|
||||
local worse_match = sorter:score('aaa', {ordinal = 'aaa/bbb'})
|
||||
|
||||
assert(better_match < worse_match, "Final match should be stronger")
|
||||
end)
|
||||
|
||||
pending('sorts multiple finds better', function()
|
||||
local sorter = require('telescope.sorters').get_fuzzy_file()
|
||||
|
||||
local multi_match = sorter:score('generics', {ordinal = 'exercises/generics/generics2.rs'})
|
||||
local one_match = sorter:score('abcdef', {ordinal = 'exercises/generics/README.md'})
|
||||
|
||||
assert(multi_match < one_match)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('fzy', function()
|
||||
local sorter = require'telescope.sorters'.get_fzy_sorter()
|
||||
local function score(prompt, line)
|
||||
return sorter:score(prompt, {ordinal = line})
|
||||
end
|
||||
|
||||
describe("matches", function()
|
||||
it("exact matches", function()
|
||||
assert.True(score("a", "a") >= 0)
|
||||
assert.True(score("a.bb", "a.bb") >= 0)
|
||||
end)
|
||||
it("ignore case", function()
|
||||
assert.True(score("AbB", "abb") >= 0)
|
||||
assert.True(score("abb", "ABB") >= 0)
|
||||
end)
|
||||
it("partial matches", function()
|
||||
assert.True(score("a", "ab") >= 0)
|
||||
assert.True(score("a", "ba") >= 0)
|
||||
assert.True(score("aba", "baabbaab") >= 0)
|
||||
end)
|
||||
it("with delimiters between", function()
|
||||
assert.True(score("abc", "a|b|c") >= 0)
|
||||
end)
|
||||
it("with empty query", function()
|
||||
assert.True(score("", "") >= 0)
|
||||
assert.True(score("", "a") >= 0)
|
||||
end)
|
||||
it("rejects non-matches", function()
|
||||
assert.True(score("a", "") < 0)
|
||||
assert.True(score("a", "b") < 0)
|
||||
assert.True(score("aa", "a") < 0)
|
||||
assert.True(score("ba", "a") < 0)
|
||||
assert.True(score("ab", "a") < 0)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("scoring", function()
|
||||
it("prefers beginnings of words", function()
|
||||
assert.True(score("amor", "app/models/order") < score("amor", "app/models/zrder"))
|
||||
end)
|
||||
it("prefers consecutive letters", function()
|
||||
assert.True(score("amo", "app/models/foo") < score("amo", "app/m/foo"))
|
||||
assert.True(score("erf", "perfect") < score("erf", "terrific"))
|
||||
end)
|
||||
it("prefers contiguous over letter following period", function()
|
||||
assert.True(score("gemfil", "Gemfile") < score("gemfil", "Gemfile.lock"))
|
||||
end)
|
||||
it("prefers shorter matches", function()
|
||||
assert.True(score("abce", "abcdef") < score("abce", "abc de"));
|
||||
assert.True(score("abc", " a b c ") < score("abc", " a b c "));
|
||||
assert.True(score("abc", " a b c ") < score("abc", " a b c "));
|
||||
end)
|
||||
it("prefers shorter candidates", function()
|
||||
assert.True(score("test", "tests") < score("test", "testing"))
|
||||
end)
|
||||
it("prefers matches at the beginning", function()
|
||||
assert.True(score("ab", "abbb") < score("ab", "babb"))
|
||||
assert.True(score("test", "testing") < score("test", "/testing"))
|
||||
end)
|
||||
it("prefers matches at some locations", function()
|
||||
assert.True(score("a", "/a") < score("a", "ba"))
|
||||
assert.True(score("a", "bA") < score("a", "ba"))
|
||||
assert.True(score("a", ".a") < score("a", "ba"))
|
||||
end)
|
||||
end)
|
||||
|
||||
local function positions(prompt, line)
|
||||
return sorter:highlighter(prompt, line)
|
||||
end
|
||||
|
||||
describe("positioning", function()
|
||||
it("favors consecutive positions", function()
|
||||
assert.same({1, 5, 6}, positions("amo", "app/models/foo"))
|
||||
end)
|
||||
it("favors word beginnings", function()
|
||||
assert.same({1, 5, 12, 13}, positions("amor", "app/models/order"))
|
||||
end)
|
||||
it("works when there are no bonuses", function()
|
||||
assert.same({2, 4}, positions("as", "tags"))
|
||||
assert.same({3, 8}, positions("as", "examples.txt"))
|
||||
end)
|
||||
it("favors smaller groupings of positions", function()
|
||||
assert.same({3, 5, 7}, positions("abc", "a/a/b/c/c"))
|
||||
assert.same({3, 5}, positions("ab", "caacbbc"))
|
||||
end)
|
||||
it("handles exact matches", function()
|
||||
assert.same({1, 2, 3}, positions("foo", "foo"))
|
||||
end)
|
||||
it("ignores empty requests", function()
|
||||
assert.same({}, positions("", ""))
|
||||
assert.same({}, positions("", "foo"))
|
||||
assert.same({}, positions("foo", ""))
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('layout_strategies', function()
|
||||
describe('center', function()
|
||||
it('should handle large terminals', function()
|
||||
-- TODO: This could call layout_strategies.center w/ some weird edge case.
|
||||
-- and then assert stuff about the dimensions.
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
|
||||
-- describe('file_finder', function()
|
||||
-- COMPLETED = false
|
||||
-- PASSED = false
|
||||
|
||||
-- require('tests.helpers').auto_find_files {
|
||||
-- input = 'pickers.lua',
|
||||
|
||||
-- condition = function()
|
||||
-- print(vim.api.nvim_buf_get_name(0))
|
||||
-- return string.find(vim.api.nvim_buf_get_name(0), 'pickers.lua')
|
||||
-- end,
|
||||
-- }
|
||||
|
||||
-- print("WAIT:", vim.wait(5000, function() return COMPLETED end))
|
||||
-- assert(PASSED)
|
||||
-- end)
|
||||
end)
|
||||
|
||||
Reference in New Issue
Block a user