Skip to content
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]: adding vim-commentary's gcgc? #22

Open
xeluxee opened this issue Oct 10, 2021 · 12 comments
Open

[Feature]: adding vim-commentary's gcgc? #22

xeluxee opened this issue Oct 10, 2021 · 12 comments
Labels
feature New feature

Comments

@xeluxee
Copy link

xeluxee commented Oct 10, 2021

vim-commentary has a normal mode gcgc keybinding to uncomment a block of commented lines.

Before ↓

printf("Hello");
// printf("Hello");
// printf("Hello");
printf("Hello");

After pressing gcgc with cursor on line 2 or 3 ↓

printf("Hello");
printf("Hello");
printf("Hello");
printf("Hello");

Also gbgb would be really useful to delete block comment without having to select it manually

@numToStr
Copy link
Owner

Is gcgc and gbgb always goes down?

@numToStr numToStr added the feature New feature label Oct 10, 2021
@numToStr
Copy link
Owner

Also this might be somewhat related to #11

@astier
Copy link

astier commented Oct 22, 2021

Can we make it gcu? It would detect the comment automatically. If cursor is on box-comment it deletes block comment. If cursor is on a comment-block consisting of single line-comments it deletes block of single-line comments. gcu is shorter and easier than having two longer mappings gcgc and gbgb. vim-commentary actually also uses gcu.

https://github.com/tpope/vim-commentary/blob/627308e30639be3e2d5402808ce18690557e8292/doc/commentary.txt#L25

@numToStr
Copy link
Owner

@astier Nice suggestion. I like the semantics of gcu ie. u indicating undo. Anyways users will always have an option to change the mapping.

@bew
Copy link

bew commented Aug 25, 2022

Hello, would you think implementing this is hard?
(does the current plugin architecture needs changes? => non-trivial)

@numToStr
Copy link
Owner

@bew This is blocked by upstream treesitter support. Basically, neovim lacks treesitter quantifier support i.e., +, which is crucial for supporting this and #11

@xeluxee
Copy link
Author

xeluxee commented Oct 9, 2022

Temporary solution based on Comment.utils. Use this until the feature becomes natively supported.
Just press gcu or gcgc to uncomment a block of commented lines

---Textobject for adjacent commented lines
local function commented_lines_textobject()
	local U = require("Comment.utils")
	local cl = vim.api.nvim_win_get_cursor(0)[1] -- current line
	local range = { srow = cl, scol = 0, erow = cl, ecol = 0 }
	local ctx = {
		ctype = U.ctype.linewise,
		range = range,
	}
	local cstr = require("Comment.ft").calculate(ctx) or vim.bo.commentstring
	local ll, rr = U.unwrap_cstr(cstr)
	local padding = true
	local is_commented = U.is_commented(ll, rr, padding)

	local line = vim.api.nvim_buf_get_lines(0, cl - 1, cl, false)
	if next(line) == nil or not is_commented(line[1]) then
		return
	end

	local rs, re = cl, cl -- range start and end
	repeat
		rs = rs - 1
		line = vim.api.nvim_buf_get_lines(0, rs - 1, rs, false)
	until next(line) == nil or not is_commented(line[1])
	rs = rs + 1
	repeat
		re = re + 1
		line = vim.api.nvim_buf_get_lines(0, re - 1, re, false)
	until next(line) == nil or not is_commented(line[1])
	re = re - 1

	vim.fn.execute("normal! " .. rs .. "GV" .. re .. "G")
end

vim.keymap.set("o", "gc", commented_lines_textobject,
	{ silent = true, desc = "Textobject for adjacent commented lines" })
vim.keymap.set("o", "u", commented_lines_textobject,
	{ silent = true, desc = "Textobject for adjacent commented lines" })

@zdcthomas
Copy link

What if instead of this specific mapping, we just create a general comment text object, and then instead of gcgc you could just use gcic

@xeluxee
Copy link
Author

xeluxee commented Mar 21, 2023

What if instead of this specific mapping, we just create a general comment text object, and then instead of gcgc you could just use gcic

Actually my snippet creates a textobject that can be used not only for commenting but also for other actions: e.g. you can delete a commented block with dgc or du.

@astier
Copy link

astier commented Mar 21, 2023

I don't really understand how your snippet works but can't it be just merged so Comment has this feature natively or is there some deal-breaker which makes it not possible?

@chrisgrieser
Copy link

chrisgrieser commented Dec 20, 2023

I found this useful enough that implemented I implemented such a text object in my plugin, nvim-various-textobjs.

to uncomment via gcu, use this mapping:

keymap("o", "u", "<cmd>lua require('various-textobjs').multiCommentedLines()<CR>")

It does not use treesitter and hence does not work with injected languages like codeblocks in markdown, but has no dependency on any comments plugin.

@iovis
Copy link

iovis commented May 18, 2024

Neovim v0.10 seems to have a text object for comments :h o_gc, this seems to do the trick for me:

vim.keymap.set("n", "gcu", "gcgc", { remap = true })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
None yet
Development

No branches or pull requests

7 participants