Add tests

This commit is contained in:
hrsh7th
2021-11-15 12:44:41 +09:00
parent 753f5b7c92
commit c194a3dbba
3 changed files with 80 additions and 48 deletions

View File

@@ -389,22 +389,21 @@ core.confirm = function(self, e, option, callback)
completion_item.textEdit.newText = '' completion_item.textEdit.newText = ''
end end
vim.fn['cmp#apply_text_edits'](ctx.bufnr, { completion_item.textEdit }) 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 if is_snippet then
config.get().snippet.expand({ config.get().snippet.expand({
body = new_text, body = new_text,
insert_text_mode = completion_item.insertTextMode, 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 end
else else
local keys = {} local keys = {}

View File

@@ -6,9 +6,7 @@ local source = require('cmp.source')
local keymap = require('cmp.utils.keymap') local keymap = require('cmp.utils.keymap')
describe('cmp.core', function() describe('cmp.core', function()
describe('confirm #confirm', function() describe('confirm', function()
before_each(spec.before)
local confirm = function(request, filter, completion_item) local confirm = function(request, filter, completion_item)
local c = core.new() local c = core.new()
local s = source.new('spec', { local s = source.new('spec', {
@@ -36,43 +34,78 @@ describe('cmp.core', function()
return state return state
end end
it('label only', function() describe('insert-mode', function()
local state = confirm('iA', 'IU', { before_each(spec.before)
label = 'AIUEO',
})
assert.are.same(state.buffer, { 'AIUEO' })
assert.are.same(state.cursor, { 1, 5 })
end)
it('text edit', function() it('label', function()
local state = confirm(keymap.t('i***AEO***<Left><Left><Left><Left><Left>'), 'IU', { local state = confirm('iA', 'IU', {
label = 'AIUEO', label = 'AIUEO',
textEdit = { })
range = { assert.are.same(state.buffer, { 'AIUEO' })
start = { assert.are.same(state.cursor, { 1, 5 })
line = 0, end)
character = 3,
}, it('insertText', function()
['end'] = { local state = confirm('iA', 'IU', {
line = 0, label = 'AIUEO',
character = 6, 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***<Left><Left><Left><Left><Left>'), '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 })
assert.are.same(state.buffer, { '***foo', 'bar', 'baz***' }) end)
assert.are.same(state.cursor, { 3, 3 })
end)
it('snippet', function() it('insertText & snippet', function()
local state = confirm('iA', 'IU', { local state = confirm('iA', 'IU', {
label = 'AIUEO', label = 'AIUEO',
insertText = 'AIUEO($0)', insertText = 'AIUEO($0)',
insertTextFormat = types.lsp.InsertTextFormat.Snippet, insertTextFormat = types.lsp.InsertTextFormat.Snippet,
}) })
assert.are.same(state.buffer, { 'AIUEO()' }) assert.are.same(state.buffer, { 'AIUEO()' })
assert.are.same(state.cursor, { 1, 6 }) assert.are.same(state.cursor, { 1, 6 })
end)
it('textEdit & snippet', function()
local state = confirm(keymap.t('i***AEO***<Left><Left><Left><Left><Left>'), '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) end)
end) end)

View File

@@ -37,7 +37,7 @@ spec.before = function()
if i == 1 then if i == 1 then
vim.api.nvim_win_set_cursor(0, { ctx.cursor.row, ctx.cursor.col + s - 2 }) vim.api.nvim_win_set_cursor(0, { ctx.cursor.row, ctx.cursor.col + s - 2 })
else 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 end
break break
end end