Skip to content

Commit

Permalink
Use commit_character from nvim-cmp event to avoid adding double par…
Browse files Browse the repository at this point in the history
…enthesis (#253)

* Pass "commit_character" from cmp event to handler

* Added commit_character check on '*' handler

So that when the commit_character is equal to the trigger character
`char` we just return. This avoid things like doulbe parenthesis being
added sometimes.

* Added `commit_character` to `lisp` handler
  • Loading branch information
dmbfm committed Jul 6, 2022
1 parent 4a95b39 commit 972a797
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lua/nvim-autopairs/completion/cmp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ M.on_confirm_done = function(opts)

return function(evt)
local entry = evt.entry
local commit_character = evt.commit_character
local bufnr = vim.api.nvim_get_current_buf()
local filetype = vim.api.nvim_buf_get_option(bufnr, 'filetype')
local item = entry:get_completion_item()
Expand All @@ -66,7 +67,7 @@ M.on_confirm_done = function(opts)

for char, value in pairs(completion_options) do
if vim.tbl_contains(value.kind, item.kind) then
value.handler(char, item, bufnr)
value.handler(char, item, bufnr, commit_character)
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions lua/nvim-autopairs/completion/handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local M = {}
---@param char string
---@param item table
---@param bufnr number
M["*"] = function(char, item, bufnr)
M["*"] = function(char, item, bufnr, commit_character)
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local char_before, char_after = utils.text_cusor_line(line, col, 1, 1, false)
Expand All @@ -15,6 +15,7 @@ M["*"] = function(char, item, bufnr)
or (item.data and item.data.funcParensDisabled)
or (item.textEdit and item.textEdit.newText and item.textEdit.newText:match "[%(%[%$]")
or (item.insertText and item.insertText:match "[%(%[%$]")
or char == commit_character
then
return
end
Expand All @@ -23,7 +24,7 @@ M["*"] = function(char, item, bufnr)
end

---Handler with "clojure", "clojurescript", "fennel", "janet
M.lisp = function (char, item, bufnr)
M.lisp = function (char, item, bufnr, commit_character)
local line = utils.text_get_current_line(bufnr)
local _, col = utils.get_cursor()
local char_before, char_after = utils.text_cusor_line(line, col, 1, 1, false)
Expand All @@ -32,7 +33,9 @@ M.lisp = function (char, item, bufnr)
if char == '' or char_before == char or char_after == char
or (item.data and item.data.funcParensDisabled)
or (item.textEdit and item.textEdit.newText and item.textEdit.newText:match "[%(%[%$]")
or (item.insertText and item.insertText:match "[%(%[%$]") then
or (item.insertText and item.insertText:match "[%(%[%$]")
or char == commit_character
then
return
end

Expand Down

0 comments on commit 972a797

Please sign in to comment.