autopairs.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. autopairs.setup {
  51. check_ts = lvim.builtin.autopairs.check_ts,
  52. enable_check_bracket_line = lvim.builtin.autopairs.enable_check_bracket_line,
  53. ts_config = lvim.builtin.autopairs.ts_config,
  54. disable_filetype = lvim.builtin.autopairs.disable_filetype,
  55. disable_in_macro = lvim.builtin.autopairs.disable_in_macro,
  56. ignored_next_char = lvim.builtin.autopairs.ignored_next_char,
  57. enable_moveright = lvim.builtin.autopairs.enable_moveright,
  58. enable_afterquote = lvim.builtin.autopairs.enable_afterquote,
  59. map_c_w = lvim.builtin.autopairs.map_c_w,
  60. map_bs = lvim.builtin.autopairs.map_bs,
  61. disable_in_visualblock = lvim.builtin.autopairs.disable_in_visualblock,
  62. fast_wrap = lvim.builtin.autopairs.fast_wrap,
  63. }
  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