autopairs.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. local M = {}
  2. function M.config()
  3. lvim.builtin.autopairs = {
  4. active = true,
  5. on_config_done = nil,
  6. ---@usage auto insert after select function or method item
  7. map_complete = true,
  8. ---@usage -- modifies the function or method delimiter by filetypes
  9. map_char = {
  10. all = "(",
  11. tex = "{",
  12. },
  13. ---@usage check treesitter
  14. check_ts = true,
  15. ts_config = {
  16. lua = { "string" },
  17. javascript = { "template_string" },
  18. java = false,
  19. },
  20. }
  21. end
  22. M.setup = function()
  23. local autopairs = require "nvim-autopairs"
  24. local Rule = require "nvim-autopairs.rule"
  25. local cond = require "nvim-autopairs.conds"
  26. autopairs.setup {
  27. check_ts = lvim.builtin.autopairs.check_ts,
  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. if package.loaded["cmp"] then
  50. require("nvim-autopairs.completion.cmp").setup {
  51. map_cr = false,
  52. map_complete = lvim.builtin.autopairs.map_complete,
  53. map_char = lvim.builtin.autopairs.map_char,
  54. }
  55. -- we map CR explicitly in cmp.lua but we still need to setup the autopairs CR keymap
  56. vim.api.nvim_set_keymap("i", "<CR>", "v:lua.MPairs.autopairs_cr()", { expr = true, noremap = true })
  57. end
  58. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  59. local ts_conds = require "nvim-autopairs.ts-conds"
  60. -- TODO: can these rules be safely added from "config.lua" ?
  61. -- press % => %% is only inside comment or string
  62. autopairs.add_rules {
  63. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  64. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  65. }
  66. if lvim.builtin.autopairs.on_config_done then
  67. lvim.builtin.autopairs.on_config_done(autopairs)
  68. end
  69. end
  70. return M