autopairs.lua 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. local M = {}
  2. function M.config()
  3. lvim.builtin.autopairs = {
  4. active = true,
  5. ---@usage map <CR> on insert mode
  6. map_cr = true,
  7. ---@usage it will auto insert after select function or method item,
  8. map_complete = map_complete_optional,
  9. ---@usage check treesitter
  10. check_ts = true,
  11. ts_config = {
  12. lua = { "string" },
  13. javascript = { "template_string" },
  14. java = false,
  15. },
  16. }
  17. end
  18. M.setup = function()
  19. -- skip it, if you use another global object
  20. _G.MUtils = {}
  21. local Log = require "core.log"
  22. local npairs = require "nvim-autopairs"
  23. local Rule = require "nvim-autopairs.rule"
  24. vim.g.completion_confirm_key = ""
  25. MUtils.completion_confirm = function()
  26. if vim.fn.pumvisible() ~= 0 then
  27. if vim.fn.complete_info()["selected"] ~= -1 then
  28. return vim.fn["compe#confirm"](npairs.esc "<cr>")
  29. else
  30. return npairs.esc "<cr>"
  31. end
  32. else
  33. return npairs.autopairs_cr()
  34. end
  35. end
  36. if package.loaded["compe"] then
  37. local map_complete_optional = vim.bo.filetype ~= "tex"
  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. npairs.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. npairs.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. end
  56. return M