From 01d7f996d8b2453b540dee3a561ca1dd17f9229c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20La=C3=ADn?= Date: Tue, 28 Nov 2023 13:23:09 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(nvim):=20added=20new=20rules?= =?UTF-8?q?=20to=20nvim-autopairs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/extras/coding/cmp/autopairs.lua | 117 +++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/.config/nvim/lua/plugins/extras/coding/cmp/autopairs.lua b/.config/nvim/lua/plugins/extras/coding/cmp/autopairs.lua index 6fc06077..97b4a3c0 100644 --- a/.config/nvim/lua/plugins/extras/coding/cmp/autopairs.lua +++ b/.config/nvim/lua/plugins/extras/coding/cmp/autopairs.lua @@ -8,15 +8,107 @@ return { opts = function() local cmp = require("cmp") local cmp_autopairs = require("nvim-autopairs.completion.cmp") - local Rule = require("nvim-autopairs.rule") + local ts_utils = require("nvim-treesitter.ts_utils") + local ts_node_func_parens_disabled = { + -- ecma + named_imports = true, + -- rust + use_declaration = true, + } + local npairs = require("nvim-autopairs") + local Rule = require("nvim-autopairs.rule") local cond = require("nvim-autopairs.conds") + local brackets = { { "(", ")" }, { "[", "]" }, { "{", "}" } } + + local get_closing_for_line = function(line) + local i = -1 + local clo = "" + + while true do + i, _ = string.find(line, "[%(%)%{%}%[%]]", i + 1) + if i == nil then + break + end + local ch = string.sub(line, i, i) + local st = string.sub(clo, 1, 1) + + if ch == "{" then + clo = "}" .. clo + elseif ch == "}" then + if st ~= "}" then + return "" + end + clo = string.sub(clo, 2) + elseif ch == "(" then + clo = ")" .. clo + elseif ch == ")" then + if st ~= ")" then + return "" + end + clo = string.sub(clo, 2) + elseif ch == "[" then + clo = "]" .. clo + elseif ch == "]" then + if st ~= "]" then + return "" + end + clo = string.sub(clo, 2) + end + end + + return clo + end + + local default_handler = cmp_autopairs.filetypes["*"]["("].handler + cmp_autopairs.filetypes["*"]["("].handler = function(char, item, bufnr, rules, commit_character) + local node_type = ts_utils.get_node_at_cursor():type() + if ts_node_func_parens_disabled[node_type] then + if item.data then + item.data.funcParensDisabled = true + else + char = "" + end + end + default_handler(char, item, bufnr, rules, commit_character) + end + + cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) npairs.add_rules({ + -- Rule for a pair with left-side ' ' and right side ' ' + Rule(" ", " ") + -- Pair will only occur if the conditional function returns true + :with_pair(function(opts) + -- We are checking if we are inserting a space in (), [], or {} + local pair = opts.line:sub(opts.col - 1, opts.col) + return vim.tbl_contains({ + brackets[1][1] .. brackets[1][2], + brackets[2][1] .. brackets[2][2], + brackets[3][1] .. brackets[3][2], + }, pair) + end) + :with_move(cond.none()) + :with_cr(cond.none()) + -- We only want to delete the pair of spaces when the cursor is as such: ( | ) + :with_del( + function(opts) + local col = vim.api.nvim_win_get_cursor(0)[2] + local context = opts.line:sub(col - 1, col + 2) + return vim.tbl_contains({ + brackets[1][1] .. " " .. brackets[1][2], + brackets[2][1] .. " " .. brackets[2][2], + brackets[3][1] .. " " .. brackets[3][2], + }, context) + end + ), + + -- Javascript arrow key Rule("%(.*%)%s*%=>$", " { }", { "typescript", "typescriptreact", "javascript" }) :use_regex(true) :set_end_pair_length(2), + -- Auto addspace on = Rule("=", "") :with_pair(cond.not_inside_quote()) :with_pair(function(opts) @@ -45,7 +137,28 @@ return { :with_move(cond.none()) :with_del(cond.none()), }) - cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) + + -- For each pair of brackets we will add another rule + for _, bracket in pairs(brackets) do + npairs.add_rules({ + -- Each of these rules is for a pair with left-side '( ' and right-side ' )' for each bracket type + Rule(bracket[1] .. " ", " " .. bracket[2]) + :with_pair(cond.none()) + :with_move(function(opts) + return opts.char == bracket[2] + end) + :with_del(cond.none()) + :use_key(bracket[2]) + -- Removes the trailing whitespace that can occur without this + :replace_map_cr(function(_) + return "2xiO" + end), + }) + end end, }, + { + "echasnovski/mini.pairs", + enabled = false, + }, }