feat: set defaults for each picker in telescope setup (#883)
This allows easier picker configuration for example:
```lua
require("telescope").setup {
pickers = {
buffers = {
show_all_buffers = true,
sort_lastused = true,
theme = "dropdown",
previewer = false,
mappings = {
i = {
["<c-q>"] = "smart_send_to_qflist",
}
}
}
}
}
```
This configuration will be applied when running `:Telescope buffers`
This commit is contained in:
@@ -19,6 +19,7 @@ globals = {
|
|||||||
"TelescopeCachedTails",
|
"TelescopeCachedTails",
|
||||||
"TelescopeCachedNgrams",
|
"TelescopeCachedNgrams",
|
||||||
"_TelescopeConfigurationValues",
|
"_TelescopeConfigurationValues",
|
||||||
|
"_TelescopeConfigurationPickers",
|
||||||
"__TelescopeKeymapStore",
|
"__TelescopeKeymapStore",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
39
README.md
39
README.md
@@ -235,6 +235,45 @@ EOF
|
|||||||
| `file_ignore_patterns` | Pattern to be ignored `{ "scratch/.*", "%.env" }` | dict |
|
| `file_ignore_patterns` | Pattern to be ignored `{ "scratch/.*", "%.env" }` | dict |
|
||||||
| `shorten_path` | Whether to shorten paths or not. | boolean |
|
| `shorten_path` | Whether to shorten paths or not. | boolean |
|
||||||
|
|
||||||
|
### Customize Default Builtin behavior
|
||||||
|
|
||||||
|
You can customize each default builtin behavior by adding the prefered options
|
||||||
|
into the table that is passed into `require("telescope").setup()`.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
require("telescope").setup {
|
||||||
|
defaults = {
|
||||||
|
-- Your defaults config goes in here
|
||||||
|
},
|
||||||
|
pickers = {
|
||||||
|
-- Your special builtin config goes in here
|
||||||
|
buffers = {
|
||||||
|
sort_lastused = true,
|
||||||
|
theme = "dropdown",
|
||||||
|
previewer = false,
|
||||||
|
mappings = {
|
||||||
|
i = {
|
||||||
|
["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
-- or right hand side can also be a the name of the action as string
|
||||||
|
["<c-d>"] = "delete_buffer",
|
||||||
|
},
|
||||||
|
n = {
|
||||||
|
["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
find_files = {
|
||||||
|
theme = "dropdown"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
extensions = {
|
||||||
|
-- your extension config goes in here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Mappings
|
## Mappings
|
||||||
|
|
||||||
Mappings are fully customizable.
|
Mappings are fully customizable.
|
||||||
|
|||||||
@@ -387,9 +387,37 @@ your desired picker by passing a lua table to the picker with all of the
|
|||||||
options you want to use. Here's an example with the live_grep picker:
|
options you want to use. Here's an example with the live_grep picker:
|
||||||
|
|
||||||
:lua require('telescope.builtin').live_grep({
|
:lua require('telescope.builtin').live_grep({
|
||||||
prompt_title = 'find string in open buffers...',
|
prompt_title = 'find string in open buffers...',
|
||||||
grep_open_files = true
|
grep_open_files = true
|
||||||
})
|
})
|
||||||
|
-- or with dropdown theme
|
||||||
|
:lua require('telescope.builtin').find_files(require('telescope.themes').get_dropdown{
|
||||||
|
previewer = false
|
||||||
|
})
|
||||||
|
|
||||||
|
You can also pass default configurations to builtin pickers. These options will
|
||||||
|
also be added if the picker is executed with `Telescope find_files`.
|
||||||
|
|
||||||
|
require("telescope").setup {
|
||||||
|
pickers = {
|
||||||
|
buffers = {
|
||||||
|
show_all_buffers = true,
|
||||||
|
sort_lastused = true,
|
||||||
|
theme = "dropdown",
|
||||||
|
previewer = false,
|
||||||
|
mappings = {
|
||||||
|
i = {
|
||||||
|
["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
-- or right hand side can also be a the name of the action as string
|
||||||
|
["<c-d>"] = "delete_buffer",
|
||||||
|
},
|
||||||
|
n = {
|
||||||
|
["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
This will use the default configuration options. Other configuration options
|
This will use the default configuration options. Other configuration options
|
||||||
are still in flux at the moment
|
are still in flux at the moment
|
||||||
|
|||||||
@@ -14,9 +14,39 @@
|
|||||||
---
|
---
|
||||||
--- <pre>
|
--- <pre>
|
||||||
--- :lua require('telescope.builtin').live_grep({
|
--- :lua require('telescope.builtin').live_grep({
|
||||||
--- prompt_title = 'find string in open buffers...',
|
--- prompt_title = 'find string in open buffers...',
|
||||||
--- grep_open_files = true
|
--- grep_open_files = true
|
||||||
--- })
|
--- })
|
||||||
|
--- -- or with dropdown theme
|
||||||
|
--- :lua require('telescope.builtin').find_files(require('telescope.themes').get_dropdown{
|
||||||
|
--- previewer = false
|
||||||
|
--- })
|
||||||
|
--- </pre>
|
||||||
|
---
|
||||||
|
--- You can also pass default configurations to builtin pickers. These options will also be added if
|
||||||
|
--- the picker is executed with `Telescope find_files`.
|
||||||
|
---
|
||||||
|
--- <pre>
|
||||||
|
--- require("telescope").setup {
|
||||||
|
--- pickers = {
|
||||||
|
--- buffers = {
|
||||||
|
--- show_all_buffers = true,
|
||||||
|
--- sort_lastused = true,
|
||||||
|
--- theme = "dropdown",
|
||||||
|
--- previewer = false,
|
||||||
|
--- mappings = {
|
||||||
|
--- i = {
|
||||||
|
--- ["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
--- -- or right hand side can also be a the name of the action as string
|
||||||
|
--- ["<c-d>"] = "delete_buffer",
|
||||||
|
--- },
|
||||||
|
--- n = {
|
||||||
|
--- ["<c-d>"] = require("telescope.actions").delete_buffer,
|
||||||
|
--- }
|
||||||
|
--- }
|
||||||
|
--- }
|
||||||
|
--- }
|
||||||
|
--- }
|
||||||
--- </pre>
|
--- </pre>
|
||||||
---
|
---
|
||||||
--- This will use the default configuration options. Other configuration options are still in flux at the moment
|
--- This will use the default configuration options. Other configuration options are still in flux at the moment
|
||||||
@@ -325,4 +355,46 @@ builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics
|
|||||||
---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false)
|
---@field hide_filename boolean: if true, hides the name of the file in the current picker (default is false)
|
||||||
builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics
|
builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics
|
||||||
|
|
||||||
|
local apply_config = function(mod)
|
||||||
|
local pickers_conf = require('telescope.config').pickers
|
||||||
|
for k, v in pairs(mod) do
|
||||||
|
local pconf = vim.deepcopy(pickers_conf[k] or {})
|
||||||
|
if pconf.theme then
|
||||||
|
local theme = pconf.theme
|
||||||
|
pconf.theme = nil
|
||||||
|
pconf = require("telescope.themes")["get_" .. theme](pconf)
|
||||||
|
end
|
||||||
|
if pconf.mappings then
|
||||||
|
local mappings = pconf.mappings
|
||||||
|
pconf.mappings = nil
|
||||||
|
pconf.attach_mappings = function(_, map)
|
||||||
|
for mode, tbl in pairs(mappings) do
|
||||||
|
for key, action in pairs(tbl) do
|
||||||
|
map(mode, key, action)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
mod[k] = function(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
if pconf.attach_mappings and opts.attach_mappings then
|
||||||
|
local attach_mappings = pconf.attach_mappings
|
||||||
|
pconf.attach_mappings = nil
|
||||||
|
local opts_attach = opts.attach_mappings
|
||||||
|
opts.attach_mappings = function(prompt_bufnr, map)
|
||||||
|
attach_mappings(prompt_bufnr, map)
|
||||||
|
return opts_attach(prompt_bufnr, map)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
v(vim.tbl_extend("force", pconf, opts))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return mod
|
||||||
|
end
|
||||||
|
|
||||||
|
-- We can't do this in one statement because tree-sitter-lua docgen gets confused if we do
|
||||||
|
builtin = apply_config(builtin)
|
||||||
return builtin
|
return builtin
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
-- Keep the values around between reloads
|
-- Keep the values around between reloads
|
||||||
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}
|
_TelescopeConfigurationValues = _TelescopeConfigurationValues or {}
|
||||||
|
_TelescopeConfigurationPickers = _TelescopeConfigurationPickers or {}
|
||||||
|
|
||||||
local function first_non_null(...)
|
local function first_non_null(...)
|
||||||
local n = select('#', ...)
|
local n = select('#', ...)
|
||||||
@@ -45,6 +46,15 @@ local config = {}
|
|||||||
|
|
||||||
config.values = _TelescopeConfigurationValues
|
config.values = _TelescopeConfigurationValues
|
||||||
config.descriptions = {}
|
config.descriptions = {}
|
||||||
|
config.pickers = _TelescopeConfigurationPickers
|
||||||
|
|
||||||
|
function config.set_pickers(pickers)
|
||||||
|
pickers = pickers or {}
|
||||||
|
|
||||||
|
for k, v in pairs(pickers) do
|
||||||
|
config.pickers[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function config.set_defaults(defaults)
|
function config.set_defaults(defaults)
|
||||||
defaults = defaults or {}
|
defaults = defaults or {}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ function telescope.setup(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
require('telescope.config').set_defaults(opts.defaults)
|
require('telescope.config').set_defaults(opts.defaults)
|
||||||
|
require('telescope.config').set_pickers(opts.pickers)
|
||||||
_extensions.set_config(opts.extensions)
|
_extensions.set_config(opts.extensions)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -111,51 +111,60 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts)
|
|||||||
if opts.silent == nil then opts.silent = true end
|
if opts.silent == nil then opts.silent = true end
|
||||||
|
|
||||||
if type(key_func) == "string" then
|
if type(key_func) == "string" then
|
||||||
a.nvim_buf_set_keymap(
|
key_func = actions[key_func]
|
||||||
|
elseif type(key_func) == "table" then
|
||||||
|
if key_func.type == "command" then
|
||||||
|
a.nvim_buf_set_keymap(
|
||||||
|
prompt_bufnr,
|
||||||
|
mode,
|
||||||
|
key_bind,
|
||||||
|
key_func[1],
|
||||||
|
opts or {
|
||||||
|
silent = true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return
|
||||||
|
elseif key_func.type == "action_key" then
|
||||||
|
key_func = actions[key_func[1]]
|
||||||
|
elseif key_func.type == "action" then
|
||||||
|
key_func = key_func[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local key_id = assign_function(prompt_bufnr, key_func)
|
||||||
|
local prefix
|
||||||
|
|
||||||
|
local map_string
|
||||||
|
if opts.expr then
|
||||||
|
map_string = string.format(
|
||||||
|
[[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]],
|
||||||
prompt_bufnr,
|
prompt_bufnr,
|
||||||
mode,
|
key_id
|
||||||
key_bind,
|
|
||||||
key_func,
|
|
||||||
opts or {
|
|
||||||
silent = true
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
local key_id = assign_function(prompt_bufnr, key_func)
|
if mode == "i" and not opts.expr then
|
||||||
local prefix
|
prefix = "<cmd>"
|
||||||
|
elseif mode == "n" then
|
||||||
local map_string
|
prefix = ":<C-U>"
|
||||||
if opts.expr then
|
|
||||||
map_string = string.format(
|
|
||||||
[[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]],
|
|
||||||
prompt_bufnr,
|
|
||||||
key_id
|
|
||||||
)
|
|
||||||
else
|
else
|
||||||
if mode == "i" and not opts.expr then
|
prefix = ":"
|
||||||
prefix = "<cmd>"
|
|
||||||
elseif mode == "n" then
|
|
||||||
prefix = ":<C-U>"
|
|
||||||
else
|
|
||||||
prefix = ":"
|
|
||||||
end
|
|
||||||
|
|
||||||
map_string = string.format(
|
|
||||||
"%slua require('telescope.mappings').execute_keymap(%s, %s)<CR>",
|
|
||||||
prefix,
|
|
||||||
prompt_bufnr,
|
|
||||||
key_id
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
a.nvim_buf_set_keymap(
|
map_string = string.format(
|
||||||
|
"%slua require('telescope.mappings').execute_keymap(%s, %s)<CR>",
|
||||||
|
prefix,
|
||||||
prompt_bufnr,
|
prompt_bufnr,
|
||||||
mode,
|
key_id
|
||||||
key_bind,
|
|
||||||
map_string,
|
|
||||||
opts
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
a.nvim_buf_set_keymap(
|
||||||
|
prompt_bufnr,
|
||||||
|
mode,
|
||||||
|
key_bind,
|
||||||
|
map_string,
|
||||||
|
opts
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap)
|
mappings.apply_keymap = function(prompt_bufnr, attach_mappings, buffer_keymap)
|
||||||
|
|||||||
Reference in New Issue
Block a user