-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
feat(commenting): Add 'inner comment' text object #29428
base: master
Are you sure you want to change the base?
Conversation
Problem: No way to change a comment while keeping the markers. Solution: Add an 'inner comment' text object, which is in line with 'in tag', 'i{', 'i[', and other surrounding text objects. The default keymap for this text object is 'igc'.
Thanks for the PR! I personally don't think this is a good addition to built-in commenting for at least the following reasons:
|
The
This could also be said about inserting
This is an interesting idea, but might be hard for users to add, without the useful functions currently in this module. One would need to get the commentstring, parse left and right parts, insert those around the current cursor position. Not trivial.
Yeah, that is true. Could be changed to |
80ce439
to
928e700
Compare
I mean, maybe not trivial, but pretty straightforward in my opinion: local insert_comment_parts = function()
local cms = vim.bo.commentstring
local offset = cms:find('%%s')
local insert = cms:gsub('%%s', '')
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
vim.api.nvim_buf_set_text(0, row - 1, col, row - 1, col, { insert })
vim.api.nvim_win_set_cursor(0, { row, col + offset - 1 })
end
vim.keymap.set('i', '<C-b>', insert_comment_parts) The only drawback is that it uses buffer's commentstring instead of the one "at cursor", which built-in commenting uses. Exporting it (and the built-in variant of this functionality) was discussed in #28997. |
Please add that to the Problem description :) That is a stronger, clearer motivating case. |
Co-authored-by: Justin M. Keyes <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to argue against this. Code looks minimal, and for comments like /** ... */
and <!-- ... -->
it's definitely useful.
@justinmk, there are issues with this. If this type of functionality is useful, exporting this type of Insert mode mapping is a more versatile approach. |
Yes, I read that and almost closed this issue, but the use-case is strong enough to make the case for this being a default.
I don't get this argument. Textobjects are the primary text manipulation interface for vim. Insert-mode stuff is what vscode does.
We added commenting so we need to claim a textobject. We can choose |
The argument is that "insert comment marks" is a more versatile approach. For example, it can be used together with The suggested "inner textobject" is primarily (if not even only) useful in one particular situation of
We did with My suggestion is to do either of the following (from most to least preferred):
Making built-in separate textobject that is useful only for a single operator is not on the list, I am afraid. |
well then we couldn't add any new builtin mappings? and imo we should also add |
Yanking only the text of a comment is definitely also a useful operator to have 'inner comment' for. |
btw, why is |
because we just map |
Of course adding built-in mappings is possible. But having
Me neither off the top of my head. But that doesn't mean not accounting for it is bad. Plus nvim-various-textobjects uses it for "greedyOuterIndentation" together with
This is already possible with
I agree, it is indeed useful. But the proposed
There is no Creating
|
Yes, that's true for multiple singleline comments following each other. However, it's something that could be fixed if this behaviour is desired. |
As built-in commenting is designed to be used only with single line comments, I don't see a way to make |
Problem
No way to change a comment while keeping the markers.
Currently changing a html comment:
<!-- a |comment -->
Type:
cgc<!-- new comment -->
<!-- new comment -->|
I think changing an entire comment is done quite often by programmers.
Solution
Add an 'inner comment' text object, which is in line with
it
,i{
,i[
, and other surrounding text objects.The default keymap for this text object is
igc
.With this change:
<!-- a |comment -->
Type:
cigcnew comment
<!-- new comment| -->
Fixes #29318.