comment.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. local M = {}
  2. function M.config()
  3. local pre_hook = nil
  4. if lvim.builtin.treesitter.context_commentstring.enable then
  5. pre_hook = function(ctx)
  6. local U = require "Comment.utils"
  7. -- Determine whether to use linewise or blockwise commentstring
  8. local type = ctx.ctype == U.ctype.linewise and "__default" or "__multiline"
  9. -- Determine the location where to calculate commentstring from
  10. local location = nil
  11. if ctx.ctype == U.ctype.blockwise then
  12. location = require("ts_context_commentstring.utils").get_cursor_location()
  13. elseif ctx.cmotion == U.cmotion.v or ctx.cmotion == U.cmotion.V then
  14. location = require("ts_context_commentstring.utils").get_visual_start_location()
  15. end
  16. return require("ts_context_commentstring.internal").calculate_commentstring {
  17. key = type,
  18. location = location,
  19. }
  20. end
  21. end
  22. lvim.builtin.comment = {
  23. active = true,
  24. on_config_done = nil,
  25. ---Add a space b/w comment and the line
  26. ---@type boolean
  27. padding = true,
  28. ---Lines to be ignored while comment/uncomment.
  29. ---Could be a regex string or a function that returns a regex string.
  30. ---Example: Use '^$' to ignore empty lines
  31. ---@type string|function
  32. ignore = "^$",
  33. ---Whether to create basic (operator-pending) and extra mappings for NORMAL/VISUAL mode
  34. ---@type table
  35. mappings = {
  36. ---operator-pending mapping
  37. ---Includes `gcc`, `gcb`, `gc[count]{motion}` and `gb[count]{motion}`
  38. basic = true,
  39. ---Extra mapping
  40. ---Includes `gco`, `gcO`, `gcA`
  41. extra = true,
  42. ---Extended mapping
  43. ---Includes `g>`, `g<`, `g>[count]{motion}` and `g<[count]{motion}`
  44. extended = false,
  45. },
  46. ---LHS of line and block comment toggle mapping in NORMAL/VISUAL mode
  47. ---@type table
  48. toggler = {
  49. ---line-comment toggle
  50. line = "gcc",
  51. ---block-comment toggle
  52. block = "gbc",
  53. },
  54. ---LHS of line and block comment operator-mode mapping in NORMAL/VISUAL mode
  55. ---@type table
  56. opleader = {
  57. ---line-comment opfunc mapping
  58. line = "gc",
  59. ---block-comment opfunc mapping
  60. block = "gb",
  61. },
  62. ---Pre-hook, called before commenting the line
  63. ---@type function|nil
  64. pre_hook = pre_hook,
  65. ---Post-hook, called after commenting is done
  66. ---@type function|nil
  67. post_hook = nil,
  68. }
  69. end
  70. function M.setup()
  71. local nvim_comment = require "Comment"
  72. nvim_comment.setup(lvim.builtin.comment)
  73. if lvim.builtin.comment.on_config_done then
  74. lvim.builtin.comment.on_config_done(nvim_comment)
  75. end
  76. end
  77. return M