aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/_defaults.lua56
-rw-r--r--runtime/lua/vim/lsp/util.lua14
-rw-r--r--runtime/lua/vim/shared.lua53
3 files changed, 84 insertions, 39 deletions
diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua
index 38cfdbdd32..911cc13e74 100644
--- a/runtime/lua/vim/_defaults.lua
+++ b/runtime/lua/vim/_defaults.lua
@@ -213,20 +213,48 @@ end
--- Default menus
do
--- Right click popup menu
- -- TODO VimScript, no l10n
- vim.cmd([[
- vnoremenu PopUp.Cut "+x
- vnoremenu PopUp.Copy "+y
- anoremenu PopUp.Paste "+gP
- vnoremenu PopUp.Paste "+P
- vnoremenu PopUp.Delete "_x
- nnoremenu PopUp.Select\ All ggVG
- vnoremenu PopUp.Select\ All gg0oG$
- inoremenu PopUp.Select\ All <C-Home><C-O>VG
- anoremenu PopUp.Inspect <Cmd>Inspect<CR>
- anoremenu PopUp.-1- <Nop>
- anoremenu PopUp.How-to\ disable\ mouse <Cmd>help disable-mouse<CR>
- ]])
+ local function def_menu(ctx)
+ vim.cmd([[
+ anoremenu PopUp.Go\ to\ definition <Cmd>lua vim.lsp.buf.definition()<CR>
+ amenu PopUp.Open\ in\ web\ browser gx
+ anoremenu PopUp.Inspect <Cmd>Inspect<CR>
+ anoremenu PopUp.-1- <Nop>
+ vnoremenu PopUp.Cut "+x
+ vnoremenu PopUp.Copy "+y
+ anoremenu PopUp.Paste "+gP
+ vnoremenu PopUp.Paste "+P
+ vnoremenu PopUp.Delete "_x
+ nnoremenu PopUp.Select\ All ggVG
+ vnoremenu PopUp.Select\ All gg0oG$
+ inoremenu PopUp.Select\ All <C-Home><C-O>VG
+ anoremenu PopUp.-2- <Nop>
+ anoremenu PopUp.How-to\ disable\ mouse <Cmd>help disable-mouse<CR>
+
+ amenu disable PopUp.Go\ to\ definition
+ amenu disable PopUp.Open\ in\ web\ browser
+ ]])
+
+ if ctx == 'url' then
+ vim.cmd([[amenu enable PopUp.Open\ in\ web\ browser]])
+ elseif ctx == 'lsp' then
+ vim.cmd([[anoremenu enable PopUp.Go\ to\ definition]])
+ end
+ end
+ def_menu()
+
+ local nvim_popupmenu_augroup = vim.api.nvim_create_augroup('nvim_popupmenu', {})
+ vim.api.nvim_create_autocmd('MenuPopup', {
+ pattern = '*',
+ group = nvim_popupmenu_augroup,
+ desc = 'Mouse popup menu',
+ -- nested = true,
+ callback = function()
+ local urls = require('vim.ui')._get_urls()
+ local url = vim.startswith(urls[1], 'http')
+ local ctx = url and 'url' or (vim.lsp.get_clients({ bufnr = 0 })[1] and 'lsp' or nil)
+ def_menu(ctx)
+ end,
+ })
end
--- Default autocommands. See |default-autocmds|
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index cbbe3a66b7..ec66449b54 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -1795,8 +1795,18 @@ function M.locations_to_items(locations, offset_encoding)
local row = pos.line
local end_row = end_pos.line
local line = lines[row] or ''
- local col = M._str_byteindex_enc(line, pos.character, offset_encoding)
- local end_col = M._str_byteindex_enc(lines[end_row] or '', end_pos.character, offset_encoding)
+ local line_len = vim.fn.strcharlen(line)
+ local end_line = lines[end_row] or ''
+ local end_line_len = vim.fn.strcharlen(end_line)
+ -- LSP spec: if character > line length, default to the line length.
+ -- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#position
+ local col = pos.character <= line_len
+ and M._str_byteindex_enc(line, pos.character, offset_encoding)
+ or line_len
+ local end_col = end_pos.character <= end_line_len
+ and M._str_byteindex_enc(end_line, end_pos.character, offset_encoding)
+ or end_line_len
+
table.insert(items, {
filename = filename,
lnum = row + 1,
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index 99530bf72e..2f10380bad 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -354,37 +354,22 @@ function vim.tbl_isempty(t)
return next(t) == nil
end
---- We only merge empty tables or tables that are not an array (indexed by integers)
-local function can_merge(v)
- return type(v) == 'table' and (vim.tbl_isempty(v) or not vim.isarray(v))
-end
-
-local function tbl_extend(behavior, deep_extend, ...)
- if behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force' then
- error('invalid "behavior": ' .. tostring(behavior))
- end
-
- if select('#', ...) < 2 then
- error(
- 'wrong number of arguments (given '
- .. tostring(1 + select('#', ...))
- .. ', expected at least 3)'
- )
- end
-
+--- Recursive worker for tbl_extend
+--- @param behavior 'error'|'keep'|'force'
+--- @param deep_extend boolean
+--- @param ... table<any,any>
+local function tbl_extend_rec(behavior, deep_extend, ...)
local ret = {} --- @type table<any,any>
if vim._empty_dict_mt ~= nil and getmetatable(select(1, ...)) == vim._empty_dict_mt then
ret = vim.empty_dict()
end
for i = 1, select('#', ...) do
- local tbl = select(i, ...)
- vim.validate('after the second argument', tbl, 'table')
- --- @cast tbl table<any,any>
+ local tbl = select(i, ...) --[[@as table<any,any>]]
if tbl then
for k, v in pairs(tbl) do
- if deep_extend and can_merge(v) and can_merge(ret[k]) then
- ret[k] = tbl_extend(behavior, true, ret[k], v)
+ if deep_extend and type(v) == 'table' and type(ret[k]) == 'table' then
+ ret[k] = tbl_extend_rec(behavior, true, ret[k], v)
elseif behavior ~= 'force' and ret[k] ~= nil then
if behavior == 'error' then
error('key found in more than one map: ' .. k)
@@ -395,9 +380,31 @@ local function tbl_extend(behavior, deep_extend, ...)
end
end
end
+
return ret
end
+--- @param behavior 'error'|'keep'|'force'
+--- @param deep_extend boolean
+--- @param ... table<any,any>
+local function tbl_extend(behavior, deep_extend, ...)
+ if behavior ~= 'error' and behavior ~= 'keep' and behavior ~= 'force' then
+ error('invalid "behavior": ' .. tostring(behavior))
+ end
+
+ local nargs = select('#', ...)
+
+ if nargs < 2 then
+ error(('wrong number of arguments (given %d, expected at least 3)'):format(1 + nargs))
+ end
+
+ for i = 1, nargs do
+ vim.validate('after the second argument', select(i, ...), 'table')
+ end
+
+ return tbl_extend_rec(behavior, deep_extend, ...)
+end
+
--- Merges two or more tables.
---
---@see |extend()|