Skip to content

Commit

Permalink
Add underlined symbol to refactor command
Browse files Browse the repository at this point in the history
  • Loading branch information
adamscott committed Dec 21, 2024
1 parent 0ec8fa7 commit 34d12e4
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class ScriptLanguage : public Object {
List<Match> matches;
};

virtual Error refactor_rename_symbol_code(const String &p_code, const String &p_path, Object *p_owner, RefactorRenameSymbolResult *r_result) { return ERR_UNAVAILABLE; }
virtual Error refactor_rename_symbol_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, RefactorRenameSymbolResult *r_result) { return ERR_UNAVAILABLE; }

// Lookup.
enum LookupResultType {
Expand Down
5 changes: 3 additions & 2 deletions editor/code_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,9 +1067,10 @@ void CodeTextEditor::_refactor_request(int p_refactor_kind) {
case ScriptLanguage::RefactorKind::REFACTOR_KIND_RENAME_SYMBOL: {
ScriptLanguage::RefactorRenameSymbolResult result;
String completion_text = text_editor->get_text_for_code_completion();
_refactor_rename_symbol_script(completion_text, &result);
String symbol = text_editor->get_text_with_cursor_char(text_editor->get_caret_line(), text_editor->get_caret_column());
_refactor_rename_symbol_script(completion_text, symbol, &result);
if (refactor_rename_symbol_func) {
refactor_rename_symbol_func(refactor_ud, completion_text, &result);
refactor_rename_symbol_func(refactor_ud, completion_text, symbol, &result);
}

for (const ScriptLanguage::RefactorRenameSymbolResult::Match &match : result.matches) {
Expand Down
4 changes: 2 additions & 2 deletions editor/code_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class FindReplaceBar : public HBoxContainer {
};

typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced);
typedef void (*CodeTextEditorRefactorRenameSymbolFunc)(void *p_ud, const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result);
typedef void (*CodeTextEditorRefactorRenameSymbolFunc)(void *p_ud, const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result);

class CodeTextEditor : public VBoxContainer {
GDCLASS(CodeTextEditor, VBoxContainer);
Expand Down Expand Up @@ -229,7 +229,7 @@ class CodeTextEditor : public VBoxContainer {
virtual void _load_theme_settings() {}
virtual void _validate_script() {}
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {}
virtual void _refactor_rename_symbol_script(const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result) {}
virtual void _refactor_rename_symbol_script(const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result) {}

void _text_changed_idle_timeout();
void _code_complete_timer_timeout();
Expand Down
8 changes: 4 additions & 4 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,17 +887,17 @@ void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLa
}
}

void ScriptTextEditor::_refactor_rename_symbol_scripts(void *p_ud, const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
void ScriptTextEditor::_refactor_rename_symbol_scripts(void *p_ud, const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
ste->_refactor_rename_symbol_script(p_code, r_result);
ste->_refactor_rename_symbol_script(p_code, p_symbol, r_result);
}

void ScriptTextEditor::_refactor_rename_symbol_script(const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
void ScriptTextEditor::_refactor_rename_symbol_script(const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
Node *base = get_tree()->get_edited_scene_root();
if (base) {
base = _find_node_for_script(base, base, script);
}
Error err = script->get_language()->refactor_rename_symbol_code(p_code, script->get_path(), base, r_result);
Error err = script->get_language()->refactor_rename_symbol_code(p_code, p_symbol, script->get_path(), base, r_result);

if (err != OK) {
print_error(vformat("Error while refactoring script: %s", error_names[err]));
Expand Down
4 changes: 2 additions & 2 deletions editor/plugins/script_text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ class ScriptTextEditor : public ScriptEditorBase {
static void _code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);
void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force);

static void _refactor_rename_symbol_scripts(void *p_ud, const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result);
void _refactor_rename_symbol_script(const String &p_code, ScriptLanguage::RefactorRenameSymbolResult *r_result);
static void _refactor_rename_symbol_scripts(void *p_ud, const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result);
void _refactor_rename_symbol_script(const String &p_code, const String &p_symbol, ScriptLanguage::RefactorRenameSymbolResult *r_result);

void _load_theme_settings();
void _set_theme_for_script();
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ class GDScriptLanguage : public ScriptLanguage {
virtual String make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const override;
virtual Error complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) override;
#ifdef TOOLS_ENABLED
virtual Error refactor_rename_symbol_code(const String &p_code, const String &p_path, Object *p_owner, RefactorRenameSymbolResult *r_result) override;
virtual Error refactor_rename_symbol_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, RefactorRenameSymbolResult *r_result) override;
virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override;
#endif
virtual String _get_indentation() const;
Expand Down
27 changes: 26 additions & 1 deletion modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3658,7 +3658,32 @@ void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_t
}

#ifdef TOOLS_ENABLED
::Error GDScriptLanguage::refactor_rename_symbol_code(const String &p_code, const String &p_path, Object *p_owner, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
::Error GDScriptLanguage::refactor_rename_symbol_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, ScriptLanguage::RefactorRenameSymbolResult *r_result) {
// if (ClassDB::class_exists(p_symbol)) {
// r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
// r_result.class_name = p_symbol;
// return OK;
// }

// if (Variant::get_type_by_name(p_symbol) < Variant::VARIANT_MAX) {
// r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
// r_result.class_name = p_symbol;
// return OK;
// }

// if (p_symbol == "Variant") {
// r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
// r_result.class_name = "Variant";
// return OK;
// }

// if (p_symbol == "PI" || p_symbol == "TAU" || p_symbol == "INF" || p_symbol == "NAN") {
// r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
// r_result.class_name = "@GDScript";
// r_result.class_member = p_symbol;
// return OK;
// }

return OK;
}

Expand Down

0 comments on commit 34d12e4

Please sign in to comment.