Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cpp <> pairs) auto add <> for templates,includes and templated types #407

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/nvim-autopairs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Check readme.md on nvim-cmp repo.
'confirm_done',
cmp_autopairs.on_confirm_done()
)
-- if u want to insert <> pairs after templates, include and templated types
cmp.event:on('confirm_done', cmp_autopairs.cpp_pairs())
<


Expand Down
71 changes: 67 additions & 4 deletions lua/nvim-autopairs/completion/cmp.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local autopairs = require('nvim-autopairs')
local handlers = require('nvim-autopairs.completion.handlers')
local cmp = require('cmp')
local utils = require('nvim-autopairs.utils')

local Kind = cmp.lsp.CompletionItemKind

Expand Down Expand Up @@ -50,9 +51,9 @@ M.on_confirm_done = function(opts)

return function(evt)
if evt.commit_character then
return
return
end

local entry = evt.entry
local commit_character = entry:get_commit_characters()
local bufnr = vim.api.nvim_get_current_buf()
Expand All @@ -61,11 +62,11 @@ M.on_confirm_done = function(opts)

-- Without options and fallback
if not opts.filetypes[filetype] and not opts.filetypes["*"] then
return
return
end

if opts.filetypes[filetype] == false then
return
return
end

-- If filetype is nil then use *
Expand All @@ -83,4 +84,66 @@ M.on_confirm_done = function(opts)
end
end

local loaded_cpp_sort = false
M.cpp_pairs = function()
local cmp_config = require('cmp.config')
local cmp_comparetors = cmp_config.get().sorting.comparators

local unpack = unpack or table.unpack
local function cpp_sort_cmp(entry1, entry2)
local kind1 = entry1.completion_item.kind
local kind2 = entry2.completion_item.kind
if vim.o.filetype ~= "cpp" then
return nil
end
if kind1 == Kind.Constructor and kind2 == Kind.Class then
return false
end
if kind1 == Kind.Class and kind2 == Kind.Constructor then
return true
end
return nil
end
if loaded_cpp_sort == false and cmp_comparetors then
cmp.setup({
sorting = {
comparators = {
cpp_sort_cmp,
unpack(cmp_comparetors),
}
}
})
loaded_cpp_sort = true
end
return function(evt)
if not (vim.o.filetype == "c" or vim.o.filetype == "cpp") then
return
end

local c = vim.api.nvim_win_get_cursor(0)[2]
local line = vim.api.nvim_get_current_line()
if line:sub(c, c) == '>' then
return
end

local entry = evt.entry
local item = entry:get_completion_item()
local pairs = ''
local functionsig = item.label
if (functionsig:sub(#functionsig, #functionsig) == '>' or
functionsig == ' template')
then
if functionsig:sub(2, 8) == 'include' then
pairs = ' '
end
pairs = pairs .. '<>'
pairs = pairs .. utils.esc(utils.key.join_left)
local old_lazyredraw = vim.o.lazyredraw
vim.o.lazyredraw = true
vim.api.nvim_feedkeys(pairs .. utils.esc("<cmd>lua vim.o.lazyredraw =" .. (old_lazyredraw and "true" or "false") .. "<cr>"),"i", false)
end
end
end


return M
Loading