Comments (14)
seconding support for newlines (it is a personal preference matter ofc)
(thanks for the hook code but would be nice to have it builtin support \n in commentstring)
from comment.nvim.
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
from comment.nvim.
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,
})
from comment.nvim.
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.
from comment.nvim.
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' }) --]]
from comment.nvim.
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 π .
from comment.nvim.
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)
from comment.nvim.
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
from comment.nvim.
Just curious, Why do you need config
in post_hook
?
from comment.nvim.
I thought I need it to local lcs, rcs = U.parse_cstr(cfg, ctx)
, but after reading the functions, it isn't needed.
from comment.nvim.
There is also U.unwrap_cstr()
that only deals with commentstring. You might wanna use that instead of U.parse_cstr()
from comment.nvim.
I already implemented that, see code above ;-)
from comment.nvim.
I need access the config to get cfg.padding
.
from comment.nvim.
Just call the following to get the config
require('Comment').get_config()
from comment.nvim.
Related Issues (20)
- How to support context filetype?
- Uses `//` as commentstring in html HOT 4
- NOT A BUG/ISSUE [a gen qs] HOT 2
- How to just do comment or uncomment?
- Willing to accept a PR if I add a setup option "default commentstring"? HOT 2
- comment config working on arch but not on linux (arch) HOT 1
- Retain visual mode after commenting HOT 1
- [BUG] When I try to comment linewise in visual mode, unrelated or extra code gets commented HOT 5
- Adding a FOSS license HOT 2
- Not working for typescript react HOT 1
- BUG: fails silently in absence of LuaJIT HOT 1
- ft.set() not working for blade files defaults to html comments HOT 1
- Support inline commenting of wrapped shell commands
- Toggler remap with <leader>/ not working HOT 2
- Feat request: Toggle code block by pressing a key in a single line (context aware)
- Feature request: Option to start comment before indentation
- Is this still needed with Neovim 0.10? HOT 3
- Comments inside Twig files are always using HTML syntax
- 注ι代η εεΊεΎζ ’ HOT 1
- Support for JSON5 filetype
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from comment.nvim.