Skip to content

Commit

Permalink
centralize leaving visual mode and use more correct way to leave visu…
Browse files Browse the repository at this point in the history
…al mode
  • Loading branch information
mehalter committed Sep 20, 2024
1 parent 64ec52c commit 39da4f0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
21 changes: 4 additions & 17 deletions lua/grug-far.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,8 @@ function M.open(options)
ensure_configured()
local resolvedOpts = opts.with_defaults(options or {}, globalOptions)
local is_visual = false
if not resolvedOpts.ignoreVisualSelection and vim.fn.mode():lower():find('v') ~= nil then
is_visual = true
end
if is_visual then
-- needed to make visual selection work
vim.cmd([[normal! vv]])
if not resolvedOpts.ignoreVisualSelection then
is_visual = utils.leaveVisualMode()
end

return M._open_internal(resolvedOpts, { is_visual = is_visual })
Expand Down Expand Up @@ -422,11 +418,7 @@ end
function M.with_visual_selection(options)
ensure_configured()

local isVisualMode = vim.fn.mode():lower():find('v') ~= nil
if isVisualMode then
-- needed to make visual selection work
vim.cmd([[normal! vv]])
end
utils.leaveVisualMode()

local resolvedOpts = opts.with_defaults(options or {}, globalOptions)
return M._open_internal(resolvedOpts, { is_visual = true })
Expand All @@ -436,12 +428,7 @@ end
--- This is provided as a utility for users so they don't have to rewrite
---@return string
function M.get_current_visual_selection()
local isVisualMode = vim.fn.mode():lower():find('v') ~= nil
if isVisualMode then
-- needed to make visual selection work
vim.cmd([[normal! vv]])
end

utils.leaveVisualMode()
local selection_lines = utils.getVisualSelectionLines()
return vim.fn.join(selection_lines, '\n')
end
Expand Down
11 changes: 11 additions & 0 deletions lua/grug-far/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ function M.ensureBufTopEmptyLines(buf, count)
end
end

--- leave visual mode if in visual mode
---@return boolean if left visual mode
function M.leaveVisualMode()
local isVisualMode = vim.fn.mode():lower():find('v') ~= nil
if isVisualMode then
-- needed to make visual selection work
vim.fn.feedkeys(':', 'nx')
end
return isVisualMode
end

--- get text lines in visual selection
---@return string[]
function M.getVisualSelectionLines()
Expand Down

0 comments on commit 39da4f0

Please sign in to comment.