-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE] Allow to use a newline instead of padding #38
Comments
To be honest, I won't be adding any special support for any specific language. And I don't think new lines in block comments would work like you showed. I tried the following and it worked for me. ft.c = { '/*%s*/', '#if 0%s#endif' }
-- or
ft.set('c', { '/*%s*/', '#if 0%s#endif' }) If you need the desired result you should add an empty line above and below the code and then do the comments. 2021-10-16.20-34-24.mp4 |
You don't have to support a special language, but allow to set it in the filetype config, like: ft.set({
lang = 'c',
line = { '/*%s*/' },
block = {
comment = '#if 0%s#endif',
padding = false,
newline = true,
}) |
I think you are mixing block comments with something else. And your use case is totally different and have nothing to do with comments. I hope you know that. |
In the end it comes down to insert a newline instead of some padding. It might be that you want that, that you have: --[[
ft.set('c', { '/*%s*/', '#if 0%s#endif' })
ft.set('cpp', { '/*%s*/', '#if 0%s#endif' })
--]] Currently you get: --[[ ft.set('c', { '/*%s*/', '#if 0%s#endif' })
ft.set('cpp', { '/*%s*/', '#if 0%s#endif' }) --]] |
The problem with new lines is that the plugin is now responsible for adding and removing newlines. And honestly, I don't want to deal with newlines for now 😐 . |
To cover your original use case. I made some helpers for you. This is not perfect but it's the best I could do. local Op = require('Comment.opfunc')
local U = require('Comment.utils')
local A = vim.api
local if_def = '#if 0%s#endif'
function _G.___comment_ifdef(vmode, uncomment)
local srow, erow = U.get_region(vmode)
if uncomment then
A.nvim_buf_set_lines(0, srow - 1, srow, false, { '' })
A.nvim_buf_set_lines(0, erow - 1, erow, false, { '' })
return
end
local new_srow = srow - 1 -- moving one line upwords
local new_erow1, new_erow2 = erow + 1, erow + 2 -- moving one line downwards
A.nvim_buf_set_lines(0, new_srow, new_srow, false, { '' })
A.nvim_buf_set_lines(0, new_erow1, new_erow1, false, { '' })
local lines = {
A.nvim_buf_get_lines(0, new_srow, srow, false)[1],
A.nvim_buf_get_lines(0, new_erow1, new_erow2, false)[1],
}
local lcs, rcs = U.unwrap_cstr(if_def)
Op.blockwise({
cfg = { padding = true },
cmode = U.cmode.comment,
lcs = lcs,
rcs = rcs,
srow = srow,
erow = new_erow2,
lines = lines,
})
end
local opts = { noremap = true, silent = true }
-- This is only for comment. Supports dot-repeat
-- Example: gla{ (comment around curly bracket)
A.nvim_set_keymap('n', 'gl', '<CMD>set operatorfunc=v:lua.___comment_ifdef<CR>g@', opts)
-- Comment/Uncomment in visual mode
A.nvim_set_keymap('x', 'gl', '<ESC><CMD>lua ___comment_ifdef(vim.fn.visualmode(), false)<CR>', opts)
A.nvim_set_keymap('x', 'gL', '<ESC><CMD>lua ___comment_ifdef(vim.fn.visualmode(), true)<CR>', opts) |
I've implemented it using a post hook now: local A = vim.api
local C = require('Comment.config'):new()
local U = require('Comment.utils')
local ft = require('Comment.ft')
ft.set('c', {'/*%s*/', '#if 0%s#endif'})
ft.set('cpp', {'/*%s*/', '#if 0%s#endif'})
local post_hook = function(ctx)
if vim.bo.filetype ~= 'c' and vim.bo.filetype ~= 'cpp' and vim.bo.filetype ~= 'lua' then
return
end
if ctx.range.srow == -1 then
return
end
if ctx.ctype == 1 then
return
end
local cfg = C:get()
local cstr = ft.get(vim.bo.filetype, ctx.ctype)
local lcs, rcs = U.unwrap_cstr(cstr)
local padding = U.get_padding(cfg.padding)
local lines = A.nvim_buf_get_lines(0, ctx.range.srow - 1, ctx.range.erow, false)
if ctx.cmode == 1 then
-- comment
local str = lines[1]
local i, j = string.find(str, lcs .. padding)
lines[1] = string.sub(str, i, j - #padding)
table.insert(lines, 2, string.sub(str, 0, i - 1) .. string.sub(str, j + #padding, #str))
str = lines[#lines]
i, j = string.find(str, rcs)
lines[#lines] = string.sub(str, 0, i - #padding - 1)
table.insert(lines, #lines + 1, string.sub(str, i, j))
elseif ctx.cmode == 2 then
-- uncomment
if #lines[1] == 0 and #lines[#lines] == 0 then
table.remove(lines, 1)
table.remove(lines, #lines)
end
end
vim.api.nvim_buf_set_lines(0, ctx.range.srow - 1, ctx.range.erow, false, lines)
end
require('Comment').setup({
post_hook = post_hook,
})
The result looks like: #if 0
/* main */
int main(void)
{
return 0;
}
#endif |
Just curious, Why do you need |
I thought I need it to |
There is also |
I already implemented that, see code above ;-) |
I need access the config to get |
Just call the following to get the config require('Comment').get_config() |
seconding support for newlines (it is a personal preference matter ofc) |
Hi,
it would be great to have support to comment out code blocks in C using
#if 0
, like:The reason is that also comment blocks can be commented out. Sadly the following doesn't work (yet):
Thanks!
The text was updated successfully, but these errors were encountered: