diff options
| author | Yuri Sizov <yuris@humnom.net> | 2023-07-12 17:14:52 +0200 |
|---|---|---|
| committer | Yuri Sizov <yuris@humnom.net> | 2023-07-12 17:14:52 +0200 |
| commit | 1e1d2a89bfff333fd4b58619fa7cb02d0b009cc9 (patch) | |
| tree | fd36f873e0ce726d47a2d880505cf5d18f98641f | |
| parent | 1978b7c717160effaf1fcb4b0e49c69a27124a9e (diff) | |
| parent | b0df2e15523d9c9b45a075bf1d23d58e2913fe62 (diff) | |
| download | redot-engine-1e1d2a89bfff333fd4b58619fa7cb02d0b009cc9.tar.gz | |
Merge pull request #44557 from iwek7/improved_comment_toggle
Improve `CodeEdit`'s toggle comments behavior
| -rw-r--r-- | editor/code_editor.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index c1a4b052c1..78b01b6c78 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1485,12 +1485,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; break; } } - // Caret positions need to be saved since they could be moved at the eol. Vector<int> caret_cols; Vector<int> selection_to_cols; @@ -1511,10 +1512,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. |
