aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua')
-rw-r--r--runtime/lua/vim/diagnostic.lua234
-rw-r--r--runtime/lua/vim/lsp.lua97
-rw-r--r--runtime/lua/vim/lsp/diagnostic.lua2
-rw-r--r--runtime/lua/vim/lsp/handlers.lua5
-rw-r--r--runtime/lua/vim/lsp/sync.lua74
-rw-r--r--runtime/lua/vim/lsp/tagfunc.lua76
-rw-r--r--runtime/lua/vim/lsp/util.lua12
-rw-r--r--runtime/lua/vim/uri.lua28
8 files changed, 307 insertions, 221 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua
index 191b9b9145..9b57467a52 100644
--- a/runtime/lua/vim/diagnostic.lua
+++ b/runtime/lua/vim/diagnostic.lua
@@ -36,7 +36,51 @@ M.handlers = setmetatable({}, {
end,
})
--- Local functions {{{
+-- Metatable that automatically creates an empty table when assigning to a missing key
+local bufnr_and_namespace_cacher_mt = {
+ __index = function(t, bufnr)
+ if not bufnr or bufnr == 0 then
+ bufnr = vim.api.nvim_get_current_buf()
+ end
+
+ rawset(t, bufnr, {})
+
+ return rawget(t, bufnr)
+ end,
+
+ __newindex = function(t, bufnr, v)
+ if not bufnr or bufnr == 0 then
+ bufnr = vim.api.nvim_get_current_buf()
+ end
+
+ rawset(t, bufnr, v)
+ end,
+}
+
+local diagnostic_cache = setmetatable({}, {
+ __index = function(t, bufnr)
+ if not bufnr or bufnr == 0 then
+ bufnr = vim.api.nvim_get_current_buf()
+ end
+
+ vim.api.nvim_buf_attach(bufnr, false, {
+ on_detach = function()
+ rawset(t, bufnr, nil) -- clear cache
+ end
+ })
+
+ rawset(t, bufnr, {})
+
+ return rawget(t, bufnr)
+ end,
+})
+
+local diagnostic_cache_extmarks = setmetatable({}, bufnr_and_namespace_cacher_mt)
+local diagnostic_attached_buffers = {}
+local diagnostic_disabled = {}
+local bufs_waiting_to_update = setmetatable({}, bufnr_and_namespace_cacher_mt)
+
+local all_namespaces = {}
---@private
local function to_severity(severity)
@@ -106,8 +150,6 @@ local function reformat_diagnostics(format, diagnostics)
return formatted
end
-local all_namespaces = {}
-
---@private
local function enabled_value(option, namespace)
local ns = namespace and M.get_namespace(namespace) or {}
@@ -213,36 +255,6 @@ local function get_bufnr(bufnr)
return bufnr
end
--- Metatable that automatically creates an empty table when assigning to a missing key
-local bufnr_and_namespace_cacher_mt = {
- __index = function(t, bufnr)
- if not bufnr or bufnr == 0 then
- bufnr = vim.api.nvim_get_current_buf()
- end
-
- if rawget(t, bufnr) == nil then
- rawset(t, bufnr, {})
- end
-
- return rawget(t, bufnr)
- end,
-
- __newindex = function(t, bufnr, v)
- if not bufnr or bufnr == 0 then
- bufnr = vim.api.nvim_get_current_buf()
- end
-
- rawset(t, bufnr, v)
- end,
-}
-
-local diagnostic_cleanup = setmetatable({}, bufnr_and_namespace_cacher_mt)
-local diagnostic_cache = setmetatable({}, bufnr_and_namespace_cacher_mt)
-local diagnostic_cache_extmarks = setmetatable({}, bufnr_and_namespace_cacher_mt)
-local diagnostic_attached_buffers = {}
-local diagnostic_disabled = {}
-local bufs_waiting_to_update = setmetatable({}, bufnr_and_namespace_cacher_mt)
-
---@private
local function is_disabled(namespace, bufnr)
local ns = M.get_namespace(namespace)
@@ -287,11 +299,6 @@ local function set_diagnostic_cache(namespace, bufnr, diagnostics)
end
---@private
-local function clear_diagnostic_cache(namespace, bufnr)
- diagnostic_cache[bufnr][namespace] = nil
-end
-
----@private
local function restore_extmarks(bufnr, last)
for ns, extmarks in pairs(diagnostic_cache_extmarks[bufnr]) do
local extmarks_current = vim.api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, {details = true})
@@ -377,6 +384,59 @@ local function clear_scheduled_display(namespace, bufnr)
end
---@private
+local function get_diagnostics(bufnr, opts, clamp)
+ opts = opts or {}
+
+ local namespace = opts.namespace
+ local diagnostics = {}
+ local buf_line_count = clamp and vim.api.nvim_buf_line_count(bufnr) or math.huge
+
+ ---@private
+ local function add(d)
+ if not opts.lnum or d.lnum == opts.lnum then
+ if clamp and (d.lnum >= buf_line_count or d.end_lnum >= buf_line_count) then
+ d = vim.deepcopy(d)
+ d.lnum = math.max(math.min(d.lnum, buf_line_count - 1), 0)
+ d.end_lnum = math.max(math.min(d.end_lnum, buf_line_count - 1), 0)
+ end
+ table.insert(diagnostics, d)
+ end
+ end
+
+ if namespace == nil and bufnr == nil then
+ for _, t in pairs(diagnostic_cache) do
+ for _, v in pairs(t) do
+ for _, diagnostic in pairs(v) do
+ add(diagnostic)
+ end
+ end
+ end
+ elseif namespace == nil then
+ for iter_namespace in pairs(diagnostic_cache[bufnr]) do
+ for _, diagnostic in pairs(diagnostic_cache[bufnr][iter_namespace]) do
+ add(diagnostic)
+ end
+ end
+ elseif bufnr == nil then
+ for _, t in pairs(diagnostic_cache) do
+ for _, diagnostic in pairs(t[namespace] or {}) do
+ add(diagnostic)
+ end
+ end
+ else
+ for _, diagnostic in pairs(diagnostic_cache[bufnr][namespace] or {}) do
+ add(diagnostic)
+ end
+ end
+
+ if opts.severity then
+ diagnostics = filter_by_severity(opts.severity, diagnostics)
+ end
+
+ return diagnostics
+end
+
+---@private
local function set_list(loclist, opts)
opts = opts or {}
local open = vim.F.if_nil(opts.open, true)
@@ -386,7 +446,7 @@ local function set_list(loclist, opts)
if loclist then
bufnr = vim.api.nvim_win_get_buf(winnr)
end
- local diagnostics = M.get(bufnr, opts)
+ local diagnostics = get_diagnostics(bufnr, opts, true)
local items = M.toqflist(diagnostics)
if loclist then
vim.fn.setloclist(winnr, {}, ' ', { title = title, items = items })
@@ -399,27 +459,12 @@ local function set_list(loclist, opts)
end
---@private
---- To (slightly) improve performance, modifies diagnostics in place.
-local function clamp_line_numbers(bufnr, diagnostics)
- local buf_line_count = vim.api.nvim_buf_line_count(bufnr)
- if buf_line_count == 0 then
- return
- end
-
- for _, diagnostic in ipairs(diagnostics) do
- diagnostic.lnum = math.max(math.min(diagnostic.lnum, buf_line_count - 1), 0)
- diagnostic.end_lnum = math.max(math.min(diagnostic.end_lnum, buf_line_count - 1), 0)
- end
-end
-
----@private
local function next_diagnostic(position, search_forward, bufnr, opts, namespace)
position[1] = position[1] - 1
bufnr = get_bufnr(bufnr)
local wrap = vim.F.if_nil(opts.wrap, true)
local line_count = vim.api.nvim_buf_line_count(bufnr)
- local diagnostics = M.get(bufnr, vim.tbl_extend("keep", opts, {namespace = namespace}))
- clamp_line_numbers(bufnr, diagnostics)
+ local diagnostics = get_diagnostics(bufnr, vim.tbl_extend("keep", opts, {namespace = namespace}), true)
local line_diagnostics = diagnostic_lines(diagnostics)
for i = 0, line_count do
local offset = i * (search_forward and 1 or -1)
@@ -431,13 +476,14 @@ local function next_diagnostic(position, search_forward, bufnr, opts, namespace)
lnum = (lnum + line_count) % line_count
end
if line_diagnostics[lnum] and not vim.tbl_isempty(line_diagnostics[lnum]) then
+ local line_length = #vim.api.nvim_buf_get_lines(bufnr, lnum, lnum + 1, true)[1]
local sort_diagnostics, is_next
if search_forward then
sort_diagnostics = function(a, b) return a.col < b.col end
- is_next = function(diagnostic) return diagnostic.col > position[2] end
+ is_next = function(d) return math.min(d.col, line_length - 1) > position[2] end
else
sort_diagnostics = function(a, b) return a.col > b.col end
- is_next = function(diagnostic) return diagnostic.col < position[2] end
+ is_next = function(d) return math.min(d.col, line_length - 1) < position[2] end
end
table.sort(line_diagnostics[lnum], sort_diagnostics)
if i == 0 then
@@ -481,10 +527,6 @@ local function diagnostic_move_pos(opts, pos)
end
end
--- }}}
-
--- Public API {{{
-
--- Configure diagnostic options globally or for a specific diagnostic
--- namespace.
---
@@ -618,19 +660,8 @@ function M.set(namespace, bufnr, diagnostics, opts)
}
if vim.tbl_isempty(diagnostics) then
- clear_diagnostic_cache(namespace, bufnr)
+ diagnostic_cache[bufnr][namespace] = nil
else
- if not diagnostic_cleanup[bufnr][namespace] then
- diagnostic_cleanup[bufnr][namespace] = true
-
- -- Clean up our data when the buffer unloads.
- vim.api.nvim_buf_attach(bufnr, false, {
- on_detach = function(_, b)
- clear_diagnostic_cache(namespace, b)
- diagnostic_cleanup[b][namespace] = nil
- end
- })
- end
set_diagnostic_cache(namespace, bufnr, diagnostics)
end
@@ -689,49 +720,7 @@ function M.get(bufnr, opts)
opts = { opts, 't', true },
}
- opts = opts or {}
-
- local namespace = opts.namespace
- local diagnostics = {}
-
- ---@private
- local function add(d)
- if not opts.lnum or d.lnum == opts.lnum then
- table.insert(diagnostics, d)
- end
- end
-
- if namespace == nil and bufnr == nil then
- for _, t in pairs(diagnostic_cache) do
- for _, v in pairs(t) do
- for _, diagnostic in pairs(v) do
- add(diagnostic)
- end
- end
- end
- elseif namespace == nil then
- for iter_namespace in pairs(diagnostic_cache[bufnr]) do
- for _, diagnostic in pairs(diagnostic_cache[bufnr][iter_namespace]) do
- add(diagnostic)
- end
- end
- elseif bufnr == nil then
- for _, t in pairs(diagnostic_cache) do
- for _, diagnostic in pairs(t[namespace] or {}) do
- add(diagnostic)
- end
- end
- else
- for _, diagnostic in pairs(diagnostic_cache[bufnr][namespace] or {}) do
- add(diagnostic)
- end
- end
-
- if opts.severity then
- diagnostics = filter_by_severity(opts.severity, diagnostics)
- end
-
- return diagnostics
+ return get_diagnostics(bufnr, opts, false)
end
--- Get the previous diagnostic closest to the cursor position.
@@ -1115,7 +1104,7 @@ function M.show(namespace, bufnr, diagnostics, opts)
M.hide(namespace, bufnr)
- diagnostics = diagnostics or M.get(bufnr, {namespace=namespace})
+ diagnostics = diagnostics or get_diagnostics(bufnr, {namespace=namespace}, true)
if not diagnostics or vim.tbl_isempty(diagnostics) then
return
@@ -1141,8 +1130,6 @@ function M.show(namespace, bufnr, diagnostics, opts)
end
end
- clamp_line_numbers(bufnr, diagnostics)
-
for handler_name, handler in pairs(M.handlers) do
if handler.show and opts[handler_name] then
handler.show(namespace, bufnr, diagnostics, opts)
@@ -1213,8 +1200,7 @@ function M.open_float(bufnr, opts)
opts = get_resolved_options({ float = float_opts }, nil, bufnr).float
end
- local diagnostics = M.get(bufnr, opts)
- clamp_line_numbers(bufnr, diagnostics)
+ local diagnostics = get_diagnostics(bufnr, opts, true)
if scope == "line" then
diagnostics = vim.tbl_filter(function(d)
@@ -1338,7 +1324,7 @@ function M.reset(namespace, bufnr)
for _, iter_bufnr in ipairs(buffers) do
local namespaces = namespace and {namespace} or vim.tbl_keys(diagnostic_cache[iter_bufnr])
for _, iter_namespace in ipairs(namespaces) do
- clear_diagnostic_cache(iter_namespace, iter_bufnr)
+ diagnostic_cache[iter_bufnr][iter_namespace] = nil
M.hide(iter_namespace, iter_bufnr)
end
end
@@ -1545,7 +1531,7 @@ function M.fromqflist(list)
for _, item in ipairs(list) do
if item.valid == 1 then
local lnum = math.max(0, item.lnum - 1)
- local col = item.col > 0 and (item.col - 1) or nil
+ local col = math.max(0, item.col - 1)
local end_lnum = item.end_lnum > 0 and (item.end_lnum - 1) or lnum
local end_col = item.end_col > 0 and (item.end_col - 1) or col
local severity = item.type ~= "" and M.severity[item.type] or M.severity.ERROR
@@ -1563,6 +1549,4 @@ function M.fromqflist(list)
return diagnostics
end
--- }}}
-
return M
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index 0fc0a7a7aa..7433e7c04d 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -115,6 +115,13 @@ local format_line_ending = {
["mac"] = '\r',
}
+---@private
+---@param bufnr (number)
+---@returns (string)
+local function buf_get_line_ending(bufnr)
+ return format_line_ending[nvim_buf_get_option(bufnr, 'fileformat')] or '\n'
+end
+
local client_index = 0
---@private
--- Returns a new, unused client id.
@@ -236,7 +243,6 @@ local function validate_client_config(config)
config = { config, 't' };
}
validate {
- root_dir = { config.root_dir, optional_validator(is_dir), "directory" };
handlers = { config.handlers, "t", true };
capabilities = { config.capabilities, "t", true };
cmd_cwd = { config.cmd_cwd, optional_validator(is_dir), "directory" };
@@ -278,9 +284,10 @@ end
---@param bufnr (number) Buffer handle, or 0 for current.
---@returns Buffer text as string.
local function buf_get_full_text(bufnr)
- local text = table.concat(nvim_buf_get_lines(bufnr, 0, -1, true), '\n')
+ local line_ending = buf_get_line_ending(bufnr)
+ local text = table.concat(nvim_buf_get_lines(bufnr, 0, -1, true), line_ending)
if nvim_buf_get_option(bufnr, 'eol') then
- text = text .. '\n'
+ text = text .. line_ending
end
return text
end
@@ -362,9 +369,9 @@ do
local incremental_changes = function(client)
local cached_buffers = state_by_client[client.id].buffers
local curr_lines = nvim_buf_get_lines(bufnr, 0, -1, true)
- local line_ending = format_line_ending[vim.api.nvim_buf_get_option(0, 'fileformat')]
+ local line_ending = buf_get_line_ending(bufnr)
local incremental_change = sync.compute_diff(
- cached_buffers[bufnr], curr_lines, firstline, lastline, new_lastline, client.offset_encoding or 'utf-16', line_ending or '\n')
+ cached_buffers[bufnr], curr_lines, firstline, lastline, new_lastline, client.offset_encoding or 'utf-16', line_ending)
cached_buffers[bufnr] = curr_lines
return incremental_change
end
@@ -566,12 +573,10 @@ end
--
--- Starts and initializes a client with the given configuration.
---
---- Parameters `cmd` and `root_dir` are required.
+--- Parameter `cmd` is required.
---
--- The following parameters describe fields in the {config} table.
---
----@param root_dir: (string) Directory where the LSP server will base
---- its rootUri on initialization.
---
---@param cmd: (required, string or list treated like |jobstart()|) Base command
--- that initiates the LSP client.
@@ -587,6 +592,11 @@ end
--- { "PRODUCTION=true"; "TEST=123"; PORT = 8080; HOST = "0.0.0.0"; }
--- </pre>
---
+---@param workspace_folders (table) List of workspace folders passed to the
+--- language server. For backwards compatibility rootUri and rootPath will be
+--- derived from the first workspace folder in this list. See `workspaceFolders` in
+--- the LSP spec.
+---
---@param capabilities Map overriding the default capabilities defined by
--- |vim.lsp.protocol.make_client_capabilities()|, passed to the language
--- server on initialization. Hint: use make_client_capabilities() and modify
@@ -610,10 +620,6 @@ end
--- as `initializationOptions`. See `initialize` in the LSP spec.
---
---@param name (string, default=client-id) Name in log messages.
---
----@param workspace_folders (table) List of workspace folders passed to the
---- language server. Defaults to root_dir if not set. See `workspaceFolders` in
---- the LSP spec
---
---@param get_language_id function(bufnr, filetype) -> language ID as string.
--- Defaults to the filetype.
@@ -663,6 +669,11 @@ end
--- - exit_timeout (number, default 500): Milliseconds to wait for server to
-- exit cleanly after sending the 'shutdown' request before sending kill -15.
-- If set to false, nvim exits immediately after sending the 'shutdown' request to the server.
+---
+---@param root_dir string Directory where the LSP
+--- server will base its workspaceFolders, rootUri, and rootPath
+--- on initialization.
+---
---@returns Client id. |vim.lsp.get_client_by_id()| Note: client may not be
--- fully initialized. Use `on_init` to do any actions once
--- the client has been initialized.
@@ -805,11 +816,24 @@ function lsp.start_client(config)
}
local version = vim.version()
- if config.root_dir and not config.workspace_folders then
- config.workspace_folders = {{
- uri = vim.uri_from_fname(config.root_dir);
- name = string.format("%s", config.root_dir);
- }};
+ local workspace_folders
+ local root_uri
+ local root_path
+ if config.workspace_folders or config.root_dir then
+ if config.root_dir and not config.workspace_folders then
+ workspace_folders = {{
+ uri = vim.uri_from_fname(config.root_dir);
+ name = string.format("%s", config.root_dir);
+ }};
+ else
+ workspace_folders = config.workspace_folders
+ end
+ root_uri = workspace_folders[1].uri
+ root_path = vim.uri_to_fname(root_uri)
+ else
+ workspace_folders = vim.NIL
+ root_uri = vim.NIL
+ root_path = vim.NIL
end
local initialize_params = {
@@ -827,10 +851,15 @@ function lsp.start_client(config)
-- The rootPath of the workspace. Is null if no folder is open.
--
-- @deprecated in favour of rootUri.
- rootPath = config.root_dir;
+ rootPath = root_path;
-- The rootUri of the workspace. Is null if no folder is open. If both
-- `rootPath` and `rootUri` are set `rootUri` wins.
- rootUri = config.root_dir and vim.uri_from_fname(config.root_dir);
+ rootUri = root_uri;
+ -- The workspace folders configured in the client when the server starts.
+ -- This property is only available if the client supports workspace folders.
+ -- It can be `null` if the client supports workspace folders but none are
+ -- configured.
+ workspaceFolders = workspace_folders;
-- User provided initialization options.
initializationOptions = config.init_options;
-- The capabilities provided by the client (editor or tool)
@@ -838,21 +867,6 @@ function lsp.start_client(config)
-- The initial trace setting. If omitted trace is disabled ("off").
-- trace = "off" | "messages" | "verbose";
trace = valid_traces[config.trace] or 'off';
- -- The workspace folders configured in the client when the server starts.
- -- This property is only available if the client supports workspace folders.
- -- It can be `null` if the client supports workspace folders but none are
- -- configured.
- --
- -- Since 3.6.0
- -- workspaceFolders?: WorkspaceFolder[] | null;
- -- export interface WorkspaceFolder {
- -- -- The associated URI for this workspace folder.
- -- uri
- -- -- The name of the workspace folder. Used to refer to this
- -- -- workspace folder in the user interface.
- -- name
- -- }
- workspaceFolders = config.workspace_folders,
}
if config.before_init then
-- TODO(ashkan) handle errors here.
@@ -1587,6 +1601,21 @@ function lsp.formatexpr(opts)
return 0
end
+--- Provides an interface between the built-in client and 'tagfunc'.
+---
+--- When used with normal mode commands (e.g. |CTRL-]|) this will invoke
+--- the "textDocument/definition" LSP method to find the tag under the cursor.
+--- Otherwise, uses "workspace/symbol". If no results are returned from
+--- any LSP servers, falls back to using built-in tags.
+---
+---@param pattern Pattern used to find a workspace symbol
+---@param flags See |tag-function|
+---
+---@returns A list of matching tags
+function lsp.tagfunc(...)
+ return require('vim.lsp.tagfunc')(...)
+end
+
---Checks whether a client is stopped.
---
---@param client_id (Number)
diff --git a/runtime/lua/vim/lsp/diagnostic.lua b/runtime/lua/vim/lsp/diagnostic.lua
index 1e6f83c1ba..76a4dc30b7 100644
--- a/runtime/lua/vim/lsp/diagnostic.lua
+++ b/runtime/lua/vim/lsp/diagnostic.lua
@@ -717,5 +717,3 @@ end
-- }}}
return M
-
--- vim: fdm=marker
diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua
index a561630c2b..fa4f2b22a5 100644
--- a/runtime/lua/vim/lsp/handlers.lua
+++ b/runtime/lua/vim/lsp/handlers.lua
@@ -350,7 +350,10 @@ M['textDocument/signatureHelp'] = M.signature_help
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentHighlight
M['textDocument/documentHighlight'] = function(_, result, ctx, _)
if not result then return end
- util.buf_highlight_references(ctx.bufnr, result, ctx.client_id)
+ local client_id = ctx.client_id
+ local client = vim.lsp.get_client_by_id(client_id)
+ if not client then return end
+ util.buf_highlight_references(ctx.bufnr, result, client.offset_encoding)
end
---@private
diff --git a/runtime/lua/vim/lsp/sync.lua b/runtime/lua/vim/lsp/sync.lua
index 585c14a00f..f185e3973f 100644
--- a/runtime/lua/vim/lsp/sync.lua
+++ b/runtime/lua/vim/lsp/sync.lua
@@ -74,43 +74,45 @@ local function byte_to_utf(line, byte, offset_encoding)
return utf_idx + 1
end
+local function compute_line_length(line, offset_encoding)
+ local length
+ local _
+ if offset_encoding == 'utf-16' then
+ _, length = str_utfindex(line)
+ elseif offset_encoding == 'utf-32' then
+ length, _ = str_utfindex(line)
+ else
+ length = #line
+ end
+ return length
+end
+
---@private
-- Given a line, byte idx, alignment, and offset_encoding convert to the aligned
-- utf-8 index and either the utf-16, or utf-32 index.
---@param line string the line to index into
---@param byte integer the byte idx
----@param align string when dealing with multibyte characters,
--- to choose the start of the current character or the beginning of the next.
--- Used for incremental sync for start/end range respectively
---@param offset_encoding string utf-8|utf-16|utf-32|nil (default: utf-8)
---@returns table<string, int> byte_idx and char_idx of first change position
-local function align_position(line, byte, align, offset_encoding)
+local function align_end_position(line, byte, offset_encoding)
local char
-- If on the first byte, or an empty string: the trivial case
if byte == 1 or #line == 0 then
char = byte
-- Called in the case of extending an empty line "" -> "a"
elseif byte == #line + 1 then
- byte = byte + str_utf_end(line, #line)
- char = byte_to_utf(line, byte, offset_encoding)
+ char = compute_line_length(line, offset_encoding) + 1
else
-- Modifying line, find the nearest utf codepoint
- if align == 'start' then
- byte = byte + str_utf_start(line, byte)
- char = byte_to_utf(line, byte, offset_encoding)
- elseif align == 'end' then
- local offset = str_utf_end(line, byte)
- -- If the byte does not fall on the start of the character, then
- -- align to the start of the next character.
- if offset > 0 then
- char = byte_to_utf(line, byte, offset_encoding) + 1
- byte = byte + offset
- else
- char = byte_to_utf(line, byte, offset_encoding)
- byte = byte + offset
- end
+ local offset = str_utf_end(line, byte)
+ -- If the byte does not fall on the start of the character, then
+ -- align to the start of the next character.
+ if offset > 0 then
+ char = byte_to_utf(line, byte, offset_encoding) + 1
+ byte = byte + offset
else
- error('`align` must be start or end.')
+ char = byte_to_utf(line, byte, offset_encoding)
+ byte = byte + offset
end
-- Extending line, find the nearest utf codepoint for the last valid character
end
@@ -154,7 +156,18 @@ local function compute_start_range(prev_lines, curr_lines, firstline, lastline,
end
-- Convert byte to codepoint if applicable
- local byte_idx, char_idx = align_position(prev_line, start_byte_idx, 'start', offset_encoding)
+ local char_idx
+ local byte_idx
+ if start_byte_idx == 1 or (#prev_line == 0 and start_byte_idx == 1)then
+ byte_idx = start_byte_idx
+ char_idx = 1
+ elseif start_byte_idx == #prev_line + 1 then
+ byte_idx = start_byte_idx
+ char_idx = compute_line_length(prev_line, offset_encoding) + 1
+ else
+ byte_idx = start_byte_idx + str_utf_start(prev_line, start_byte_idx)
+ char_idx = byte_to_utf(prev_line, start_byte_idx, offset_encoding)
+ end
-- Return the start difference (shared for new and prev lines)
return { line_idx = firstline, byte_idx = byte_idx, char_idx = char_idx }
@@ -219,11 +232,12 @@ local function compute_end_range(prev_lines, curr_lines, start_range, firstline,
-- Iterate from end to beginning of shortest line
local prev_end_byte_idx = prev_line_length - byte_offset + 1
+
-- Handle case where lines match
if prev_end_byte_idx == 0 then
prev_end_byte_idx = 1
end
- local prev_byte_idx, prev_char_idx = align_position(prev_line, prev_end_byte_idx, 'start', offset_encoding)
+ local prev_byte_idx, prev_char_idx = align_end_position(prev_line, prev_end_byte_idx, offset_encoding)
local prev_end_range = { line_idx = prev_line_idx, byte_idx = prev_byte_idx, char_idx = prev_char_idx }
local curr_end_range
@@ -236,7 +250,7 @@ local function compute_end_range(prev_lines, curr_lines, start_range, firstline,
if curr_end_byte_idx == 0 then
curr_end_byte_idx = 1
end
- local curr_byte_idx, curr_char_idx = align_position(curr_line, curr_end_byte_idx, 'start', offset_encoding)
+ local curr_byte_idx, curr_char_idx = align_end_position(curr_line, curr_end_byte_idx, offset_encoding)
curr_end_range = { line_idx = curr_line_idx, byte_idx = curr_byte_idx, char_idx = curr_char_idx }
end
@@ -280,18 +294,6 @@ local function extract_text(lines, start_range, end_range, line_ending)
end
end
-local function compute_line_length(line, offset_encoding)
- local length
- local _
- if offset_encoding == 'utf-16' then
- _, length = str_utfindex(line)
- elseif offset_encoding == 'utf-32' then
- length, _ = str_utfindex(line)
- else
- length = #line
- end
- return length
-end
---@private
-- rangelength depends on the offset encoding
-- bytes for utf-8 (clangd with extenion)
diff --git a/runtime/lua/vim/lsp/tagfunc.lua b/runtime/lua/vim/lsp/tagfunc.lua
new file mode 100644
index 0000000000..5c55e8559f
--- /dev/null
+++ b/runtime/lua/vim/lsp/tagfunc.lua
@@ -0,0 +1,76 @@
+local lsp = vim.lsp
+local util = vim.lsp.util
+
+---@private
+local function mk_tag_item(name, range, uri, offset_encoding)
+ local bufnr = vim.uri_to_bufnr(uri)
+ -- This is get_line_byte_from_position is 0-indexed, call cursor expects a 1-indexed position
+ local byte = util._get_line_byte_from_position(bufnr, range.start, offset_encoding) + 1
+ return {
+ name = name,
+ filename = vim.uri_to_fname(uri),
+ cmd = string.format('call cursor(%d, %d)|', range.start.line + 1, byte),
+ }
+end
+
+---@private
+local function query_definition(pattern)
+ local params = lsp.util.make_position_params()
+ local results_by_client, err = lsp.buf_request_sync(0, 'textDocument/definition', params, 1000)
+ if err then
+ return {}
+ end
+ local results = {}
+ local add = function(range, uri, offset_encoding)
+ table.insert(results, mk_tag_item(pattern, range, uri, offset_encoding))
+ end
+ for client_id, lsp_results in pairs(results_by_client) do
+ local client = lsp.get_client_by_id(client_id)
+ local result = lsp_results.result or {}
+ if result.range then -- Location
+ add(result.range, result.uri)
+ else -- Location[] or LocationLink[]
+ for _, item in pairs(result) do
+ if item.range then -- Location
+ add(item.range, item.uri, client.offset_encoding)
+ else -- LocationLink
+ add(item.targetSelectionRange, item.targetUri, client.offset_encoding)
+ end
+ end
+ end
+ end
+ return results
+end
+
+---@private
+local function query_workspace_symbols(pattern)
+ local results_by_client, err = lsp.buf_request_sync(0, 'workspace/symbol', { query = pattern }, 1000)
+ if err then
+ return {}
+ end
+ local results = {}
+ for client_id, symbols in pairs(results_by_client) do
+ local client = lsp.get_client_by_id(client_id)
+ for _, symbol in pairs(symbols.result or {}) do
+ local loc = symbol.location
+ local item = mk_tag_item(symbol.name, loc.range, loc.uri, client.offset_encoding)
+ item.kind = lsp.protocol.SymbolKind[symbol.kind] or 'Unknown'
+ table.insert(results, item)
+ end
+ end
+ return results
+end
+
+---@private
+local function tagfunc(pattern, flags)
+ local matches
+ if string.match(flags, 'c') then
+ matches = query_definition(pattern)
+ else
+ matches = query_workspace_symbols(pattern)
+ end
+ -- fall back to tags if no matches
+ return #matches > 0 and matches or vim.NIL
+end
+
+return tagfunc
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index a4b7b9922b..ba5c20ef9f 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -1341,19 +1341,17 @@ do --[[ References ]]
---
---@param bufnr buffer id
---@param references List of `DocumentHighlight` objects to highlight
+ ---@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to utf-16
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#documentHighlight
- function M.buf_highlight_references(bufnr, references, client_id)
+ function M.buf_highlight_references(bufnr, references, offset_encoding)
validate { bufnr = {bufnr, 'n', true} }
- local client = vim.lsp.get_client_by_id(client_id)
- if not client then
- return
- end
+ offset_encoding = offset_encoding or 'utf-16'
for _, reference in ipairs(references) do
local start_line, start_char = reference["range"]["start"]["line"], reference["range"]["start"]["character"]
local end_line, end_char = reference["range"]["end"]["line"], reference["range"]["end"]["character"]
- local start_idx = get_line_byte_from_position(bufnr, { line = start_line, character = start_char }, client.offset_encoding)
- local end_idx = get_line_byte_from_position(bufnr, { line = start_line, character = end_char }, client.offset_encoding)
+ local start_idx = get_line_byte_from_position(bufnr, { line = start_line, character = start_char }, offset_encoding)
+ local end_idx = get_line_byte_from_position(bufnr, { line = start_line, character = end_char }, offset_encoding)
local document_highlight_kind = {
[protocol.DocumentHighlightKind.Text] = "LspReferenceText";
diff --git a/runtime/lua/vim/uri.lua b/runtime/lua/vim/uri.lua
index 5d8d4fa169..d08d2a3ee3 100644
--- a/runtime/lua/vim/uri.lua
+++ b/runtime/lua/vim/uri.lua
@@ -56,8 +56,8 @@ local function is_windows_file_uri(uri)
end
--- Get a URI from a file path.
----@param path (string): Path to file
----@return URI
+---@param path string Path to file
+---@return string URI
local function uri_from_fname(path)
local volume_path, fname = path:match("^([a-zA-Z]:)(.*)")
local is_windows = volume_path ~= nil
@@ -78,8 +78,8 @@ local URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*):.*'
local WINDOWS_URI_SCHEME_PATTERN = '^([a-zA-Z]+[a-zA-Z0-9+-.]*):[a-zA-Z]:.*'
--- Get a URI from a bufnr
----@param bufnr (number): Buffer number
----@return URI
+---@param bufnr number
+---@return string URI
local function uri_from_bufnr(bufnr)
local fname = vim.api.nvim_buf_get_name(bufnr)
local volume_path = fname:match("^([a-zA-Z]:).*")
@@ -99,8 +99,8 @@ local function uri_from_bufnr(bufnr)
end
--- Get a filename from a URI
----@param uri (string): The URI
----@return Filename
+---@param uri string
+---@return string filename or unchanged URI for non-file URIs
local function uri_to_fname(uri)
local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
if scheme ~= 'file' then
@@ -117,17 +117,13 @@ local function uri_to_fname(uri)
return uri
end
---- Return or create a buffer for a uri.
----@param uri (string): The URI
----@return bufnr.
----@note Creates buffer but does not load it
+--- Get the buffer for a uri.
+--- Creates a new unloaded buffer if no buffer for the uri already exists.
+--
+---@param uri string
+---@return number bufnr
local function uri_to_bufnr(uri)
- local scheme = assert(uri:match(URI_SCHEME_PATTERN), 'URI must contain a scheme: ' .. uri)
- if scheme == 'file' then
- return vim.fn.bufadd(uri_to_fname(uri))
- else
- return vim.fn.bufadd(uri)
- end
+ return vim.fn.bufadd(uri_to_fname(uri))
end
return {