From c194a3dbba7b6d0118d9cbddc93d16a497651fc5 Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Mon, 15 Nov 2021 12:44:41 +0900 Subject: [PATCH] Add tests --- lua/cmp/core.lua | 21 ++++----- lua/cmp/core_spec.lua | 105 +++++++++++++++++++++++++++-------------- lua/cmp/utils/spec.lua | 2 +- 3 files changed, 80 insertions(+), 48 deletions(-) diff --git a/lua/cmp/core.lua b/lua/cmp/core.lua index ad981ad..6ee30c2 100644 --- a/lua/cmp/core.lua +++ b/lua/cmp/core.lua @@ -389,22 +389,21 @@ core.confirm = function(self, e, option, callback) completion_item.textEdit.newText = '' end vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit }) + local texts = vim.split(completion_item.textEdit.newText, '\n') + local position = completion_item.textEdit.range.start + position.line = position.line + (#texts - 1) + if #texts == 1 then + position.character = position.character + vim.str_utfindex(texts[1]) + else + position.character = vim.str_utfindex(texts[#texts]) + end + local pos = types.lsp.Position.to_vim(0, position) + vim.api.nvim_win_set_cursor(0, { pos.row, pos.col - 1 }) if is_snippet then config.get().snippet.expand({ body = new_text, insert_text_mode = completion_item.insertTextMode, }) - else - local texts = vim.split(completion_item.textEdit.newText, '\n') - local position = completion_item.textEdit.range.start - position.line = position.line + (#texts - 1) - if #texts == 1 then - position.character = position.character + vim.str_utfindex(texts[1]) - else - position.character = vim.str_utfindex(texts[#texts]) - end - local pos = types.lsp.Position.to_vim(0, position) - vim.api.nvim_win_set_cursor(0, { pos.row, pos.col - 1 }) end else local keys = {} diff --git a/lua/cmp/core_spec.lua b/lua/cmp/core_spec.lua index 1df5076..ae45c72 100644 --- a/lua/cmp/core_spec.lua +++ b/lua/cmp/core_spec.lua @@ -6,9 +6,7 @@ local source = require('cmp.source') local keymap = require('cmp.utils.keymap') describe('cmp.core', function() - describe('confirm #confirm', function() - before_each(spec.before) - + describe('confirm', function() local confirm = function(request, filter, completion_item) local c = core.new() local s = source.new('spec', { @@ -36,43 +34,78 @@ describe('cmp.core', function() return state end - it('label only', function() - local state = confirm('iA', 'IU', { - label = 'AIUEO', - }) - assert.are.same(state.buffer, { 'AIUEO' }) - assert.are.same(state.cursor, { 1, 5 }) - end) + describe('insert-mode', function() + before_each(spec.before) - it('text edit', function() - local state = confirm(keymap.t('i***AEO***'), 'IU', { - label = 'AIUEO', - textEdit = { - range = { - start = { - line = 0, - character = 3, - }, - ['end'] = { - line = 0, - character = 6, + it('label', function() + local state = confirm('iA', 'IU', { + label = 'AIUEO', + }) + assert.are.same(state.buffer, { 'AIUEO' }) + assert.are.same(state.cursor, { 1, 5 }) + end) + + it('insertText', function() + local state = confirm('iA', 'IU', { + label = 'AIUEO', + insertText = '_AIUEO_', + }) + assert.are.same(state.buffer, { '_AIUEO_' }) + assert.are.same(state.cursor, { 1, 7 }) + end) + + it('text edit', function() + local state = confirm(keymap.t('i***AEO***'), 'IU', { + label = 'AIUEO', + textEdit = { + range = { + start = { + line = 0, + character = 3, + }, + ['end'] = { + line = 0, + character = 6, + }, }, + newText = 'foo\nbar\nbaz', }, - newText = 'foo\nbar\nbaz', - }, - }) - assert.are.same(state.buffer, { '***foo', 'bar', 'baz***' }) - assert.are.same(state.cursor, { 3, 3 }) - end) + }) + assert.are.same(state.buffer, { '***foo', 'bar', 'baz***' }) + assert.are.same(state.cursor, { 3, 3 }) + end) - it('snippet', function() - local state = confirm('iA', 'IU', { - label = 'AIUEO', - insertText = 'AIUEO($0)', - insertTextFormat = types.lsp.InsertTextFormat.Snippet, - }) - assert.are.same(state.buffer, { 'AIUEO()' }) - assert.are.same(state.cursor, { 1, 6 }) + it('insertText & snippet', function() + local state = confirm('iA', 'IU', { + label = 'AIUEO', + insertText = 'AIUEO($0)', + insertTextFormat = types.lsp.InsertTextFormat.Snippet, + }) + assert.are.same(state.buffer, { 'AIUEO()' }) + assert.are.same(state.cursor, { 1, 6 }) + end) + + it('textEdit & snippet', function() + local state = confirm(keymap.t('i***AEO***'), 'IU', { + label = 'AIUEO', + insertTextFormat = types.lsp.InsertTextFormat.Snippet, + textEdit = { + range = { + start = { + line = 0, + character = 3, + }, + ['end'] = { + line = 0, + character = 6, + }, + }, + newText = 'foo\nba$0r\nbaz', + }, + }) + assert.are.same(state.buffer, { '***foo', 'bar', 'baz***' }) + assert.are.same(state.cursor, { 2, 2 }) + end) end) end) end) diff --git a/lua/cmp/utils/spec.lua b/lua/cmp/utils/spec.lua index 6996cc4..6fb49f6 100644 --- a/lua/cmp/utils/spec.lua +++ b/lua/cmp/utils/spec.lua @@ -37,7 +37,7 @@ spec.before = function() if i == 1 then vim.api.nvim_win_set_cursor(0, { ctx.cursor.row, ctx.cursor.col + s - 2 }) else - vim.api.nvim_win_set_cursor(0, { ctx.cursor.row, s - 1 }) + vim.api.nvim_win_set_cursor(0, { ctx.cursor.row + i - 1, s - 1 }) end break end