From 006e899bb34dbf024aeff1dae97468ae5547a415 Mon Sep 17 00:00:00 2001 From: ajreckof <66184050+ajreckof@users.noreply.github.com> Date: Tue, 23 May 2023 05:12:34 +0200 Subject: [PATCH] sort code completions with rules Fixups Add levenshtein distance for comparisons, remove kind sort order, try to improve as many different use cases as possible Trying again to improve code completion Sort code autocompletion options by similarity based on input To make it really brief, uses a combination `String.similiary`, the category system introduced in a previous PR, and some filtering to yield more predictable results, instead of scattering every completion option at seemingly random. It also gives much higher priority to strings that contain the base in full, closer to the beginning or are perfect matches. Also moves CodeCompletionOptionCompare to code_edit.cpp Co-Authored-By: Micky <66727710+Mickeon@users.noreply.github.com> Co-Authored-By: Eric M <41730826+EricEzaM@users.noreply.github.com> --- core/object/script_language.cpp | 47 ++++- core/object/script_language.h | 14 +- doc/classes/CodeEdit.xml | 14 ++ doc/classes/ScriptLanguageExtension.xml | 2 +- editor/code_editor.cpp | 2 +- editor/plugins/script_text_editor.cpp | 2 - editor/plugins/script_text_editor.h | 47 ----- modules/gdscript/gdscript_editor.cpp | 15 +- scene/gui/code_edit.cpp | 228 ++++++++++-------------- scene/gui/code_edit.h | 20 ++- tests/scene/test_code_edit.h | 96 +++++++++- 11 files changed, 288 insertions(+), 199 deletions(-) diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 71f40660f4bb..6f047d80aaa2 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -34,7 +34,6 @@ #include "core/core_string_names.h" #include "core/debugger/engine_debugger.h" #include "core/debugger/script_debugger.h" -#include "core/variant/typed_array.h" #include @@ -461,6 +460,52 @@ void ScriptLanguage::get_core_type_words(List *p_core_type_words) const void ScriptLanguage::frame() { } +TypedArray ScriptLanguage::CodeCompletionOption::get_option_characteristics(const String &p_base) { + // Return characacteristics of the match found by order of importance. + // Matches will be ranked by a lexicographical order on the vector returned by this function. + // The lower values indicate better matches and that they should go before in the order of appearance. + if (last_matches == matches) { + return charac; + } + charac.clear(); + // Ensure base is not empty and at the same time that matches is not empty too. + if (p_base.length() == 0) { + last_matches = matches; + charac.push_back(location); + return charac; + } + charac.push_back(matches.size()); + charac.push_back((matches[0].first == 0) ? 0 : 1); + charac.push_back(location); + const char32_t *target_char = &p_base[0]; + int bad_case = 0; + for (const Pair &match_segment : matches) { + const char32_t *string_to_complete_char = &display[match_segment.first]; + for (int j = 0; j < match_segment.second; j++, string_to_complete_char++, target_char++) { + if (*string_to_complete_char != *target_char) { + bad_case++; + } + } + } + charac.push_back(bad_case); + charac.push_back(matches[0].first); + last_matches = matches; + return charac; +} + +void ScriptLanguage::CodeCompletionOption::clear_characteristics() { + charac = TypedArray(); +} + +TypedArray ScriptLanguage::CodeCompletionOption::get_option_cached_characteristics() const { + // Only returns the cached value and warns if it was not updated since the last change of matches. + if (last_matches != matches) { + WARN_PRINT("Characteristics are not up to date."); + } + + return charac; +} + bool PlaceHolderScriptInstance::set(const StringName &p_name, const Variant &p_value) { if (script->is_placeholder_fallback_enabled()) { return false; diff --git a/core/object/script_language.h b/core/object/script_language.h index 696c9a94a5f5..829f01fbcc68 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -35,6 +35,7 @@ #include "core/io/resource.h" #include "core/templates/pair.h" #include "core/templates/rb_map.h" +#include "core/variant/typed_array.h" class ScriptLanguage; template @@ -305,8 +306,8 @@ class ScriptLanguage : public Object { virtual Error open_in_external_editor(const Ref