Skip to content

Instantly share code, notes, and snippets.

@sunjon
Created November 20, 2021 09:28
Show Gist options
  • Save sunjon/8d1fd69e0f243ad708790f6788afc7e2 to your computer and use it in GitHub Desktop.
Save sunjon/8d1fd69e0f243ad708790f6788afc7e2 to your computer and use it in GitHub Desktop.
local U = require "Comment.utils"
local VISUAL_LINE = U.cmotion["V"]
local min, max = math.min, math.max
require("Comment").setup {
opleader = {
-- line = "gc", -- TODO: had to switch these to achieve the ft-lua behaviour wanted, but messes up everything else
line = "gb",
-- block = "gb",
block = "gc",
},
ignore = function()
-- ignore empty lines for lua files
if vim.bo.filetype == "lua" then
return "^$"
end
end,
pre_hook = function(ctx)
-- determine whether to use linewise or blockwise commentstrng (only for linewise visual selections)
if vim.bo.filetype == "lua" and (ctx.cmotion == VISUAL_LINE) then
local diff = max(ctx.range.srow, ctx.range.erow) - min(ctx.range.srow, ctx.range.erow)
ctx.ctype = (diff > 0) and U.ctype.block or U.ctype.line
end
end,
}
@numToStr
Copy link

numToStr commented Nov 20, 2021

try this

pre_hook = function(ctx)
    -- determine whether to use linewise or blockwise commentstrng (only for linewise visual selections)
    if vim.bo.filetype == 'lua' and (ctx.cmotion == VISUAL_LINE) then
        local diff = max(ctx.range.srow, ctx.range.erow) - min(ctx.range.srow, ctx.range.erow)
        ctx.ctype = (diff > 0) and U.ctype.block or U.ctype.line
        return require('Comment.ft').calculate(ctx)
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment