aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/lua/vim/lsp/util.lua2
-rw-r--r--test/functional/plugin/lsp_spec.lua102
2 files changed, 101 insertions, 3 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index e77b9f199c..6a1e799489 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -160,7 +160,7 @@ 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 ~= nil and M.buf_versions[bufnr] > text_document.version then
+ if text_document.version ~= vim.NIL and M.buf_versions[bufnr] > text_document.version then
print("Buffer ", text_document.uri, " newer than edits.")
return
end
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua
index 1a0063b43e..79f6ef9dd2 100644
--- a/test/functional/plugin/lsp_spec.lua
+++ b/test/functional/plugin/lsp_spec.lua
@@ -770,13 +770,14 @@ describe('LSP', function()
exec_lua([[require'vim.lsp'; return vim.fn.getcompletion('Lsp', 'highlight')]]))
end)
- describe('apply_edits', function()
+ describe('apply_text_edits', function()
before_each(function()
insert(dedent([[
First line of text
Second line of text
Third line of text
- Fourth line of text]]))
+ Fourth line of text
+ å å ɧ 汉语 ↥ 🤦 🦄]]))
end)
it('applies apply simple edits', function()
local edits = {
@@ -790,6 +791,7 @@ describe('LSP', function()
'2econd line of text';
'3ird line of text';
'Fourth line of text';
+ 'å å ɧ 汉语 ↥ 🤦 🦄';
}, buf_lines(1))
end)
it('applies complex edits', function()
@@ -813,8 +815,104 @@ describe('LSP', function()
'The next line of text';
'another line of text';
'before this!';
+ 'å å ɧ 汉语 ↥ 🤦 🦄';
}, buf_lines(1))
end)
+ pending('applies non-ASCII characters edits', function()
+ -- FIXME: We don't handle non-ASCII characters well in UTF-16
+ local edits = {
+ make_edit(4, 0, 4, 14, {"a a h"});
+ }
+ exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1)
+ eq({
+ 'First line of text';
+ 'Second line of text';
+ 'Third line of text';
+ 'Fourth line of text';
+ 'a a h';
+ }, buf_lines(1))
+ end)
+ end)
+
+ describe('apply_text_document_edit', function()
+ local target_bufnr
+ before_each(function()
+ target_bufnr = exec_lua [[
+ local bufnr = vim.fn.bufadd("fake/uri")
+ local lines = {"1st line of text", "2nd line of text"}
+ vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
+ return bufnr
+ ]]
+ end)
+ it('correctly goes ahead with the edit if all is normal', function()
+ local text_document_edit = {
+ edits = {
+ make_edit(0, 0, 0, 3, "First")
+ },
+ textDocument = {
+ uri = "file://fake/uri";
+ version = 5
+ }
+ }
+ exec_lua([[
+ local args = {...}
+ local target_bufnr = args[2]
+ vim.lsp.util.buf_versions[target_bufnr] = 4
+ vim.lsp.util.apply_text_document_edit(...)
+ ]], text_document_edit, target_bufnr)
+ eq({
+ 'First line of text';
+ '2nd line of text';
+ }, buf_lines(target_bufnr))
+ end)
+ it('correctly goes ahead with the edit if the version is vim.NIL', function()
+ -- we get vim.NIL when we decode json null value.
+ local json = exec_lua[[
+ return vim.fn.json_decode("{ \"a\": 1, \"b\": null }")
+ ]]
+ eq(json.b, exec_lua("return vim.NIL"))
+
+ local text_document_edit = {
+ edits = {
+ make_edit(0, 0, 0, 3, "First")
+ },
+ textDocument = {
+ uri = "file://fake/uri";
+ version = exec_lua("return vim.NIL")
+ }
+ }
+ exec_lua([[
+ local args = {...}
+ local target_bufnr = args[2]
+ vim.lsp.util.buf_versions[target_bufnr] = vim.NIL
+ vim.lsp.util.apply_text_document_edit(...)
+ ]], text_document_edit, target_bufnr)
+ eq({
+ 'First line of text';
+ '2nd line of text';
+ }, buf_lines(target_bufnr))
+ end)
+ it('skips the edit if the version of the edit is behind the local buffer ', function()
+ local text_document_edit = {
+ edits = {
+ make_edit(0, 0, 0, 3, "First")
+ },
+ textDocument = {
+ uri = "file://fake/uri";
+ version = 1
+ }
+ }
+ exec_lua([[
+ local args = {...}
+ local target_bufnr = args[2]
+ vim.lsp.util.buf_versions[target_bufnr] = 2
+ vim.lsp.util.apply_text_document_edit(...)
+ ]], text_document_edit, target_bufnr)
+ eq({
+ '1st line of text';
+ '2nd line of text';
+ }, buf_lines(target_bufnr))
+ end)
end)
describe('completion_list_to_complete_items', function()