Skip to content

[WIP] Feature: Comment toggling#188

Merged
glennsl merged 12 commits into
onivim:masterfrom
glennsl:feature/op/comment-toggling
Mar 25, 2020
Merged

[WIP] Feature: Comment toggling#188
glennsl merged 12 commits into
onivim:masterfrom
glennsl:feature/op/comment-toggling

Conversation

@glennsl
Copy link
Copy Markdown
Member

@glennsl glennsl commented Mar 24, 2020

This adds a new operator, gc/gcc, which will comment or uncomment the current or selected lines. It also adds a new function to the API, vimOptionSetLineComment to specify the comment string that will be used for the current buffer.

The way it works is a bit crude currently, but it does the job. The comment string will only be inserted (and checked) at the very beginning of each line, ignoring indentation, and lines are commented or uncommented individually rather than as a block. If I can get a decent workflow going I might come back to this to give it some more polish. But until then it's fair game if anyone else wants a go at it.

@glennsl glennsl requested a review from bryphe March 24, 2020 15:21
Comment thread src/normal.c
}
}

void toggle_comment_lines(linenr_T start, linenr_T end)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These function should probably live somewhere else. I'm not sure where though.

Comment thread src/structs.h
int b_diff_failed; // internal diff failed for this buffer
#endif

char_u *b_oni_line_comment;
Copy link
Copy Markdown
Member Author

@glennsl glennsl Mar 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how OK it is to add a new field to the buf_T structure. But since it's at the end it should at least have a relatively low chance of impacting FFI and such.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for calling this out. Should be safe, as we're the only consumer of the library

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 24, 2020

I think I might have a different version of clang-format. Mine is 9.0.1

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 24, 2020

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Comment on lines +19 to +29
MU_TEST(test_toggle_uncommented)
{
vimInput("g");
vimInput("c");
vimInput("c");

char_u *line = vimBufferGetLine(curbuf, vimCursorGetLine());
printf("LINE: %s\n", line);

mu_check(strcmp(line, "//This is the first line of a test file") == 0);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice set of test cases!

@bryphe
Copy link
Copy Markdown
Member

bryphe commented Mar 24, 2020

When I run this against Onivim 2 (with a resolution: "libvim": "link:../libvimsrc"), I get this error when commenting (using gcc in a Reason file):
image

If I then uncomment, I get a crash here:

[ERROR]    +0ms Oni2.Exception : Exception (Invalid_argument "index out of bounds"):
Raised by primitive operation at file "src/Core/BufferLine.re", line 144, characters 11-30
Called from file "src/Feature/Editor/BufferViewTokenizer.re", line 64, characters 29-75
Called from file "src/Feature/Editor/OverlaysView.re", line 75, characters 8-141
Called from file "src/Feature/Editor/EditorSurface.re", line 233, characters 4-308
Called from file "src/UI/EditorGroupView.re", line 148, characters 10-702
Called from file "src/UI/EditorLayoutView.re", line 54, characters 12-74
Called from file "list.ml", line 92, characters 20-23
Called from file "src/UI/EditorLayoutView.re", line 37, characters 2-583
Called from file "src/UI/EditorLayoutView.re", line 66, characters 17-46
Called from file "src/UI/EditorView.re", line 38, characters 17-43
Called from file "src/UI/Root.re", line 116, characters 8-28
Called from file "src/bin_editor/Oni2_editor.re", line 138, characters 19-47
Called from file "src/Core/Tick.re", line 112, characters 9-15
Called from file "src/Core/Tick.re", line 90, characters 15-32
Called from file "list.ml", line 92, characters 20-23
Called from file "list.ml", line 92, characters 32-39
Called from file "src/Core/Tick.re", line 99, characters 22-52
Called from file "src/Core/App.re", line 236, characters 4-23
Called from file "src/sdl2.re", line 648, characters 10-20
Called from file "src/bin_editor/Oni2_editor.re", line 240, characters 2-18

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 24, 2020

Hmm, that's unfortunate. I can easily reproduce. I'll have to try to make this a failing test.

Interesting though that there's no error in visual mode. Although it still crashes when uncommenting.

@bryphe
Copy link
Copy Markdown
Member

bryphe commented Mar 24, 2020

Hmm, that's unfortunate. I can easily reproduce. I'll have to try to make this a failing test.

Right, it was strange to me, because I see a test case that exercises the un-commenting. It seems to me, based on the exception, an issue with the cursor position.

We use the curwin->w_cursor.col to get the cursor position - I wonder if that could be out of sync? It might be worth adding a case to validate the cursor position is expected (calling vimCursorGetColumn will give the same byte-position that reason-libvim/oni2 use)

Interesting though that there's no error in visual mode. Although it still crashes when uncommenting.

It must be hitting this condition here:

if (i == (int)(sizeof(opchars) / sizeof(char[3]) - 1))

Although it's not clear to me why; as the sizeof should be updated with your extra entry in the opchars table 🤔

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 24, 2020

We use the curwin->w_cursor.col to get the cursor position - I wonder if that could be out of sync? It might be worth adding a case to validate the cursor position is expected (calling vimCursorGetColumn will give the same byte-position that reason-libvim/oni2 use)

Good idea. Will try that!

Although it's not clear to me why; as the sizeof should be updated with your extra entry in the opchars table thinking

It's being called with cc for some reason, rather than gc. So I must still be missing some wiring somewhere.

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 24, 2020

I believe I've fixed both, but I'm not able to get esy to rebuild linked dependencies, so I'm unable to test it in Oni2 proper

Comment thread src/normal.c
Comment thread src/normal.c
Comment thread src/normal.c
@glennsl glennsl force-pushed the feature/op/comment-toggling branch from b29204a to 6ac48e6 Compare March 25, 2020 10:44
@bryphe
Copy link
Copy Markdown
Member

bryphe commented Mar 25, 2020

Thanks for fixing those last issues, @glennsl ! I picked up the latest and it is working great - undo, redo, motions, macros🥇

Very excited to have this in!

@glennsl
Copy link
Copy Markdown
Member Author

glennsl commented Mar 25, 2020

Woohoo! Will you do a release so I can pick it up properly in reason-libvim and oni2 as well?

@glennsl glennsl merged commit 9d3b7fb into onivim:master Mar 25, 2020
@glennsl glennsl deleted the feature/op/comment-toggling branch March 25, 2020 14:22
@bryphe
Copy link
Copy Markdown
Member

bryphe commented Mar 25, 2020

Done! Released as libvim@8.10869.35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants