autopairs.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. local M = {}
  2. function M.config()
  3. lvim.builtin.autopairs = {
  4. active = true,
  5. on_config_done = nil,
  6. ---@usage -- modifies the function or method delimiter by filetypes
  7. map_char = {
  8. all = "(",
  9. tex = "{",
  10. },
  11. enable_check_bracket_line = false,
  12. ---@usage check treesitter
  13. check_ts = true,
  14. ts_config = {
  15. lua = { "string" },
  16. javascript = { "template_string" },
  17. java = false,
  18. },
  19. }
  20. end
  21. M.setup = function()
  22. local autopairs = require "nvim-autopairs"
  23. local Rule = require "nvim-autopairs.rule"
  24. local cond = require "nvim-autopairs.conds"
  25. autopairs.setup {
  26. check_ts = lvim.builtin.autopairs.check_ts,
  27. enable_check_bracket_line = lvim.builtin.autopairs.enable_check_bracket_line,
  28. ts_config = lvim.builtin.autopairs.ts_config,
  29. }
  30. -- vim.g.completion_confirm_key = ""
  31. autopairs.add_rule(Rule("$$", "$$", "tex"))
  32. autopairs.add_rules {
  33. Rule("$", "$", { "tex", "latex" }) -- don't add a pair if the next character is %
  34. :with_pair(cond.not_after_regex_check "%%") -- don't add a pair if the previous character is xxx
  35. :with_pair(cond.not_before_regex_check("xxx", 3)) -- don't move right when repeat character
  36. :with_move(cond.none()) -- don't delete if the next character is xx
  37. :with_del(cond.not_after_regex_check "xx") -- disable add newline when press <cr>
  38. :with_cr(cond.none()),
  39. }
  40. autopairs.add_rules {
  41. Rule("$$", "$$", "tex"):with_pair(function(opts)
  42. print(vim.inspect(opts))
  43. if opts.line == "aa $$" then
  44. -- don't add pair on that line
  45. return false
  46. end
  47. end),
  48. }
  49. local cmp_status_ok, cmp = pcall(require, "cmp")
  50. if cmp_status_ok then
  51. -- If you want insert `(` after select function or method item
  52. local cmp_autopairs = require "nvim-autopairs.completion.cmp"
  53. local map_char = lvim.builtin.autopairs.map_char
  54. cmp.event:on("confirm_done", cmp_autopairs.on_confirm_done { map_char = map_char })
  55. end
  56. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  57. local ts_conds = require "nvim-autopairs.ts-conds"
  58. -- TODO: can these rules be safely added from "config.lua" ?
  59. -- press % => %% is only inside comment or string
  60. autopairs.add_rules {
  61. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  62. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  63. }
  64. if lvim.builtin.autopairs.on_config_done then
  65. lvim.builtin.autopairs.on_config_done(autopairs)
  66. end
  67. end
  68. return M