From bbcf6301e674c90f66ee206c818cbf8ca840367c Mon Sep 17 00:00:00 2001 From: Jon Arrien Date: Sat, 12 Oct 2024 11:17:37 +0200 Subject: [PATCH] refactor: use enter to pick completion element This commit makes the plugin behave like any other telescope plugin, where `` picks the selected element. This plugin was prioritizing user's input instead. This implementation was done to avoid some special cases. The issue, it required pressing `` key to move the selected element to prompt before being able to use `` properly. Otherwise, fuzzy finding text (incomplete in most cases) was executed triggering an error. --- README.md | 18 +++++++++++------- lua/cmdline/actions.lua | 17 +++++++++++++---- lua/cmdline/config.lua | 6 +++--- lua/telescope/_extensions/cmdline.lua | 10 +++++++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2a864ff..fa2c5ad 100644 --- a/README.md +++ b/README.md @@ -77,23 +77,29 @@ You can customise cmdline settings in telescope configuration. ```lua require("telescope").setup({ - ... + -- ... extensions = { cmdline = { + -- Adjust telescope picker size and layout picker = { layout_config = { width = 120, height = 25, } }, + -- Adjust your mappings mappings = { complete = '', run_selection = '', run_input = '', }, + -- Triggers any shell command using overseer.nvim (`:!`) + overseer = { + enabled = true, + }, }, } - ... + -- ... }) ``` @@ -101,15 +107,13 @@ require("telescope").setup({ ## Mappings -- `` Run user input if entered, otherwise run first selection +- `` Runs selected command from completion, otherwise runs user input - `` complete current selection into input (useful for :e, :split, :vsplit, :tabnew) -- ``Run selection directly - `` edit current selection in prompt Cmdline can be executed using `:Telescope cmdline`, but it doesn't -include any mapping by default. Normally I use ':' to trigger it, but -it's true there are some caveats or edge cases. - +include any mapping by default. Normally I suggest using ':' to +trigger it, but it's true there are some caveats or edge cases. Please, configure the mapping which suits best for you: ```lua diff --git a/lua/cmdline/actions.lua b/lua/cmdline/actions.lua index d0959d6..1fb62bc 100644 --- a/lua/cmdline/actions.lua +++ b/lua/cmdline/actions.lua @@ -29,14 +29,23 @@ local run = function(cmd) end -- System command - if config.values.overseer.enabled and string.sub(cmd, 1, 1) == '!' then - vim.api.nvim_exec2('OverseerRunCmd ' .. cmd:sub(2), {}) - vim.api.nvim_exec2('OverseerOpen', {}) + if string.sub(cmd, 1, 1) == '!' then + if config.values.overseer.enabled then + vim.api.nvim_exec2('OverseerRunCmd ' .. cmd:sub(2), {}) + vim.api.nvim_exec2('OverseerOpen', {}) + else + vim.cmd.split("term://" .. cmd:sub(2)) + end return end -- Run command and get output - local data = vim.api.nvim_exec2(cmd, { output = true }) + local executed, data = pcall(vim.api.nvim_exec2, cmd, { output = true }) + if not executed then + vim.notify('Error executing command: ' .. cmd, vim.log.levels.ERROR, {}) + return + end + -- local data = vim.api.nvim_exec2(cmd, { output = true }) local output = data.output -- Skip output on silent or custom commands diff --git a/lua/cmdline/config.lua b/lua/cmdline/config.lua index 5f07b77..ab14725 100644 --- a/lua/cmdline/config.lua +++ b/lua/cmdline/config.lua @@ -20,11 +20,11 @@ local defaults = { }, mappings = { complete = '', - run_selection = '', - run_input = '', + run_selection = '', + run_input = '', }, overseer = { - enabled = true, + enabled = false, }, output_pane = { enabled = false, diff --git a/lua/telescope/_extensions/cmdline.lua b/lua/telescope/_extensions/cmdline.lua index 89ae112..1cc10c7 100644 --- a/lua/telescope/_extensions/cmdline.lua +++ b/lua/telescope/_extensions/cmdline.lua @@ -51,10 +51,14 @@ local make_picker = function(opts) finder = make_finder(config), sorter = sorter(opts), attach_mappings = function(_, map) - map("i", config.mappings.complete, action.complete_input) -- - map("i", config.mappings.run_input, action.run_input) -- - map("i", config.mappings.run_selection, action.run_selection) -- + -- Autocomplete using + map("i", config.mappings.complete, action.complete_input) + -- Run selection with + map("i", config.mappings.run_selection, action.run_selection) + -- Run command from input field with (special cases) ?? + map("i", config.mappings.run_input, action.run_input) map("i", "", action.edit) + require("telescope.actions").close:enhance { post = function() cmdline.preview.clean(vim.api.nvim_win_get_buf(0))