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.lua106
1 files changed, 84 insertions, 22 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 494eebf9ea..5c6d183ac1 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -94,18 +94,45 @@ local edit_sort_key = sort_by_key(function(e)
return {e.A[1], e.A[2], e.i}
end)
+local function get_line_byte_from_line_character(bufnr, lnum, cnum)
+ -- Skip check when the byte and character position is the same
+ if cnum > 0 then
+ local lines = api.nvim_buf_get_lines(bufnr, lnum, lnum+1, false)
+
+ if #lines > 0 then
+ return vim.str_byteindex(lines[1], cnum)
+ end
+ end
+
+ return cnum
+end
+
function M.apply_text_edits(text_edits, bufnr)
if not next(text_edits) then return end
+ if not api.nvim_buf_is_loaded(bufnr) then
+ vim.fn.bufload(bufnr)
+ end
local start_line, finish_line = math.huge, -1
local cleaned = {}
for i, e in ipairs(text_edits) do
+ -- adjust start and end column for UTF-16 encoding of non-ASCII characters
+ local start_row = e.range.start.line
+ local start_col = get_line_byte_from_line_character(
+ bufnr,
+ start_row,
+ e.range.start.character)
+ local end_row = e.range["end"].line
+ local end_col = get_line_byte_from_line_character(
+ bufnr,
+ end_row,
+ e.range["end"].character)
start_line = math.min(e.range.start.line, start_line)
finish_line = math.max(e.range["end"].line, finish_line)
-- TODO(ashkan) sanity check ranges for overlap.
table.insert(cleaned, {
i = i;
- A = {e.range.start.line; e.range.start.character};
- B = {e.range["end"].line; e.range["end"].character};
+ A = {start_row; start_col};
+ B = {end_row; end_col};
lines = vim.split(e.newText, '\n', true);
})
end
@@ -113,9 +140,6 @@ function M.apply_text_edits(text_edits, bufnr)
-- Reverse sort the orders so we can apply them without interfering with
-- eachother. Also add i as a sort key to mimic a stable sort.
table.sort(cleaned, edit_sort_key)
- if not api.nvim_buf_is_loaded(bufnr) then
- vim.fn.bufload(bufnr)
- end
local lines = api.nvim_buf_get_lines(bufnr, start_line, finish_line + 1, false)
local fix_eol = api.nvim_buf_get_option(bufnr, 'fixeol')
local set_eol = fix_eol and api.nvim_buf_line_count(bufnr) <= finish_line + 1
@@ -159,10 +183,12 @@ end
function M.apply_text_document_edit(text_document_edit)
local text_document = text_document_edit.textDocument
local bufnr = vim.uri_to_bufnr(text_document.uri)
- -- `VersionedTextDocumentIdentifier`s version may be nil https://microsoft.github.io/language-server-protocol/specification#versionedTextDocumentIdentifier
- if text_document.version ~= vim.NIL and M.buf_versions[bufnr] > text_document.version then
- print("Buffer ", text_document.uri, " newer than edits.")
- return
+ if text_document.version then
+ -- `VersionedTextDocumentIdentifier`s version may be null https://microsoft.github.io/language-server-protocol/specification#versionedTextDocumentIdentifier
+ if text_document.version ~= vim.NIL and M.buf_versions[bufnr] ~= nil and M.buf_versions[bufnr] > text_document.version then
+ print("Buffer ", text_document.uri, " newer than edits.")
+ return
+ end
end
M.apply_text_edits(text_document_edit.edits, bufnr)
end
@@ -203,6 +229,13 @@ local function remove_unmatch_completion_items(items, prefix)
end, items)
end
+-- Acording to LSP spec, if the client set "completionItemKind.valueSet",
+-- the client must handle it properly even if it receives a value outside the specification.
+-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion
+function M._get_completion_item_kind_name(completion_item_kind)
+ return protocol.CompletionItemKind[completion_item_kind] or "Unknown"
+end
+
--- Getting vim complete-items with incomplete flag.
-- @params CompletionItem[], CompletionList or nil (https://microsoft.github.io/language-server-protocol/specification#textDocument_completion)
-- @return { matches = complete-items table, incomplete = boolean }
@@ -234,7 +267,7 @@ function M.text_document_completion_list_to_complete_items(result, prefix)
table.insert(matches, {
word = word,
abbr = completion_item.label,
- kind = protocol.CompletionItemKind[completion_item.kind] or '',
+ kind = M._get_completion_item_kind_name(completion_item.kind),
menu = completion_item.detail or '',
info = info,
icase = 1,
@@ -436,8 +469,9 @@ function M.jump_to_location(location)
local items = {{tagname=vim.fn.expand('<cword>'), from=from}}
vim.fn.settagstack(vim.fn.win_getid(), {items=items}, 't')
- --- Jump to new location
+ --- Jump to new location (adjusting for UTF-16 encoding of characters)
api.nvim_set_current_buf(bufnr)
+ api.nvim_buf_set_option(0, 'buflisted', true)
local range = location.range or location.targetSelectionRange
local row = range.start.line
local col = range.start.character
@@ -701,20 +735,29 @@ do
return severity_highlights[severity]
end
- function M.show_line_diagnostics()
+ function M.get_line_diagnostics()
local bufnr = api.nvim_get_current_buf()
- local line = api.nvim_win_get_cursor(0)[1] - 1
+ local linenr = api.nvim_win_get_cursor(0)[1] - 1
+
+ local buffer_diagnostics = M.diagnostics_by_buf[bufnr]
+
+ if not buffer_diagnostics then
+ return {}
+ end
+
+ local diagnostics_by_line = M.diagnostics_group_by_line(buffer_diagnostics)
+ return diagnostics_by_line[linenr] or {}
+ end
+
+ function M.show_line_diagnostics()
-- local marks = api.nvim_buf_get_extmarks(bufnr, diagnostic_ns, {line, 0}, {line, -1}, {})
-- if #marks == 0 then
-- return
-- end
local lines = {"Diagnostics:"}
local highlights = {{0, "Bold"}}
-
- local buffer_diagnostics = M.diagnostics_by_buf[bufnr]
- if not buffer_diagnostics then return end
- local line_diagnostics = M.diagnostics_group_by_line(buffer_diagnostics)[line]
- if not line_diagnostics then return end
+ local line_diagnostics = M.get_line_diagnostics()
+ if vim.tbl_isempty(line_diagnostics) then return end
for i, diagnostic in ipairs(line_diagnostics) do
-- for i, mark in ipairs(marks) do
@@ -934,6 +977,13 @@ function M.set_qflist(items)
})
end
+-- Acording to LSP spec, if the client set "symbolKind.valueSet",
+-- the client must handle it properly even if it receives a value outside the specification.
+-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol
+function M._get_symbol_kind_name(symbol_kind)
+ return protocol.SymbolKind[symbol_kind] or "Unknown"
+end
+
--- Convert symbols to quickfix list items
---
--@symbols DocumentSymbol[] or SymbolInformation[]
@@ -942,7 +992,7 @@ function M.symbols_to_items(symbols, bufnr)
for _, symbol in ipairs(_symbols) do
if symbol.location then -- SymbolInformation type
local range = symbol.location.range
- local kind = protocol.SymbolKind[symbol.kind]
+ local kind = M._get_symbol_kind_name(symbol.kind)
table.insert(_items, {
filename = vim.uri_to_fname(symbol.location.uri),
lnum = range.start.line + 1,
@@ -951,7 +1001,7 @@ function M.symbols_to_items(symbols, bufnr)
text = '['..kind..'] '..symbol.name,
})
elseif symbol.range then -- DocumentSymbole type
- local kind = protocol.SymbolKind[symbol.kind]
+ local kind = M._get_symbol_kind_name(symbol.kind)
table.insert(_items, {
-- bufnr = _bufnr,
filename = vim.api.nvim_buf_get_name(_bufnr),
@@ -1018,14 +1068,26 @@ function M.try_trim_markdown_code_blocks(lines)
end
local str_utfindex = vim.str_utfindex
-function M.make_position_params()
+local function make_position_param()
local row, col = unpack(api.nvim_win_get_cursor(0))
row = row - 1
local line = api.nvim_buf_get_lines(0, row, row+1, true)[1]
col = str_utfindex(line, col)
+ return { line = row; character = col; }
+end
+
+function M.make_position_params()
return {
textDocument = M.make_text_document_params();
- position = { line = row; character = col; }
+ position = make_position_param()
+ }
+end
+
+function M.make_range_params()
+ local position = make_position_param()
+ return {
+ textDocument = { uri = vim.uri_from_bufnr(0) },
+ range = { start = position; ["end"] = position; }
}
end