From 5165a90ef6915f20178a4abb1f008f442f1f17c5 Mon Sep 17 00:00:00 2001 From: hsandt Date: Mon, 4 Feb 2019 20:17:44 +0100 Subject: [Script Editor] Refactored ScriptTextEditor::_edit_option by extracting EDIT_TOGGLE_COMMENT case CodeEditor::toggle_inline_comment is now used by both ScriptTextEditor and ShaderEditor --- editor/code_editor.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to 'editor/code_editor.cpp') diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 4ab9a72694..123817824b 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1095,6 +1095,78 @@ void CodeTextEditor::clone_lines_down() { text_editor->update(); } +void CodeTextEditor::toggle_inline_comment(const String &delimiter) { + text_editor->begin_complex_operation(); + if (text_editor->is_selection_active()) { + int begin = text_editor->get_selection_from_line(); + int end = text_editor->get_selection_to_line(); + + // End of selection ends on the first column of the last line, ignore it. + if (text_editor->get_selection_to_column() == 0) + end -= 1; + + int col_to = text_editor->get_selection_to_column(); + int cursor_pos = text_editor->cursor_get_column(); + + // Check if all lines in the selected block are commented + bool is_commented = true; + for (int i = begin; i <= end; i++) { + if (!text_editor->get_line(i).begins_with(delimiter)) { + is_commented = false; + break; + } + } + for (int i = begin; i <= end; i++) { + String line_text = text_editor->get_line(i); + + if (line_text.strip_edges().empty()) { + line_text = delimiter; + } else { + if (is_commented) { + line_text = line_text.substr(delimiter.length(), line_text.length()); + } else { + line_text = delimiter + line_text; + } + } + text_editor->set_line(i, line_text); + } + + // Adjust selection & cursor position. + int offset = is_commented ? -1 : 1; + int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0; + + if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) + cursor_pos += 1; + + if (text_editor->get_selection_to_column() != 0 && col_to != text_editor->get_line(text_editor->get_selection_to_line()).length() + 1) + col_to += offset; + + if (text_editor->cursor_get_column() != 0) + cursor_pos += offset; + + text_editor->select(begin, col_from, text_editor->get_selection_to_line(), col_to); + text_editor->cursor_set_column(cursor_pos); + + } else { + int begin = text_editor->cursor_get_line(); + String line_text = text_editor->get_line(begin); + + int col = text_editor->cursor_get_column(); + if (line_text.begins_with(delimiter)) { + line_text = line_text.substr(delimiter.length(), line_text.length()); + col -= 1; + } else { + line_text = delimiter + line_text; + col += 1; + } + + text_editor->set_line(begin, line_text); + text_editor->cursor_set_column(col); + } + text_editor->end_complex_operation(); + text_editor->update(); +} + void CodeTextEditor::goto_line(int p_line) { text_editor->deselect(); text_editor->unfold_line(p_line); -- cgit v1.2.3 From 49b4faf43a012ba6e0bdae612bab6547f230cedb Mon Sep 17 00:00:00 2001 From: hsandt Date: Mon, 4 Feb 2019 20:23:48 +0100 Subject: [Code Editor] Fixed toggle_inline_comment moving cursor position by 1 even with longer delimiter like "//" --- editor/code_editor.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'editor/code_editor.cpp') diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 123817824b..0ad7d5adc9 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -1132,7 +1132,7 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { } // Adjust selection & cursor position. - int offset = is_commented ? -1 : 1; + int offset = (is_commented ? -1 : 1) * delimiter.length(); int col_from = text_editor->get_selection_from_column() > 0 ? text_editor->get_selection_from_column() + offset : 0; if (is_commented && text_editor->cursor_get_column() == text_editor->get_line(text_editor->cursor_get_line()).length() + 1) @@ -1150,14 +1150,15 @@ void CodeTextEditor::toggle_inline_comment(const String &delimiter) { } else { int begin = text_editor->cursor_get_line(); String line_text = text_editor->get_line(begin); + int delimiter_length = delimiter.length(); int col = text_editor->cursor_get_column(); if (line_text.begins_with(delimiter)) { - line_text = line_text.substr(delimiter.length(), line_text.length()); - col -= 1; + line_text = line_text.substr(delimiter_length, line_text.length()); + col -= delimiter_length; } else { line_text = delimiter + line_text; - col += 1; + col += delimiter_length; } text_editor->set_line(begin, line_text); -- cgit v1.2.3