nvim: refactor autopairs

This commit is contained in:
Primrose 2025-08-21 08:56:59 +02:00
parent 63074ebee6
commit e1a06f2bf8
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA

View file

@ -4,35 +4,25 @@ local npairs = require("nvim-autopairs")
local cmp = require("cmp")
local cmp_autopairs = require("nvim-autopairs.completion.cmp")
npairs.setup {
disable_filetype = { "fennel", "clojure", "lisp", "racket", "scheme" },
local langs = {
haskell_like = { "haskell", "nix" },
ml_like = { "ocaml", "why3", "skel", "isabelle" },
html_like = { "html", "htmldjango" },
latex_like = { "typst", "latex" },
lisps = { "fennel", "clojure", "lisp", "racket", "scheme" },
has_bracket = { "typst", "python", "haskell", "nix", "sh" },
c_style_block_comment = { "typst", "go", "java", "rust", "rust", "scss", "nix" },
all = nil,
}
npairs.setup {
disable_filetype = langs.lisps,
}
cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done())
for _, punct in pairs { ",", ";" } do
npairs.add_rule(
Rule("", punct)
:with_move(function(opts) return opts.char == punct end)
:with_pair(function() return false end)
:with_del(function() return false end)
:with_cr(function() return false end)
:use_key(punct)
)
end
local function double_trouble(opts, pattern) return select(2, opts.line:gsub(pattern, "")) % 2 == 0 end
npairs.add_rules {
Rule("$", "$", "typst")
:with_pair(function(opts) return double_trouble(opts, "%$") end)
:with_del(function(opts) return double_trouble(opts, "%$") end)
:with_move(cond.done()),
Rule("<", ">", { "html" })
:with_pair(function(opts) return double_trouble(opts, "[<>]") end)
:with_del(function(opts) return double_trouble(opts, "[<>]") end)
:with_move(function(opts) return opts.char == ">" end),
}
local function has_pair_on_line(opts, pattern) return select(2, opts.line:gsub(pattern, "")) % 2 == 0 end
local function pair_with_insertion(a1, ins, a2, lang)
npairs.add_rule(
@ -47,35 +37,59 @@ local function pair_with_insertion(a1, ins, a2, lang)
)
end
pair_with_insertion("(", "*", ")", { "ocaml", "why3", "skel", "isabelle" })
pair_with_insertion("(*", " ", "*)", { "ocaml", "why3", "skel", "isabelle" })
for _, punct in pairs { ",", ";" } do
npairs.add_rule(
Rule("", punct)
:with_move(function(opts) return opts.char == punct end)
:with_pair(function() return false end)
:with_del(function() return false end)
:with_cr(function() return false end)
:use_key(punct)
)
end
pair_with_insertion("[", " ", "]", { "typst", "python", "haskell", "nix", "sh" })
pair_with_insertion("{", " ", "}", nil)
pair_with_insertion("(", "*", ")", langs.ml_like)
pair_with_insertion("(*", " ", "*)", langs.ml_like)
pair_with_insertion("[", " ", "]", langs.has_bracket)
pair_with_insertion("{", " ", "}", langs.all)
npairs.add_rules {
Rule("/*", "*/", { "typst", "go", "rust", "scss", "nix" }):with_del(cond.done()):with_move(cond.none()), -- allow java doc
}
pair_with_insertion("/*", " ", "*/", { "typst", "go", "java", "rust", "scss", "nix" })
pair_with_insertion("/*", " ", "*/", langs.c_style_block_comment)
pair_with_insertion("{", "#", "}", { "html", "htmldjango" })
pair_with_insertion("{#", " ", "#}", { "html", "htmldjango" })
pair_with_insertion("{#", "-", "#}", { "html", "htmldjango" })
pair_with_insertion("{#-", " ", "-#}", { "html", "htmldjango" })
pair_with_insertion("{", "#", "}", langs.html_like)
pair_with_insertion("{#", " ", "#}", langs.html_like)
pair_with_insertion("{#", "-", "#}", langs.html_like)
pair_with_insertion("{#-", " ", "-#}", langs.html_like)
pair_with_insertion("{", "%", "}", { "html", "htmldjango" })
pair_with_insertion("{%", " ", "%}", { "html", "htmldjango" })
pair_with_insertion("{%", "-", "%}", { "html", "htmldjango" })
pair_with_insertion("{%-", " ", "-%}", { "html", "htmldjango" })
pair_with_insertion("{", "%", "}", langs.html_like)
pair_with_insertion("{%", " ", "%}", langs.html_like)
pair_with_insertion("{%", "-", "%}", langs.html_like)
pair_with_insertion("{%-", " ", "-%}", langs.html_like)
pair_with_insertion("{{", " ", "}}", { "html", "htmldjango" })
pair_with_insertion("{{", "-", "}}", { "html", "htmldjango" })
pair_with_insertion("{{-", " ", "-}}", { "html", "htmldjango" })
pair_with_insertion("{{", " ", "}}", langs.html_like)
pair_with_insertion("{{", "-", "}}", langs.html_like)
pair_with_insertion("{{-", " ", "-}}", langs.html_like)
pair_with_insertion("$", " ", "$", { "typst" })
npairs.add_rules {
Rule("$", "$", "typst")
:with_pair(function(opts) return has_pair_on_line(opts, "%$") end)
:with_del(function(opts) return has_pair_on_line(opts, "%$") end)
:with_move(cond.done()),
}
pair_with_insertion("$", " ", "$", langs.latex_like)
pair_with_insertion("{", "-", "}", { "haskell" })
pair_with_insertion("{-", " ", "-}", { "haskell" }) -- block comment
pair_with_insertion("{-", "#", "-}", { "haskell" }) -- language flags
pair_with_insertion("{-#", " ", "#-}", { "haskell" })
pair_with_insertion("(", " ", ")", { "haskell", "nix" })
pair_with_insertion("{", "-", "}", "haskell")
pair_with_insertion("{-", " ", "-}", "haskell") -- block comment
pair_with_insertion("{-", "#", "-}", "haskell") -- language flags
pair_with_insertion("{-#", " ", "#-}", "haskell")
pair_with_insertion("(", " ", ")", langs.haskell_like)
npairs.add_rules {
Rule("<", ">", langs.html_like)
:with_pair(function(opts) return has_pair_on_line(opts, "[<>]") end)
:with_del(function(opts) return has_pair_on_line(opts, "[<>]") end)
:with_move(function(opts) return opts.char == ">" end),
}