diff options
Diffstat (limited to 'test/functional/plugin/lsp_spec.lua')
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 179 |
1 files changed, 115 insertions, 64 deletions
diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 79f6ef9dd2..f41a5323a8 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -779,7 +779,7 @@ describe('LSP', function() Fourth line of text å å ɧ 汉语 ↥ 🤦 🦄]])) end) - it('applies apply simple edits', function() + it('applies simple edits', function() local edits = { make_edit(0, 0, 0, 0, {"123"}); make_edit(1, 0, 1, 1, {"2"}); @@ -818,10 +818,9 @@ describe('LSP', function() 'å å ɧ 汉语 ↥ 🤦 🦄'; }, buf_lines(1)) end) - pending('applies non-ASCII characters edits', function() - -- FIXME: We don't handle non-ASCII characters well in UTF-16 + it('applies non-ASCII characters edits', function() local edits = { - make_edit(4, 0, 4, 14, {"a a h"}); + make_edit(4, 3, 4, 4, {"ä"}); } exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) eq({ @@ -829,40 +828,61 @@ describe('LSP', function() 'Second line of text'; 'Third line of text'; 'Fourth line of text'; - 'a a h'; + 'å ä ɧ 汉语 ↥ 🤦 🦄'; }, buf_lines(1)) end) + + describe('with LSP end line after what Vim considers to be the end line', function() + it('applies edits when the last linebreak is considered a new line', function() + local edits = { + make_edit(0, 0, 5, 0, {"All replaced"}); + } + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + eq({'All replaced'}, buf_lines(1)) + end) + it('applies edits when the end line is 2 larger than vim\'s', function() + local edits = { + make_edit(0, 0, 6, 0, {"All replaced"}); + } + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + eq({'All replaced'}, buf_lines(1)) + end) + it('applies edits with a column offset', function() + local edits = { + make_edit(0, 0, 5, 2, {"All replaced"}); + } + exec_lua('vim.lsp.util.apply_text_edits(...)', edits, 1) + eq({'All replaced'}, buf_lines(1)) + end) + end) end) describe('apply_text_document_edit', function() local target_bufnr + local text_document_edit = function(editVersion) + return { + edits = { + make_edit(0, 0, 0, 3, "First ↥ 🤦 🦄") + }, + textDocument = { + uri = "file://fake/uri"; + version = editVersion + } + } + end before_each(function() target_bufnr = exec_lua [[ - local bufnr = vim.fn.bufadd("fake/uri") - local lines = {"1st line of text", "2nd line of text"} + local bufnr = vim.uri_to_bufnr("file://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) + exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit(5)) eq({ - 'First line of text'; - '2nd line of text'; + '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() @@ -872,49 +892,56 @@ describe('LSP', function() ]] 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) + exec_lua('vim.lsp.util.apply_text_document_edit(...)', text_document_edit(exec_lua("return vim.NIL"))) eq({ - 'First line of text'; - '2nd line of text'; + '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 - } + local apply_edit_mocking_current_version = function(edit, versionedBuf) + exec_lua([[ + local args = {...} + local versionedBuf = args[2] + vim.lsp.util.buf_versions[versionedBuf.bufnr] = versionedBuf.currentVersion + vim.lsp.util.apply_text_document_edit(...) + ]], edit, versionedBuf) + end + + local baseText = { + '1st line of text'; + '2nd line of 语text'; } - 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(baseText, buf_lines(target_bufnr)) + + -- Apply an edit for an old version, should skip + apply_edit_mocking_current_version(text_document_edit(2), {currentVersion=7; bufnr=target_bufnr}) + eq(baseText, buf_lines(target_bufnr)) -- no change + + -- Sanity check that next version to current does apply change + apply_edit_mocking_current_version(text_document_edit(8), {currentVersion=7; bufnr=target_bufnr}) eq({ - '1st line of text'; - '2nd line of text'; + 'First ↥ 🤦 🦄 line of text'; + '2nd line of 语text'; }, buf_lines(target_bufnr)) end) end) - + describe('workspace_apply_edit', function() + it('workspace/applyEdit returns ApplyWorkspaceEditResponse', function() + local expected = { + applied = true; + failureReason = nil; + } + eq(expected, exec_lua [[ + local apply_edit = { + label = nil; + edit = {}; + } + return vim.lsp.callbacks['workspace/applyEdit'](nil, nil, apply_edit) + ]]) + end) + end) describe('completion_list_to_complete_items', function() -- Completion option precedence: -- textEdit.newText > insertText > label @@ -934,12 +961,12 @@ describe('LSP', function() } local completion_list_items = {items=completion_list} local expected = { - { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label = 'foobar' } } } } }, - { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foobar', textEdit={} } } } } }, - { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar' } } } } }, - { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar', textEdit={} } } } } }, - { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foodar', textEdit={newText='foobar'} } } } } }, - { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = '', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', textEdit={newText='foobar'} } } } } }, + { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label = 'foobar' } } } } }, + { abbr = 'foobar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foobar', textEdit={} } } } } }, + { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar' } } } } }, + { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foobar', textEdit={} } } } } }, + { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', insertText='foodar', textEdit={newText='foobar'} } } } } }, + { abbr = 'foocar', dup = 1, empty = 1, icase = 1, info = ' ', kind = 'Unknown', menu = '', word = 'foobar', user_data = { nvim = { lsp = { completion_item = { label='foocar', textEdit={newText='foobar'} } } } } }, } eq(expected, exec_lua([[return vim.lsp.util.text_document_completion_list_to_complete_items(...)]], completion_list, prefix)) @@ -1239,4 +1266,28 @@ describe('LSP', function() ]]) end) end) + + describe('lsp.util._get_completion_item_kind_name', function() + describe('returns the name specified by protocol', function() + eq("Text", exec_lua("return vim.lsp.util._get_completion_item_kind_name(1)")) + eq("TypeParameter", exec_lua("return vim.lsp.util._get_completion_item_kind_name(25)")) + end) + describe('returns the name not specified by protocol', function() + eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(nil)")) + eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(vim.NIL)")) + eq("Unknown", exec_lua("return vim.lsp.util._get_completion_item_kind_name(1000)")) + end) + end) + + describe('lsp.util._get_symbol_kind_name', function() + describe('returns the name specified by protocol', function() + eq("File", exec_lua("return vim.lsp.util._get_symbol_kind_name(1)")) + eq("TypeParameter", exec_lua("return vim.lsp.util._get_symbol_kind_name(26)")) + end) + describe('returns the name not specified by protocol', function() + eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(nil)")) + eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(vim.NIL)")) + eq("Unknown", exec_lua("return vim.lsp.util._get_symbol_kind_name(1000)")) + end) + end) end) |