autopairs.lua 2.7 KB

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