autopairs.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 bracket in same line
  12. enable_check_bracket_line = false,
  13. ---@usage check treesitter
  14. check_ts = true,
  15. ts_config = {
  16. lua = { "string", "source" },
  17. javascript = { "string", "template_string" },
  18. java = false,
  19. },
  20. disable_filetype = { "TelescopePrompt", "spectre_panel" },
  21. ignored_next_char = string.gsub([[ [%w%%%'%[%"%.] ]], "%s+", ""),
  22. enable_moveright = true,
  23. ---@usage disable when recording or executing a macro
  24. disable_in_macro = false,
  25. ---@usage add bracket pairs after quote
  26. enable_afterquote = true,
  27. ---@usage map the <BS> key
  28. map_bs = true,
  29. ---@usage map <c-w> to delete a pair if possible
  30. map_c_w = false,
  31. ---@usage disable when insert after visual block mode
  32. disable_in_visualblock = false,
  33. ---@usage change default fast_wrap
  34. fast_wrap = {
  35. map = "<M-e>",
  36. chars = { "{", "[", "(", '"', "'" },
  37. pattern = string.gsub([[ [%'%"%)%>%]%)%}%,] ]], "%s+", ""),
  38. offset = 0, -- Offset from pattern match
  39. end_key = "$",
  40. keys = "qwertyuiopzxcvbnmasdfghjkl",
  41. check_comma = true,
  42. highlight = "Search",
  43. highlight_grey = "Comment",
  44. },
  45. }
  46. end
  47. M.setup = function()
  48. local autopairs = require "nvim-autopairs"
  49. local Rule = require "nvim-autopairs.rule"
  50. local cond = require "nvim-autopairs.conds"
  51. autopairs.setup {
  52. check_ts = lvim.builtin.autopairs.check_ts,
  53. enable_check_bracket_line = lvim.builtin.autopairs.enable_check_bracket_line,
  54. ts_config = lvim.builtin.autopairs.ts_config,
  55. disable_filetype = lvim.builtin.autopairs.disable_filetype,
  56. disable_in_macro = lvim.builtin.autopairs.disable_in_macro,
  57. ignored_next_char = lvim.builtin.autopairs.ignored_next_char,
  58. enable_moveright = lvim.builtin.autopairs.enable_moveright,
  59. enable_afterquote = lvim.builtin.autopairs.enable_afterquote,
  60. map_c_w = lvim.builtin.autopairs.map_c_w,
  61. map_bs = lvim.builtin.autopairs.map_bs,
  62. disable_in_visualblock = lvim.builtin.autopairs.disable_in_visualblock,
  63. fast_wrap = lvim.builtin.autopairs.fast_wrap,
  64. }
  65. autopairs.add_rule(Rule("$$", "$$", "tex"))
  66. autopairs.add_rules {
  67. Rule("$", "$", { "tex", "latex" }) -- don't add a pair if the next character is %
  68. :with_pair(cond.not_after_regex_check "%%") -- don't add a pair if the previous character is xxx
  69. :with_pair(cond.not_before_regex_check("xxx", 3)) -- don't move right when repeat character
  70. :with_move(cond.none()) -- don't delete if the next character is xx
  71. :with_del(cond.not_after_regex_check "xx") -- disable add newline when press <cr>
  72. :with_cr(cond.none()),
  73. }
  74. autopairs.add_rules {
  75. Rule("$$", "$$", "tex"):with_pair(function(opts)
  76. print(vim.inspect(opts))
  77. if opts.line == "aa $$" then
  78. -- don't add pair on that line
  79. return false
  80. end
  81. end),
  82. }
  83. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  84. local ts_conds = require "nvim-autopairs.ts-conds"
  85. -- TODO: can these rules be safely added from "config.lua" ?
  86. -- press % => %% is only inside comment or string
  87. autopairs.add_rules {
  88. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  89. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  90. }
  91. if lvim.builtin.autopairs.on_config_done then
  92. lvim.builtin.autopairs.on_config_done(autopairs)
  93. end
  94. end
  95. return M