From 0771020c8357eee5ea9c395782089d867db84c05 Mon Sep 17 00:00:00 2001 From: marynate Date: Sun, 4 May 2014 11:22:49 +0800 Subject: [PATCH] Add auto code completion (without press Ctrl+Space manually) Disalbe auto code completion even there's only one option Hide auto-completion if only one completion option and it's been typed Support use tab key to accept code completion option --- scene/gui/text_edit.cpp | 6 +++++- tools/editor/code_editor.cpp | 14 +++++++++++++- tools/editor/code_editor.h | 2 ++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index bd15e14ccd5..2ac9c667712 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -1106,7 +1106,7 @@ void TextEdit::_input_event(const InputEvent& p_input_event) { return; } - if (k.scancode==KEY_RETURN) { + if (k.scancode==KEY_RETURN || k.scancode==KEY_TAB) { _confirm_completion(); accept_event(); @@ -2896,6 +2896,7 @@ void TextEdit::_update_completion_candidates() { completion_current=completion_options[completion_index]; +#if 0 // even there's only one option, user still get the chance to choose using it or not if (completion_options.size()==1) { //one option to complete, just complete it automagically _confirm_completion(); @@ -2904,6 +2905,9 @@ void TextEdit::_update_completion_candidates() { return; } +#endif + if (completion_options.size()==1 && s==completion_options[0]) + _cancel_completion(); completion_enabled=true; diff --git a/tools/editor/code_editor.cpp b/tools/editor/code_editor.cpp index a780c0173ea..994e3e1e682 100644 --- a/tools/editor/code_editor.cpp +++ b/tools/editor/code_editor.cpp @@ -479,10 +479,15 @@ void CodeTextEditor::_line_col_changed() { void CodeTextEditor::_text_changed() { - + code_complete_timer->start(); idle->start(); } +void CodeTextEditor::_code_complete_timer_timeout() { + + text_editor->query_code_comple(); +} + void CodeTextEditor::_complete_request(const String& p_request, int p_line) { List entries; @@ -551,6 +556,7 @@ void CodeTextEditor::_bind_methods() { ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed); ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change); ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout); + ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout); ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request); } @@ -575,6 +581,11 @@ CodeTextEditor::CodeTextEditor() { idle->set_one_shot(true); idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2)); + code_complete_timer = memnew(Timer); + add_child(code_complete_timer); + code_complete_timer->set_one_shot(true); + code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f)); + error = memnew( Label ); add_child(error); error->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,5); @@ -593,6 +604,7 @@ CodeTextEditor::CodeTextEditor() { cs.push_back("."); text_editor->set_completion(true,cs); idle->connect("timeout", this,"_text_changed_idle_timeout"); + code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout"); EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change"); } diff --git a/tools/editor/code_editor.h b/tools/editor/code_editor.h index 0ca0e1e234b..2ca2ae98732 100644 --- a/tools/editor/code_editor.h +++ b/tools/editor/code_editor.h @@ -128,6 +128,7 @@ class CodeTextEditor : public Control { Label *line_col; Label *info; Timer *idle; + Timer *code_complete_timer; Label *error; @@ -145,6 +146,7 @@ protected: void _text_changed_idle_timeout(); + void _code_complete_timer_timeout(); void _text_changed(); void _line_col_changed(); void _notification(int);