From 9280fd9e4566bd4c118c88d85e948a2837fd86be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20La=C3=ADn?= Date: Thu, 16 Nov 2023 02:19:20 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(nvim):=20autopairs=20extra?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lua/plugins/extras/coding/autopairs.lua | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .config/nvim/lua/plugins/extras/coding/autopairs.lua diff --git a/.config/nvim/lua/plugins/extras/coding/autopairs.lua b/.config/nvim/lua/plugins/extras/coding/autopairs.lua new file mode 100644 index 00000000..6fc06077 --- /dev/null +++ b/.config/nvim/lua/plugins/extras/coding/autopairs.lua @@ -0,0 +1,51 @@ +return { + { + "hrsh7th/nvim-cmp", + dependencies = { + "windwp/nvim-autopairs", + opts = {}, + }, + opts = function() + local cmp = require("cmp") + local cmp_autopairs = require("nvim-autopairs.completion.cmp") + local Rule = require("nvim-autopairs.rule") + local npairs = require("nvim-autopairs") + local cond = require("nvim-autopairs.conds") + + npairs.add_rules({ + Rule("%(.*%)%s*%=>$", " { }", { "typescript", "typescriptreact", "javascript" }) + :use_regex(true) + :set_end_pair_length(2), + + Rule("=", "") + :with_pair(cond.not_inside_quote()) + :with_pair(function(opts) + local last_char = opts.line:sub(opts.col - 1, opts.col - 1) + if last_char:match("[%w%=%s]") then + return true + end + return false + end) + :replace_endpair(function(opts) + local prev_2char = opts.line:sub(opts.col - 2, opts.col - 1) + local next_char = opts.line:sub(opts.col, opts.col) + next_char = next_char == " " and "" or " " + if prev_2char:match("%w$") then + return " =" .. next_char + end + if prev_2char:match("%=$") then + return next_char + end + if prev_2char:match("=") then + return "=" .. next_char + end + return "" + end) + :set_end_pair_length(0) + :with_move(cond.none()) + :with_del(cond.none()), + }) + cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done()) + end, + }, +}