autopairs.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 auto insert after select function or method item
  8. -- NOTE: This should be wrapped into a function so that it is re-evaluated when opening new files
  9. map_complete = vim.bo.filetype ~= "tex",
  10. ---@usage check treesitter
  11. check_ts = true,
  12. ts_config = {
  13. lua = { "string" },
  14. javascript = { "template_string" },
  15. java = false,
  16. },
  17. }
  18. end
  19. M.setup = function()
  20. -- skip it, if you use another global object
  21. _G.MUtils = {}
  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. require("nvim-autopairs.completion.compe").setup {
  38. map_cr = lvim.builtin.autopairs.map_cr,
  39. map_complete = lvim.builtin.autopairs.map_complete,
  40. }
  41. end
  42. npairs.setup {
  43. check_ts = lvim.builtin.autopairs.check_ts,
  44. ts_config = lvim.builtin.autopairs.ts_config,
  45. }
  46. require("nvim-treesitter.configs").setup { autopairs = { enable = true } }
  47. local ts_conds = require "nvim-autopairs.ts-conds"
  48. -- TODO: can these rules be safely added from "config.lua" ?
  49. -- press % => %% is only inside comment or string
  50. npairs.add_rules {
  51. Rule("%", "%", "lua"):with_pair(ts_conds.is_ts_node { "string", "comment" }),
  52. Rule("$", "$", "lua"):with_pair(ts_conds.is_not_ts_node { "function" }),
  53. }
  54. end
  55. return M