diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-09-26 08:20:05 +0200 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-09-26 08:20:05 +0200 |
commit | 3c63b09a706163ffaab7d39a0313c5f374a2a2c1 (patch) | |
tree | 0f63ff183609dddad0c8d8eda28873a0ad9c5162 /editor/code_editor.cpp | |
parent | e25408df4b4967dfbddbeb9c9b16de78ee5ead31 (diff) | |
parent | 12299e0f5310c2bf5df62a0284c9e2b151403b1e (diff) | |
download | redot-engine-3c63b09a706163ffaab7d39a0313c5f374a2a2c1.tar.gz |
Merge pull request #81486 from jsjtxietian/Ignoring-empty-lines--when-uncommenting
Ignore empty lines when uncommenting code
Diffstat (limited to 'editor/code_editor.cpp')
-rw-r--r-- | editor/code_editor.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 9d2b54ef1a..56e405bfcf 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1492,14 +1492,22 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { } // Check first if there's any uncommented lines in selection. bool is_commented = true; + bool is_all_empty = true; for (int line = from; line <= to; line++) { // `+ 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) { + // Empty lines should not be counted. + bool is_empty = text_editor->get_line(line).strip_edges().is_empty(); + is_all_empty = is_all_empty && is_empty; + if (!is_empty && (delimiter_idx == -1 || text_editor->get_delimiter_start_key(delimiter_idx) != delimiter)) { is_commented = false; break; } } + + // Special case for commenting empty lines, treat it/them as uncommented lines. + is_commented = is_commented && !is_all_empty; + // Caret positions need to be saved since they could be moved at the eol. Vector<int> caret_cols; Vector<int> selection_to_cols; @@ -1515,10 +1523,11 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { // Comment/uncomment. for (int line = from; line <= to; line++) { String line_text = text_editor->get_line(line); - if (line_text.strip_edges().is_empty()) { + if (is_all_empty) { text_editor->set_line(line, delimiter); continue; } + if (is_commented) { text_editor->set_line(line, line_text.replace_first(delimiter, "")); } else { |