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,12 +389,6 @@ 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 })
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 texts = vim.split(completion_item.textEdit.newText, '\n')
local position = completion_item.textEdit.range.start local position = completion_item.textEdit.range.start
position.line = position.line + (#texts - 1) position.line = position.line + (#texts - 1)
@@ -405,6 +399,11 @@ core.confirm = function(self, e, option, callback)
end end
local pos = types.lsp.Position.to_vim(0, position) local pos = types.lsp.Position.to_vim(0, position)
vim.api.nvim_win_set_cursor(0, { pos.row, pos.col - 1 }) 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,
})
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,7 +34,10 @@ describe('cmp.core', function()
return state return state
end end
it('label only', function() describe('insert-mode', function()
before_each(spec.before)
it('label', function()
local state = confirm('iA', 'IU', { local state = confirm('iA', 'IU', {
label = 'AIUEO', label = 'AIUEO',
}) })
@@ -44,6 +45,15 @@ describe('cmp.core', function()
assert.are.same(state.cursor, { 1, 5 }) assert.are.same(state.cursor, { 1, 5 })
end) 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() it('text edit', function()
local state = confirm(keymap.t('i***AEO***<Left><Left><Left><Left><Left>'), 'IU', { local state = confirm(keymap.t('i***AEO***<Left><Left><Left><Left><Left>'), 'IU', {
label = 'AIUEO', label = 'AIUEO',
@@ -65,7 +75,7 @@ describe('cmp.core', function()
assert.are.same(state.cursor, { 3, 3 }) assert.are.same(state.cursor, { 3, 3 })
end) 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)',
@@ -74,5 +84,28 @@ describe('cmp.core', function()
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) 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)

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