summaryrefslogtreecommitdiffstats
path: root/editor/plugins/script_text_editor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/plugins/script_text_editor.cpp')
-rw-r--r--editor/plugins/script_text_editor.cpp253
1 files changed, 217 insertions, 36 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp
index 7496ad8cca..422c656351 100644
--- a/editor/plugins/script_text_editor.cpp
+++ b/editor/plugins/script_text_editor.cpp
@@ -6,6 +6,7 @@
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -68,26 +69,6 @@ Ref<Script> ScriptTextEditor::get_edited_script() const {
return script;
}
-bool ScriptTextEditor::goto_method(const String &p_method) {
-
- Vector<String> functions = get_functions();
-
- String method_search = p_method + ":";
-
- for (int i = 0; i < functions.size(); i++) {
- String function = functions[i];
-
- if (function.begins_with(method_search)) {
-
- int line = function.get_slice(":", 1).to_int();
- goto_line(line - 1);
- return true;
- }
- }
-
- return false;
-}
-
void ScriptTextEditor::_load_theme_settings() {
TextEdit *text_edit = code_editor->get_text_edit();
@@ -143,7 +124,6 @@ void ScriptTextEditor::_load_theme_settings() {
text_edit->add_keyword_color("Transform", basetype_color);
text_edit->add_keyword_color("Color", basetype_color);
text_edit->add_keyword_color("Image", basetype_color);
- text_edit->add_keyword_color("InputEvent", basetype_color);
text_edit->add_keyword_color("Rect2", basetype_color);
text_edit->add_keyword_color("NodePath", basetype_color);
@@ -238,6 +218,10 @@ void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_
code_editor->get_text_edit()->cursor_set_column(1);
}
+bool ScriptTextEditor::show_members_overview() {
+ return true;
+}
+
void ScriptTextEditor::update_settings() {
code_editor->update_editor_settings();
@@ -259,6 +243,48 @@ Variant ScriptTextEditor::get_edit_state() {
return state;
}
+void ScriptTextEditor::_convert_case(CaseStyle p_case) {
+ TextEdit *te = code_editor->get_text_edit();
+ Ref<Script> scr = get_edited_script();
+ if (scr.is_null()) {
+ return;
+ }
+
+ if (te->is_selection_active()) {
+ te->begin_complex_operation();
+
+ int begin = te->get_selection_from_line();
+ int end = te->get_selection_to_line();
+ int begin_col = te->get_selection_from_column();
+ int end_col = te->get_selection_to_column();
+
+ for (int i = begin; i <= end; i++) {
+ String new_line = te->get_line(i);
+
+ switch (p_case) {
+ case UPPER: {
+ new_line = new_line.to_upper();
+ } break;
+ case LOWER: {
+ new_line = new_line.to_lower();
+ } break;
+ case CAPITALIZE: {
+ new_line = new_line.capitalize();
+ } break;
+ }
+
+ if (i == begin) {
+ new_line = te->get_line(i).left(begin_col) + new_line.right(begin_col);
+ }
+ if (i == end) {
+ new_line = new_line.left(end_col) + te->get_line(i).right(end_col);
+ }
+ te->set_line(i, new_line);
+ }
+ te->end_complex_operation();
+ }
+}
+
void ScriptTextEditor::trim_trailing_whitespace() {
TextEdit *tx = code_editor->get_text_edit();
@@ -289,13 +315,120 @@ void ScriptTextEditor::trim_trailing_whitespace() {
}
}
+void ScriptTextEditor::convert_indent_to_spaces() {
+ TextEdit *tx = code_editor->get_text_edit();
+ Ref<Script> scr = get_edited_script();
+
+ if (scr.is_null()) {
+ return;
+ }
+
+ int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
+ String indent = "";
+
+ for (int i = 0; i < indent_size; i++) {
+ indent += " ";
+ }
+
+ int cursor_line = tx->cursor_get_line();
+ int cursor_column = tx->cursor_get_column();
+
+ bool changed_indentation = false;
+ for (int i = 0; i < tx->get_line_count(); i++) {
+ String line = tx->get_line(i);
+
+ if (line.length() <= 0) {
+ continue;
+ }
+
+ int j = 0;
+ while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
+ if (line[j] == '\t') {
+ if (!changed_indentation) {
+ tx->begin_complex_operation();
+ changed_indentation = true;
+ }
+ if (cursor_line == i && cursor_column > j) {
+ cursor_column += indent_size - 1;
+ }
+ line = line.left(j) + indent + line.right(j + 1);
+ }
+ j++;
+ }
+ if (changed_indentation) {
+ tx->set_line(i, line);
+ }
+ }
+ if (changed_indentation) {
+ tx->cursor_set_column(cursor_column);
+ tx->end_complex_operation();
+ tx->update();
+ }
+}
+
+void ScriptTextEditor::convert_indent_to_tabs() {
+ TextEdit *tx = code_editor->get_text_edit();
+ Ref<Script> scr = get_edited_script();
+
+ if (scr.is_null()) {
+ return;
+ }
+
+ int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
+ indent_size -= 1;
+
+ int cursor_line = tx->cursor_get_line();
+ int cursor_column = tx->cursor_get_column();
+
+ bool changed_indentation = false;
+ for (int i = 0; i < tx->get_line_count(); i++) {
+ String line = tx->get_line(i);
+
+ if (line.length() <= 0) {
+ continue;
+ }
+
+ int j = 0;
+ int space_count = -1;
+ while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
+ if (line[j] != '\t') {
+ space_count++;
+
+ if (space_count == indent_size) {
+ if (!changed_indentation) {
+ tx->begin_complex_operation();
+ changed_indentation = true;
+ }
+ if (cursor_line == i && cursor_column > j) {
+ cursor_column -= indent_size;
+ }
+ line = line.left(j - indent_size) + "\t" + line.right(j + 1);
+ j = 0;
+ space_count = -1;
+ }
+ } else {
+ space_count = -1;
+ }
+ j++;
+ }
+ if (changed_indentation) {
+ tx->set_line(i, line);
+ }
+ }
+ if (changed_indentation) {
+ tx->cursor_set_column(cursor_column);
+ tx->end_complex_operation();
+ tx->update();
+ }
+}
+
void ScriptTextEditor::tag_saved_version() {
code_editor->get_text_edit()->tag_saved_version();
}
void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
- code_editor->get_text_edit()->cursor_set_line(p_line);
+ code_editor->get_text_edit()->call_deferred("cursor_set_line", p_line);
}
void ScriptTextEditor::ensure_focus() {
@@ -327,7 +460,7 @@ String ScriptTextEditor::get_name() {
} else if (script->get_name() != "")
name = script->get_name();
else
- name = script->get_class() + "(" + itos(script->get_instance_ID()) + ")";
+ name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
return name;
}
@@ -428,6 +561,8 @@ void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_fo
if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
return;
+ ERR_FAIL_COND(!get_tree());
+
Set<Ref<Script> > scripts;
Node *base = get_tree()->get_edited_scene_root();
@@ -503,7 +638,17 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c
}
ScriptLanguage::LookupResult result;
- if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path().get_base_dir(), base, result) == OK) {
+ if (p_symbol.is_resource_file()) {
+ List<String> scene_extensions;
+ ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
+
+ if (scene_extensions.find(p_symbol.get_extension())) {
+ EditorNode::get_singleton()->load_scene(p_symbol);
+ } else {
+ EditorNode::get_singleton()->load_resource(p_symbol);
+ }
+
+ } else if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path().get_base_dir(), base, result) == OK) {
_goto_line(p_row);
@@ -826,10 +971,24 @@ void ScriptTextEditor::_edit_option(int p_op) {
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
+ case EDIT_CONVERT_INDENT_TO_SPACES: {
+ convert_indent_to_spaces();
+ } break;
+ case EDIT_CONVERT_INDENT_TO_TABS: {
+ convert_indent_to_tabs();
+ } break;
case EDIT_PICK_COLOR: {
color_panel->popup();
} break;
-
+ case EDIT_TO_UPPERCASE: {
+ _convert_case(UPPER);
+ } break;
+ case EDIT_TO_LOWERCASE: {
+ _convert_case(LOWER);
+ } break;
+ case EDIT_CAPITALIZE: {
+ _convert_case(CAPITALIZE);
+ } break;
case SEARCH_FIND: {
code_editor->get_find_replace_bar()->popup_search();
@@ -948,6 +1107,10 @@ Control *ScriptTextEditor::get_edit_menu() {
return edit_hb;
}
+void ScriptTextEditor::clear_edit_menu() {
+ memdelete(edit_hb);
+}
+
void ScriptTextEditor::reload(bool p_soft) {
TextEdit *te = code_editor->get_text_edit();
@@ -1087,15 +1250,18 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
}
}
-void ScriptTextEditor::_text_edit_gui_input(const InputEvent &ev) {
- if (ev.type == InputEvent::MOUSE_BUTTON) {
- InputEventMouseButton mb = ev.mouse_button;
- if (mb.button_index == BUTTON_RIGHT && !mb.pressed) {
+void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
+
+ Ref<InputEventMouseButton> mb = ev;
+
+ if (mb.is_valid()) {
+
+ if (mb->get_button_index() == BUTTON_RIGHT && !mb->is_pressed()) {
int col, row;
TextEdit *tx = code_editor->get_text_edit();
- tx->_get_mouse_pos(Point2i(mb.global_x, mb.global_y) - tx->get_global_pos(), row, col);
- Vector2 mpos = Vector2(mb.global_x, mb.global_y) - tx->get_global_pos();
+ tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
+ Vector2 mpos = mb->get_global_position() - tx->get_global_position();
bool have_selection = (tx->get_selection_text().length() > 0);
bool have_color = (tx->get_word_at_pos(mpos) == "Color");
if (have_color) {
@@ -1123,9 +1289,7 @@ void ScriptTextEditor::_text_edit_gui_input(const InputEvent &ev) {
float alpha = color.size() > 3 ? color[3] : 1.0f;
color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
}
- color_panel->set_pos(get_global_transform().xform(get_local_mouse_pos()));
- Size2 ms = Size2(300, color_picker->get_combined_minimum_size().height + 10);
- color_panel->set_size(ms);
+ color_panel->set_position(get_global_transform().xform(get_local_mouse_pos()));
} else {
have_color = false;
}
@@ -1173,7 +1337,7 @@ void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color) {
context_menu->add_separator();
context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
}
- context_menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
+ context_menu->set_position(get_global_transform().xform(get_local_mouse_pos()));
context_menu->set_size(Vector2(1, 1));
context_menu->popup();
}
@@ -1182,6 +1346,7 @@ ScriptTextEditor::ScriptTextEditor() {
code_editor = memnew(CodeTextEditor);
add_child(code_editor);
+ code_editor->add_constant_override("separation", 0);
code_editor->set_area_as_parent_rect();
code_editor->connect("validate_script", this, "_validate_script");
code_editor->connect("load_theme_settings", this, "_load_theme_settings");
@@ -1207,7 +1372,6 @@ ScriptTextEditor::ScriptTextEditor() {
add_child(color_panel);
color_picker = memnew(ColorPicker);
color_panel->add_child(color_picker);
- color_panel->set_child_rect(color_picker); //NOT
color_picker->connect("color_changed", this, "_color_changed");
edit_hb = memnew(HBoxContainer);
@@ -1236,6 +1400,8 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
#endif
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
+ edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
+ edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
edit_menu->get_popup()->add_separator();
@@ -1243,6 +1409,15 @@ ScriptTextEditor::ScriptTextEditor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
+ edit_menu->get_popup()->add_separator();
+ PopupMenu *convert_case = memnew(PopupMenu);
+ convert_case->set_name("convert_case");
+ edit_menu->get_popup()->add_child(convert_case);
+ edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
+ convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
+ convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
+ convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
+ convert_case->connect("id_pressed", this, "_edit_option");
search_menu = memnew(MenuButton);
edit_hb->add_child(search_menu);
@@ -1304,6 +1479,8 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
#endif
ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T);
+ ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent To Spaces"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_Y);
+ ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent To Tabs"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_X);
ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
@@ -1311,6 +1488,10 @@ void ScriptTextEditor::register_editor() {
ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD);
ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA);
+ ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Convert To Uppercase"), KEY_MASK_SHIFT | KEY_F4);
+ ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Convert To Lowercase"), KEY_MASK_SHIFT | KEY_F3);
+ ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F2);
+
ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F);
ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);