autopairs.lua 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 status_ok, autopairs = pcall(require, "nvim-autopairs")
  49. if not status_ok then
  50. return
  51. end
  52. local Rule = require "nvim-autopairs.rule"
  53. autopairs.setup {
  54. check_ts = lvim.builtin.autopairs.check_ts,
  55. enable_check_bracket_line = lvim.builtin.autopairs.enable_check_bracket_line,
  56. ts_config = lvim.builtin.autopairs.ts_config,
  57. disable_filetype = lvim.builtin.autopairs.disable_filetype,
  58. disable_in_macro = lvim.builtin.autopairs.disable_in_macro,
  59. ignored_next_char = lvim.builtin.autopairs.ignored_next_char,
  60. enable_moveright = lvim.builtin.autopairs.enable_moveright,
  61. enable_afterquote = lvim.builtin.autopairs.enable_afterquote,
  62. map_c_w = lvim.builtin.autopairs.map_c_w,
  63. map_bs = lvim.builtin.autopairs.map_bs,
  64. disable_in_visualblock = lvim.builtin.autopairs.disable_in_visualblock,
  65. fast_wrap = lvim.builtin.autopairs.fast_wrap,
  66. }
  67. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  68. local ts_conds = require "nvim-autopairs.ts-conds"
  69. -- TODO: can these rules be safely added from "config.lua" ?
  70. -- press % => %% is only inside comment or string
  71. autopairs.add_rules {
  72. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  73. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  74. }
  75. if lvim.builtin.autopairs.on_config_done then
  76. lvim.builtin.autopairs.on_config_done(autopairs)
  77. end
  78. pcall(function()
  79. local cmp_autopairs = require "nvim-autopairs.completion.cmp"
  80. require("cmp").event:on("confirm_done", cmp_autopairs.on_confirm_done())
  81. end)
  82. end
  83. return M