-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Autofill comment when inserting a new line in documentation #95955
base: master
Are you sure you want to change the base?
Autofill comment when inserting a new line in documentation #95955
Conversation
scene/gui/code_edit.cpp
Outdated
// Insert a documentation comment if the caret was previously on a comment block. | ||
String prev_line = get_line(get_caret_line(i) - 1); | ||
|
||
if (prev_line.strip_edges().begins_with("##")) { | ||
insert_text_at_caret("## ", i); | ||
} |
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.
This is a feature specific to GDScript and the Editor. I am not savvy enough to say where this should go, but definitely not in the CodeEdit class, which is designed to be much more generalized.
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.
Yes, the initial purpose of this PR was to gather information on how to do it best and to learn the codebase. @kitbdev gave me a clue about it; see #95955 (comment). I can enhance it now :)
Thank you for your feedback!
Then it can probably be set here: godot/editor/plugins/script_text_editor.cpp Lines 252 to 262 in 568589c
|
It totally makes sense. I'll give it a try as soon as I can in the file you just pointed out. Thank you for your feedback! Edit: I took a look at the file you told me, @kitbdev. I can get the delimiters in the I continue to search for the answer in any case :) |
Perhaps this makes sense not only for doc comments, but for regular ones too. Kate has interesting behavior:
Shift+Enter is also useful for Markdown and YAML lists. |
Why not use |
I agree, but this method is only available in I'm digging into the codebase; I need time to get used to it, I suppose 😊 |
bdb21c6
to
fb52d24
Compare
I did my best to enhance it and to be more flexible. Feedback is very welcome 🙏 |
b644c56
to
b85b8a3
Compare
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.
Some tests in tests\scene\test_code_edit.h
for testing the newline functionality would be nice too.
scene/gui/code_edit.cpp
Outdated
@@ -1863,6 +1879,35 @@ int CodeEdit::is_in_comment(int p_line, int p_column) const { | |||
return _is_in_delimiter(p_line, p_column, TYPE_COMMENT); |
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.
return _is_in_delimiter(p_line, p_column, TYPE_COMMENT); | |
return _is_in_delimiter(p_line, p_column, TYPE_COMMENT) || _is_in_delimiter(p_line, p_column, TYPE_DOC_COMMENT) ; |
Maybe this should also return true for doc comments, so its behavior doesn't change. Or the uses of is_in_comment
need to be updated to also check for doc comments.
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.
I agree, it makes sense! Thanks for pointing this out!
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.
hum… for some reason I don't know yet, adding this check breaks the tests and some features too, like line folding.
I'll take a closer look at it after work.
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.
OK I understood! _is_in_delimiter
returns an integer and not a boolean, so it can be used this way.
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.
I just pushed the fix :)
scene/gui/code_edit.cpp
Outdated
@@ -1154,6 +1154,22 @@ void CodeEdit::_new_line(bool p_split_current_line, bool p_above) { | |||
set_caret_line(get_caret_line(i) - 1, false, true, 0, i); | |||
set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i); | |||
} | |||
|
|||
// Insert a documentation comment if the caret was previously on a comment block. | |||
int prev_line = get_caret_line(i) - 1; |
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.
This should adapt if p_above
is true (ctrl+shift+enter).
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.
Fix has been pushed :)
Sorry for the delayed answer, @dalexeev! Yes, indeed, that would be nice, but the proposal is limited to the documentation comment. I don't know what people think about it and if we should extend the feature to comments too. I am open to discussion. |
scene/gui/code_edit.cpp
Outdated
@@ -1154,6 +1154,22 @@ void CodeEdit::_new_line(bool p_split_current_line, bool p_above) { | |||
set_caret_line(get_caret_line(i) - 1, false, true, 0, i); | |||
set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i); | |||
} | |||
|
|||
// Insert a documentation comment if the caret was previously on a documentation comment block. |
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.
Note that a doc comment can be multiline (like /**
*/
).
Regarding the Kate-like behavior, I agree that it's too much and requires a separate proposal. But I think it would be reasonable to discuss extending the feature to regular comments as part of this proposal/PR. In fact, it would even simplify the implementation. When I added doc comment highlighting, I didn't add a new API to Also, this can break compatibility in some ways. Previously, doc comments were considered comments, so |
I totally agreed with you. I can start working on this.
Yes, it would be easier to implement it for both.
Sadly, my knowledge regarding the Godot codebase is limited; that's why I tried to implement this proposal to gain experience in both the Godot codebase and C++. I would be glad to have some clues about how to implement it—just a clue, not the full implementation—and I will give it a try to respect the best practices in this codebase. For example, is By the way, what do you think, is it necessary to add an option to enable / disable this feature? Many thanks for the help! |
scene/gui/code_edit.cpp
Outdated
for (const String doc_comment : get_doc_comment_delimiters()) { | ||
const String beg = doc_comment.get_slice(" ", 0); | ||
|
||
if (prev_line_content.begins_with(beg)) { | ||
insert_text_at_caret(doc_comment + " ", i); | ||
break; | ||
} | ||
} |
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.
There should be a separate kind of key for the delimiters, since other styles have a different characters for start /**
, end */
, and in between new lines *
.
Maybe the presence of this new line key can be what enables this behavior, and then we may not need a separate doc comment type?
I think this behavior should only be on doc comments as initially proposed, if it is on regular comments it may be annoying to add new lines for regular code around comments. Though the implementation can be generalized for all types of delimiters if it is easier. Also this shouldn't cause compatibility issues, CodeEdits behavior should stay the same, since the user hasn't specified to add doc comments instead of regular comments, so all the regular comment delimiter functions should work the same.
It shouldn't be needed to have a dedicated enable option, a user can just not use doc comments, or not have a newline delimiter key, depending on how it is implemented. |
It seems like opinions are divided regarding expanding this behavior to regular comments. Originally, my idea was to reproduce the VSCode behavior that I used for years, and I always missed the autofill for the documentation block. I'm new to Godot Core development, so how is this kind of situation resolved? How do we decide which way to take? I'm waiting for new opinions before start working on this PR again :) |
If the current implementation for the doc comment is fine for everyone, I will add some tests, but I prefer to be sure at the moment it is fine before digging into the tests world. |
36e42e2
to
6b67dd6
Compare
6b67dd6
to
f3526b2
Compare
f3526b2
to
d12aa00
Compare
d12aa00
to
ab64319
Compare
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.
Will try to test the functionality next week
scene/gui/code_edit.h
Outdated
@@ -457,6 +460,9 @@ class CodeEdit : public TextEdit { | |||
Point2 get_delimiter_start_position(int p_line, int p_column) const; | |||
Point2 get_delimiter_end_position(int p_line, int p_column) const; | |||
|
|||
Vector<String> get_block_key_delimiters() const; | |||
void set_block_key_delimiters(const List<String> *p_delimiters); |
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.
void set_block_key_delimiters(const List<String> *p_delimiters); | |
void set_block_key_delimiters(const List<String> &p_delimiters); |
No reason to pass it as a pointer unless it's optional, in that case it needs a null check
But this should really use Vector<String>
IMO, it transfers to a Vector
anyway so there's going to be a conversion anyway, so I'd say:
void set_block_key_delimiters(const List<String> *p_delimiters); | |
void set_block_key_delimiters(const Vector<String> &p_delimiters); |
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.
I'm wondering if it is viable to convert it to a Vector, as the API requires a List from the key delimiters. So I need to convert it to a Vector and then pass it to the method. It seems to complicate it a little bit. What do you think?
See the update here: https://github.com/godotengine/godot/pull/95955/files#diff-fa2cafb4b2d8b59e5baeba0b861f7f2f6bf6c213d6387254fee5a7682478e588R264-R273
Many thanks for the review 🙏 I'm pushing soon the feedbacks! Edit: Once again, I'm too used to Gitlab 😅 |
Tested and works as expected generally, but I still hold that it's unintuitive and confusing to not have any way to abort this, but to me it's not necessarily a dealbreaker, but I'd suggest having some way to stop the doc comment and return to normal At minimum though I think this should be controlled by a setting as it is pretty annoying to get stuck in this mode, and either add support for exiting somehow (possibly using But adding an option to control this would be sufficient for me, but I do think that's a necessity |
After thinking about it, I think this should only be for multi-line comments and maybe splitting single line comments. This would help match other editors too. Doc comments that are only a single line are pretty common, probably more common than ones that are multiple lines. So having this behavior by default on single line doc comments can be unwanted and surprising. Edit: Pressing enter again to remove the comment instead could also work, but the script editor hasn't done stuff like that before, so it may be more work. |
Thank you for the review and the thoughts shared about this proposal 🙏 I will come back with a new implementation as soon as I am satisfied with what I did! Edit: I have no words, yeah, same shortcut again… 😅 |
d68a18d
to
2216cf7
Compare
Just to be clear, the current approach is still good for multi-line comments, so the new implementation can be in a new PR. |
You mean doc comments and regular comments, but only multiline like If that is the case, it will only be triggered for C# in the current implementation. |
It will also work on the shader editor. |
I didn't abandon this MR; I've been quite busy with my work lately. I'll come back to it soon 😊 |
Don't worry take your time! |
2216cf7
to
846d8d8
Compare
This PR implements the following proposal godotengine/godot-proposals#8244.
Here it is in action:
CleanShot.2024-08-22.at.18.06.52.mp4
Please keep in mind that this is my first contribution and my first step into the C++ world, but I really want to contribute to Godot :)
Any feedback, help, or advice is very welcome!