autopairs.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. local M = {}
  2. function M.config()
  3. lvim.builtin.autopairs = {
  4. active = true,
  5. on_config_done = nil,
  6. ---@usage map <CR> on insert mode
  7. map_cr = true,
  8. ---@usage auto insert after select function or method item
  9. -- NOTE: This should be wrapped into a function so that it is re-evaluated when opening new files
  10. map_complete = vim.bo.filetype ~= "tex",
  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. -- skip it, if you use another global object
  22. _G.MUtils = {}
  23. local autopairs = require "nvim-autopairs"
  24. local Rule = require "nvim-autopairs.rule"
  25. vim.g.completion_confirm_key = ""
  26. MUtils.completion_confirm = function()
  27. if vim.fn.pumvisible() ~= 0 then
  28. if vim.fn.complete_info()["selected"] ~= -1 then
  29. return vim.fn["compe#confirm"](autopairs.esc "<cr>")
  30. else
  31. return autopairs.esc "<cr>"
  32. end
  33. else
  34. return autopairs.autopairs_cr()
  35. end
  36. end
  37. if package.loaded["compe"] then
  38. require("nvim-autopairs.completion.compe").setup {
  39. map_cr = lvim.builtin.autopairs.map_cr,
  40. map_complete = lvim.builtin.autopairs.map_complete,
  41. }
  42. end
  43. autopairs.setup {
  44. check_ts = lvim.builtin.autopairs.check_ts,
  45. ts_config = lvim.builtin.autopairs.ts_config,
  46. }
  47. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  48. local ts_conds = require "nvim-autopairs.ts-conds"
  49. -- TODO: can these rules be safely added from "config.lua" ?
  50. -- press % => %% is only inside comment or string
  51. autopairs.add_rules {
  52. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  53. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  54. }
  55. if lvim.builtin.autopairs.on_config_done then
  56. lvim.builtin.autopairs.on_config_done(autopairs)
  57. end
  58. end
  59. return M