浏览代码

[Feature] use `Comment.nvim` instead of `nvim-comment` (#1736)

Abouzar Parvan 3 年之前
父节点
当前提交
3dc24cd0c3
共有 3 个文件被更改,包括 56 次插入16 次删除
  1. 53 13
      lua/lvim/core/comment.lua
  2. 2 2
      lua/lvim/core/which-key.lua
  3. 1 1
      lua/lvim/plugins.lua

+ 53 - 13
lua/lvim/core/comment.lua

@@ -1,26 +1,66 @@
 local M = {}
 
 function M.config()
+  local pre_hook = nil
+  if lvim.builtin.treesitter.context_commentstring.enable then
+    pre_hook = function(_ctx)
+      return require("ts_context_commentstring.internal").calculate_commentstring()
+    end
+  end
   lvim.builtin.comment = {
     active = true,
     on_config_done = nil,
-    -- Linters prefer comment and line to have a space in between markers
-    marker_padding = true,
-    -- should comment out empty or whitespace only lines
-    comment_empty = false,
-    -- Should key mappings be created
-    create_mappings = true,
-    -- Normal mode mapping left hand side
-    line_mapping = "gcc",
-    -- Visual/Operator mapping left hand side
-    operator_mapping = "gc",
-    -- Hook function to call before commenting takes place
-    hook = nil,
+    ---Add a space b/w comment and the line
+    ---@type boolean
+    padding = true,
+
+    ---Lines to be ignored while comment/uncomment.
+    ---Could be a regex string or a function that returns a regex string.
+    ---Example: Use '^$' to ignore empty lines
+    ---@type string|function
+    ignore = "^$",
+
+    ---Whether to create basic (operator-pending) and extra mappings for NORMAL/VISUAL mode
+    ---@type table
+    mappings = {
+      ---operator-pending mapping
+      ---Includes `gcc`, `gcb`, `gc[count]{motion}` and `gb[count]{motion}`
+      basic = true,
+      ---extended mapping
+      ---Includes `g>`, `g<`, `g>[count]{motion}` and `g<[count]{motion}`
+      extra = false,
+    },
+
+    ---LHS of line and block comment toggle mapping in NORMAL/VISUAL mode
+    ---@type table
+    toggler = {
+      ---line-comment toggle
+      line = "gcc",
+      ---block-comment toggle
+      block = "gbc",
+    },
+
+    ---LHS of line and block comment operator-mode mapping in NORMAL/VISUAL mode
+    ---@type table
+    opleader = {
+      ---line-comment opfunc mapping
+      line = "gc",
+      ---block-comment opfunc mapping
+      block = "gb",
+    },
+
+    ---Pre-hook, called before commenting the line
+    ---@type function|nil
+    pre_hook = pre_hook,
+
+    ---Post-hook, called after commenting is done
+    ---@type function|nil
+    post_hook = nil,
   }
 end
 
 function M.setup()
-  local nvim_comment = require "nvim_comment"
+  local nvim_comment = require "Comment"
 
   nvim_comment.setup(lvim.builtin.comment)
   if lvim.builtin.comment.on_config_done then

+ 2 - 2
lua/lvim/core/which-key.lua

@@ -61,12 +61,12 @@ M.config = function()
     -- NOTE: Prefer using : over <cmd> as the latter avoids going back in normal-mode.
     -- see https://neovim.io/doc/user/map.html#:map-cmd
     vmappings = {
-      ["/"] = { ":CommentToggle<CR>", "Comment" },
+      ["/"] = { "<ESC><CMD>lua ___comment_gc(vim.fn.visualmode())<CR>", "Comment" },
     },
     mappings = {
       ["w"] = { "<cmd>w!<CR>", "Save" },
       ["q"] = { "<cmd>q!<CR>", "Quit" },
-      ["/"] = { "<cmd>CommentToggle<CR>", "Comment" },
+      ["/"] = { "<cmd>lua require('Comment').toggle()<CR>", "Comment" },
       ["c"] = { "<cmd>BufferClose!<CR>", "Close Buffer" },
       ["f"] = { "<cmd>Telescope find_files<CR>", "Find File" },
       ["h"] = { "<cmd>nohlsearch<CR>", "No Highlight" },

+ 1 - 1
lua/lvim/plugins.lua

@@ -102,7 +102,7 @@ return {
 
   -- Comments
   {
-    "terrortylor/nvim-comment",
+    "numToStr/Comment.nvim",
     event = "BufRead",
     config = function()
       require("lvim.core.comment").setup()