From f0af1b68b5bc3c14c9ca68900c3db145cd8de24a Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 12:43:57 +0300 Subject: [PATCH 1/9] feat(cpp <> pairs) auto add <> for templates,includes and templated types --- lua/nvim-autopairs/completion/cmp.lua | 58 ++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 5e8abf03..16220a79 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -52,7 +52,7 @@ M.on_confirm_done = function(opts) if evt.commit_character then return end - + local entry = evt.entry local commit_character = entry:get_commit_characters() local bufnr = vim.api.nvim_get_current_buf() @@ -83,4 +83,60 @@ M.on_confirm_done = function(opts) end end +M.cpp_pairs = 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 = vim.api.nvim_replace_termcodes(pairs .. "", true, false, true) + vim.api.nvim_feedkeys(pairs, "n", false) + end +end + +local cmp_config = require('cmp.config') +local cmp_comparetors = cmp_config.get().sorting.comparators +local kind = cmp.lsp.CompletionItemKind + +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 + +cmp.setup({ + sorting = { + comparators = { + cpp_sort_cmp, + unpack(cmp_comparetors), + } + } +}) + return M From 7e98d2b0ee7d0f198f8257654528fa7a3dc84744 Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 12:49:12 +0300 Subject: [PATCH 2/9] cleaned the code up a bit --- lua/nvim-autopairs/completion/cmp.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 16220a79..a07d8c57 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -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 @@ -105,14 +106,13 @@ M.cpp_pairs = function(evt) pairs = ' ' end pairs = pairs .. '<>' - pairs = vim.api.nvim_replace_termcodes(pairs .. "", true, false, true) + pairs = pairs .. utils.esc('') vim.api.nvim_feedkeys(pairs, "n", false) end end local cmp_config = require('cmp.config') local cmp_comparetors = cmp_config.get().sorting.comparators -local kind = cmp.lsp.CompletionItemKind local unpack = unpack or table.unpack local function cpp_sort_cmp(entry1, entry2) @@ -121,10 +121,10 @@ local function cpp_sort_cmp(entry1, entry2) if vim.o.filetype ~= "cpp" then return nil end - if kind1 == kind.Constructor and kind2 == kind.Class then + if kind1 == Kind.Constructor and kind2 == Kind.Class then return false end - if kind1 == kind.Class and kind2 == kind.Constructor then + if kind1 == Kind.Class and kind2 == Kind.Constructor then return true end return nil From 89930f9783be5233a85436454cb058a0ed17309e Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 12:53:11 +0300 Subject: [PATCH 3/9] docs --- doc/nvim-autopairs.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/nvim-autopairs.txt b/doc/nvim-autopairs.txt index ccc9bcdf..4186048d 100644 --- a/doc/nvim-autopairs.txt +++ b/doc/nvim-autopairs.txt @@ -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()) < From 238672724d58ac554c73074da938462c304c6968 Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 13:31:21 +0300 Subject: [PATCH 4/9] made the function call return the function --- lua/nvim-autopairs/completion/cmp.lua | 99 ++++++++++++++------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index a07d8c57..5aab6132 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -1,7 +1,6 @@ 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 @@ -53,7 +52,7 @@ M.on_confirm_done = function(opts) if evt.commit_character then return end - + local entry = evt.entry local commit_character = entry:get_commit_characters() local bufnr = vim.api.nvim_get_current_buf() @@ -84,59 +83,61 @@ M.on_confirm_done = function(opts) end end -M.cpp_pairs = function(evt) - if not (vim.o.filetype == "c" or vim.o.filetype == "cpp") then - return - end +M.cpp_pairs = function() + local cmp_config = require('cmp.config') + local cmp_comparetors = cmp_config.get().sorting.comparators - 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 + 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 - 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 = ' ' + cmp.setup({ + sorting = { + comparators = { + cpp_sort_cmp, + unpack(cmp_comparetors), + } + } + }) + 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 - pairs = pairs .. '<>' - pairs = pairs .. utils.esc('') - vim.api.nvim_feedkeys(pairs, "n", false) - end -end -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 + 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('') + vim.api.nvim_feedkeys(pairs, "n", false) + end + end end -cmp.setup({ - sorting = { - comparators = { - cpp_sort_cmp, - unpack(cmp_comparetors), - } - } -}) return M From 9e6381c6b6a0137f3ce9c1339788c8a61e9c5a1a Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 13:35:33 +0300 Subject: [PATCH 5/9] readded utils --- lua/nvim-autopairs/completion/cmp.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 5aab6132..7900a81f 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -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 From ac2fc3a4c15887bb68db4bd310f2f81537791ffb Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Sat, 14 Oct 2023 13:44:17 +0300 Subject: [PATCH 6/9] added a check to make sure the sorter doesn't get loaded twice --- lua/nvim-autopairs/completion/cmp.lua | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 7900a81f..22332221 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -51,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() @@ -62,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 * @@ -84,6 +84,7 @@ 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 @@ -103,15 +104,17 @@ M.cpp_pairs = function() end return nil end - - cmp.setup({ - sorting = { - comparators = { - cpp_sort_cmp, - unpack(cmp_comparetors), + if loaded_cpp_sort == false 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 From f9d364447d1d82e2bf0c1df15168475c57f74da5 Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Tue, 17 Oct 2023 13:01:14 +0300 Subject: [PATCH 7/9] fix undo blocks --- lua/nvim-autopairs/completion/cmp.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 22332221..331aeb68 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -137,8 +137,10 @@ M.cpp_pairs = function() pairs = ' ' end pairs = pairs .. '<>' - pairs = pairs .. utils.esc('') - vim.api.nvim_feedkeys(pairs, "n", false) + pairs = pairs .. utils.esc(utils.key.join_left) + OLD_lazyredraw = vim.o.lazyredraw + vim.o.lazyredraw = true + vim.api.nvim_feedkeys(pairs .. utils.esc("lua vim.o.lazyredraw = OLD_lazyredraw"),"i", false) end end end From 9b97afe07905f928006cf8ffbf161c8e4d71eebf Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Tue, 17 Oct 2023 13:07:07 +0300 Subject: [PATCH 8/9] remove the global variable --- lua/nvim-autopairs/completion/cmp.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index 331aeb68..f09b4a5b 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -138,9 +138,9 @@ M.cpp_pairs = function() end pairs = pairs .. '<>' pairs = pairs .. utils.esc(utils.key.join_left) - OLD_lazyredraw = vim.o.lazyredraw + local old_lazyredraw = vim.o.lazyredraw vim.o.lazyredraw = true - vim.api.nvim_feedkeys(pairs .. utils.esc("lua vim.o.lazyredraw = OLD_lazyredraw"),"i", false) + vim.api.nvim_feedkeys(pairs .. utils.esc("lua vim.o.lazyredraw =" .. (old_lazyredraw and "true" or "false") .. ""),"i", false) end end end From cad714ca82199500a5a93cfa79839a8a5b6f185e Mon Sep 17 00:00:00 2001 From: Sam-programs Date: Fri, 20 Oct 2023 09:06:49 +0300 Subject: [PATCH 9/9] nil check incase the user didn't setup cmp --- lua/nvim-autopairs/completion/cmp.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-autopairs/completion/cmp.lua b/lua/nvim-autopairs/completion/cmp.lua index f09b4a5b..b36859b3 100644 --- a/lua/nvim-autopairs/completion/cmp.lua +++ b/lua/nvim-autopairs/completion/cmp.lua @@ -104,7 +104,7 @@ M.cpp_pairs = function() end return nil end - if loaded_cpp_sort == false then + if loaded_cpp_sort == false and cmp_comparetors then cmp.setup({ sorting = { comparators = {