From 56e2fad31927e40406bc888aab99fa8f3f417d1f Mon Sep 17 00:00:00 2001 From: "S.V.I. Vilcrow" Date: Mon, 13 Feb 2023 07:33:10 +0300 Subject: [PATCH] Fixed the jumping to function definition using 'Ctrl+LMB' and the 'Lookup Symbol' button. --- doc/classes/CodeEdit.xml | 10 +++++++++- editor/plugins/script_text_editor.cpp | 11 ++++++++--- modules/gdscript/gdscript_editor.cpp | 10 +++++----- scene/gui/code_edit.cpp | 26 +++++++++++++++----------- scene/gui/code_edit.h | 3 ++- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/doc/classes/CodeEdit.xml b/doc/classes/CodeEdit.xml index 1bc214aea73..88549460208 100644 --- a/doc/classes/CodeEdit.xml +++ b/doc/classes/CodeEdit.xml @@ -237,12 +237,20 @@ Returns the full text with char [code]0xFFFF[/code] at the caret location. - + Returns the full text with char [code]0xFFFF[/code] at the cursor location. + + + + + + Returns the full text with char [code]0xFFFF[/code] at the specified location. + + diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index 9fe1d8af99f..5140569b680 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -814,6 +814,8 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c } ScriptLanguage::LookupResult result; + String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column); + Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result); if (ScriptServer::is_global_class(p_symbol)) { EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol)); } else if (p_symbol.is_resource_file()) { @@ -826,7 +828,7 @@ void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_c EditorNode::get_singleton()->load_resource(p_symbol); } - } else if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK) { + } else if (lc_error == OK) { _goto_line(p_row); switch (result.type) { @@ -947,7 +949,10 @@ void ScriptTextEditor::_validate_symbol(const String &p_symbol) { } ScriptLanguage::LookupResult result; - if (ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK || (ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton)) { + String lc_text = code_editor->get_text_editor()->get_text_for_symbol_lookup(); + Error lc_error = script->get_language()->lookup_code(lc_text, p_symbol, script->get_path(), base, result); + bool is_singleton = ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton; + if (ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || lc_error == OK || is_singleton) { text_edit->set_symbol_lookup_word_as_valid(true); } else if (p_symbol.is_relative_path()) { String path = _get_absolute_path(p_symbol); @@ -1827,7 +1832,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref &ev) { bool open_docs = false; bool goto_definition = false; - if (word_at_pos.is_resource_file()) { + if (ScriptServer::is_global_class(word_at_pos) || word_at_pos.is_resource_file()) { open_docs = true; } else { Node *base = get_tree()->get_edited_scene_root(); diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index 8cfd48b52b3..1b33e131c77 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -1401,11 +1401,11 @@ static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, } break; case GDScriptParser::Node::SELF: { if (p_context.current_class) { - r_type.type.kind = GDScriptParser::DataType::CLASS; - r_type.type.type_source = GDScriptParser::DataType::INFERRED; - r_type.type.is_constant = true; - r_type.type.class_type = p_context.current_class; - r_type.value = p_context.base; + if (p_context.type != GDScriptParser::COMPLETION_SUPER_METHOD) { + r_type.type = p_context.current_class->get_datatype(); + } else { + r_type.type = p_context.current_class->base_type; + } found = true; } } break; diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index b084cb5bea1..c67074af830 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -2105,9 +2105,8 @@ bool CodeEdit::is_symbol_lookup_on_click_enabled() const { return symbol_lookup_on_click_enabled; } -String CodeEdit::get_text_for_symbol_lookup() { +String CodeEdit::get_text_for_symbol_lookup() const { Point2i mp = get_local_mouse_pos(); - Point2i pos = get_line_column_at_pos(mp, false); int line = pos.y; int col = pos.x; @@ -2116,25 +2115,29 @@ String CodeEdit::get_text_for_symbol_lookup() { return String(); } - StringBuilder lookup_text; + return get_text_with_cursor_char(line, col); +} + +String CodeEdit::get_text_with_cursor_char(int p_line, int p_column) const { const int text_size = get_line_count(); + StringBuilder result; for (int i = 0; i < text_size; i++) { String line_text = get_line(i); - - if (i == line) { - lookup_text += line_text.substr(0, col); + if (i == p_line && p_column >= 0 && p_column <= line_text.size()) { + result += line_text.substr(0, p_column); /* Not unicode, represents the cursor. */ - lookup_text += String::chr(0xFFFF); - lookup_text += line_text.substr(col, line_text.size()); + result += String::chr(0xFFFF); + result += line_text.substr(p_column, line_text.size()); } else { - lookup_text += line_text; + result += line_text; } if (i != text_size - 1) { - lookup_text += "\n"; + result += "\n"; } } - return lookup_text.as_string(); + + return result.as_string(); } void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) { @@ -2312,6 +2315,7 @@ void CodeEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled); ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup); + ClassDB::bind_method(D_METHOD("get_text_with_cursor_char", "line", "column"), &CodeEdit::get_text_with_cursor_char); ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid); diff --git a/scene/gui/code_edit.h b/scene/gui/code_edit.h index 55fc5aa2aeb..61c14c3adb9 100644 --- a/scene/gui/code_edit.h +++ b/scene/gui/code_edit.h @@ -424,7 +424,8 @@ public: void set_symbol_lookup_on_click_enabled(bool p_enabled); bool is_symbol_lookup_on_click_enabled() const; - String get_text_for_symbol_lookup(); + String get_text_for_symbol_lookup() const; + String get_text_with_cursor_char(int p_line, int p_column) const; void set_symbol_lookup_word_as_valid(bool p_valid);