Skip to content

Commit

Permalink
fix: avoid expand snippet when not trigger completion done manually
Browse files Browse the repository at this point in the history
  • Loading branch information
haorenW1025 committed Apr 10, 2020
1 parent 1427e15 commit 3bad19d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doc/tags
.vim
4 changes: 1 addition & 3 deletions autoload/completion.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
" Perform a Hack to confirm completion
function! completion#completion_confirm() abort
lua require'completion'.toggleConfirm()
call nvim_input("<C-Y>")
if g:completion_enable_auto_paren == 1
lua require'completion'.autoAddParens()
endif
endfunction

function! completion#wrap_completion() abort
Expand Down
40 changes: 26 additions & 14 deletions lua/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ local api = vim.api
local util = require 'utility'
local source = require 'source'
local signature = require'signature_help'
local hover = require'hover'
local M = {}

------------------------------------------------------------------------
-- local function --
------------------------------------------------------------------------

M.completionConfirm = false

-- Manager variable to keep all state accross completion
local manager = {
insertChar = false,
Expand Down Expand Up @@ -52,7 +55,7 @@ local autoOpenHoverInPopup = function(bufnr)
if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
api.nvim_win_close(M.winnr, true)
end
M.winner = nil
M.winnr = nil
end
if manager.textHover == true and item['selected'] ~= -1 then
if item['selected'] == -2 then
Expand Down Expand Up @@ -117,24 +120,33 @@ end
-- member function --
------------------------------------------------------------------------

function M.autoAddParens()
local complete_info = vim.fn.complete_info()
local complete_items = complete_info['items']
local index = complete_info['selected']
if index < 0 then return end
local complete_item = complete_items[index+1]
function M.autoAddParens(complete_item)
if complete_item.kind == nil then return end
if string.match(complete_item.kind, '.*Function.*') ~= nil or string.match(complete_item.kind, '.*Method.*') then
api.nvim_input("()<ESC>i")
end
end

-- Workaround to avoid expand snippets when not confirm
-- confirmCompletion is now triggered by CompleteDone autocmd to solve issue with noselect
-- Will cause snippets to expand with not pressing confirm key
-- Add a flag completionConfirm to avoid this issue
function M.toggleConfirm()
M.completionConfirm = true
end

function M.confirmCompletion()
local complete_item = api.nvim_get_vvar('completed_item')
if complete_item.kind == 'UltiSnips' then
api.nvim_call_function('UltiSnips#ExpandSnippet', {})
elseif complete_item.kind == 'Neosnippet' then
api.nvim_input("<c-r>".."=neosnippet#expand('"..complete_item.word.."')".."<CR>")
if M.completionConfirm == true then
local complete_item = api.nvim_get_vvar('completed_item')
if api.nvim_get_var('completion_enable_auto_paren') then
M.autoAddParens(complete_item)
end
if complete_item.kind == 'UltiSnips' then
api.nvim_call_function('UltiSnips#ExpandSnippet', {})
elseif complete_item.kind == 'Neosnippet' then
api.nvim_input("<c-r>".."=neosnippet#expand('"..complete_item.word.."')".."<CR>")
end
M.completionConfirm = false
end
if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
api.nvim_win_close(M.winnr, true)
Expand Down Expand Up @@ -244,12 +256,12 @@ M.completionToggle = function()
end

M.on_attach = function()
require 'hover'.modifyCallback()
hover.modifyCallback()
api.nvim_command [[augroup CompletionCommand]]
api.nvim_command("autocmd InsertEnter <buffer> lua require'completion'.on_InsertEnter()")
api.nvim_command("autocmd InsertLeave <buffer> lua require'completion'.on_InsertLeave()")
api.nvim_command("autocmd InsertCharPre <buffer> lua require'completion'.on_InsertCharPre()")
api.nvim_command("autocmd CompleteDonePre <buffer> lua require'completion'.confirmCompletion()")
api.nvim_command("autocmd CompleteDone <buffer> lua require'completion'.confirmCompletion()")
api.nvim_command [[augroup end]]
api.nvim_buf_set_keymap(0, 'i', api.nvim_get_var('completion_confirm_key'),
'<cmd>call completion#wrap_completion()<CR>', {silent=true, noremap=true})
Expand Down
8 changes: 4 additions & 4 deletions lua/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ local function find_window_by_var(name, value)
end
end

local function focusable_float(unique_name, fn)
M.focusable_float = function(unique_name, fn)
if npcall(api.nvim_win_get_var, 0, unique_name) then
return api.nvim_command("wincmd p")
end
Expand Down Expand Up @@ -87,7 +87,7 @@ local make_floating_popup_options = function(width, height, opts)
}
end

local fancy_floating_markdown = function(contents, opts)
M.fancy_floating_markdown = function(contents, opts)
local pad_left = opts and opts.pad_left
local pad_right = opts and opts.pad_right
local stripped = {}
Expand Down Expand Up @@ -228,7 +228,7 @@ function M.modifyCallback()
-- if M.winnr ~= nil and api.nvim_win_is_valid(M.winnr) then
-- api.nvim_win_close(M.winnr, true)
-- end
focusable_float(method, function()
M.focusable_float(method, function()
if not (result and result.contents) then
-- return { 'No information available' }
return
Expand All @@ -252,7 +252,7 @@ function M.modifyCallback()
align = 'left'
end

bufnr, winnr = fancy_floating_markdown(markdown_lines, {
bufnr, winnr = M.fancy_floating_markdown(markdown_lines, {
pad_left = 1; pad_right = 1;
col = position['col']; width = position['width']; row = position['row']-1;
align = align
Expand Down

0 comments on commit 3bad19d

Please sign in to comment.