From c21c87d23da2f4cf95549cb043f4ab009f521860 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Tue, 28 May 2024 21:58:12 +0200 Subject: [PATCH] chore: Use same way to exit visual mode everywhere and move to utils Signed-off-by: Tomas Slusny --- lua/CopilotChat/init.lua | 5 +---- lua/CopilotChat/integrations/fzflua.lua | 2 ++ lua/CopilotChat/integrations/telescope.lua | 6 ++---- lua/CopilotChat/utils.lua | 8 ++++++++ 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 3d361ea4..2f13c927 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -315,11 +315,8 @@ function M.open(config, source, no_insert) winnr = vim.api.nvim_get_current_win(), }) - -- Exit insert mode if we are in insert mode vim.cmd('stopinsert') - - -- Exit visual mode if we are in visual mode - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'x', false) + utils.exit_visual_mode() -- Recreate the window if the layout has changed if should_reset then diff --git a/lua/CopilotChat/integrations/fzflua.lua b/lua/CopilotChat/integrations/fzflua.lua index a0953aac..ff1f3cfd 100644 --- a/lua/CopilotChat/integrations/fzflua.lua +++ b/lua/CopilotChat/integrations/fzflua.lua @@ -1,5 +1,6 @@ local fzflua = require('fzf-lua') local chat = require('CopilotChat') +local utils = require('CopilotChat.utils') local M = {} @@ -11,6 +12,7 @@ function M.pick(pick_actions, opts) return end + utils.exit_visual_mode() opts = vim.tbl_extend('force', { prompt = pick_actions.prompt .. '> ', preview = fzflua.shell.raw_preview_action_cmd(function(items) diff --git a/lua/CopilotChat/integrations/telescope.lua b/lua/CopilotChat/integrations/telescope.lua index 76dbd4a7..18c905bb 100644 --- a/lua/CopilotChat/integrations/telescope.lua +++ b/lua/CopilotChat/integrations/telescope.lua @@ -6,6 +6,7 @@ local themes = require('telescope.themes') local conf = require('telescope.config').values local previewers = require('telescope.previewers') local chat = require('CopilotChat') +local utils = require('CopilotChat.utils') local M = {} @@ -17,10 +18,7 @@ function M.pick(pick_actions, opts) return end - if vim.fn.mode():lower():find('v') then - vim.cmd('normal! v') - end - + utils.exit_visual_mode() opts = themes.get_dropdown(opts or {}) pickers .new(opts, { diff --git a/lua/CopilotChat/utils.lua b/lua/CopilotChat/utils.lua index 32de559b..8759499a 100644 --- a/lua/CopilotChat/utils.lua +++ b/lua/CopilotChat/utils.lua @@ -100,4 +100,12 @@ function M.table_equals(a, b) return true end +--- Exit visual mode if we are in it +function M.exit_visual_mode() + if vim.fn.mode():lower():find('v') then + -- NOTE: vim.cmd('normal! v') does not work properly when executed from keymap + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes('', true, false, true), 'x', false) + end +end + return M