Переглянути джерело

Smarter toggle comment block. issue #10720

Added check to see if all lines in selection are commented (is_commented)
If so remove the comment from each line else add a comment.
If the line is empty, with trailing spaces or tabs, remove those.
This will add an extra comment character to the lines that are already commented,
so when you uncomment a block it retains the previous commented lines.

Following Zylann logic, if the selected lines are all commented out,
it removes one # from each. Otherwise, it adds one.
It works how it is, but i would like some feedback on how to implement this.
Paulo Gomes 8 роки тому
батько
коміт
af40c8698b
1 змінених файлів з 17 додано та 4 видалено
  1. 17 4
      editor/plugins/script_text_editor.cpp

+ 17 - 4
editor/plugins/script_text_editor.cpp

@@ -953,13 +953,26 @@ void ScriptTextEditor::_edit_option(int p_op) {
 				if (tx->get_selection_to_column() == 0)
 					end -= 1;
 
+				// Check if all lines in the selected block are commented
+				bool is_commented = true;
+				for (int i = begin; i <= end; i++) {
+					if (!tx->get_line(i).begins_with("#")) {
+						is_commented = false;
+						break;
+					}
+				}
 				for (int i = begin; i <= end; i++) {
 					String line_text = tx->get_line(i);
 
-					if (line_text.begins_with("#"))
-						line_text = line_text.substr(1, line_text.length());
-					else
-						line_text = "#" + line_text;
+					if (line_text.strip_edges().empty()) {
+						line_text = "#";
+					} else {
+						if (is_commented) {
+							line_text = line_text.substr(1, line_text.length());
+						} else {
+							line_text = "#" + line_text;
+						}
+					}
 					tx->set_line(i, line_text);
 				}
 			} else {