refactor(defaults): use vim.region for visual star (*,#)

Problem:
The parent commit added a new vim.get_visual_selection() function to
improve visual star. But that is redundant with vim.region(). Any
current limitations of vim.region() should be fixed instead of adding
a new function.

Solution:
Delete vim.get_visual_selection().
Use vim.region() to get the visual selection.
TODO: fails with visual "block" selections.
This commit is contained in:
Justin M. Keyes
2023-07-05 19:47:43 +02:00
parent abd380e28d
commit f39ca5df23
3 changed files with 42 additions and 103 deletions

View File

@@ -1408,26 +1408,7 @@ deprecate({name}, {alternative}, {version}, {plugin}, {backtrace})
• {backtrace} boolean|nil Prints backtrace. Defaults to true. • {backtrace} boolean|nil Prints backtrace. Defaults to true.
Return: ~ Return: ~
Deprecated message, or nil if no message was shown. (string|nil) # Deprecated message, or nil if no message was shown.
*vim.get_visual_selection()*
get_visual_selection({list}, {append_empty})
Gets the content of the visual selection.
The result is either a string or, if {list} is `true`, a list of strings.
If not in any |visual-mode|, `nil` is returned.
Parameters: ~
• {list} boolean|nil Return a list of strings instead of a
string. See |getreg()|. Defaults to `false`.
• {append_empty} boolean|nil Append an empty string to the result when
in |linewise-visual| mode and {list} is `true`. This
will preserve the trailing newline of the selection
when the result is concatenated with `"\n"`. Defaults
to `false`.
Return: ~
string|table
inspect({object}, {options}) *vim.inspect()* inspect({object}, {options}) *vim.inspect()*
Gets a human-readable representation of the given object. Gets a human-readable representation of the given object.

View File

@@ -114,15 +114,14 @@ DEFAULT MAPPINGS
*default-mappings* *default-mappings*
Nvim creates the following default mappings at |startup|. You can disable any Nvim creates the following default mappings at |startup|. You can disable any
of these in your config by simply removing the mapping, e.g. ":unmap Y". of these in your config by simply removing the mapping, e.g. ":unmap Y".
>vim
nnoremap Y y$ - |Y-default|
nnoremap <C-L> <Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR> - |i_CTRL-U-default|
inoremap <C-U> <C-G>u<C-U> - |i_CTRL-W-default|
inoremap <C-W> <C-G>u<C-W> - |CTRL-L-default|
nnoremap & :&&<CR> - |&-default|
< - |v_#-default|
Default mappings composed of Lua code are not listed above. Use ":map" to see - |v_star-default|
the |map-listing|.
DEFAULT AUTOCOMMANDS DEFAULT AUTOCOMMANDS
*default-autocmds* *default-autocmds*

View File

@@ -535,55 +535,6 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
return region return region
end end
--- Gets the content of the visual selection.
---
--- The result is either a string or, if {list} is `true`, a list of strings.
--- If not in any |visual-mode|, `nil` is returned.
---
--- @param list boolean|nil
--- Return a list of strings instead of a string. See |getreg()|.
--- Defaults to `false`.
--- @param append_empty boolean|nil
--- Append an empty string to the result when in |linewise-visual| mode and {list} is `true`.
--- This will preserve the trailing newline of the selection when the result is concatenated with `"\n"`.
--- Defaults to `false`.
--- @return string|table
function vim.get_visual_selection(list, append_empty)
list = list or false
append_empty = append_empty or false
local mode = vim.api.nvim_get_mode().mode
if mode ~= 'v' and mode ~= 'V' and mode:byte() ~= 22 then
return nil
end
local reg_name_unnamed = '"'
local reg_name_yank = '0'
local reg_info_unnamed = vim.fn.getreginfo(reg_name_unnamed)
local reg_info_yank = vim.fn.getreginfo(reg_name_yank)
local opt_clipboard = vim.o.clipboard
local opt_report = vim.o.report
vim.o.clipboard = ''
vim.o.report = vim.v.maxcol
vim.api.nvim_feedkeys('y', 'nx', false)
local yanked = vim.fn.getreg(reg_name_yank, 1, list)
vim.fn.setreg(reg_name_unnamed, reg_info_unnamed)
vim.fn.setreg(reg_name_yank, reg_info_yank)
vim.o.clipboard = opt_clipboard
vim.o.report = opt_report
if list and append_empty and mode == 'V' then
table.insert(yanked, '')
end
return yanked
end
--- Defers calling {fn} until {timeout} ms passes. --- Defers calling {fn} until {timeout} ms passes.
--- ---
--- Use to do a one-shot timer that calls {fn} --- Use to do a one-shot timer that calls {fn}
@@ -1034,7 +985,7 @@ end
--- Defaults to "Nvim". --- Defaults to "Nvim".
---@param backtrace boolean|nil Prints backtrace. Defaults to true. ---@param backtrace boolean|nil Prints backtrace. Defaults to true.
--- ---
---@return Deprecated message, or nil if no message was shown. ---@return string|nil # Deprecated message, or nil if no message was shown.
function vim.deprecate(name, alternative, version, plugin, backtrace) function vim.deprecate(name, alternative, version, plugin, backtrace)
local msg = ('%s is deprecated'):format(name) local msg = ('%s is deprecated'):format(name)
plugin = plugin or 'Nvim' plugin = plugin or 'Nvim'
@@ -1049,9 +1000,7 @@ function vim.deprecate(name, alternative, version, plugin, backtrace)
if displayed and backtrace ~= false then if displayed and backtrace ~= false then
vim.notify(debug.traceback('', 2):sub(2), vim.log.levels.WARN) vim.notify(debug.traceback('', 2):sub(2), vim.log.levels.WARN)
end end
if displayed then return displayed and msg or nil
return msg
end
end end
--- Create builtin mappings (incl. menus). --- Create builtin mappings (incl. menus).
@@ -1060,27 +1009,37 @@ function vim._init_default_mappings()
-- mappings -- mappings
---@private ---@private
local function _search_for_visual_selection(search_prefix) local function region_chunks(region)
if search_prefix ~= '/' and search_prefix ~= '?' then local chunks = {}
return local maxcol = vim.v.maxcol
for line, cols in vim.spairs(region) do
local endcol = cols[2] == maxcol and -1 or cols[2]
local chunk = vim.api.nvim_buf_get_text(0, line, cols[1], line, endcol, {})[1]
table.insert(chunks, chunk)
end end
-- Escape these characters return chunks
local replacements = { end
[search_prefix] = [[\]] .. search_prefix,
[ [[\]] ] = [[\\]], ---@private
['\t'] = [[\t]], local function _visual_search(cmd)
['\n'] = [[\n]], assert(cmd == '/' or cmd == '?')
} vim.api.nvim_feedkeys('\27', 'nx', true) -- Escape visual mode.
local pattern = '[' .. table.concat(vim.tbl_keys(replacements), '') .. ']' local region = vim.region(0, "'<", "'>", vim.fn.visualmode(), vim.o.selection == 'inclusive')
local visual_selection = vim.get_visual_selection(false) local chunks = region_chunks(region)
local escaped_visual_selection = string.gsub(visual_selection, pattern, replacements) local esc_chunks = vim
local search_cmd = search_prefix .. [[\V]] .. escaped_visual_selection .. '\n' .iter(chunks)
:map(function(v)
return vim.fn.escape(v, [[/\]])
end)
:totable()
local esc_pat = table.concat(esc_chunks, [[\n]])
local search_cmd = ([[%s\V%s%s]]):format(cmd, esc_pat, '\n')
vim.api.nvim_feedkeys(search_cmd, 'nx', true) vim.api.nvim_feedkeys(search_cmd, 'nx', true)
end end
---@private ---@private
local function map(mode, lhs, rhs) local function map(mode, lhs, rhs)
vim.keymap.set(mode, lhs, rhs, { noremap = true, desc = 'Nvim builtin' }) vim.keymap.set(mode, lhs, rhs, { desc = 'Nvim builtin' })
end end
map('n', 'Y', 'y$') map('n', 'Y', 'y$')
@@ -1088,12 +1047,12 @@ function vim._init_default_mappings()
map('n', '<C-L>', '<Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR>') map('n', '<C-L>', '<Cmd>nohlsearch<Bar>diffupdate<Bar>normal! <C-L><CR>')
map('i', '<C-U>', '<C-G>u<C-U>') map('i', '<C-U>', '<C-G>u<C-U>')
map('i', '<C-W>', '<C-G>u<C-W>') map('i', '<C-W>', '<C-G>u<C-W>')
map('x', '*', function() vim.keymap.set('x', '*', function()
_search_for_visual_selection('/') _visual_search('/')
end) end, { desc = 'Nvim builtin', silent = true })
map('x', '#', function() vim.keymap.set('x', '#', function()
_search_for_visual_selection('?') _visual_search('?')
end) end, { desc = 'Nvim builtin', silent = true })
-- Use : instead of <Cmd> so that ranges are supported. #19365 -- Use : instead of <Cmd> so that ranges are supported. #19365
map('n', '&', ':&&<CR>') map('n', '&', ':&&<CR>')