From 6dc8398944fd86038b07d77fcab92cd282555dee Mon Sep 17 00:00:00 2001 From: ckipp01 Date: Mon, 27 Apr 2020 10:55:06 +0200 Subject: [LSP] check for vim.NIL and add apply_text_document_edit tests --- runtime/lua/vim/lsp/util.lua | 2 +- test/functional/plugin/lsp_spec.lua | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index 68f3b35df3..82b9a0b3aa 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 fdbe45c09a..53530eb513 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -817,6 +817,62 @@ describe('LSP', function() end) end) + describe('apply_text_document_edit', function() + before_each(function() + insert(dedent([[ + First line of text + Second line of text]])) + end) + it('correctly goes ahead with the edit when all is normal', function() + local text_document_edit = { + edits = { + make_edit(0, 0, 0, 0, "hi") + }, + textDocument = { + uri = "file://fake/uri"; + version = 5 + } + } + exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) + eq({ + 'hiline of text'; + 'Second line of text'; + }, buf_lines(1)) + end) + it('correctly goes ahead with the edit whe the version is nil', function() + local text_document_edit = { + edits = { + make_edit(0, 0, 0, 0, "hi") + }, + textDocument = { + uri = "file://fake/uri"; + version = vim.NIL + } + } + exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) + eq({ + 'hiline of text'; + 'Second line of text'; + }, buf_lines(1)) + 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, 0, "hi") + }, + textDocument = { + uri = "file://fake/uri"; + version = 1 + } + } + exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) + eq({ + 'First line of text'; + 'Second line of text'; + }, buf_lines(1)) + end) + end) + describe('completion_list_to_complete_items', function() -- Completion option precedence: -- textEdit.newText > insertText > label -- cgit From 0107a194fa6a82c3d919af2e2b606eeb74bc2352 Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Sun, 3 May 2020 13:01:00 +0900 Subject: lsp: fix apply_text_document_edit test lsp.util.buf_versions must be set in advance. Use helper.insert to create an anonymous buffer, so create a named buffer for testing without using insert. --- test/functional/plugin/lsp_spec.lua | 67 +++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 53530eb513..94e85d6fe6 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -818,58 +818,83 @@ describe('LSP', function() end) describe('apply_text_document_edit', function() + local target_bufnr before_each(function() - insert(dedent([[ - First line of text - Second line of text]])) + 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 when all is normal', function() + it('correctly goes ahead with the edit if all is normal', function() local text_document_edit = { edits = { - make_edit(0, 0, 0, 0, "hi") + make_edit(0, 0, 0, 3, "First") }, textDocument = { uri = "file://fake/uri"; version = 5 } } - exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) + 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({ - 'hiline of text'; - 'Second line of text'; - }, buf_lines(1)) + 'First line of text'; + '2nd line of text'; + }, buf_lines(target_bufnr)) end) - it('correctly goes ahead with the edit whe the version is nil', function() + 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, 0, "hi") + make_edit(0, 0, 0, 3, "First") }, textDocument = { uri = "file://fake/uri"; - version = vim.NIL + version = exec_lua("return vim.NIL") } } - exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 1) + 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({ - 'hiline of text'; - 'Second line of text'; - }, buf_lines(1)) + '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, 0, "hi") + make_edit(0, 0, 0, 3, "First") }, textDocument = { uri = "file://fake/uri"; version = 1 } } - exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit, 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({ - 'First line of text'; - 'Second line of text'; - }, buf_lines(1)) + '1st line of text'; + '2nd line of text'; + }, buf_lines(target_bufnr)) end) end) -- cgit From 67634da71403737bd3f0c4c037b9ccf3382903ae Mon Sep 17 00:00:00 2001 From: Hirokazu Hata Date: Mon, 4 May 2020 09:05:16 +0900 Subject: lsp: add a lsp.util.apply_text_edits test(pending) We don't handle non-ASCII characters well in UTF-16. So I add a non-ASCII characters test case. --- test/functional/plugin/lsp_spec.lua | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 94e85d6fe6..63188a9b09 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,6 +815,21 @@ 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) -- cgit