Abort visual commenting if it makes no sense #199
Replies: 6 comments 4 replies
-
Very interesting use case. I tried and the following seems to work require('Comment').setup({
pre_hook = function(ctx)
local start_range = {
srow = ctx.range.srow,
erow = ctx.range.srow,
scol = ctx.range.scol,
ecol = ctx.range.scol,
}
local end_range = {
srow = ctx.range.erow,
erow = ctx.range.erow,
scol = ctx.range.ecol,
ecol = ctx.range.ecol,
}
ctx.range = start_range
local start_cstr = require('Comment.ft').calculate(ctx)
ctx.range = end_range
local end_cstr = require('Comment.ft').calculate(ctx)
return start_cstr == end_cstr and start_cstr or ''
end,
}) FYI, you don't need https://github.com/JoosepAlviste/nvim-ts-context-commentstring unless you need |
Beta Was this translation helpful? Give feedback.
-
Great snippet, it works nicely! The reaon I use <html>
<body>
<script>
let name = "raafat"
</script>
</body>
</html> Which after a while of linewise commenting turns into: <html>
<body>
<script>
<!-- // <!-- // <!-- // <!-- // let name = "raafat" --> --> --> -->
</script>
</body>
</html> The same happens with |
Beta Was this translation helpful? Give feedback.
-
You must be hitting this issue #62 (comment), which will, sadly, always remain a limitation |
Beta Was this translation helpful? Give feedback.
-
I see. |
Beta Was this translation helpful? Give feedback.
-
Hmmmm I am not sure as I don't use |
Beta Was this translation helpful? Give feedback.
-
I've asked the plugin author for a I ended up with this pre_hook = function(ctx)
local comment_utils = require 'Comment.utils'
local tsctxcs_utils = require 'ts_context_commentstring.utils'
local tsctxcs_internal = require 'ts_context_commentstring.internal'
local comment_strings = {}
local cs_type = ctx.ctype == comment_utils.ctype.line and '__default' or '__multiline'
local calc_cs = function (posision)
return tsctxcs_internal.calculate_commentstring({
key = cs_type,
location = posision
})
end
local abort_cs = function (msg)
vim.notify(msg)
return '%s'
end
-- visual commenting
if ctx.cmotion == comment_utils.cmotion.v or ctx.cmotion == comment_utils.cmotion.V then
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_start_location()))
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_end_location()))
-- block commenting
elseif ctx.ctype == comment_utils.ctype.block then
table.insert(comment_strings, calc_cs(tsctxcs_utils.get_cursor_location()))
-- linewise commenting
else
table.insert(comment_strings, calc_cs(nil))
end
-- compare comment_strings and return one
local last_cs = nil
for i, cs in ipairs(comment_strings) do
if i > 1 and cs ~= last_cs then
return abort_cs('Commenting aborted due to mismatching comment strings')
end
last_cs = cs
end
return last_cs
end It works great however:
Both of those issues are a result of not having an aborting mechanism in the Let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
Hey there, awesome plugin BTW!
This might not be strictly
Comment.nvim
specific but I'd love a pointer in the right direction.This issue is about nested filetype support so as the readme says I'm using nvim-ts-context-commentstring which integrates great with the readme pre_hook snippet
Consider the following example
Visually selecting lines 1 through 5 and commenting produces an unwanted mess:
My aim is to calculate the
commentstring
at the start and end of the visual selection and only comment if they match.I've found out that returning
%s
in thepre_hook
results in "aborting" (is there a proper way to do this?).Let me know what you think.
Beta Was this translation helpful? Give feedback.
All reactions