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

Improve CodeEdit's toggle comments behavior #44557

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1484,12 +1484,13 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
// Check first if there's any uncommented lines in selection.
bool is_commented = true;
for (int line = from; line <= to; line++) {
if (!text_editor->get_line(line).begins_with(delimiter)) {
// `+ delimiter.length()` here because comment delimiter is not actually `in comment` so we check first character after it
int delimiter_idx = text_editor->is_in_comment(line, text_editor->get_first_non_whitespace_column(line) + delimiter.length());
if (delimiter_idx == -1 || text_editor->get_delimiter_start_key(delimiter_idx) != delimiter) {
is_commented = false;
Copy link
Member

Choose a reason for hiding this comment

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

Probably need to also check the delimiter matches with get_delimiter_start_key.

Copy link
Contributor Author

@iwek7 iwek7 Jul 1, 2022

Choose a reason for hiding this comment

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

I've added this check here and in other branch of this if as well, so that for instance shader code like

/* code... */

will have // added at the beggining when toggled

break;
}
}

// Caret positions need to be saved since they could be moved at the eol.
Vector<int> caret_cols;
Vector<int> selection_to_cols;
Expand All @@ -1510,10 +1511,10 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) {
continue;
}
if (is_commented) {
text_editor->set_line(line, line_text.substr(delimiter.length(), line_text.length()));
continue;
text_editor->set_line(line, line_text.replace_first(delimiter, ""));
} else {
text_editor->set_line(line, line_text.insert(text_editor->get_first_non_whitespace_column(line), delimiter));
}
text_editor->set_line(line, delimiter + line_text);
}

// Readjust carets and selections.
Expand Down