Added support for multi-cursor comments to the comment plugin#3543
Added support for multi-cursor comments to the comment plugin#3543MiguelRoldao wants to merge 4 commits intomicro-editor:masterfrom
Conversation
Andriamanitra
left a comment
There was a problem hiding this comment.
I played around with this a bit and everything seems to work alright even in weird edge cases like multiple cursors on the same line, selections with comments inside them etc. It's really nice that the cursor position is now updated accordingly when a line gets commented.
The only slight annoyance I found is that if the selection ends in a newline, the line immediately after the selection gets commented too. This is not how it used to work, and not how I would like it to work. To reproduce:
- Open a file like the one below
# some comment
print("hello")
print("world")- Select the first two lines by holding shift and pressing arrow down twice.
- Comment selection. Before this PR you would get comments only on the first two lines, but now you get a
#on the empty line too.
Andriamanitra
left a comment
There was a problem hiding this comment.
The latest changes introduced some bugs. I opened a PR in your fork with suggested fixes that you may use if you wish.
| if cursor.CurSelection[endSel].X == 0 then | ||
| lines[cursor.CurSelection[endSel].Y] = nil | ||
| staticEnd = true | ||
| end |
There was a problem hiding this comment.
This can end up overwriting lines that were already set to true by another cursor.
There was a problem hiding this comment.
Well caught! I have merged your suggestions. It solves the bug, and didn't seem to change any other behavior.
|
Hi, Sorry to be late to this contribution. But can I re-state a strong preference for comment characters at the start of the line. #2282. This is especially useful for commenting out sections that already contain comments: void somefunc(int a)
{
// if (a) {
// a += 2;
// // After adding two we can print
// printf("a = %d\n", a);
// } else {
// // Print "none" if not a
// printf("none");
// }
}I know others feel differently, so an optional setting seems the sensible solution. Would it be possible to use the "*.lua": {
"commenttype": "^-- %s"
},Or would another setting item be required to indicate comment location? "*.lua": {
"commenttype": "-- %s",
"commentloc": "^"
},Sorry I can't offer a solution, I don't think I have the knowledge to get it close to right. Kind Regards Gavin Holt |
|
I honestly prefer the way it is implemented as of now. Having the least amount of white space between the comment characters and the text (while keeping all the comment chars of a multiline comment in the same column). However, it should be pretty straightforward to implement a "commentindent" option:
I'd have no problem dedicating an hour to this. However, I don't know if this feature is inside the scope of this PR. Maybe @Andriamanitra has something to say about it. |
|
The indentation issue (#2282) is mostly unrelated so I would fix it in a separate PR, but I don't know the preferences of the maintainers (@dmaluka and @JoeKar). Personally I think indented comments are way more readable, but I guess there's no harm in adding an option as long as the default behavior stays the same. |
I like as it is by default right now including the alignment. Furthermore I support this comment.
Yep. But if so then independent of this PR. |
runtime/plugins/comment/comment.lua
Outdated
| @@ -1,4 +1,4 @@ | |||
| VERSION = "1.0.0" | |||
| VERSION = "1.1.0" | |||
There was a problem hiding this comment.
As discussed in #3673 (review) there is no need to change this version number, since the plugin is builtin.
|
Sorry I have been away. Sometimes life gets busy, and suddenly its been almost a year. Anyways, the version is back to how it's supposed to be. Also merged with the master branch to resolve conflicts. I have been using this PR since and haven't found any further issues. Anything else that needs to be dealt with before merging @Andriamanitra @JoeKar ? |
runtime/plugins/comment/comment.lua
Outdated
| local curData = {} | ||
| -- gather cursor data and lines to (un)comment | ||
| for i = 0,#bp.Buf:getCursors()-1 do | ||
| local cursor = bp.Buf:getCursor(i) |
|
So is the only thing remaining to separately add a PR to create an option to enable this behavior? Then the two could be merged to main together? |
|
No, the PRs history requires a cleanup. |
Ahh yes. I get what you meant now. If Miguel doesn't take care of it before long, I may take a stab at this. |
|
Any news here? |
|
Sorry for the delay. I've rebased this branch as requested as well as some history cleanup. |
JoeKar
left a comment
There was a problem hiding this comment.
- added exception for selections that end in a new line (for backwards compatibility) I'm unsure, if this should stay independent
- bugfixes should be part of the first changing commit
- removed unused code can stay independent, but shouldn't include this version change
runtime/plugins/comment/comment.lua
Outdated
| @@ -1,4 +1,4 @@ | |||
| VERSION = "1.0.0" | |||
| VERSION = "1.1.0" | |||
runtime/plugins/comment/comment.lua
Outdated
| local curData = {} | ||
| -- gather cursor data and lines to (un)comment | ||
| for i = 0,#bp.Buf:getCursors()-1 do | ||
| local cursor = bp.Buf:getCursor(i) |
runtime/plugins/comment/comment.lua
Outdated
| bp.Cursor:StoreVisualX() | ||
| end | ||
|
|
||
| -- unused |
There was a problem hiding this comment.
No need to add this comment, when the whole function is removed with dba64db.
To add multi-cursor support I had to re-engineer the algorithm of the plug-in quite a bit. I tried to reuse as much code that was already implemented as possible to avoid introducing new bugs. I've tested some use cases and I haven't detected any problems so far.
The new algorithm has the following workings:
Note: All cursor logic is now contained in comment().
I also sneakily added a comment type for the language I'm developing in my master's thesis :P (https://freest-lang.github.io/)