autopairs.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- if not package.loaded['nvim-autopairs'] then
  2. -- return
  3. -- end
  4. local Log = require "core.log"
  5. local status_ok, _ = pcall(require, "nvim-autopairs")
  6. if not status_ok then
  7. Log:get_default().error "Failed to load autopairs"
  8. return
  9. end
  10. local npairs = require "nvim-autopairs"
  11. local Rule = require "nvim-autopairs.rule"
  12. -- skip it, if you use another global object
  13. _G.MUtils = {}
  14. vim.g.completion_confirm_key = ""
  15. MUtils.completion_confirm = function()
  16. if vim.fn.pumvisible() ~= 0 then
  17. if vim.fn.complete_info()["selected"] ~= -1 then
  18. return vim.fn["compe#confirm"](npairs.esc "<cr>")
  19. else
  20. return npairs.esc "<cr>"
  21. end
  22. else
  23. return npairs.autopairs_cr()
  24. end
  25. end
  26. if package.loaded["compe"] then
  27. local map_complete_optional = vim.bo.filetype ~= "tex"
  28. require("nvim-autopairs.completion.compe").setup {
  29. map_cr = true, -- map <CR> on insert mode
  30. map_complete = map_complete_optional, -- it will auto insert `(` after select function or method item
  31. }
  32. end
  33. npairs.setup {
  34. check_ts = true,
  35. ts_config = {
  36. lua = { "string" }, -- it will not add pair on that treesitter node
  37. javascript = { "template_string" },
  38. java = false, -- don't check treesitter on java
  39. },
  40. }
  41. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  42. local ts_conds = require "nvim-autopairs.ts-conds"
  43. -- press % => %% is only inside comment or string
  44. npairs.add_rules {
  45. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  46. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  47. }