diff options
-rw-r--r-- | core/input/input_map.cpp | 6 | ||||
-rw-r--r-- | core/object/script_language.h | 2 | ||||
-rw-r--r-- | doc/classes/CodeEdit.xml | 325 | ||||
-rw-r--r-- | doc/classes/ProjectSettings.xml | 2 | ||||
-rw-r--r-- | doc/classes/TextEdit.xml | 36 | ||||
-rw-r--r-- | editor/code_editor.cpp | 33 | ||||
-rw-r--r-- | editor/editor_settings.cpp | 1 | ||||
-rw-r--r-- | editor/plugins/script_text_editor.cpp | 26 | ||||
-rw-r--r-- | editor/plugins/shader_editor_plugin.cpp | 10 | ||||
-rw-r--r-- | editor/plugins/visual_shader_editor_plugin.cpp | 8 | ||||
-rw-r--r-- | scene/gui/code_edit.cpp | 1432 | ||||
-rw-r--r-- | scene/gui/code_edit.h | 173 | ||||
-rw-r--r-- | scene/gui/text_edit.cpp | 837 | ||||
-rw-r--r-- | scene/gui/text_edit.h | 47 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.cpp | 9 |
15 files changed, 2059 insertions, 888 deletions
diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index a43ad4ed7d..878ce820fb 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -474,10 +474,14 @@ const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() { default_builtin_cache.insert("ui_text_completion_query", inputs); inputs = List<Ref<InputEvent>>(); - inputs.push_back(InputEventKey::create_reference(KEY_TAB)); inputs.push_back(InputEventKey::create_reference(KEY_ENTER)); + inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER)); default_builtin_cache.insert("ui_text_completion_accept", inputs); + inputs = List<Ref<InputEvent>>(); + inputs.push_back(InputEventKey::create_reference(KEY_TAB)); + default_builtin_cache.insert("ui_text_completion_replace", inputs); + // Newlines inputs = List<Ref<InputEvent>>(); inputs.push_back(InputEventKey::create_reference(KEY_ENTER)); diff --git a/core/object/script_language.h b/core/object/script_language.h index bb46c718b2..9ed3c7e80f 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -242,6 +242,8 @@ public: }; struct ScriptCodeCompletionOption { + /* Keep enum in Sync with: */ + /* /scene/gui/code_edit.h - CodeEdit::CodeCompletionKind */ enum Kind { KIND_CLASS, KIND_FUNCTION, diff --git a/doc/classes/CodeEdit.xml b/doc/classes/CodeEdit.xml index 42a6df866f..4076198df6 100644 --- a/doc/classes/CodeEdit.xml +++ b/doc/classes/CodeEdit.xml @@ -8,6 +8,91 @@ <tutorials> </tutorials> <methods> + <method name="_confirm_code_completion" qualifiers="virtual"> + <return type="void"> + </return> + <argument index="0" name="replace" type="bool"> + </argument> + <description> + Override this method to define how the selected entry should be inserted. If [code]replace[/code] is true, any existing text should be replaced. + </description> + </method> + <method name="_filter_code_completion_candidates" qualifiers="virtual"> + <return type="Array"> + </return> + <argument index="0" name="candidates" type="Array"> + </argument> + <description> + Override this method to define what items in [code]candidates[/code] should be displayed. + Both [code]candidates[/code] and the return is a [Array] of [Dictionary], see [method get_code_completion_option] for [Dictionary] content. + </description> + </method> + <method name="_request_code_completion" qualifiers="virtual"> + <return type="void"> + </return> + <argument index="0" name="force" type="bool"> + </argument> + <description> + Override this method to define what happens when the user requests code completion. If [code]force[/code] is true, any checks should be bypassed. + </description> + </method> + <method name="add_code_completion_option"> + <return type="void"> + </return> + <argument index="0" name="type" type="int" enum="CodeEdit.CodeCompletionKind"> + </argument> + <argument index="1" name="display_text" type="String"> + </argument> + <argument index="2" name="insert_text" type="String"> + </argument> + <argument index="3" name="text_color" type="Color" default="Color( 1, 1, 1, 1 )"> + </argument> + <argument index="4" name="icon" type="Resource" default="null"> + </argument> + <argument index="5" name="value" type="Variant" default="0"> + </argument> + <description> + Submits an item to the queue of potential candidates for the autocomplete menu. Call [method update_code_completion_options] to update the list. + [b]Note[/b]: This list will replace all current candidates. + </description> + </method> + <method name="add_comment_delimiter"> + <return type="void"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <argument index="1" name="end_key" type="String"> + </argument> + <argument index="2" name="line_only" type="bool" default="false"> + </argument> + <description> + Adds a comment delimiter. + Both the start and end keys must be symbols. Only the start key has to be unique. + Line only denotes if the region should continue until the end of the line or carry over on to the next line. If the end key is blank this is automatically set to [code]true[/code]. + </description> + </method> + <method name="add_string_delimiter"> + <return type="void"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <argument index="1" name="end_key" type="String"> + </argument> + <argument index="2" name="line_only" type="bool" default="false"> + </argument> + <description> + Adds a string delimiter. + Both the start and end keys must be symbols. Only the start key has to be unique. + Line only denotes if the region should continue until the end of the line or carry over on to the next line. If the end key is blank this is automatically set to [code]true[/code]. + </description> + </method> + <method name="cancel_code_completion"> + <return type="void"> + </return> + <description> + Cancels the autocomplete menu. + </description> + </method> <method name="clear_bookmarked_lines"> <return type="void"> </return> @@ -20,12 +105,35 @@ <description> </description> </method> + <method name="clear_comment_delimiters"> + <return type="void"> + </return> + <description> + Removes all comment delimiters. + </description> + </method> <method name="clear_executing_lines"> <return type="void"> </return> <description> </description> </method> + <method name="clear_string_delimiters"> + <return type="void"> + </return> + <description> + Removes all string delimiters. + </description> + </method> + <method name="confirm_code_completion"> + <return type="void"> + </return> + <argument index="0" name="replace" type="bool" default="false"> + </argument> + <description> + Inserts the selected entry into the text. If [code]replace[/code] is true, any existing text is replaced rather then merged. + </description> + </method> <method name="get_bookmarked_lines" qualifiers="const"> <return type="Array"> </return> @@ -38,12 +146,128 @@ <description> </description> </method> + <method name="get_code_completion_option" qualifiers="const"> + <return type="Dictionary"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + Gets the completion option at [code]index[/code]. The return [Dictionary] has the following key-values: + [code]kind[/code]: [enum CodeCompletionKind] + [code]display_text[/code]: Text that is shown on the autocomplete menu. + [code]insert_text[/code]: Text that is to be inserted when this item is selected. + [code]font_color[/code]: Color of the text on the autocomplete menu. + [code]icon[/code]: Icon to draw on the autocomplete menu. + [code]default_value[/code]: Value of the symbol. + </description> + </method> + <method name="get_code_completion_options" qualifiers="const"> + <return type="Dictionary[]"> + </return> + <description> + Gets all completion options, see [method get_code_completion_option] for return content. + </description> + </method> + <method name="get_code_completion_selected_index" qualifiers="const"> + <return type="int"> + </return> + <description> + Gets the index of the current selected completion option. + </description> + </method> + <method name="get_delimiter_end_key" qualifiers="const"> + <return type="String"> + </return> + <argument index="0" name="delimiter_index" type="int"> + </argument> + <description> + Gets the end key for a string or comment region index. + </description> + </method> + <method name="get_delimiter_end_postion" qualifiers="const"> + <return type="Vector2"> + </return> + <argument index="0" name="line" type="int"> + </argument> + <argument index="1" name="column" type="int"> + </argument> + <description> + If [code]line[/code] [code]column[/code] is in a string or comment, returns the end position of the region. If not or no end could be found, both [Vector2] values will be [code]-1[/code]. + </description> + </method> + <method name="get_delimiter_start_key" qualifiers="const"> + <return type="String"> + </return> + <argument index="0" name="delimiter_index" type="int"> + </argument> + <description> + Gets the start key for a string or comment region index. + </description> + </method> + <method name="get_delimiter_start_postion" qualifiers="const"> + <return type="Vector2"> + </return> + <argument index="0" name="line" type="int"> + </argument> + <argument index="1" name="column" type="int"> + </argument> + <description> + If [code]line[/code] [code]column[/code] is in a string or comment, returns the start position of the region. If not or no start could be found, both [Vector2] values will be [code]-1[/code]. + </description> + </method> <method name="get_executing_lines" qualifiers="const"> <return type="Array"> </return> <description> </description> </method> + <method name="get_text_for_code_completion" qualifiers="const"> + <return type="String"> + </return> + <description> + Returns the full text with char [code]0xFFFF[/code] at the caret location. + </description> + </method> + <method name="has_comment_delimiter" qualifiers="const"> + <return type="bool"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <description> + Returns [code]true[/code] if comment [code]start_key[/code] exists. + </description> + </method> + <method name="has_string_delimiter" qualifiers="const"> + <return type="bool"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <description> + Returns [code]true[/code] if string [code]start_key[/code] exists. + </description> + </method> + <method name="is_in_comment" qualifiers="const"> + <return type="int"> + </return> + <argument index="0" name="line" type="int"> + </argument> + <argument index="1" name="column" type="int" default="-1"> + </argument> + <description> + Return delimiter index if [code]line[/code] [code]column[/code] is in a comment. If [code]column[/code] is not provided, will return delimiter index if the entire [code]line[/code] is a comment. Otherwise [code]-1[/code]. + </description> + </method> + <method name="is_in_string" qualifiers="const"> + <return type="int"> + </return> + <argument index="0" name="line" type="int"> + </argument> + <argument index="1" name="column" type="int" default="-1"> + </argument> + <description> + Return the delimiter index if [code]line[/code] [code]column[/code] is in a string. If [code]column[/code] is not provided, will return the delimiter index if the entire [code]line[/code] is a string. Otherwise [code]-1[/code]. + </description> + </method> <method name="is_line_bookmarked" qualifiers="const"> <return type="bool"> </return> @@ -68,6 +292,60 @@ <description> </description> </method> + <method name="remove_comment_delimiter"> + <return type="void"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <description> + Removes the comment delimiter with [code]start_key[/code]. + </description> + </method> + <method name="remove_string_delimiter"> + <return type="void"> + </return> + <argument index="0" name="start_key" type="String"> + </argument> + <description> + Removes the string delimiter with [code]start_key[/code]. + </description> + </method> + <method name="request_code_completion"> + <return type="void"> + </return> + <argument index="0" name="force" type="bool" default="false"> + </argument> + <description> + Emits [signal request_code_completion], if [code]force[/code] is true will bypass all checks. Otherwise will check that the caret is in a word or in front of a prefix. Will ignore the request if all current options are of type file path, node path or signal. + </description> + </method> + <method name="set_code_completion_selected_index"> + <return type="void"> + </return> + <argument index="0" name="index" type="int"> + </argument> + <description> + Sets the current selected completion option. + </description> + </method> + <method name="set_code_hint"> + <return type="void"> + </return> + <argument index="0" name="code_hint" type="String"> + </argument> + <description> + Sets the code hint text. Pass an empty string to clear. + </description> + </method> + <method name="set_code_hint_draw_below"> + <return type="void"> + </return> + <argument index="0" name="draw_below" type="bool"> + </argument> + <description> + Sets if the code hint should draw below the text. + </description> + </method> <method name="set_line_as_bookmarked"> <return type="void"> </return> @@ -98,8 +376,30 @@ <description> </description> </method> + <method name="update_code_completion_options"> + <return type="void"> + </return> + <argument index="0" name="force" type="bool"> + </argument> + <description> + Submits all completion options added with [method add_code_completion_option]. Will try to force the autoccomplete menu to popup, if [code]force[/code] is [code]true[/code]. + [b]Note[/b]: This will replace all current candidates. + </description> + </method> </methods> <members> + <member name="code_completion_enabled" type="bool" setter="set_code_completion_enabled" getter="is_code_completion_enabled" default="false"> + Sets whether code completion is allowed. + </member> + <member name="code_completion_prefixes" type="String[]" setter="set_code_completion_prefixes" getter="get_code_comletion_prefixes" default="[ ]"> + Sets prefixes that will trigger code completion. + </member> + <member name="delimiter_comments" type="String[]" setter="set_comment_delimiters" getter="get_comment_delimiters" default="[ ]"> + Sets the comment delimiters. All existing comment delimiters will be removed. + </member> + <member name="delimiter_strings" type="String[]" setter="set_string_delimiters" getter="get_string_delimiters" default="[ ]"> + Sets the string delimiters. All existing string delimiters will be removed. + </member> <member name="draw_bookmarks" type="bool" setter="set_draw_bookmarks_gutter" getter="is_drawing_bookmarks_gutter" default="false"> </member> <member name="draw_breakpoints_gutter" type="bool" setter="set_draw_breakpoints_gutter" getter="is_drawing_breakpoints_gutter" default="false"> @@ -123,8 +423,33 @@ <description> </description> </signal> + <signal name="request_code_completion"> + <description> + Emitted when the user requests code completion. + </description> + </signal> </signals> <constants> + <constant name="KIND_CLASS" value="0" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_FUNCTION" value="1" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_SIGNAL" value="2" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_VARIABLE" value="3" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_MEMBER" value="4" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_ENUM" value="5" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_CONSTANT" value="6" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_NODE_PATH" value="7" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_FILE_PATH" value="8" enum="CodeCompletionKind"> + </constant> + <constant name="KIND_PLAIN_TEXT" value="9" enum="CodeCompletionKind"> + </constant> </constants> <theme_items> <theme_item name="background_color" type="Color" default="Color( 0, 0, 0, 0 )"> diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index 73899daaea..a200858a3c 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -703,6 +703,8 @@ </member> <member name="input/ui_text_completion_query" type="Dictionary" setter="" getter=""> </member> + <member name="input/ui_text_completion_replace" type="Dictionary" setter="" getter=""> + </member> <member name="input/ui_text_dedent" type="Dictionary" setter="" getter=""> </member> <member name="input/ui_text_delete" type="Dictionary" setter="" getter=""> diff --git a/doc/classes/TextEdit.xml b/doc/classes/TextEdit.xml index 29067b5534..74811318dc 100644 --- a/doc/classes/TextEdit.xml +++ b/doc/classes/TextEdit.xml @@ -128,6 +128,13 @@ Folds the given line, if possible (see [method can_fold]). </description> </method> + <method name="get_caret_draw_pos" qualifiers="const"> + <return type="Vector2"> + </return> + <description> + Gets the caret pixel draw poistion. + </description> + </method> <method name="get_gutter_count" qualifiers="const"> <return type="int"> </return> @@ -315,6 +322,13 @@ Insert the specified text at the cursor position. </description> </method> + <method name="is_caret_visible" qualifiers="const"> + <return type="bool"> + </return> + <description> + Returns [code]true[/code] if the caret is visible on the screen. + </description> + </method> <method name="is_folded" qualifiers="const"> <return type="bool"> </return> @@ -812,10 +826,6 @@ <description> </description> </signal> - <signal name="request_completion"> - <description> - </description> - </signal> <signal name="symbol_lookup"> <argument index="0" name="symbol" type="String"> </argument> @@ -964,24 +974,6 @@ </theme_item> <theme_item name="code_folding_color" type="Color" default="Color( 0.8, 0.8, 0.8, 0.8 )"> </theme_item> - <theme_item name="completion" type="StyleBox"> - </theme_item> - <theme_item name="completion_background_color" type="Color" default="Color( 0.17, 0.16, 0.2, 1 )"> - </theme_item> - <theme_item name="completion_existing_color" type="Color" default="Color( 0.87, 0.87, 0.87, 0.13 )"> - </theme_item> - <theme_item name="completion_font_color" type="Color" default="Color( 0.67, 0.67, 0.67, 1 )"> - </theme_item> - <theme_item name="completion_lines" type="int" default="7"> - </theme_item> - <theme_item name="completion_max_width" type="int" default="50"> - </theme_item> - <theme_item name="completion_scroll_color" type="Color" default="Color( 1, 1, 1, 1 )"> - </theme_item> - <theme_item name="completion_scroll_width" type="int" default="3"> - </theme_item> - <theme_item name="completion_selected_color" type="Color" default="Color( 0.26, 0.26, 0.27, 1 )"> - </theme_item> <theme_item name="current_line_color" type="Color" default="Color( 0.25, 0.25, 0.26, 0.8 )"> Sets the [Color] of the breakpoints. [member breakpoint_gutter] has to be enabled. </theme_item> diff --git a/editor/code_editor.cpp b/editor/code_editor.cpp index 97ba5d7a5f..be42eab636 100644 --- a/editor/code_editor.cpp +++ b/editor/code_editor.cpp @@ -816,12 +816,12 @@ void CodeTextEditor::_code_complete_timer_timeout() { if (!is_visible_in_tree()) { return; } - text_editor->query_code_comple(); + text_editor->request_code_completion(); } void CodeTextEditor::_complete_request() { List<ScriptCodeCompletionOption> entries; - String ctext = text_editor->get_text_for_completion(); + String ctext = text_editor->get_text_for_code_completion(); _code_complete_script(ctext, &entries); bool forced = false; if (code_complete_func) { @@ -832,16 +832,17 @@ void CodeTextEditor::_complete_request() { } for (List<ScriptCodeCompletionOption>::Element *E = entries.front(); E; E = E->next()) { - ScriptCodeCompletionOption *e = &E->get(); - e->icon = _get_completion_icon(*e); - e->font_color = completion_font_color; - if (e->insert_text.begins_with("\"") || e->insert_text.begins_with("\'")) { - e->font_color = completion_string_color; - } else if (e->insert_text.begins_with("#") || e->insert_text.begins_with("//")) { - e->font_color = completion_comment_color; + ScriptCodeCompletionOption &e = E->get(); + + Color font_color = completion_font_color; + if (e.insert_text.begins_with("\"") || e.insert_text.begins_with("\'")) { + font_color = completion_string_color; + } else if (e.insert_text.begins_with("#") || e.insert_text.begins_with("//")) { + font_color = completion_comment_color; } + text_editor->add_code_completion_option((CodeEdit::CodeCompletionKind)e.kind, e.display, e.insert_text, font_color, _get_completion_icon(e), e.default_value); } - text_editor->code_complete(entries, forced); + text_editor->update_code_completion_options(forced); } Ref<Texture2D> CodeTextEditor::_get_completion_icon(const ScriptCodeCompletionOption &p_option) { @@ -1563,9 +1564,7 @@ void CodeTextEditor::_on_settings_change() { EDITOR_GET("text_editor/completion/code_complete_delay")); // Call hint settings. - text_editor->set_callhint_settings( - EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line"), - EDITOR_GET("text_editor/completion/callhint_tooltip_offset")); + text_editor->set_code_hint_draw_below(EDITOR_GET("text_editor/completion/put_callhint_tooltip_below_current_line")); idle->set_wait_time(EDITOR_GET("text_editor/completion/idle_parse_delay")); } @@ -1847,15 +1846,17 @@ CodeTextEditor::CodeTextEditor() { text_editor->connect("gui_input", callable_mp(this, &CodeTextEditor::_text_editor_gui_input)); text_editor->connect("cursor_changed", callable_mp(this, &CodeTextEditor::_line_col_changed)); text_editor->connect("text_changed", callable_mp(this, &CodeTextEditor::_text_changed)); - text_editor->connect("request_completion", callable_mp(this, &CodeTextEditor::_complete_request)); - Vector<String> cs; + text_editor->connect("request_code_completion", callable_mp(this, &CodeTextEditor::_complete_request)); + TypedArray<String> cs; cs.push_back("."); cs.push_back(","); cs.push_back("("); cs.push_back("="); cs.push_back("$"); cs.push_back("@"); - text_editor->set_completion(true, cs); + cs.push_back("\""); + cs.push_back("\'"); + text_editor->set_code_completion_prefixes(cs); idle->connect("timeout", callable_mp(this, &CodeTextEditor::_text_changed_idle_timeout)); code_complete_timer->connect("timeout", callable_mp(this, &CodeTextEditor::_code_complete_timer_timeout)); diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 1255526a4a..e4072f7d4b 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -571,7 +571,6 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) { _initial_set("text_editor/completion/code_complete_delay", 0.3); hints["text_editor/completion/code_complete_delay"] = PropertyInfo(Variant::FLOAT, "text_editor/completion/code_complete_delay", PROPERTY_HINT_RANGE, "0.01, 5, 0.01"); _initial_set("text_editor/completion/put_callhint_tooltip_below_current_line", true); - _initial_set("text_editor/completion/callhint_tooltip_offset", Vector2()); _initial_set("text_editor/completion/complete_file_paths", true); _initial_set("text_editor/completion/add_type_hints", false); _initial_set("text_editor/completion/use_single_quotes", false); diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp index fe5d830239..a29e51e8fb 100644 --- a/editor/plugins/script_text_editor.cpp +++ b/editor/plugins/script_text_editor.cpp @@ -203,6 +203,26 @@ void ScriptTextEditor::_set_theme_for_script() { CodeEdit *text_edit = code_editor->get_text_editor(); text_edit->get_syntax_highlighter()->update_cache(); + List<String> strings; + script->get_language()->get_string_delimiters(&strings); + text_edit->clear_string_delimiters(); + for (List<String>::Element *E = strings.front(); E; E = E->next()) { + String string = E->get(); + String beg = string.get_slice(" ", 0); + String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String(); + text_edit->add_string_delimiter(beg, end, end == ""); + } + + List<String> comments; + script->get_language()->get_comment_delimiters(&comments); + text_edit->clear_comment_delimiters(); + for (List<String>::Element *E = comments.front(); E; E = E->next()) { + String comment = E->get(); + String beg = comment.get_slice(" ", 0); + String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String(); + text_edit->add_comment_delimiter(beg, end, end == ""); + } + /* add keywords for auto completion */ // singleton autoloads (as types, just as engine singletons are) Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list(); @@ -1056,7 +1076,7 @@ void ScriptTextEditor::_edit_option(int p_op) { _edit_option_toggle_inline_comment(); } break; case EDIT_COMPLETE: { - tx->query_code_comple(); + tx->request_code_completion(true); } break; case EDIT_AUTO_INDENT: { String text = tx->get_text(); @@ -1804,9 +1824,7 @@ ScriptTextEditor::ScriptTextEditor() { update_settings(); - code_editor->get_text_editor()->set_callhint_settings( - EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"), - EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset")); + code_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line")); code_editor->get_text_editor()->set_select_identifiers_on_hover(true); code_editor->get_text_editor()->set_context_menu_enabled(false); diff --git a/editor/plugins/shader_editor_plugin.cpp b/editor/plugins/shader_editor_plugin.cpp index a210a46127..3beef78bfc 100644 --- a/editor/plugins/shader_editor_plugin.cpp +++ b/editor/plugins/shader_editor_plugin.cpp @@ -154,6 +154,10 @@ void ShaderTextEditor::_load_theme_settings() { syntax_highlighter->add_color_region("/*", "*/", comment_color, false); syntax_highlighter->add_color_region("//", "", comment_color, true); + text_editor->clear_comment_delimiters(); + text_editor->add_comment_delimiter("/*", "*/", false); + text_editor->add_comment_delimiter("//", "", true); + if (warnings_panel) { // Warnings panel warnings_panel->add_theme_font_override("normal_font", EditorNode::get_singleton()->get_gui_base()->get_theme_font("main", "EditorFonts")); @@ -348,7 +352,7 @@ void ShaderEditor::_menu_option(int p_option) { } break; case EDIT_COMPLETE: { - shader_editor->get_text_editor()->query_code_comple(); + shader_editor->get_text_editor()->request_code_completion(); } break; case SEARCH_FIND: { shader_editor->get_find_replace_bar()->popup_search(); @@ -659,9 +663,7 @@ ShaderEditor::ShaderEditor(EditorNode *p_node) { EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &ShaderEditor::_editor_settings_changed)); ProjectSettingsEditor::get_singleton()->connect("confirmed", callable_mp(this, &ShaderEditor::_project_settings_changed)); - shader_editor->get_text_editor()->set_callhint_settings( - EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"), - EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset")); + shader_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line")); shader_editor->get_text_editor()->set_select_identifiers_on_hover(true); shader_editor->get_text_editor()->set_context_menu_enabled(false); diff --git a/editor/plugins/visual_shader_editor_plugin.cpp b/editor/plugins/visual_shader_editor_plugin.cpp index a0757439c3..98e1a1489e 100644 --- a/editor/plugins/visual_shader_editor_plugin.cpp +++ b/editor/plugins/visual_shader_editor_plugin.cpp @@ -848,6 +848,10 @@ void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id) { expression_syntax_highlighter->add_color_region("/*", "*/", comment_color, false); expression_syntax_highlighter->add_color_region("//", "", comment_color, true); + expression_box->clear_comment_delimiters(); + expression_box->add_comment_delimiter("/*", "*/", false); + expression_box->add_comment_delimiter("//", "", true); + expression_box->set_text(expression); expression_box->set_context_menu_enabled(false); expression_box->set_draw_line_numbers(true); @@ -2984,6 +2988,10 @@ void VisualShaderEditor::_notification(int p_what) { syntax_highlighter->add_color_region("/*", "*/", comment_color, false); syntax_highlighter->add_color_region("//", "", comment_color, true); + preview_text->clear_comment_delimiters(); + preview_text->add_comment_delimiter("/*", "*/", false); + preview_text->add_comment_delimiter("//", "", true); + error_text->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts")); error_text->add_theme_font_size_override("font_size", get_theme_font_size("status_source_size", "EditorFonts")); error_text->add_theme_color_override("font_color", get_theme_color("error_color", "Editor")); diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index 28a0ea0100..d5000e88d7 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -30,6 +30,18 @@ #include "code_edit.h" +#include "core/os/keyboard.h" +#include "core/string/string_builder.h" +#include "core/string/ustring.h" + +static bool _is_whitespace(char32_t c) { + return c == '\t' || c == ' '; +} + +static bool _is_char(char32_t c) { + return !is_symbol(c); +} + void CodeEdit::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: @@ -52,12 +64,325 @@ void CodeEdit::_notification(int p_what) { folding_color = get_theme_color("code_folding_color"); can_fold_icon = get_theme_icon("can_fold"); folded_icon = get_theme_icon("folded"); + + code_completion_max_width = get_theme_constant("completion_max_width") * cache.font->get_char_size('x').x; + code_completion_max_lines = get_theme_constant("completion_lines"); + code_completion_scroll_width = get_theme_constant("completion_scroll_width"); + code_completion_scroll_color = get_theme_color("completion_scroll_color"); + code_completion_background_color = get_theme_color("completion_background_color"); + code_completion_selected_color = get_theme_color("completion_selected_color"); + code_completion_existing_color = get_theme_color("completion_existing_color"); } break; case NOTIFICATION_DRAW: { + RID ci = get_canvas_item(); + const bool caret_visible = is_caret_visible(); + const bool rtl = is_layout_rtl(); + const int row_height = get_row_height(); + + bool code_completion_below = false; + if (caret_visible && code_completion_active && code_completion_options.size() > 0) { + Ref<StyleBox> csb = get_theme_stylebox("completion"); + + const int code_completion_options_count = code_completion_options.size(); + const int lines = MIN(code_completion_options_count, code_completion_max_lines); + const int icon_hsep = get_theme_constant("hseparation", "ItemList"); + const Size2 icon_area_size(row_height, row_height); + + code_completion_rect.size.width = code_completion_longest_line + icon_hsep + icon_area_size.width + 2; + code_completion_rect.size.height = lines * row_height; + + const Point2 caret_pos = get_caret_draw_pos(); + const int total_height = csb->get_minimum_size().y + code_completion_rect.size.height; + if (caret_pos.y + row_height + total_height > get_size().height) { + code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + cache.line_spacing; + } else { + code_completion_rect.position.y = caret_pos.y + (cache.line_spacing / 2.0f); + code_completion_below = true; + } + + const int scroll_width = code_completion_options_count > code_completion_max_lines ? code_completion_scroll_width : 0; + const int code_completion_base_width = cache.font->get_string_size(code_completion_base).width; + if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) { + code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width; + } else { + code_completion_rect.position.x = caret_pos.x - code_completion_base_width; + } + + draw_style_box(csb, Rect2(code_completion_rect.position - csb->get_offset(), code_completion_rect.size + csb->get_minimum_size() + Size2(scroll_width, 0))); + if (code_completion_background_color.a > 0.01) { + RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), code_completion_background_color); + } + + code_completion_line_ofs = CLAMP(code_completion_current_selected - lines / 2, 0, code_completion_options_count - lines); + RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), code_completion_selected_color); + draw_rect(Rect2(code_completion_rect.position + Vector2(icon_area_size.x + icon_hsep, 0), Size2(MIN(code_completion_base_width, code_completion_rect.size.width - (icon_area_size.x + icon_hsep)), code_completion_rect.size.height)), code_completion_existing_color); + + for (int i = 0; i < lines; i++) { + int l = code_completion_line_ofs + i; + ERR_CONTINUE(l < 0 || l >= code_completion_options_count); + + Ref<TextLine> tl; + tl.instance(); + tl->add_string(code_completion_options[l].display, cache.font, cache.font_size); + + int yofs = (row_height - tl->get_size().y) / 2; + Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs); + + /* Draw completion icon if it is valid. */ + const Ref<Texture2D> &icon = code_completion_options[l].icon; + Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height); + if (icon.is_valid()) { + Size2 icon_size = icon_area.size * 0.7; + icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size)); + } + title_pos.x = icon_area.position.x + icon_area.size.width + icon_hsep; + + tl->set_width(code_completion_rect.size.width - (icon_area_size.x + icon_hsep)); + if (rtl) { + if (code_completion_options[l].default_value.get_type() == Variant::COLOR) { + draw_rect(Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value); + } + tl->set_align(HALIGN_RIGHT); + } else { + if (code_completion_options[l].default_value.get_type() == Variant::COLOR) { + draw_rect(Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value); + } + tl->set_align(HALIGN_LEFT); + } + tl->draw(ci, title_pos, code_completion_options[l].font_color); + } + + /* Draw a small scroll rectangle to show a position in the options. */ + if (scroll_width) { + float r = (float)code_completion_max_lines / code_completion_options_count; + float o = (float)code_completion_line_ofs / code_completion_options_count; + draw_rect(Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), code_completion_scroll_color); + } + } + + /* Code hint */ + if (caret_visible && code_hint != "" && (!code_completion_active || (code_completion_below != code_hint_draw_below))) { + const Ref<Font> font = cache.font; + const int font_height = font->get_height(cache.font_size); + Ref<StyleBox> sb = get_theme_stylebox("panel", "TooltipPanel"); + Color font_color = get_theme_color("font_color", "TooltipLabel"); + + Vector<String> code_hint_lines = code_hint.split("\n"); + int line_count = code_hint_lines.size(); + + int max_width = 0; + for (int i = 0; i < line_count; i++) { + max_width = MAX(max_width, font->get_string_size(code_hint_lines[i], cache.font_size).x); + } + Size2 minsize = sb->get_minimum_size() + Size2(max_width, line_count * font_height + (cache.line_spacing * line_count - 1)); + + int offset = font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), cache.font_size).x; + if (code_hint_xpos == -0xFFFF) { + code_hint_xpos = get_caret_draw_pos().x - offset; + } + Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y); + if (code_hint_draw_below) { + hint_ofs.y += cache.line_spacing / 2.0f; + } else { + hint_ofs.y -= (minsize.y + row_height) - cache.line_spacing; + } + + draw_style_box(sb, Rect2(hint_ofs, minsize)); + + int line_spacing = 0; + for (int i = 0; i < line_count; i++) { + const String &line = code_hint_lines[i]; + + int begin = 0; + int end = 0; + if (line.find(String::chr(0xFFFF)) != -1) { + begin = font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), cache.font_size).x; + end = font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), cache.font_size).x; + } + + Point2 round_ofs = hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent() + font_height * i + line_spacing); + round_ofs = round_ofs.round(); + draw_string(font, round_ofs, line.replace(String::chr(0xFFFF), ""), HALIGN_LEFT, -1, cache.font_size, font_color); + if (end > 0) { + Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font_height + font_height * i + line_spacing - 1); + draw_line(b, b + Vector2(end - begin, 0), font_color); + } + line_spacing += cache.line_spacing; + } + } } break; } } +void CodeEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { + Ref<InputEventMouseButton> mb = p_gui_input; + + if (mb.is_valid()) { + if (code_completion_active && code_completion_rect.has_point(mb->get_position())) { + if (!mb->is_pressed()) { + return; + } + + switch (mb->get_button_index()) { + case MOUSE_BUTTON_WHEEL_UP: { + if (code_completion_current_selected > 0) { + code_completion_current_selected--; + update(); + } + } break; + case MOUSE_BUTTON_WHEEL_DOWN: { + if (code_completion_current_selected < code_completion_options.size() - 1) { + code_completion_current_selected++; + update(); + } + } break; + case MOUSE_BUTTON_LEFT: { + code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_row_height(), 0, code_completion_options.size() - 1); + if (mb->is_double_click()) { + confirm_code_completion(); + } + update(); + } break; + } + return; + } + cancel_code_completion(); + set_code_hint(""); + } + + Ref<InputEventKey> k = p_gui_input; + bool update_code_completion = false; + if (!k.is_valid()) { + TextEdit::_gui_input(p_gui_input); + return; + } + + /* If a modifier has been pressed, and nothing else, return. */ + if (!k->is_pressed() || k->get_keycode() == KEY_CTRL || k->get_keycode() == KEY_ALT || k->get_keycode() == KEY_SHIFT || k->get_keycode() == KEY_META) { + return; + } + + /* Allow unicode handling if: */ + /* No Modifiers are pressed (except shift) */ + bool allow_unicode_handling = !(k->is_command_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed()); + + /* AUTO-COMPLETE */ + if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) { + request_code_completion(true); + accept_event(); + return; + } + + if (code_completion_active) { + if (k->is_action("ui_up", true)) { + if (code_completion_current_selected > 0) { + code_completion_current_selected--; + } else { + code_completion_current_selected = code_completion_options.size() - 1; + } + update(); + accept_event(); + return; + } + if (k->is_action("ui_down", true)) { + if (code_completion_current_selected < code_completion_options.size() - 1) { + code_completion_current_selected++; + } else { + code_completion_current_selected = 0; + } + update(); + accept_event(); + return; + } + if (k->is_action("ui_page_up", true)) { + code_completion_current_selected = MAX(0, code_completion_current_selected - code_completion_max_lines); + update(); + accept_event(); + return; + } + if (k->is_action("ui_page_down", true)) { + code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + code_completion_max_lines); + update(); + accept_event(); + return; + } + if (k->is_action("ui_home", true)) { + code_completion_current_selected = 0; + update(); + accept_event(); + return; + } + if (k->is_action("ui_end", true)) { + code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + code_completion_max_lines); + update(); + accept_event(); + return; + } + if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) { + confirm_code_completion(k->is_action("ui_text_completion_replace", true)); + accept_event(); + return; + } + if (k->is_action("ui_cancel", true)) { + cancel_code_completion(); + accept_event(); + return; + } + if (k->is_action("ui_text_backspace", true)) { + backspace_at_cursor(); + _filter_code_completion_candidates(); + accept_event(); + return; + } + + if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) { + update_code_completion = true; + } else { + update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32); + } + + if (!update_code_completion) { + cancel_code_completion(); + } + } + + /* MISC */ + if (k->is_action("ui_cancel", true)) { + set_code_hint(""); + accept_event(); + return; + } + if (allow_unicode_handling && k->get_unicode() == ')') { + set_code_hint(""); + } + + /* Remove shift otherwise actions will not match. */ + k = k->duplicate(); + k->set_shift_pressed(false); + + if (k->is_action("ui_text_caret_up", true) || + k->is_action("ui_text_caret_down", true) || + k->is_action("ui_text_caret_line_start", true) || + k->is_action("ui_text_caret_line_end", true) || + k->is_action("ui_text_caret_page_up", true) || + k->is_action("ui_text_caret_page_down", true)) { + set_code_hint(""); + } + + TextEdit::_gui_input(p_gui_input); + + if (update_code_completion) { + _filter_code_completion_candidates(); + } +} + +Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const { + if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (is_readonly() && (!is_selecting_enabled() || get_line_count() == 0))) { + return CURSOR_ARROW; + } + return TextEdit::get_cursor_shape(p_pos); +} + /* Main Gutter */ void CodeEdit::_update_draw_main_gutter() { set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines); @@ -275,6 +600,455 @@ void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_regi folded_icon->draw_rect(get_canvas_item(), p_region, false, folding_color); } +/* Delimiters */ +// Strings +void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) { + _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING); +} + +void CodeEdit::remove_string_delimiter(const String &p_start_key) { + _remove_delimiter(p_start_key, TYPE_STRING); +} + +bool CodeEdit::has_string_delimiter(const String &p_start_key) const { + return _has_delimiter(p_start_key, TYPE_STRING); +} + +void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) { + _set_delimiters(p_string_delimiters, TYPE_STRING); +} + +void CodeEdit::clear_string_delimiters() { + _clear_delimiters(TYPE_STRING); +} + +TypedArray<String> CodeEdit::get_string_delimiters() const { + return _get_delimiters(TYPE_STRING); +} + +int CodeEdit::is_in_string(int p_line, int p_column) const { + return _is_in_delimiter(p_line, p_column, TYPE_STRING); +} + +// Comments +void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) { + _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT); +} + +void CodeEdit::remove_comment_delimiter(const String &p_start_key) { + _remove_delimiter(p_start_key, TYPE_COMMENT); +} + +bool CodeEdit::has_comment_delimiter(const String &p_start_key) const { + return _has_delimiter(p_start_key, TYPE_COMMENT); +} + +void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) { + _set_delimiters(p_comment_delimiters, TYPE_COMMENT); +} + +void CodeEdit::clear_comment_delimiters() { + _clear_delimiters(TYPE_COMMENT); +} + +TypedArray<String> CodeEdit::get_comment_delimiters() const { + return _get_delimiters(TYPE_COMMENT); +} + +int CodeEdit::is_in_comment(int p_line, int p_column) const { + return _is_in_delimiter(p_line, p_column, TYPE_COMMENT); +} + +String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const { + ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), ""); + return delimiters[p_delimiter_idx].start_key; +} + +String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const { + ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), ""); + return delimiters[p_delimiter_idx].end_key; +} + +Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const { + if (delimiters.size() == 0) { + return Point2(-1, -1); + } + ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1)); + ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1)); + + Point2 start_position; + start_position.y = -1; + start_position.x = -1; + + bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value()) != -1; + + /* Check the keys for this line. */ + for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) { + if (E->key() > p_column) { + break; + } + in_region = E->value() != -1; + start_position.x = in_region ? E->key() : -1; + } + + /* Region was found on this line and is not a multiline continuation. */ + if (start_position.x != -1 && start_position.x != get_line(p_line).length() + 1) { + start_position.y = p_line; + return start_position; + } + + /* Not in a region */ + if (!in_region) { + return start_position; + } + + /* Region starts on a previous line */ + for (int i = p_line - 1; i >= 0; i--) { + if (delimiter_cache[i].size() < 1) { + continue; + } + start_position.y = i; + start_position.x = delimiter_cache[i].back()->key(); + + /* Make sure it's not a multiline continuation. */ + if (start_position.x != get_line(i).length() + 1) { + break; + } + } + return start_position; +} + +Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const { + if (delimiters.size() == 0) { + return Point2(-1, -1); + } + ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1)); + ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1)); + + Point2 end_position; + end_position.y = -1; + end_position.x = -1; + + int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value(); + + /* Check the keys for this line. */ + for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) { + end_position.x = (E->value() == -1) ? E->key() : -1; + if (E->key() > p_column) { + break; + } + region = E->value(); + } + + /* Region was found on this line and is not a multiline continuation. */ + if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) { + end_position.y = p_line; + return end_position; + } + + /* Not in a region */ + if (region == -1) { + end_position.x = -1; + return end_position; + } + + /* Region ends on a later line */ + for (int i = p_line + 1; i < get_line_count(); i++) { + if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) { + continue; + } + end_position.x = delimiter_cache[i].front()->key(); + + /* Make sure it's not a multiline continuation. */ + if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) { + end_position.y = i; + break; + } + end_position.x = -1; + } + return end_position; +} + +/* Code hint */ +void CodeEdit::set_code_hint(const String &p_hint) { + code_hint = p_hint; + code_hint_xpos = -0xFFFF; + update(); +} + +void CodeEdit::set_code_hint_draw_below(bool p_below) { + code_hint_draw_below = p_below; + update(); +} + +/* Code Completion */ +void CodeEdit::set_code_completion_enabled(bool p_enable) { + code_completion_enabled = p_enable; +} + +bool CodeEdit::is_code_completion_enabled() const { + return code_completion_enabled; +} + +void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) { + code_completion_prefixes.clear(); + for (int i = 0; i < p_prefixes.size(); i++) { + code_completion_prefixes.insert(p_prefixes[i]); + } +} + +TypedArray<String> CodeEdit::get_code_completion_prefixes() const { + TypedArray<String> prefixes; + for (Set<String>::Element *E = code_completion_prefixes.front(); E; E = E->next()) { + prefixes.push_back(E->get()); + } + return prefixes; +} + +String CodeEdit::get_text_for_code_completion() const { + StringBuilder completion_text; + const int text_size = get_line_count(); + for (int i = 0; i < text_size; i++) { + String line = get_line(i); + + if (i == cursor_get_line()) { + completion_text += line.substr(0, cursor_get_column()); + /* Not unicode, represents the caret. */ + completion_text += String::chr(0xFFFF); + completion_text += line.substr(cursor_get_column(), line.size()); + } else { + completion_text += line; + } + + if (i != text_size - 1) { + completion_text += "\n"; + } + } + return completion_text.as_string(); +} + +void CodeEdit::request_code_completion(bool p_force) { + ScriptInstance *si = get_script_instance(); + if (si && si->has_method("_request_code_completion")) { + si->call("_request_code_completion", p_force); + return; + } + + /* Don't re-query if all existing options are quoted types, eg path, signal. */ + bool ignored = code_completion_active && !code_completion_options.is_empty(); + if (ignored) { + ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT; + const ScriptCodeCompletionOption *previous_option = nullptr; + for (int i = 0; i < code_completion_options.size(); i++) { + const ScriptCodeCompletionOption ¤t_option = code_completion_options[i]; + if (!previous_option) { + previous_option = ¤t_option; + kind = current_option.kind; + } + if (previous_option->kind != current_option.kind) { + ignored = false; + break; + } + } + ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL); + } + + if (ignored) { + return; + } + + if (p_force) { + emit_signal("request_code_completion"); + return; + } + + String line = get_line(cursor_get_line()); + int ofs = CLAMP(cursor_get_column(), 0, line.length()); + + if (ofs > 0 && (is_in_string(cursor_get_line(), ofs) != -1 || _is_char(line[ofs - 1]) || code_completion_prefixes.has(String::chr(line[ofs - 1])))) { + emit_signal("request_code_completion"); + } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[ofs - 2]))) { + emit_signal("request_code_completion"); + } +} + +void CodeEdit::add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color, const RES &p_icon, const Variant &p_value) { + ScriptCodeCompletionOption completion_option; + completion_option.kind = (ScriptCodeCompletionOption::Kind)p_type; + completion_option.display = p_display_text; + completion_option.insert_text = p_insert_text; + completion_option.font_color = p_text_color; + completion_option.icon = p_icon; + completion_option.default_value = p_value; + code_completion_option_submitted.push_back(completion_option); +} + +void CodeEdit::update_code_completion_options(bool p_forced) { + code_completion_forced = p_forced; + code_completion_option_sources = code_completion_option_submitted; + code_completion_option_submitted.clear(); + _filter_code_completion_candidates(); +} + +TypedArray<Dictionary> CodeEdit::get_code_completion_options() const { + if (!code_completion_active) { + return TypedArray<Dictionary>(); + } + + TypedArray<Dictionary> completion_options; + completion_options.resize(code_completion_options.size()); + for (int i = 0; i < code_completion_options.size(); i++) { + Dictionary option; + option["kind"] = code_completion_options[i].kind; + option["display_text"] = code_completion_options[i].display; + option["insert_text"] = code_completion_options[i].insert_text; + option["font_color"] = code_completion_options[i].font_color; + option["icon"] = code_completion_options[i].icon; + option["default_value"] = code_completion_options[i].default_value; + completion_options[i] = option; + } + return completion_options; +} + +Dictionary CodeEdit::get_code_completion_option(int p_index) const { + if (!code_completion_active) { + return Dictionary(); + } + ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary()); + + Dictionary option; + option["kind"] = code_completion_options[p_index].kind; + option["display_text"] = code_completion_options[p_index].display; + option["insert_text"] = code_completion_options[p_index].insert_text; + option["font_color"] = code_completion_options[p_index].font_color; + option["icon"] = code_completion_options[p_index].icon; + option["default_value"] = code_completion_options[p_index].default_value; + return option; +} + +int CodeEdit::get_code_completion_selected_index() const { + return (code_completion_active) ? code_completion_current_selected : -1; +} + +void CodeEdit::set_code_completion_selected_index(int p_index) { + if (!code_completion_active) { + return; + } + ERR_FAIL_INDEX(p_index, code_completion_options.size()); + code_completion_current_selected = p_index; + update(); +} + +void CodeEdit::confirm_code_completion(bool p_replace) { + if (is_readonly() || !code_completion_active) { + return; + } + + ScriptInstance *si = get_script_instance(); + if (si && si->has_method("_confirm_code_completion")) { + si->call("_confirm_code_completion", p_replace); + return; + } + begin_complex_operation(); + + int caret_line = cursor_get_line(); + + const String &insert_text = code_completion_options[code_completion_current_selected].insert_text; + const String &display_text = code_completion_options[code_completion_current_selected].display; + + if (p_replace) { + /* Find end of current section */ + const String line = get_line(caret_line); + int caret_col = cursor_get_column(); + int caret_remove_line = caret_line; + + bool merge_text = true; + int in_string = is_in_string(caret_line, caret_col); + if (in_string != -1) { + Point2 string_end = get_delimiter_end_position(caret_line, caret_col); + if (string_end.x != -1) { + merge_text = false; + caret_remove_line = string_end.y; + caret_col = string_end.x - 1; + } + } + + if (merge_text) { + for (; caret_col < line.length(); caret_col++) { + if (!_is_char(line[caret_col])) { + break; + } + } + } + + /* Replace. */ + _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_remove_line, caret_col); + cursor_set_column(cursor_get_column() - code_completion_base.length(), false); + insert_text_at_cursor(insert_text); + } else { + /* Get first non-matching char. */ + const String line = get_line(caret_line); + int caret_col = cursor_get_column(); + int matching_chars = code_completion_base.length(); + for (; matching_chars <= insert_text.length(); matching_chars++) { + if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) { + break; + } + caret_col++; + } + + /* Remove base completion text. */ + _remove_text(caret_line, cursor_get_column() - code_completion_base.length(), caret_line, cursor_get_column()); + cursor_set_column(cursor_get_column() - code_completion_base.length(), false); + + /* Merge with text. */ + insert_text_at_cursor(insert_text.substr(0, code_completion_base.length())); + cursor_set_column(caret_col, false); + insert_text_at_cursor(insert_text.substr(matching_chars)); + } + + /* TODO: merge with autobrace completion, when in CodeEdit. */ + /* Handle merging of symbols eg strings, brackets. */ + const String line = get_line(caret_line); + char32_t next_char = line[cursor_get_column()]; + char32_t last_completion_char = insert_text[insert_text.length() - 1]; + char32_t last_completion_char_display = display_text[display_text.length() - 1]; + + if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) { + _remove_text(caret_line, cursor_get_column(), caret_line, cursor_get_column() + 1); + } + + if (last_completion_char == '(') { + if (next_char == last_completion_char) { + _remove_text(caret_line, cursor_get_column() - 1, caret_line, cursor_get_column()); + } else if (auto_brace_completion_enabled) { + insert_text_at_cursor(")"); + cursor_set_column(cursor_get_column() - 1); + } + } else if (last_completion_char == ')' && next_char == '(') { + _remove_text(caret_line, cursor_get_column() - 2, caret_line, cursor_get_column()); + if (line[cursor_get_column() + 1] != ')') { + cursor_set_column(cursor_get_column() - 1); + } + } + + end_complex_operation(); + + cancel_code_completion(); + if (last_completion_char == '(') { + request_code_completion(); + } +} + +void CodeEdit::cancel_code_completion() { + if (!code_completion_active) { + return; + } + code_completion_forced = false; + code_completion_active = false; + update(); +} + void CodeEdit::_bind_methods() { /* Main Gutter */ ClassDB::bind_method(D_METHOD("_main_gutter_draw_callback"), &CodeEdit::_main_gutter_draw_callback); @@ -320,6 +1094,76 @@ void CodeEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter); ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter); + /* Delimiters */ + // Strings + ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter); + ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter); + + ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters); + ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters); + ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters); + + ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1)); + + // Comments + ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter); + ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter); + + ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters); + ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters); + ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters); + + ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1)); + + // Util + ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key); + ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key); + + ClassDB::bind_method(D_METHOD("get_delimiter_start_postion", "line", "column"), &CodeEdit::get_delimiter_start_position); + ClassDB::bind_method(D_METHOD("get_delimiter_end_postion", "line", "column"), &CodeEdit::get_delimiter_end_position); + + /* Code hint */ + ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint); + ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below); + + /* Code Completion */ + BIND_ENUM_CONSTANT(KIND_CLASS); + BIND_ENUM_CONSTANT(KIND_FUNCTION); + BIND_ENUM_CONSTANT(KIND_SIGNAL); + BIND_ENUM_CONSTANT(KIND_VARIABLE); + BIND_ENUM_CONSTANT(KIND_MEMBER); + BIND_ENUM_CONSTANT(KIND_ENUM); + BIND_ENUM_CONSTANT(KIND_CONSTANT); + BIND_ENUM_CONSTANT(KIND_NODE_PATH); + BIND_ENUM_CONSTANT(KIND_FILE_PATH); + BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT); + + ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion); + ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_code_completion_option", "type", "display_text", "insert_text", "text_color", "icon", "value"), &CodeEdit::add_code_completion_option, DEFVAL(Color(1, 1, 1)), DEFVAL(RES()), DEFVAL(Variant::NIL)); + ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options); + ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options); + ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option); + ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index); + ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index); + + ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion); + + ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled); + ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled); + + ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes); + ClassDB::bind_method(D_METHOD("get_code_comletion_prefixes"), &CodeEdit::get_code_completion_prefixes); + + // Overridable + BIND_VMETHOD(MethodInfo("_confirm_code_completion", PropertyInfo(Variant::BOOL, "replace"))); + BIND_VMETHOD(MethodInfo("_request_code_completion", PropertyInfo(Variant::BOOL, "force"))); + BIND_VMETHOD(MethodInfo(Variant::ARRAY, "_filter_code_completion_candidates", PropertyInfo(Variant::ARRAY, "candidates"))); + + /* Inspector */ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter"); @@ -331,7 +1175,17 @@ void CodeEdit::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter"); + ADD_GROUP("Delimiters", "delimiter_"); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters"); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters"); + + ADD_GROUP("Code Completion", "code_completion_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled"), "set_code_completion_enabled", "is_code_completion_enabled"); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_comletion_prefixes"); + + /* Signals */ ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line"))); + ADD_SIGNAL(MethodInfo("request_code_completion")); } void CodeEdit::_gutter_clicked(int p_line, int p_gutter) { @@ -360,7 +1214,566 @@ void CodeEdit::_gutter_clicked(int p_line, int p_gutter) { } } +void CodeEdit::_update_gutter_indexes() { + for (int i = 0; i < get_gutter_count(); i++) { + if (get_gutter_name(i) == "main_gutter") { + main_gutter = i; + continue; + } + + if (get_gutter_name(i) == "line_numbers") { + line_number_gutter = i; + continue; + } + + if (get_gutter_name(i) == "fold_gutter") { + fold_gutter = i; + continue; + } + } +} + +/* Delimiters */ +void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) { + if (delimiters.size() == 0) { + return; + } + + int line_count = get_line_count(); + if (p_to_line == -1) { + p_to_line = line_count; + } + + int start_line = MIN(p_from_line, p_to_line); + int end_line = MAX(p_from_line, p_to_line); + + /* Make sure delimiter_cache has all the lines. */ + if (start_line != end_line) { + if (p_to_line < p_from_line) { + for (int i = end_line; i > start_line; i--) { + delimiter_cache.remove(i); + } + } else { + for (int i = start_line; i < end_line; i++) { + delimiter_cache.insert(i, Map<int, int>()); + } + } + } + + int in_region = -1; + for (int i = start_line; i < MIN(end_line + 1, line_count); i++) { + int current_end_region = (i <= 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value(); + in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value(); + + const String &str = get_line(i); + const int line_length = str.length(); + delimiter_cache.write[i].clear(); + + if (str.length() == 0) { + if (in_region != -1) { + delimiter_cache.write[i][0] = in_region; + } + if (i == end_line && current_end_region != in_region) { + end_line++; + end_line = MIN(end_line, line_count); + } + continue; + } + + int end_region = -1; + for (int j = 0; j < line_length; j++) { + int from = j; + for (; from < line_length; from++) { + if (str[from] == '\\') { + from++; + continue; + } + break; + } + + /* check if we are in entering a region */ + bool same_line = false; + if (in_region == -1) { + for (int d = 0; d < delimiters.size(); d++) { + /* check there is enough room */ + int chars_left = line_length - from; + int start_key_length = delimiters[d].start_key.length(); + int end_key_length = delimiters[d].end_key.length(); + if (chars_left < start_key_length) { + continue; + } + + /* search the line */ + bool match = true; + const char32_t *start_key = delimiters[d].start_key.get_data(); + for (int k = 0; k < start_key_length; k++) { + if (start_key[k] != str[from + k]) { + match = false; + break; + } + } + if (!match) { + continue; + } + same_line = true; + in_region = d; + delimiter_cache.write[i][from + 1] = d; + from += start_key_length; + + /* check if it's the whole line */ + if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) { + j = line_length; + if (delimiters[d].line_only) { + delimiter_cache.write[i][line_length + 1] = -1; + } else { + end_region = in_region; + } + } + break; + } + + if (j == line_length || in_region == -1) { + continue; + } + } + + /* if we are in one find the end key */ + /* search the line */ + int region_end_index = -1; + int end_key_length = delimiters[in_region].end_key.length(); + const char32_t *end_key = delimiters[in_region].end_key.get_data(); + for (; from < line_length; from++) { + if (line_length - from < end_key_length) { + break; + } + + if (!is_symbol(str[from])) { + continue; + } + + if (str[from] == '\\') { + from++; + continue; + } + + region_end_index = from; + for (int k = 0; k < end_key_length; k++) { + if (end_key[k] != str[from + k]) { + region_end_index = -1; + break; + } + } + + if (region_end_index != -1) { + break; + } + } + + j = from + (end_key_length - 1); + end_region = (region_end_index == -1) ? in_region : -1; + if (!same_line || region_end_index != -1) { + delimiter_cache.write[i][j + 1] = end_region; + } + in_region = -1; + } + + if (i == end_line && current_end_region != end_region) { + end_line++; + end_line = MIN(end_line, line_count); + } + } +} + +int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const { + if (delimiters.size() == 0) { + return -1; + } + ERR_FAIL_INDEX_V(p_line, get_line_count(), 0); + + int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value(); + bool in_region = region != -1 && delimiters[region].type == p_type; + for (Map<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) { + /* If column is specified, loop untill the key is larger then the column. */ + if (p_column != -1) { + if (E->key() > p_column) { + break; + } + in_region = E->value() != -1 && delimiters[E->value()].type == p_type; + region = in_region ? E->value() : -1; + continue; + } + + /* If no column, calulate if the entire line is a region */ + /* excluding whitespace. */ + const String line = get_line(p_line); + if (!in_region) { + if (E->value() == -1 || delimiters[E->value()].type != p_type) { + break; + } + + region = E->value(); + in_region = true; + for (int i = E->key() - 2; i >= 0; i--) { + if (!_is_whitespace(line[i])) { + return -1; + } + } + } + + if (delimiters[region].line_only) { + return region; + } + + int end_col = E->key(); + if (E->value() != -1) { + if (!E->next()) { + return region; + } + end_col = E->next()->key(); + } + + for (int i = end_col; i < line.length(); i++) { + if (!_is_whitespace(line[i])) { + return -1; + } + } + return region; + } + return in_region ? region : -1; +} + +void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) { + if (p_start_key.length() > 0) { + for (int i = 0; i < p_start_key.length(); i++) { + ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol"); + } + } + + if (p_end_key.length() > 0) { + for (int i = 0; i < p_end_key.length(); i++) { + ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol"); + } + } + + int at = 0; + for (int i = 0; i < delimiters.size(); i++) { + ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists."); + if (p_start_key.length() < delimiters[i].start_key.length()) { + at++; + } + } + + Delimiter delimiter; + delimiter.type = p_type; + delimiter.start_key = p_start_key; + delimiter.end_key = p_end_key; + delimiter.line_only = p_line_only || p_end_key == ""; + delimiters.insert(at, delimiter); + if (!setting_delimiters) { + delimiter_cache.clear(); + _update_delimiter_cache(); + } +} + +void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) { + for (int i = 0; i < delimiters.size(); i++) { + if (delimiters[i].start_key != p_start_key) { + continue; + } + + if (delimiters[i].type != p_type) { + break; + } + + delimiters.remove(i); + if (!setting_delimiters) { + delimiter_cache.clear(); + _update_delimiter_cache(); + } + break; + } +} + +bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const { + for (int i = 0; i < delimiters.size(); i++) { + if (delimiters[i].start_key == p_start_key) { + return delimiters[i].type == p_type; + } + } + return false; +} + +void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) { + setting_delimiters = true; + _clear_delimiters(p_type); + + for (int i = 0; i < p_delimiters.size(); i++) { + String key = p_delimiters[i].is_null() ? "" : p_delimiters[i]; + + const String start_key = key.get_slice(" ", 0); + const String end_key = key.get_slice_count(" ") > 1 ? key.get_slice(" ", 1) : String(); + + _add_delimiter(start_key, end_key, end_key == "", p_type); + } + setting_delimiters = false; + _update_delimiter_cache(); +} + +void CodeEdit::_clear_delimiters(DelimiterType p_type) { + for (int i = delimiters.size() - 1; i >= 0; i--) { + if (delimiters[i].type == p_type) { + delimiters.remove(i); + } + } + delimiter_cache.clear(); +} + +TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const { + TypedArray<String> r_delimiters; + for (int i = 0; i < delimiters.size(); i++) { + if (delimiters[i].type != p_type) { + continue; + } + r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key)); + } + return r_delimiters; +} + +/* Code Completion */ +void CodeEdit::_filter_code_completion_candidates() { + ScriptInstance *si = get_script_instance(); + if (si && si->has_method("_filter_code_completion_candidates")) { + code_completion_options.clear(); + code_completion_base = ""; + + /* Build options argument. */ + TypedArray<Dictionary> completion_options_sources; + completion_options_sources.resize(code_completion_option_sources.size()); + int i = 0; + for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) { + Dictionary option; + option["kind"] = E->get().kind; + option["display_text"] = E->get().display; + option["insert_text"] = E->get().insert_text; + option["font_color"] = E->get().font_color; + option["icon"] = E->get().icon; + option["default_value"] = E->get().default_value; + completion_options_sources[i] = option; + i++; + } + + TypedArray<Dictionary> completion_options = si->call("_filter_code_completion_candidates", completion_options_sources); + + /* No options to complete, cancel. */ + if (completion_options.size() == 0) { + cancel_code_completion(); + return; + } + + /* Convert back into options. */ + int max_width = 0; + for (i = 0; i < completion_options.size(); i++) { + ScriptCodeCompletionOption option; + option.kind = (ScriptCodeCompletionOption::Kind)(int)completion_options[i].get("kind"); + option.display = completion_options[i].get("display_text"); + option.insert_text = completion_options[i].get("insert_text"); + option.font_color = completion_options[i].get("font_color"); + option.icon = completion_options[i].get("icon"); + option.default_value = completion_options[i].get("default_value"); + + max_width = MAX(max_width, cache.font->get_string_size(option.display).width); + code_completion_options.push_back(option); + } + + code_completion_longest_line = MIN(max_width, code_completion_max_width); + code_completion_current_selected = 0; + code_completion_active = true; + update(); + return; + } + + const int caret_line = cursor_get_line(); + const int caret_column = cursor_get_column(); + const String line = get_line(caret_line); + + if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) { + cancel_code_completion(); + return; + } + + /* Get string status, are we in one or at the close. */ + int in_string = is_in_string(caret_line, caret_column); + int first_quote_col = -1; + if (in_string != -1) { + Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column); + first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1; + } else if (caret_column > 0) { + if (is_in_string(caret_line, caret_column - 1) != -1) { + first_quote_col = caret_column - 1; + } + } + + int cofs = caret_column; + String string_to_complete; + bool prev_is_word = false; + + /* Cancel if we are at the close of a string. */ + if (in_string == -1 && first_quote_col == cofs - 1) { + cancel_code_completion(); + return; + /* In a string, therefore we are trying to complete the string text. */ + } else if (in_string != -1 && first_quote_col != -1) { + int key_length = delimiters[in_string].start_key.length(); + string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length); + /* If we have a space, previous word might be a keyword. eg "func |". */ + } else if (cofs > 0 && line[cofs - 1] == ' ') { + int ofs = cofs - 1; + while (ofs >= 0 && line[ofs] == ' ') { + ofs--; + } + prev_is_word = _is_char(line[ofs]); + /* Otherwise get current word and set cofs to the start. */ + } else { + int start_cofs = cofs; + while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || _is_char(line[cofs - 1]))) { + cofs--; + } + string_to_complete = line.substr(cofs, start_cofs - cofs); + } + + /* If all else fails, check for a prefix. */ + /* Single space between caret and prefix is okay. */ + bool prev_is_prefix = false; + if (cofs > 0 && code_completion_prefixes.has(String::chr(line[cofs - 1]))) { + prev_is_prefix = true; + } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(String::chr(line[cofs - 2]))) { + prev_is_prefix = true; + } + + if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) { + cancel_code_completion(); + return; + } + + /* Filter Options. */ + /* For now handle only tradional quoted strings. */ + bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'"; + + code_completion_options.clear(); + code_completion_base = string_to_complete; + + Vector<ScriptCodeCompletionOption> completion_options_casei; + Vector<ScriptCodeCompletionOption> completion_options_subseq; + Vector<ScriptCodeCompletionOption> completion_options_subseq_casei; + + int max_width = 0; + String string_to_complete_lower = string_to_complete.to_lower(); + for (List<ScriptCodeCompletionOption>::Element *E = code_completion_option_sources.front(); E; E = E->next()) { + ScriptCodeCompletionOption &option = E->get(); + + if (single_quote && option.display.is_quoted()) { + option.display = option.display.unquote().quote("'"); + } + + if (in_string != -1) { + String quote = single_quote ? "'" : "\""; + option.display = option.display.unquote().quote(quote); + option.insert_text = option.insert_text.unquote().quote(quote); + } + + if (option.display.length() == 0) { + continue; + } + + if (string_to_complete.length() == 0) { + code_completion_options.push_back(option); + max_width = MAX(max_width, cache.font->get_string_size(option.display).width); + continue; + } + + /* This code works the same as: + + if (option.display.begins_with(s)) { + completion_options.push_back(option); + } else if (option.display.to_lower().begins_with(s.to_lower())) { + completion_options_casei.push_back(option); + } else if (s.is_subsequence_of(option.display)) { + completion_options_subseq.push_back(option); + } else if (s.is_subsequence_ofi(option.display)) { + completion_options_subseq_casei.push_back(option); + } + + But is more performant due to being inlined and looping over the characters only once + */ + + String display_lower = option.display.to_lower(); + + const char32_t *ssq = &string_to_complete[0]; + const char32_t *ssq_lower = &string_to_complete_lower[0]; + + const char32_t *tgt = &option.display[0]; + const char32_t *tgt_lower = &display_lower[0]; + + const char32_t *ssq_last_tgt = nullptr; + const char32_t *ssq_lower_last_tgt = nullptr; + + for (; *tgt; tgt++, tgt_lower++) { + if (*ssq == *tgt) { + ssq++; + ssq_last_tgt = tgt; + } + if (*ssq_lower == *tgt_lower) { + ssq_lower++; + ssq_lower_last_tgt = tgt; + } + } + + /* Matched the whole subsequence in s. */ + if (!*ssq) { + /* Finished matching in the first s.length() characters. */ + if (ssq_last_tgt == &option.display[string_to_complete.length() - 1]) { + code_completion_options.push_back(option); + } else { + completion_options_subseq.push_back(option); + } + max_width = MAX(max_width, cache.font->get_string_size(option.display).width); + /* Matched the whole subsequence in s_lower. */ + } else if (!*ssq_lower) { + /* Finished matching in the first s.length() characters. */ + if (ssq_lower_last_tgt == &option.display[string_to_complete.length() - 1]) { + completion_options_casei.push_back(option); + } else { + completion_options_subseq_casei.push_back(option); + } + max_width = MAX(max_width, cache.font->get_string_size(option.display).width); + } + } + + code_completion_options.append_array(completion_options_casei); + code_completion_options.append_array(completion_options_subseq); + code_completion_options.append_array(completion_options_subseq_casei); + + /* No options to complete, cancel. */ + if (code_completion_options.size() == 0) { + cancel_code_completion(); + return; + } + + /* A perfect match, stop completion. */ + if (code_completion_options.size() == 1 && string_to_complete == code_completion_options[0].display) { + cancel_code_completion(); + return; + } + + code_completion_longest_line = MIN(max_width, code_completion_max_width); + code_completion_current_selected = 0; + code_completion_active = true; + update(); +} + void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) { + _update_delimiter_cache(p_from_line, p_to_line); + if (p_from_line == p_to_line) { return; } @@ -392,25 +1805,6 @@ void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) { } } -void CodeEdit::_update_gutter_indexes() { - for (int i = 0; i < get_gutter_count(); i++) { - if (get_gutter_name(i) == "main_gutter") { - main_gutter = i; - continue; - } - - if (get_gutter_name(i) == "line_numbers") { - line_number_gutter = i; - continue; - } - - if (get_gutter_name(i) == "fold_gutter") { - fold_gutter = i; - continue; - } - } -} - CodeEdit::CodeEdit() { /* Text Direction */ set_layout_direction(LAYOUT_DIRECTION_LTR); diff --git a/scene/gui/code_edit.h b/scene/gui/code_edit.h index d0c39ec0f1..6305eacf83 100644 --- a/scene/gui/code_edit.h +++ b/scene/gui/code_edit.h @@ -36,6 +36,22 @@ class CodeEdit : public TextEdit { GDCLASS(CodeEdit, TextEdit) +public: + /* Keep enum in sync with: */ + /* /core/object/script_language.h - ScriptCodeCompletionOption::Kind */ + enum CodeCompletionKind { + KIND_CLASS, + KIND_FUNCTION, + KIND_SIGNAL, + KIND_VARIABLE, + KIND_MEMBER, + KIND_ENUM, + KIND_CONSTANT, + KIND_NODE_PATH, + KIND_FILE_PATH, + KIND_PLAIN_TEXT, + }; + private: /* Main Gutter */ enum MainGutterType { @@ -80,16 +96,113 @@ private: void _fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region); void _gutter_clicked(int p_line, int p_gutter); - void _lines_edited_from(int p_from_line, int p_to_line); - void _update_gutter_indexes(); + /* Delimiters */ + enum DelimiterType { + TYPE_STRING, + TYPE_COMMENT, + }; + + struct Delimiter { + DelimiterType type; + String start_key = ""; + String end_key = ""; + bool line_only = true; + }; + bool setting_delimiters = false; + Vector<Delimiter> delimiters; + /* + * Vector entry per line, contains a Map of column numbers to delimiter index, -1 marks the end of a region. + * e.g the following text will be stored as so: + * + * 0: nothing here + * 1: + * 2: # test + * 3: "test" text "multiline + * 4: + * 5: test + * 6: string" + * + * Vector [ + * 0 = [] + * 1 = [] + * 2 = [ + * 1 = 1 + * 6 = -1 + * ] + * 3 = [ + * 1 = 0 + * 6 = -1 + * 13 = 0 + * ] + * 4 = [ + * 0 = 0 + * ] + * 5 = [ + * 5 = 0 + * ] + * 6 = [ + * 7 = -1 + * ] + * ] + */ + Vector<Map<int, int>> delimiter_cache; + + void _update_delimiter_cache(int p_from_line = 0, int p_to_line = -1); + int _is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const; + + void _add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type); + void _remove_delimiter(const String &p_start_key, DelimiterType p_type); + bool _has_delimiter(const String &p_start_key, DelimiterType p_type) const; + + void _set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type); + void _clear_delimiters(DelimiterType p_type); + TypedArray<String> _get_delimiters(DelimiterType p_type) const; + + /* Code Hint */ + String code_hint = ""; + + bool code_hint_draw_below = true; + int code_hint_xpos = -0xFFFF; + + /* Code Completion */ + bool code_completion_enabled = false; + bool code_completion_forced = false; + + int code_completion_max_width = 0; + int code_completion_max_lines = 7; + int code_completion_scroll_width = 0; + Color code_completion_scroll_color = Color(0, 0, 0, 0); + Color code_completion_background_color = Color(0, 0, 0, 0); + Color code_completion_selected_color = Color(0, 0, 0, 0); + Color code_completion_existing_color = Color(0, 0, 0, 0); + + bool code_completion_active = false; + Vector<ScriptCodeCompletionOption> code_completion_options; + int code_completion_line_ofs = 0; + int code_completion_current_selected = 0; + int code_completion_longest_line = 0; + Rect2i code_completion_rect; + + Set<String> code_completion_prefixes; + List<ScriptCodeCompletionOption> code_completion_option_submitted; + List<ScriptCodeCompletionOption> code_completion_option_sources; + String code_completion_base; + + void _filter_code_completion_candidates(); + + void _lines_edited_from(int p_from_line, int p_to_line); + protected: + void _gui_input(const Ref<InputEvent> &p_gui_input) override; void _notification(int p_what); static void _bind_methods(); public: + virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override; + /* Main Gutter */ void set_draw_breakpoints_gutter(bool p_draw); bool is_drawing_breakpoints_gutter() const; @@ -128,8 +241,64 @@ public: void set_draw_fold_gutter(bool p_draw); bool is_drawing_fold_gutter() const; + /* Delimiters */ + void add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false); + void remove_string_delimiter(const String &p_start_key); + bool has_string_delimiter(const String &p_start_key) const; + + void set_string_delimiters(const TypedArray<String> &p_string_delimiters); + void clear_string_delimiters(); + TypedArray<String> get_string_delimiters() const; + + int is_in_string(int p_line, int p_column = -1) const; + + void add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false); + void remove_comment_delimiter(const String &p_start_key); + bool has_comment_delimiter(const String &p_start_key) const; + + void set_comment_delimiters(const TypedArray<String> &p_comment_delimiters); + void clear_comment_delimiters(); + TypedArray<String> get_comment_delimiters() const; + + int is_in_comment(int p_line, int p_column = -1) const; + + String get_delimiter_start_key(int p_delimiter_idx) const; + String get_delimiter_end_key(int p_delimiter_idx) const; + + Point2 get_delimiter_start_position(int p_line, int p_column) const; + Point2 get_delimiter_end_position(int p_line, int p_column) const; + + /* Code hint */ + void set_code_hint(const String &p_hint); + void set_code_hint_draw_below(bool p_below); + + /* Code Completion */ + void set_code_completion_enabled(bool p_enable); + bool is_code_completion_enabled() const; + + void set_code_completion_prefixes(const TypedArray<String> &p_prefixes); + TypedArray<String> get_code_completion_prefixes() const; + + String get_text_for_code_completion() const; + + void request_code_completion(bool p_force = false); + + void add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color = Color(1, 1, 1), const RES &p_icon = RES(), const Variant &p_value = Variant::NIL); + void update_code_completion_options(bool p_forced = false); + + TypedArray<Dictionary> get_code_completion_options() const; + Dictionary get_code_completion_option(int p_index) const; + + int get_code_completion_selected_index() const; + void set_code_completion_selected_index(int p_index); + + void confirm_code_completion(bool p_replace = false); + void cancel_code_completion(); + CodeEdit(); ~CodeEdit(); }; +VARIANT_ENUM_CAST(CodeEdit::CodeCompletionKind); + #endif // CODEEDIT_H diff --git a/scene/gui/text_edit.cpp b/scene/gui/text_edit.cpp index 65d4433f4e..07ccad70b1 100644 --- a/scene/gui/text_edit.cpp +++ b/scene/gui/text_edit.cpp @@ -801,9 +801,6 @@ void TextEdit::_notification(int p_what) { } } - bool is_cursor_line_visible = false; - Point2 cursor_pos; - // Get the highlighted words. String highlighted_text = get_selection_text(); @@ -987,6 +984,8 @@ void TextEdit::_notification(int p_what) { } // draw main text + cursor.visible = false; + const int caret_wrap_index = get_cursor_wrap_index(); int row_height = get_row_height(); int line = first_visible_line; for (int i = 0; i < draw_amount; i++) { @@ -1267,7 +1266,6 @@ void TextEdit::_notification(int p_what) { } } - const int line_top_offset_y = ofs_y; ofs_y += (row_height - text_height) / 2; const Vector<TextServer::Glyph> visual = TS->shaped_text_get_glyphs(rid); @@ -1372,9 +1370,9 @@ void TextEdit::_notification(int p_what) { #else int caret_width = 1; #endif - if (!clipped && cursor.line == line && ((line_wrap_index == line_wrap_amount) || (cursor.column != TS->shaped_text_get_range(rid).y))) { - is_cursor_line_visible = true; - cursor_pos.y = line_top_offset_y; + + if (!clipped && cursor.line == line && line_wrap_index == caret_wrap_index) { + cursor.draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index); if (ime_text.length() == 0) { Rect2 l_caret, t_caret; @@ -1395,57 +1393,60 @@ void TextEdit::_notification(int p_what) { } if ((l_caret != Rect2() && (l_dir == TextServer::DIRECTION_AUTO || l_dir == (TextServer::Direction)input_direction)) || (t_caret == Rect2())) { - cursor_pos.x = char_margin + ofs_x + l_caret.position.x; + cursor.draw_pos.x = char_margin + ofs_x + l_caret.position.x; } else { - cursor_pos.x = char_margin + ofs_x + t_caret.position.x; + cursor.draw_pos.x = char_margin + ofs_x + t_caret.position.x; } - if (draw_caret && cursor_pos.x >= xmargin_beg && cursor_pos.x < xmargin_end) { - if (block_caret || insert_mode) { - //Block or underline caret, draw trailing carets at full height. - int h = cache.font->get_height(cache.font_size); - - if (t_caret != Rect2()) { - if (insert_mode) { - t_caret.position.y = TS->shaped_text_get_descent(rid); - t_caret.size.y = caret_width; - } else { - t_caret.position.y = -TS->shaped_text_get_ascent(rid); - t_caret.size.y = h; - } - t_caret.position += Vector2(char_margin + ofs_x, ofs_y); + if (cursor.draw_pos.x >= xmargin_beg && cursor.draw_pos.x < xmargin_end) { + cursor.visible = true; + if (draw_caret) { + if (block_caret || insert_mode) { + //Block or underline caret, draw trailing carets at full height. + int h = cache.font->get_height(cache.font_size); + + if (t_caret != Rect2()) { + if (insert_mode) { + t_caret.position.y = TS->shaped_text_get_descent(rid); + t_caret.size.y = caret_width; + } else { + t_caret.position.y = -TS->shaped_text_get_ascent(rid); + t_caret.size.y = h; + } + t_caret.position += Vector2(char_margin + ofs_x, ofs_y); + + draw_rect(t_caret, cache.caret_color, false); + } else { // End of the line. + if (insert_mode) { + l_caret.position.y = TS->shaped_text_get_descent(rid); + l_caret.size.y = caret_width; + } else { + l_caret.position.y = -TS->shaped_text_get_ascent(rid); + l_caret.size.y = h; + } + l_caret.position += Vector2(char_margin + ofs_x, ofs_y); + l_caret.size.x = cache.font->get_char_size('M', 0, cache.font_size).x; - draw_rect(t_caret, cache.caret_color, false); - } else { // End of the line. - if (insert_mode) { - l_caret.position.y = TS->shaped_text_get_descent(rid); - l_caret.size.y = caret_width; - } else { - l_caret.position.y = -TS->shaped_text_get_ascent(rid); - l_caret.size.y = h; + draw_rect(l_caret, cache.caret_color, false); + } + } else { + // Normal caret. + if (l_caret != Rect2() && l_dir == TextServer::DIRECTION_AUTO) { + // Draw extra marker on top of mid caret. + Rect2 trect = Rect2(l_caret.position.x - 3 * caret_width, l_caret.position.y, 6 * caret_width, caret_width); + trect.position += Vector2(char_margin + ofs_x, ofs_y); + RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, cache.caret_color); } l_caret.position += Vector2(char_margin + ofs_x, ofs_y); - l_caret.size.x = cache.font->get_char_size('M', 0, cache.font_size).x; + l_caret.size.x = caret_width; - draw_rect(l_caret, cache.caret_color, false); - } - } else { - // Normal caret. - if (l_caret != Rect2() && l_dir == TextServer::DIRECTION_AUTO) { - // Draw extra marker on top of mid caret. - Rect2 trect = Rect2(l_caret.position.x - 3 * caret_width, l_caret.position.y, 6 * caret_width, caret_width); - trect.position += Vector2(char_margin + ofs_x, ofs_y); - RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, cache.caret_color); - } - l_caret.position += Vector2(char_margin + ofs_x, ofs_y); - l_caret.size.x = caret_width; - - draw_rect(l_caret, cache.caret_color); + draw_rect(l_caret, cache.caret_color); - t_caret.position += Vector2(char_margin + ofs_x, ofs_y); - t_caret.size.x = caret_width; + t_caret.position += Vector2(char_margin + ofs_x, ofs_y); + t_caret.size.x = caret_width; - draw_rect(t_caret, cache.caret_color); + draw_rect(t_caret, cache.caret_color); + } } } } else { @@ -1465,7 +1466,7 @@ void TextEdit::_notification(int p_what) { } rect.size.y = caret_width; draw_rect(rect, cache.caret_color); - cursor_pos.x = rect.position.x; + cursor.draw_pos.x = rect.position.x; } } { @@ -1484,7 +1485,7 @@ void TextEdit::_notification(int p_what) { } rect.size.y = caret_width * 3; draw_rect(rect, cache.caret_color); - cursor_pos.x = rect.position.x; + cursor.draw_pos.x = rect.position.x; } } } @@ -1492,227 +1493,10 @@ void TextEdit::_notification(int p_what) { } } - bool completion_below = false; - if (completion_active && is_cursor_line_visible && completion_options.size() > 0) { - // Completion panel - - const Ref<StyleBox> csb = get_theme_stylebox("completion"); - const int maxlines = get_theme_constant("completion_lines"); - const int cmax_width = get_theme_constant("completion_max_width") * cache.font->get_char_size('x', 0, cache.font_size).x; - const Color scrollc = get_theme_color("completion_scroll_color"); - - const int completion_options_size = completion_options.size(); - const int row_count = MIN(completion_options_size, maxlines); - const int completion_rows_height = row_count * row_height; - const int completion_base_width = cache.font->get_string_size(completion_base, cache.font_size).width; - - int scroll_rectangle_width = get_theme_constant("completion_scroll_width"); - int width = 0; - - // Compute max width of the panel based on the longest completion option - if (completion_options_size < 50) { - for (int i = 0; i < completion_options_size; i++) { - int line_width = MIN(cache.font->get_string_size(completion_options[i].display, cache.font_size).x, cmax_width); - if (line_width > width) { - width = line_width; - } - } - } else { - width = cmax_width; - } - - // Add space for completion icons. - const int icon_hsep = get_theme_constant("hseparation", "ItemList"); - const Size2 icon_area_size(row_height, row_height); - const int icon_area_width = icon_area_size.width + icon_hsep; - width += icon_area_width; - - const int line_from = CLAMP(completion_index - row_count / 2, 0, completion_options_size - row_count); - - for (int i = 0; i < row_count; i++) { - int l = line_from + i; - ERR_CONTINUE(l < 0 || l >= completion_options_size); - if (completion_options[l].default_value.get_type() == Variant::COLOR) { - width += icon_area_size.width; - break; - } - } - - // Position completion panel - completion_rect.size.width = width + 2; - completion_rect.size.height = completion_rows_height; - - if (completion_options_size <= maxlines) { - scroll_rectangle_width = 0; - } - - const Point2 csb_offset = csb->get_offset(); - - const int total_width = completion_rect.size.width + csb->get_minimum_size().x + scroll_rectangle_width; - const int total_height = completion_rect.size.height + csb->get_minimum_size().y; - - const int rect_left_border_x = cursor_pos.x - completion_base_width - icon_area_width - csb_offset.x; - const int rect_right_border_x = rect_left_border_x + total_width; - - if (rect_left_border_x < 0) { - // Anchor the completion panel to the left - completion_rect.position.x = 0; - } else if (rect_right_border_x > get_size().width) { - // Anchor the completion panel to the right - completion_rect.position.x = get_size().width - total_width; - } else { - // Let the completion panel float with the cursor - completion_rect.position.x = rect_left_border_x; - } - - if (cursor_pos.y + row_height + total_height > get_size().height && cursor_pos.y > total_height) { - // Completion panel above the cursor line - completion_rect.position.y = cursor_pos.y - total_height; - } else { - // Completion panel below the cursor line - completion_rect.position.y = cursor_pos.y + row_height; - completion_below = true; - } - - draw_style_box(csb, Rect2(completion_rect.position - csb_offset, completion_rect.size + csb->get_minimum_size() + Size2(scroll_rectangle_width, 0))); - - if (cache.completion_background_color.a > 0.01) { - RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(completion_rect.position, completion_rect.size + Size2(scroll_rectangle_width, 0)), cache.completion_background_color); - } - RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(completion_rect.position.x, completion_rect.position.y + (completion_index - line_from) * get_row_height()), Size2(completion_rect.size.width, get_row_height())), cache.completion_selected_color); - - draw_rect(Rect2(completion_rect.position + Vector2(icon_area_size.x + icon_hsep, 0), Size2(MIN(completion_base_width, completion_rect.size.width - (icon_area_size.x + icon_hsep)), completion_rect.size.height)), cache.completion_existing_color); - - for (int i = 0; i < row_count; i++) { - int l = line_from + i; - ERR_CONTINUE(l < 0 || l >= completion_options_size); - - Ref<TextLine> tl; - tl.instance(); - tl->add_string(completion_options[l].display, cache.font, cache.font_size); - - int yofs = (row_height - tl->get_size().y) / 2; - Point2 title_pos(completion_rect.position.x, completion_rect.position.y + i * row_height + yofs); - - // Draw completion icon if it is valid. - Ref<Texture2D> icon = completion_options[l].icon; - Rect2 icon_area(completion_rect.position.x, completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height); - if (icon.is_valid()) { - const real_t max_scale = 0.7f; - const real_t side = max_scale * icon_area.size.width; - real_t scale = MIN(side / icon->get_width(), side / icon->get_height()); - Size2 icon_size = icon->get_size() * scale; - draw_texture_rect(icon, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size)); - } - - title_pos.x = icon_area.position.x + icon_area.size.width + icon_hsep; - - tl->set_width(completion_rect.size.width - (icon_area_size.x + icon_hsep)); - - if (rtl) { - if (completion_options[l].default_value.get_type() == Variant::COLOR) { - draw_rect(Rect2(Point2(completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)completion_options[l].default_value); - } - tl->set_align(HALIGN_RIGHT); - } else { - if (completion_options[l].default_value.get_type() == Variant::COLOR) { - draw_rect(Rect2(Point2(completion_rect.position.x + completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size), (Color)completion_options[l].default_value); - } - tl->set_align(HALIGN_LEFT); - } - if (cache.outline_size > 0 && cache.outline_color.a > 0) { - tl->draw_outline(ci, title_pos, cache.outline_size, cache.outline_color); - } - tl->draw(ci, title_pos, completion_options[l].font_color); - } - - if (scroll_rectangle_width) { - // Draw a small scroll rectangle to show a position in the options. - float r = (float)maxlines / completion_options_size; - float o = (float)line_from / completion_options_size; - draw_rect(Rect2(completion_rect.position.x + completion_rect.size.width, completion_rect.position.y + o * completion_rect.size.y, scroll_rectangle_width, completion_rect.size.y * r), scrollc); - } - - completion_line_ofs = line_from; - } - - // Check to see if the hint should be drawn. - bool show_hint = false; - if (is_cursor_line_visible && completion_hint != "") { - if (completion_active) { - if (completion_below && !callhint_below) { - show_hint = true; - } else if (!completion_below && callhint_below) { - show_hint = true; - } - } else { - show_hint = true; - } - } - - if (show_hint) { - Ref<StyleBox> sb = get_theme_stylebox("panel", "TooltipPanel"); - Ref<Font> font = cache.font; - Color font_color = get_theme_color("font_color", "TooltipLabel"); - - int max_w = 0; - int sc = completion_hint.get_slice_count("\n"); - int offset = 0; - int spacing = 0; - for (int i = 0; i < sc; i++) { - String l = completion_hint.get_slice("\n", i); - int len = font->get_string_size(l, cache.font_size).x; - max_w = MAX(len, max_w); - if (i == 0) { - offset = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF))), cache.font_size).x; - } else { - spacing += cache.line_spacing; - } - } - - Size2 size2 = Size2(max_w, sc * font->get_height(cache.font_size) + spacing); - Size2 minsize = size2 + sb->get_minimum_size(); - - if (completion_hint_offset == -0xFFFF) { - completion_hint_offset = cursor_pos.x - offset; - } - - Point2 hint_ofs = Vector2(completion_hint_offset, cursor_pos.y) + callhint_offset; - - if (callhint_below) { - hint_ofs.y += row_height + sb->get_offset().y; - } else { - hint_ofs.y -= minsize.y + sb->get_offset().y; - } - - draw_style_box(sb, Rect2(hint_ofs, minsize)); - - spacing = 0; - for (int i = 0; i < sc; i++) { - int begin = 0; - int end = 0; - String l = completion_hint.get_slice("\n", i); - - if (l.find(String::chr(0xFFFF)) != -1) { - begin = font->get_string_size(l.substr(0, l.find(String::chr(0xFFFF))), cache.font_size).x; - end = font->get_string_size(l.substr(0, l.rfind(String::chr(0xFFFF))), cache.font_size).x; - } - - Point2 round_ofs = hint_ofs + sb->get_offset() + Vector2(0, font->get_ascent(cache.font_size) + font->get_height(cache.font_size) * i + spacing); - round_ofs = round_ofs.round(); - draw_string(font, round_ofs, l.replace(String::chr(0xFFFF), ""), HALIGN_LEFT, -1, cache.font_size, font_color); - if (end > 0) { - Vector2 b = hint_ofs + sb->get_offset() + Vector2(begin, font->get_height(cache.font_size) + font->get_height(cache.font_size) * i + spacing - 1); - draw_line(b, b + Vector2(end - begin, 0), font_color); - } - spacing += cache.line_spacing; - } - } - if (has_focus()) { if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) { DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id()); - DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + cursor_pos, get_viewport()->get_window_id()); + DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + cursor.draw_pos, get_viewport()->get_window_id()); } } } break; @@ -2427,8 +2211,6 @@ void TextEdit::_move_cursor_up(bool p_select) { if (p_select) { _post_shift_selection(); } - - _cancel_code_hint(); } void TextEdit::_move_cursor_down(bool p_select) { @@ -2451,8 +2233,6 @@ void TextEdit::_move_cursor_down(bool p_select) { if (p_select) { _post_shift_selection(); } - - _cancel_code_hint(); } void TextEdit::_move_cursor_to_line_start(bool p_select) { @@ -2492,9 +2272,6 @@ void TextEdit::_move_cursor_to_line_start(bool p_select) { if (p_select) { _post_shift_selection(); } - - _cancel_completion(); - completion_hint = ""; } void TextEdit::_move_cursor_to_line_end(bool p_select) { @@ -2520,8 +2297,6 @@ void TextEdit::_move_cursor_to_line_end(bool p_select) { if (p_select) { _post_shift_selection(); } - _cancel_completion(); - completion_hint = ""; } void TextEdit::_move_cursor_page_up(bool p_select) { @@ -2538,9 +2313,6 @@ void TextEdit::_move_cursor_page_up(bool p_select) { if (p_select) { _post_shift_selection(); } - - _cancel_completion(); - completion_hint = ""; } void TextEdit::_move_cursor_page_down(bool p_select) { @@ -2557,9 +2329,6 @@ void TextEdit::_move_cursor_page_down(bool p_select) { if (p_select) { _post_shift_selection(); } - - _cancel_completion(); - completion_hint = ""; } void TextEdit::_backspace(bool p_word, bool p_all_to_left) { @@ -2692,11 +2461,7 @@ void TextEdit::_move_cursor_document_end(bool p_select) { } } -void TextEdit::_handle_unicode_character(uint32_t unicode, bool p_had_selection, bool p_update_auto_complete) { - if (p_update_auto_complete) { - _reset_caret_blink_timer(); - } - +void TextEdit::_handle_unicode_character(uint32_t unicode, bool p_had_selection) { if (p_had_selection) { _delete_selection(); } @@ -2713,11 +2478,6 @@ void TextEdit::_handle_unicode_character(uint32_t unicode, bool p_had_selection, const char32_t chr[2] = { (char32_t)unicode, 0 }; - // Clear completion hint when function closed - if (completion_hint != "" && unicode == ')') { - completion_hint = ""; - } - if (auto_brace_completion_enabled && _is_pair_symbol(chr[0])) { _consume_pair_symbol(chr[0]); } else { @@ -2727,10 +2487,6 @@ void TextEdit::_handle_unicode_character(uint32_t unicode, bool p_had_selection, if ((insert_mode && !p_had_selection) || (selection.active != p_had_selection)) { end_complex_operation(); } - - if (p_update_auto_complete) { - _update_completion_candidates(); - } } void TextEdit::_get_mouse_pos(const Point2i &p_mouse, int &r_row, int &r_col) const { @@ -2886,40 +2642,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { // Ignore mouse clicks in IME input mode. return; } - if (completion_active && completion_rect.has_point(mpos)) { - if (!mb->is_pressed()) { - return; - } - - if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP) { - if (completion_index > 0) { - completion_index--; - completion_current = completion_options[completion_index]; - update(); - } - } - if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_DOWN) { - if (completion_index < completion_options.size() - 1) { - completion_index++; - completion_current = completion_options[completion_index]; - update(); - } - } - - if (mb->get_button_index() == MOUSE_BUTTON_LEFT) { - completion_index = CLAMP(completion_line_ofs + (mpos.y - completion_rect.position.y) / get_row_height(), 0, completion_options.size() - 1); - - completion_current = completion_options[completion_index]; - update(); - if (mb->is_double_click()) { - _confirm_completion(); - } - } - return; - } else { - _cancel_completion(); - _cancel_code_hint(); - } if (mb->is_pressed()) { if (mb->get_button_index() == MOUSE_BUTTON_WHEEL_UP && !mb->is_command_pressed()) { @@ -3216,96 +2938,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { // Check and handle all built in shortcuts. - // AUTO-COMPLETE - - if (k->is_action("ui_text_completion_query", true)) { - query_code_comple(); - accept_event(); - return; - } - - if (completion_active) { - if (k->is_action("ui_up", true)) { - if (completion_index > 0) { - completion_index--; - } else { - completion_index = completion_options.size() - 1; - } - completion_current = completion_options[completion_index]; - update(); - accept_event(); - return; - } - if (k->is_action("ui_down", true)) { - if (completion_index < completion_options.size() - 1) { - completion_index++; - } else { - completion_index = 0; - } - completion_current = completion_options[completion_index]; - update(); - accept_event(); - return; - } - if (k->is_action("ui_page_up", true)) { - completion_index -= get_theme_constant("completion_lines"); - if (completion_index < 0) { - completion_index = 0; - } - completion_current = completion_options[completion_index]; - update(); - accept_event(); - return; - } - if (k->is_action("ui_page_down", true)) { - completion_index += get_theme_constant("completion_lines"); - if (completion_index >= completion_options.size()) { - completion_index = completion_options.size() - 1; - } - completion_current = completion_options[completion_index]; - update(); - accept_event(); - return; - } - if (k->is_action("ui_home", true)) { - if (completion_index > 0) { - completion_index = 0; - completion_current = completion_options[completion_index]; - update(); - } - accept_event(); - return; - } - if (k->is_action("ui_end", true)) { - if (completion_index < completion_options.size() - 1) { - completion_index = completion_options.size() - 1; - completion_current = completion_options[completion_index]; - update(); - } - accept_event(); - return; - } - if (k->is_action("ui_text_completion_accept", true)) { - _confirm_completion(); - accept_event(); - return; - } - if (k->is_action("ui_cancel", true)) { - _cancel_completion(); - accept_event(); - return; - } - - // Handle Unicode here (if no modifiers active) and update autocomplete. - if (k->get_unicode() >= 32) { - if (allow_unicode_handling && !readonly) { - _handle_unicode_character(k->get_unicode(), had_selection, true); - accept_event(); - return; - } - } - } - // NEWLINES. if (k->is_action("ui_text_newline_above", true)) { _new_line(false, true); @@ -3348,9 +2980,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } if (k->is_action("ui_text_backspace", true)) { _backspace(); - if (completion_active) { - _update_completion_candidates(); - } accept_event(); return; } @@ -3423,7 +3052,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { } // MISC. - if (k->is_action("ui_menu", true)) { if (context_menu_enabled) { menu->set_position(get_screen_transform().xform(_get_cursor_pixel_pos())); @@ -3440,14 +3068,6 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { accept_event(); return; } - if (k->is_action("ui_cancel", true)) { - if (completion_hint != "") { - completion_hint = ""; - update(); - } - accept_event(); - return; - } if (k->is_action("ui_swap_input_direction", true)) { _swap_current_input_direction(); accept_event(); @@ -3533,7 +3153,7 @@ void TextEdit::_gui_input(const Ref<InputEvent> &p_gui_input) { if (allow_unicode_handling && !readonly && k->get_unicode() >= 32) { // Handle Unicode (if no modifiers active). - _handle_unicode_character(k->get_unicode(), had_selection, false); + _handle_unicode_character(k->get_unicode(), had_selection); accept_event(); return; } @@ -4294,6 +3914,14 @@ void TextEdit::cursor_set_line(int p_row, bool p_adjust_viewport, bool p_can_be_ } } +Point2 TextEdit::get_caret_draw_pos() const { + return cursor.draw_pos; +} + +bool TextEdit::is_caret_visible() const { + return cursor.visible; +} + int TextEdit::cursor_get_column() const { return cursor.column; } @@ -4471,10 +4099,6 @@ Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { return CURSOR_POINTING_HAND; } - if ((completion_active && completion_rect.has_point(p_pos)) || (is_readonly() && (!is_selecting_enabled() || text.size() == 0))) { - return CURSOR_ARROW; - } - int row, col; _get_mouse_pos(p_pos, row, col); @@ -4686,26 +4310,6 @@ String TextEdit::get_text_for_lookup_completion() { return longthing; } -String TextEdit::get_text_for_completion() { - String longthing; - int len = text.size(); - for (int i = 0; i < len; i++) { - if (i == cursor.line) { - longthing += text[i].substr(0, cursor.column); - longthing += String::chr(0xFFFF); // Not unicode, represents the cursor. - longthing += text[i].substr(cursor.column, text[i].size()); - } else { - longthing += text[i]; - } - - if (i != len - 1) { - longthing += "\n"; - } - } - - return longthing; -}; - String TextEdit::get_line(int line) const { if (line < 0 || line >= text.size()) { return ""; @@ -4788,10 +4392,6 @@ void TextEdit::_update_caches() { cache.style_normal = get_theme_stylebox("normal"); cache.style_focus = get_theme_stylebox("focus"); cache.style_readonly = get_theme_stylebox("read_only"); - cache.completion_background_color = get_theme_color("completion_background_color"); - cache.completion_selected_color = get_theme_color("completion_selected_color"); - cache.completion_existing_color = get_theme_color("completion_existing_color"); - cache.completion_font_color = get_theme_color("completion_font_color"); cache.font = get_theme_font("font"); cache.font_size = get_theme_font_size("font_size"); cache.outline_color = get_theme_color("font_outline_color"); @@ -5886,7 +5486,6 @@ void TextEdit::undo() { if (undo_stack_pos->get().type == TextOperation::TYPE_REMOVE) { cursor_set_line(undo_stack_pos->get().to_line, false); cursor_set_column(undo_stack_pos->get().to_column); - _cancel_code_hint(); } else { cursor_set_line(undo_stack_pos->get().from_line, false); cursor_set_column(undo_stack_pos->get().from_column); @@ -6162,313 +5761,6 @@ float TextEdit::get_v_scroll_speed() const { return v_scroll_speed; } -void TextEdit::set_completion(bool p_enabled, const Vector<String> &p_prefixes) { - completion_prefixes.clear(); - completion_enabled = p_enabled; - for (int i = 0; i < p_prefixes.size(); i++) { - completion_prefixes.insert(p_prefixes[i]); - } -} - -void TextEdit::_confirm_completion() { - begin_complex_operation(); - - _remove_text(cursor.line, cursor.column - completion_base.length(), cursor.line, cursor.column); - cursor_set_column(cursor.column - completion_base.length(), false); - insert_text_at_cursor(completion_current.insert_text); - - // When inserted into the middle of an existing string/method, don't add an unnecessary quote/bracket. - String line = text[cursor.line]; - char32_t next_char = line[cursor.column]; - char32_t last_completion_char = completion_current.insert_text[completion_current.insert_text.length() - 1]; - char32_t last_completion_char_display = completion_current.display[completion_current.display.length() - 1]; - - if ((last_completion_char == '"' || last_completion_char == '\'') && (last_completion_char == next_char || last_completion_char_display == next_char)) { - _remove_text(cursor.line, cursor.column, cursor.line, cursor.column + 1); - } - - if (last_completion_char == '(') { - if (next_char == last_completion_char) { - _base_remove_text(cursor.line, cursor.column - 1, cursor.line, cursor.column); - } else if (auto_brace_completion_enabled) { - insert_text_at_cursor(")"); - cursor.column--; - } - } else if (last_completion_char == ')' && next_char == '(') { - _base_remove_text(cursor.line, cursor.column - 2, cursor.line, cursor.column); - if (line[cursor.column + 1] != ')') { - cursor.column--; - } - } - - end_complex_operation(); - - _cancel_completion(); - - if (last_completion_char == '(') { - query_code_comple(); - } -} - -void TextEdit::_cancel_code_hint() { - completion_hint = ""; - update(); -} - -void TextEdit::_cancel_completion() { - if (!completion_active) { - return; - } - - completion_active = false; - completion_forced = false; - update(); -} - -static bool _is_completable(char32_t c) { - return !_is_symbol(c) || c == '"' || c == '\''; -} - -void TextEdit::_update_completion_candidates() { - String l = text[cursor.line]; - int cofs = CLAMP(cursor.column, 0, l.length()); - - String s; - - // Look for keywords first. - - bool inquote = false; - int first_quote = -1; - int restore_quotes = -1; - - int c = cofs - 1; - while (c >= 0) { - if (l[c] == '"' || l[c] == '\'') { - inquote = !inquote; - if (first_quote == -1) { - first_quote = c; - } - restore_quotes = 0; - } else if (restore_quotes == 0 && l[c] == '$') { - restore_quotes = 1; - } else if (restore_quotes == 0 && !_is_whitespace(l[c])) { - restore_quotes = -1; - } - c--; - } - - bool pre_keyword = false; - bool cancel = false; - - if (!inquote && first_quote == cofs - 1) { - // No completion here. - cancel = true; - } else if (inquote && first_quote != -1) { - s = l.substr(first_quote, cofs - first_quote); - } else if (cofs > 0 && l[cofs - 1] == ' ') { - int kofs = cofs - 1; - String kw; - while (kofs >= 0 && l[kofs] == ' ') { - kofs--; - } - - while (kofs >= 0 && l[kofs] > 32 && _is_completable(l[kofs])) { - kw = String::chr(l[kofs]) + kw; - kofs--; - } - - pre_keyword = keywords.has(kw); - - } else { - while (cofs > 0 && l[cofs - 1] > 32 && (l[cofs - 1] == '/' || _is_completable(l[cofs - 1]))) { - s = String::chr(l[cofs - 1]) + s; - if (l[cofs - 1] == '\'' || l[cofs - 1] == '"' || l[cofs - 1] == '$') { - break; - } - - cofs--; - } - } - - if (cursor.column > 0 && l[cursor.column - 1] == '(' && !pre_keyword && !completion_forced) { - cancel = true; - } - - update(); - - bool prev_is_prefix = false; - if (cofs > 0 && completion_prefixes.has(String::chr(l[cofs - 1]))) { - prev_is_prefix = true; - } - // Check with one space before prefix, to allow indent. - if (cofs > 1 && l[cofs - 1] == ' ' && completion_prefixes.has(String::chr(l[cofs - 2]))) { - prev_is_prefix = true; - } - - if (cancel || (!pre_keyword && s == "" && (cofs == 0 || !prev_is_prefix))) { - // None to complete, cancel. - _cancel_completion(); - return; - } - - completion_options.clear(); - completion_index = 0; - completion_base = s; - Vector<float> sim_cache; - bool single_quote = s.begins_with("'"); - Vector<ScriptCodeCompletionOption> completion_options_casei; - Vector<ScriptCodeCompletionOption> completion_options_subseq; - Vector<ScriptCodeCompletionOption> completion_options_subseq_casei; - - String s_lower = s.to_lower(); - - for (List<ScriptCodeCompletionOption>::Element *E = completion_sources.front(); E; E = E->next()) { - ScriptCodeCompletionOption &option = E->get(); - - if (single_quote && option.display.is_quoted()) { - option.display = option.display.unquote().quote("'"); - } - - if (inquote && restore_quotes == 1 && !option.display.is_quoted()) { - String quote = single_quote ? "'" : "\""; - option.display = option.display.quote(quote); - option.insert_text = option.insert_text.quote(quote); - } - - if (option.display.length() == 0) { - continue; - } else if (s.length() == 0) { - completion_options.push_back(option); - } else { - // This code works the same as: - /* - if (option.display.begins_with(s)) { - completion_options.push_back(option); - } else if (option.display.to_lower().begins_with(s.to_lower())) { - completion_options_casei.push_back(option); - } else if (s.is_subsequence_of(option.display)) { - completion_options_subseq.push_back(option); - } else if (s.is_subsequence_ofi(option.display)) { - completion_options_subseq_casei.push_back(option); - } - */ - // But is more performant due to being inlined and looping over the characters only once - - String display_lower = option.display.to_lower(); - - const char32_t *ssq = &s[0]; - const char32_t *ssq_lower = &s_lower[0]; - - const char32_t *tgt = &option.display[0]; - const char32_t *tgt_lower = &display_lower[0]; - - const char32_t *ssq_last_tgt = nullptr; - const char32_t *ssq_lower_last_tgt = nullptr; - - for (; *tgt; tgt++, tgt_lower++) { - if (*ssq == *tgt) { - ssq++; - ssq_last_tgt = tgt; - } - if (*ssq_lower == *tgt_lower) { - ssq_lower++; - ssq_lower_last_tgt = tgt; - } - } - - if (!*ssq) { // Matched the whole subsequence in s - if (ssq_last_tgt == &option.display[s.length() - 1]) { // Finished matching in the first s.length() characters - completion_options.push_back(option); - } else { - completion_options_subseq.push_back(option); - } - } else if (!*ssq_lower) { // Matched the whole subsequence in s_lower - if (ssq_lower_last_tgt == &option.display[s.length() - 1]) { // Finished matching in the first s.length() characters - completion_options_casei.push_back(option); - } else { - completion_options_subseq_casei.push_back(option); - } - } - } - } - - completion_options.append_array(completion_options_casei); - completion_options.append_array(completion_options_subseq); - completion_options.append_array(completion_options_subseq_casei); - - if (completion_options.size() == 0) { - // No options to complete, cancel. - _cancel_completion(); - return; - } - - if (completion_options.size() == 1 && s == completion_options[0].display) { - // A perfect match, stop completion. - _cancel_completion(); - return; - } - - // The top of the list is the best match. - completion_current = completion_options[0]; - completion_enabled = true; -} - -void TextEdit::query_code_comple() { - String l = text[cursor.line]; - int ofs = CLAMP(cursor.column, 0, l.length()); - - bool inquote = false; - - int c = ofs - 1; - while (c >= 0) { - if (l[c] == '"' || l[c] == '\'') { - inquote = !inquote; - } - c--; - } - - bool ignored = completion_active && !completion_options.is_empty(); - if (ignored) { - ScriptCodeCompletionOption::Kind kind = ScriptCodeCompletionOption::KIND_PLAIN_TEXT; - const ScriptCodeCompletionOption *previous_option = nullptr; - for (int i = 0; i < completion_options.size(); i++) { - const ScriptCodeCompletionOption ¤t_option = completion_options[i]; - if (!previous_option) { - previous_option = ¤t_option; - kind = current_option.kind; - } - if (previous_option->kind != current_option.kind) { - ignored = false; - break; - } - } - ignored = ignored && (kind == ScriptCodeCompletionOption::KIND_FILE_PATH || kind == ScriptCodeCompletionOption::KIND_NODE_PATH || kind == ScriptCodeCompletionOption::KIND_SIGNAL); - } - - if (!ignored) { - if (ofs > 0 && (inquote || _is_completable(l[ofs - 1]) || completion_prefixes.has(String::chr(l[ofs - 1])))) { - emit_signal("request_completion"); - } else if (ofs > 1 && l[ofs - 1] == ' ' && completion_prefixes.has(String::chr(l[ofs - 2]))) { // Make it work with a space too, it's good enough. - emit_signal("request_completion"); - } - } -} - -void TextEdit::set_code_hint(const String &p_hint) { - completion_hint = p_hint; - completion_hint_offset = -0xFFFF; - update(); -} - -void TextEdit::code_complete(const List<ScriptCodeCompletionOption> &p_strings, bool p_forced) { - completion_sources = p_strings; - completion_active = true; - completion_forced = p_forced; - completion_current = ScriptCodeCompletionOption(); - completion_index = 0; - _update_completion_candidates(); -} - String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { int row, col; _get_mouse_pos(p_pos, row, col); @@ -6916,6 +6208,8 @@ void TextEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("cursor_set_column", "column", "adjust_viewport"), &TextEdit::cursor_set_column, DEFVAL(true)); ClassDB::bind_method(D_METHOD("cursor_set_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index"), &TextEdit::cursor_set_line, DEFVAL(true), DEFVAL(true), DEFVAL(0)); + ClassDB::bind_method(D_METHOD("get_caret_draw_pos"), &TextEdit::get_caret_draw_pos); + ClassDB::bind_method(D_METHOD("is_caret_visible"), &TextEdit::is_caret_visible); ClassDB::bind_method(D_METHOD("cursor_get_column"), &TextEdit::cursor_get_column); ClassDB::bind_method(D_METHOD("cursor_get_line"), &TextEdit::cursor_get_line); ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enable"), &TextEdit::cursor_set_blink_enabled); @@ -7096,7 +6390,6 @@ void TextEdit::_bind_methods() { ADD_SIGNAL(MethodInfo("cursor_changed")); ADD_SIGNAL(MethodInfo("text_changed")); ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line"))); - ADD_SIGNAL(MethodInfo("request_completion")); ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter"))); ADD_SIGNAL(MethodInfo("gutter_added")); ADD_SIGNAL(MethodInfo("gutter_removed")); diff --git a/scene/gui/text_edit.h b/scene/gui/text_edit.h index 6ca50f3e2d..f963e664d1 100644 --- a/scene/gui/text_edit.h +++ b/scene/gui/text_edit.h @@ -173,6 +173,8 @@ private: }; struct Cursor { + Point2 draw_pos; + bool visible = false; int last_fit_x = 0; int line = 0; int column = 0; ///< cursor @@ -239,20 +241,6 @@ private: Dictionary _get_line_syntax_highlighting(int p_line); - Set<String> completion_prefixes; - bool completion_enabled = false; - List<ScriptCodeCompletionOption> completion_sources; - Vector<ScriptCodeCompletionOption> completion_options; - bool completion_active = false; - bool completion_forced = false; - ScriptCodeCompletionOption completion_current; - String completion_base; - int completion_index = 0; - Rect2i completion_rect; - int completion_line_ofs = 0; - String completion_hint; - int completion_hint_offset = 0; - bool setting_text = false; // data @@ -306,10 +294,10 @@ private: bool highlight_all_occurrences = false; bool scroll_past_end_of_file_enabled = false; - bool auto_brace_completion_enabled = false; bool brace_matching_enabled = false; bool highlight_current_line = false; bool auto_indent = false; + String cut_copy_line; bool insert_mode = false; bool select_identifiers_enabled = false; @@ -341,9 +329,6 @@ private: bool next_operation_is_complex = false; - bool callhint_below = false; - Vector2 callhint_offset; - String search_text; uint32_t search_flags = 0; int search_result_line = 0; @@ -437,10 +422,6 @@ private: PopupMenu *menu_ctl; void _clear(); - void _cancel_completion(); - void _cancel_code_hint(); - void _confirm_completion(); - void _update_completion_candidates(); int _calculate_spaces_till_next_left_indent(int column); int _calculate_spaces_till_next_right_indent(int column); @@ -463,9 +444,11 @@ private: void _delete_selection(); void _move_cursor_document_start(bool p_select); void _move_cursor_document_end(bool p_select); - void _handle_unicode_character(uint32_t unicode, bool p_had_selection, bool p_update_auto_complete); + void _handle_unicode_character(uint32_t unicode, bool p_had_selection); protected: + bool auto_brace_completion_enabled = false; + struct Cache { Ref<Texture2D> tab_icon; Ref<Texture2D> space_icon; @@ -477,10 +460,6 @@ protected: int font_size = 16; int outline_size = 0; Color outline_color; - Color completion_background_color; - Color completion_selected_color; - Color completion_existing_color; - Color completion_font_color; Color caret_color; Color caret_background_color; Color font_color; @@ -505,7 +484,7 @@ protected: void _insert_text(int p_line, int p_char, const String &p_text, int *r_end_line = nullptr, int *r_end_char = nullptr); void _remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column); void _insert_text_at_cursor(const String &p_text); - void _gui_input(const Ref<InputEvent> &p_gui_input); + virtual void _gui_input(const Ref<InputEvent> &p_gui_input); void _notification(int p_what); void _consume_pair_symbol(char32_t ch); @@ -681,10 +660,6 @@ public: brace_matching_enabled = p_enabled; update(); } - inline void set_callhint_settings(bool below, Vector2 offset) { - callhint_below = below; - callhint_offset = offset; - } void set_auto_indent(bool p_auto_indent); void center_viewport_to_cursor(); @@ -695,6 +670,8 @@ public: void cursor_set_column(int p_col, bool p_adjust_viewport = true); void cursor_set_line(int p_row, bool p_adjust_viewport = true, bool p_can_be_hidden = true, int p_wrap_index = 0); + Point2 get_caret_draw_pos() const; + bool is_caret_visible() const; int cursor_get_column() const; int cursor_get_line() const; Vector2i _get_cursor_pixel_pos(bool p_adjust_viewport = true); @@ -811,11 +788,6 @@ public: void set_tooltip_request_func(Object *p_obj, const StringName &p_function, const Variant &p_udata); - void set_completion(bool p_enabled, const Vector<String> &p_prefixes); - void code_complete(const List<ScriptCodeCompletionOption> &p_strings, bool p_forced = false); - void set_code_hint(const String &p_hint); - void query_code_comple(); - void set_select_identifiers_on_hover(bool p_enable); bool is_selecting_identifiers_on_hover_enabled() const; @@ -833,7 +805,6 @@ public: PopupMenu *get_menu() const; - String get_text_for_completion(); String get_text_for_lookup_completion(); virtual bool is_text_field() const override; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 3a5b3534c6..b91a5c0b7f 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -432,7 +432,6 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_stylebox("normal", "TextEdit", make_stylebox(tree_bg_png, 3, 3, 3, 3, 0, 0, 0, 0)); theme->set_stylebox("focus", "TextEdit", focus); theme->set_stylebox("read_only", "TextEdit", make_stylebox(tree_bg_disabled_png, 4, 4, 4, 4, 0, 0, 0, 0)); - theme->set_stylebox("completion", "TextEdit", make_stylebox(tree_bg_png, 3, 3, 3, 3, 0, 0, 0, 0)); theme->set_icon("tab", "TextEdit", make_icon(tab_png)); theme->set_icon("space", "TextEdit", make_icon(space_png)); @@ -441,11 +440,6 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_font_size("font_size", "TextEdit", -1); theme->set_color("background_color", "TextEdit", Color(0, 0, 0, 0)); - theme->set_color("completion_background_color", "TextEdit", Color(0.17, 0.16, 0.2)); - theme->set_color("completion_selected_color", "TextEdit", Color(0.26, 0.26, 0.27)); - theme->set_color("completion_existing_color", "TextEdit", Color(0.87, 0.87, 0.87, 0.13)); - theme->set_color("completion_scroll_color", "TextEdit", control_font_pressed_color); - theme->set_color("completion_font_color", "TextEdit", Color(0.67, 0.67, 0.67)); theme->set_color("font_color", "TextEdit", control_font_color); theme->set_color("font_selected_color", "TextEdit", Color(0, 0, 0)); theme->set_color("font_readonly_color", "TextEdit", Color(control_font_color.r, control_font_color.g, control_font_color.b, 0.5f)); @@ -458,9 +452,6 @@ void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const theme->set_color("brace_mismatch_color", "TextEdit", Color(1, 0.2, 0.2)); theme->set_color("word_highlighted_color", "TextEdit", Color(0.8, 0.9, 0.9, 0.15)); - theme->set_constant("completion_lines", "TextEdit", 7); - theme->set_constant("completion_max_width", "TextEdit", 50); - theme->set_constant("completion_scroll_width", "TextEdit", 3); theme->set_constant("line_spacing", "TextEdit", 4 * scale); theme->set_constant("outline_size", "TextEdit", 0); |