autopairs.lua 2.4 KB

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