autopairs.lua 3.1 KB

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