aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/lsp/util.lua')
-rw-r--r--runtime/lua/vim/lsp/util.lua274
1 files changed, 188 insertions, 86 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 32b220746f..f8e5b6a90d 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -3,7 +3,7 @@ local snippet = require('vim.lsp._snippet_grammar')
local validate = vim.validate
local api = vim.api
local list_extend = vim.list_extend
-local highlight = require('vim.highlight')
+local highlight = vim.highlight
local uv = vim.uv
local npcall = vim.F.npcall
@@ -180,6 +180,7 @@ local _str_byteindex_enc = M._str_byteindex_enc
---@param new_lines (table) list of strings to replace the original
---@return table The modified {lines} object
function M.set_lines(lines, A, B, new_lines)
+ vim.deprecate('vim.lsp.util.set_lines()', 'nil', '0.12')
-- 0-indexing to 1-indexing
local i_0 = A[1] + 1
-- If it extends past the end, truncate it to the end. This is because the
@@ -346,7 +347,7 @@ end
---@private
---@deprecated Use vim.lsp.status() or access client.progress directly
function M.get_progress_messages()
- vim.deprecate('vim.lsp.util.get_progress_messages', 'vim.lsp.status', '0.11.0')
+ vim.deprecate('vim.lsp.util.get_progress_messages()', 'vim.lsp.status()', '0.11')
local new_messages = {}
local progress_remove = {}
@@ -418,6 +419,9 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
if not next(text_edits) then
return
end
+
+ assert(bufnr ~= 0, 'Explicit buffer number is required')
+
if not api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
@@ -456,7 +460,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
-- save and restore local marks since they get deleted by nvim_buf_set_lines
local marks = {}
- for _, m in pairs(vim.fn.getmarklist(bufnr or vim.api.nvim_get_current_buf())) do
+ for _, m in pairs(vim.fn.getmarklist(bufnr)) do
if m.mark:match("^'[a-z]$") then
marks[m.mark:sub(2, 2)] = { m.pos[2], m.pos[3] - 1 } -- api-indexed
end
@@ -515,7 +519,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
local max = api.nvim_buf_line_count(bufnr)
-- no need to restore marks that still exist
- for _, m in pairs(vim.fn.getmarklist(bufnr or vim.api.nvim_get_current_buf())) do
+ for _, m in pairs(vim.fn.getmarklist(bufnr)) do
marks[m.mark:sub(2, 2)] = nil
end
-- restore marks
@@ -547,12 +551,16 @@ end
--- Can be used to extract the completion items from a
--- `textDocument/completion` request, which may return one of
--- `CompletionItem[]`, `CompletionList` or null.
+---
+--- Note that this method doesn't apply `itemDefaults` to `CompletionList`s, and hence the returned
+--- results might be incorrect.
+---
---@deprecated
---@param result table The result of a `textDocument/completion` request
---@return lsp.CompletionItem[] List of completion items
---@see https://microsoft.github.io/language-server-protocol/specification#textDocument_completion
function M.extract_completion_items(result)
- vim.deprecate('vim.lsp.util.extract_completion_items', nil, '0.11')
+ vim.deprecate('vim.lsp.util.extract_completion_items()', nil, '0.11')
if type(result) == 'table' and result.items then
-- result is a `CompletionList`
return result.items
@@ -570,6 +578,7 @@ end
---
---@param text_document_edit table: a `TextDocumentEdit` object
---@param index integer: Optional index of the edit, if from a list of edits (or nil, if not from a list)
+---@param offset_encoding? string
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentEdit
function M.apply_text_document_edit(text_document_edit, index, offset_encoding)
local text_document = text_document_edit.textDocument
@@ -612,7 +621,7 @@ end
---@param input string unparsed snippet
---@return string parsed snippet
function M.parse_snippet(input)
- vim.deprecate('vim.lsp.util.parse_snippet', nil, '0.11')
+ vim.deprecate('vim.lsp.util.parse_snippet()', nil, '0.11')
local ok, parsed = pcall(function()
return snippet.parse(input)
end)
@@ -634,76 +643,129 @@ end
---@return table[] items
---@see complete-items
function M.text_document_completion_list_to_complete_items(result, prefix)
- vim.deprecate('vim.lsp.util.text_document_completion_list_to_complete_items', nil, '0.11')
- return require('vim.lsp._completion')._lsp_to_complete_items(result, prefix)
+ vim.deprecate('vim.lsp.util.text_document_completion_list_to_complete_items()', nil, '0.11')
+ return vim.lsp._completion._lsp_to_complete_items(result, prefix)
end
---- Like vim.fn.bufwinid except it works across tabpages.
-local function bufwinid(bufnr)
- for _, win in ipairs(api.nvim_list_wins()) do
- if api.nvim_win_get_buf(win) == bufnr then
- return win
+local function path_components(path)
+ return vim.split(path, '/', { plain = true })
+end
+
+local function path_under_prefix(path, prefix)
+ for i, c in ipairs(prefix) do
+ if c ~= path[i] then
+ return false
end
end
+ return true
end
---- Get list of buffers for a directory
-local function get_dir_bufs(path)
- path = path:gsub('([^%w])', '%%%1')
+--- Get list of buffers whose filename matches the given path prefix (normalized full path)
+---@return integer[]
+local function get_bufs_with_prefix(prefix)
+ prefix = path_components(prefix)
local buffers = {}
for _, v in ipairs(vim.api.nvim_list_bufs()) do
- local bufname = vim.api.nvim_buf_get_name(v):gsub('buffer://', '')
- if bufname:find(path) then
+ local bname = vim.api.nvim_buf_get_name(v)
+ local path = path_components(vim.fs.normalize(bname, { expand_env = false }))
+ if path_under_prefix(path, prefix) then
table.insert(buffers, v)
end
end
return buffers
end
+local function escape_gsub_repl(s)
+ return (s:gsub('%%', '%%%%'))
+end
+
+--- @class vim.lsp.util.rename.Opts
+--- @inlinedoc
+--- @field overwrite? boolean
+--- @field ignoreIfExists? boolean
+
--- Rename old_fname to new_fname
---
----@param opts (table)
--- overwrite? bool
--- ignoreIfExists? bool
+--- Existing buffers are renamed as well, while maintaining their bufnr.
+---
+--- It deletes existing buffers that conflict with the renamed file name only when
+--- * `opts` requests overwriting; or
+--- * the conflicting buffers are not loaded, so that deleting thme does not result in data loss.
+---
+--- @param old_fname string
+--- @param new_fname string
+--- @param opts? vim.lsp.util.rename.Opts Options:
function M.rename(old_fname, new_fname, opts)
opts = opts or {}
- local target_exists = uv.fs_stat(new_fname) ~= nil
- if target_exists and not opts.overwrite or opts.ignoreIfExists then
- vim.notify('Rename target already exists. Skipping rename.')
+ local skip = not opts.overwrite or opts.ignoreIfExists
+
+ local old_fname_full = vim.uv.fs_realpath(vim.fs.normalize(old_fname, { expand_env = false }))
+ if not old_fname_full then
+ vim.notify('Invalid path: ' .. old_fname, vim.log.levels.ERROR)
return
end
- local oldbufs = {}
- local win = nil
-
- if vim.fn.isdirectory(old_fname) == 1 then
- oldbufs = get_dir_bufs(old_fname)
- else
- local oldbuf = vim.fn.bufadd(old_fname)
- table.insert(oldbufs, oldbuf)
- win = bufwinid(oldbuf)
+ local target_exists = uv.fs_stat(new_fname) ~= nil
+ if target_exists and skip then
+ vim.notify(new_fname .. ' already exists. Skipping rename.', vim.log.levels.ERROR)
+ return
end
- for _, b in ipairs(oldbufs) do
- vim.fn.bufload(b)
- -- The there may be pending changes in the buffer
- api.nvim_buf_call(b, function()
- vim.cmd('w!')
+ local buf_rename = {} ---@type table<integer, {from: string, to: string}>
+ local old_fname_pat = '^' .. vim.pesc(old_fname_full)
+ for b in
+ vim.iter(get_bufs_with_prefix(old_fname_full)):filter(function(b)
+ -- No need to care about unloaded or nofile buffers. Also :saveas won't work for them.
+ return api.nvim_buf_is_loaded(b)
+ and not vim.list_contains({ 'nofile', 'nowrite' }, vim.bo[b].buftype)
end)
+ do
+ -- Renaming a buffer may conflict with another buffer that happens to have the same name. In
+ -- most cases, this would have been already detected by the file conflict check above, but the
+ -- conflicting buffer may not be associated with a file. For example, 'buftype' can be "nofile"
+ -- or "nowrite", or the buffer can be a normal buffer but has not been written to the file yet.
+ -- Renaming should fail in such cases to avoid losing the contents of the conflicting buffer.
+ local old_bname = vim.api.nvim_buf_get_name(b)
+ local new_bname = old_bname:gsub(old_fname_pat, escape_gsub_repl(new_fname))
+ if vim.fn.bufexists(new_bname) == 1 then
+ local existing_buf = vim.fn.bufnr(new_bname)
+ if api.nvim_buf_is_loaded(existing_buf) and skip then
+ vim.notify(
+ new_bname .. ' already exists in the buffer list. Skipping rename.',
+ vim.log.levels.ERROR
+ )
+ return
+ end
+ -- no need to preserve if such a buffer is empty
+ api.nvim_buf_delete(existing_buf, {})
+ end
+
+ buf_rename[b] = { from = old_bname, to = new_bname }
end
- local ok, err = os.rename(old_fname, new_fname)
+ local newdir = assert(vim.fs.dirname(new_fname))
+ vim.fn.mkdir(newdir, 'p')
+
+ local ok, err = os.rename(old_fname_full, new_fname)
assert(ok, err)
- if vim.fn.isdirectory(new_fname) == 0 then
- local newbuf = vim.fn.bufadd(new_fname)
- if win then
- api.nvim_win_set_buf(win, newbuf)
- end
+ local old_undofile = vim.fn.undofile(old_fname_full)
+ if uv.fs_stat(old_undofile) ~= nil then
+ local new_undofile = vim.fn.undofile(new_fname)
+ vim.fn.mkdir(assert(vim.fs.dirname(new_undofile)), 'p')
+ os.rename(old_undofile, new_undofile)
end
- for _, b in ipairs(oldbufs) do
- api.nvim_buf_delete(b, {})
+ for b, rename in pairs(buf_rename) do
+ -- Rename with :saveas. This does two things:
+ -- * Unset BF_WRITE_MASK, so that users don't get E13 when they do :write.
+ -- * Send didClose and didOpen via textDocument/didSave handler.
+ api.nvim_buf_call(b, function()
+ vim.cmd('keepalt saveas! ' .. vim.fn.fnameescape(rename.to))
+ end)
+ -- Delete the new buffer with the old name created by :saveas. nvim_buf_delete and
+ -- :bwipeout are futile because the buffer will be added again somewhere else.
+ vim.cmd('bdelete! ' .. vim.fn.bufnr(rename.from))
end
end
@@ -745,7 +807,7 @@ end
---
---@param workspace_edit table `WorkspaceEdit`
---@param offset_encoding string utf-8|utf-16|utf-32 (required)
---see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit
+---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_applyEdit
function M.apply_workspace_edit(workspace_edit, offset_encoding)
if offset_encoding == nil then
vim.notify_once(
@@ -789,7 +851,7 @@ end
--- Note that if the input is of type `MarkupContent` and its kind is `plaintext`,
--- then the corresponding value is returned without further modifications.
---
----@param input (`MarkedString` | `MarkedString[]` | `MarkupContent`)
+---@param input (lsp.MarkedString | lsp.MarkedString[] | lsp.MarkupContent)
---@param contents (table|nil) List of strings to extend with converted lines. Defaults to {}.
---@return string[] extended with lines of converted markdown.
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover
@@ -1055,7 +1117,7 @@ function M.show_document(location, offset_encoding, opts)
vim.fn.settagstack(vim.fn.win_getid(), { items = items }, 't')
end
- local win = opts.reuse_win and bufwinid(bufnr)
+ local win = opts.reuse_win and vim.fn.win_findbuf(bufnr)[1]
or focus and api.nvim_get_current_win()
or create_window_without_focus()
@@ -1068,7 +1130,7 @@ function M.show_document(location, offset_encoding, opts)
-- location may be Location or LocationLink
local range = location.range or location.targetSelectionRange
if range then
- --- Jump to new location (adjusting for encoding of characters)
+ -- Jump to new location (adjusting for encoding of characters)
local row = range.start.line
local col = get_line_byte_from_position(bufnr, range.start, offset_encoding)
api.nvim_win_set_cursor(win, { row + 1, col })
@@ -1105,6 +1167,7 @@ end
--- - for LocationLink, targetRange is shown (e.g., body of function definition)
---
---@param location table a single `Location` or `LocationLink`
+---@param opts table
---@return integer|nil buffer id of float window
---@return integer|nil window id of float window
function M.preview_location(location, opts)
@@ -1218,6 +1281,7 @@ end
---
--- If you want to open a popup with fancy markdown, use `open_floating_preview` instead
---
+---@param bufnr integer
---@param contents table of lines to show in window
---@param opts table with optional fields
--- - height of floating window
@@ -1410,7 +1474,7 @@ function M.stylize_markdown(bufnr, contents, opts)
return stripped
end
---- @class lsp.util.NormalizeMarkdownOptions
+--- @class (private) vim.lsp.util._normalize_markdown.Opts
--- @field width integer Thematic breaks are expanded to this size. Defaults to 80.
--- Normalizes Markdown input to a canonical form.
@@ -1426,7 +1490,7 @@ end
---
---@private
---@param contents string[]
----@param opts? lsp.util.NormalizeMarkdownOptions
+---@param opts? vim.lsp.util._normalize_markdown.Opts
---@return string[] table of lines containing normalized Markdown
---@see https://github.github.com/gfm
function M._normalize_markdown(contents, opts)
@@ -1497,7 +1561,7 @@ local function close_preview_autocmd(events, winnr, bufnrs)
end
end
----@internal
+---@private
--- Computes size of float needed to show contents (with optional wrapping)
---
---@param contents table of lines to show in window
@@ -1573,24 +1637,50 @@ function M._make_floating_popup_size(contents, opts)
return width, height
end
+--- @class vim.lsp.util.open_floating_preview.Opts
+--- @inlinedoc
+---
+--- Height of floating window
+--- @field height? integer
+---
+--- Width of floating window
+--- @field width? integer
+---
+--- Wrap long lines
+--- (default: `true`)
+--- @field wrap? boolean
+---
+--- Character to wrap at for computing height when wrap is enabled
+--- @field wrap_at? integer
+---
+--- Maximal width of floating window
+--- @field max_width? integer
+---
+--- Maximal height of floating window
+--- @field max_height? integer
+---
+--- If a popup with this id is opened, then focus it
+--- @field focus_id? string
+---
+--- List of events that closes the floating window
+--- @field close_events? table
+---
+--- Make float focusable.
+--- (default: `true`)
+--- @field focusable? boolean
+---
+--- If `true`, and if {focusable} is also `true`, focus an existing floating
+--- window with the same {focus_id}
+--- (default: `true`)
+--- @field focus? boolean
+
--- Shows contents in a floating window.
---
---@param contents table of lines to show in window
---@param syntax string of syntax to set for opened buffer
----@param opts table with optional fields (additional keys are filtered with |vim.lsp.util.make_floating_popup_options()|
---- before they are passed on to |nvim_open_win()|)
---- - height: (integer) height of floating window
---- - width: (integer) width of floating window
---- - wrap: (boolean, default true) wrap long lines
---- - wrap_at: (integer) character to wrap at for computing height when wrap is enabled
---- - max_width: (integer) maximal width of floating window
---- - max_height: (integer) maximal height of floating window
---- - focus_id: (string) if a popup with this id is opened, then focus it
---- - close_events: (table) list of events that closes the floating window
---- - focusable: (boolean, default true) Make float focusable
---- - focus: (boolean, default true) If `true`, and if {focusable}
---- is also `true`, focus an existing floating window with the same
---- {focus_id}
+---@param opts? vim.lsp.util.open_floating_preview.Opts with optional fields
+--- (additional keys are filtered with |vim.lsp.util.make_floating_popup_options()|
+--- before they are passed on to |nvim_open_win()|)
---@return integer bufnr of newly created float window
---@return integer winid of newly created float window preview window
function M.open_floating_preview(contents, syntax, opts)
@@ -1754,6 +1844,14 @@ local position_sort = sort_by_key(function(v)
return { v.start.line, v.start.character }
end)
+---@class vim.lsp.util.locations_to_items.ret
+---@inlinedoc
+---@field filename string
+---@field lnum integer 1-indexed line number
+---@field col integer 1-indexed column
+---@field text string
+---@field user_data lsp.Location|lsp.LocationLink
+
--- Returns the items with the byte position calculated correctly and in sorted
--- order, for display in quickfix and location lists.
---
@@ -1763,10 +1861,10 @@ end)
--- The result can be passed to the {list} argument of |setqflist()| or
--- |setloclist()|.
---
----@param locations table list of `Location`s or `LocationLink`s
+---@param locations lsp.Location[]|lsp.LocationLink[]
---@param offset_encoding string offset_encoding for locations utf-8|utf-16|utf-32
--- default to first client of buffer
----@return table list of items
+---@return vim.lsp.util.locations_to_items.ret[]
function M.locations_to_items(locations, offset_encoding)
if offset_encoding == nil then
vim.notify_once(
@@ -1777,6 +1875,7 @@ function M.locations_to_items(locations, offset_encoding)
end
local items = {}
+ ---@type table<string, {start: lsp.Position, location: lsp.Location|lsp.LocationLink}[]>
local grouped = setmetatable({}, {
__index = function(t, k)
local v = {}
@@ -1791,6 +1890,7 @@ function M.locations_to_items(locations, offset_encoding)
table.insert(grouped[uri], { start = range.start, location = d })
end
+ ---@type string[]
local keys = vim.tbl_keys(grouped)
table.sort(keys)
-- TODO(ashkan) I wish we could do this lazily.
@@ -1799,16 +1899,13 @@ function M.locations_to_items(locations, offset_encoding)
table.sort(rows, position_sort)
local filename = vim.uri_to_fname(uri)
- -- list of row numbers
- local uri_rows = {}
+ local line_numbers = {}
for _, temp in ipairs(rows) do
- local pos = temp.start
- local row = pos.line
- table.insert(uri_rows, row)
+ table.insert(line_numbers, temp.start.line)
end
-- get all the lines for this uri
- local lines = get_lines(vim.uri_to_bufnr(uri), uri_rows)
+ local lines = get_lines(vim.uri_to_bufnr(uri), line_numbers)
for _, temp in ipairs(rows) do
local pos = temp.start
@@ -1837,6 +1934,7 @@ end
--- Converts symbols to quickfix list items.
---
---@param symbols table DocumentSymbol[] or SymbolInformation[]
+---@param bufnr integer
function M.symbols_to_items(symbols, bufnr)
local function _symbols_to_items(_symbols, _items, _bufnr)
for _, symbol in ipairs(_symbols) do
@@ -1879,6 +1977,7 @@ end
---@param lines table list of lines to trim
---@return table trimmed list of lines
function M.trim_empty_lines(lines)
+ vim.deprecate('vim.lsp.util.trim_empty_lines()', 'vim.split() with `trimempty`', '0.12')
local start = 1
for i = 1, #lines do
if lines[i] ~= nil and #lines[i] > 0 then
@@ -1905,6 +2004,7 @@ end
---@param lines table list of lines
---@return string filetype or "markdown" if it was unchanged.
function M.try_trim_markdown_code_blocks(lines)
+ vim.deprecate('vim.lsp.util.try_trim_markdown_code_blocks()', 'nil', '0.12')
local language_id = lines[1]:match('^```(.*)')
if language_id then
local has_inner_code_fence = false
@@ -2089,7 +2189,7 @@ end
--- Creates a `DocumentFormattingParams` object for the current buffer and cursor position.
---
---@param options table|nil with valid `FormattingOptions` entries
----@return `DocumentFormattingParams` object
+---@return lsp.DocumentFormattingParams object
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_formatting
function M.make_formatting_params(options)
validate({ options = { options, 't', true } })
@@ -2106,8 +2206,8 @@ end
--- Returns the UTF-32 and UTF-16 offsets for a position in a certain buffer.
---
---@param buf integer buffer number (0 for current)
----@param row 0-indexed line
----@param col 0-indexed byte offset in line
+---@param row integer 0-indexed line
+---@param col integer 0-indexed byte offset in line
---@param offset_encoding string utf-8|utf-16|utf-32 defaults to `offset_encoding` of first client of `buf`
---@return integer `offset_encoding` index of the character in line {row} column {col} in buffer {buf}
function M.character_offset(buf, row, col, offset_encoding)
@@ -2130,8 +2230,10 @@ end
---
---@param settings table language server settings
---@param section string indicating the field of the settings table
----@return table|string The value of settings accessed via section
+---@return table|string|vim.NIL The value of settings accessed via section. `vim.NIL` if not found.
+---@deprecated
function M.lookup_section(settings, section)
+ vim.deprecate('vim.lsp.util.lookup_section()', 'vim.tbl_get() with `vim.split`', '0.12')
for part in vim.gsplit(section, '.', { plain = true }) do
settings = settings[part]
if settings == nil then
@@ -2170,16 +2272,16 @@ local function make_line_range_params(bufnr, start_line, end_line, offset_encodi
}
end
----@private
---- Request updated LSP information for a buffer.
----
----@class lsp.util.RefreshOptions
+---@class (private) vim.lsp.util._refresh.Opts
---@field bufnr integer? Buffer to refresh (default: 0)
---@field only_visible? boolean Whether to only refresh for the visible regions of the buffer (default: false)
---@field client_id? integer Client ID to refresh (default: all clients)
---
+
+---@private
+--- Request updated LSP information for a buffer.
+---
---@param method string LSP method to call
----@param opts? lsp.util.RefreshOptions Options table
+---@param opts? vim.lsp.util._refresh.Opts Options table
function M._refresh(method, opts)
opts = opts or {}
local bufnr = opts.bufnr
@@ -2228,6 +2330,6 @@ end
M._get_line_byte_from_position = get_line_byte_from_position
---@nodoc
-M.buf_versions = {}
+M.buf_versions = {} ---@type table<integer,integer>
return M