diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-03-28 08:56:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-28 08:56:27 +0200 |
commit | 7fe5bece45123b21a558f1aa445802e6247699c4 (patch) | |
tree | fa35eb945e3a08ae0ecd3b7cf3655bb9d7941ec6 /core/object | |
parent | c422dc5feb8cc4b16c096c1123f5df8ea96e32bc (diff) | |
parent | f9e1c094a2b12d1351d6fc84e0ed18271d159a93 (diff) |
Merge pull request #58931 from EricEzaM/proposals/4189-better-code-completion
Sort autocomplete/code completion options in a better way
Diffstat (limited to 'core/object')
-rw-r--r-- | core/object/script_language.h | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/core/object/script_language.h b/core/object/script_language.h index 2122f785b6..8f6ee90069 100644 --- a/core/object/script_language.h +++ b/core/object/script_language.h @@ -233,6 +233,14 @@ struct ScriptCodeCompletionOption { KIND_FILE_PATH, KIND_PLAIN_TEXT, }; + + enum Location { + LOCATION_LOCAL = 0, + LOCATION_PARENT_MASK = (1 << 8), + LOCATION_OTHER_USER_CODE = (1 << 9), + LOCATION_OTHER = (1 << 10), + }; + Kind kind = KIND_PLAIN_TEXT; String display; String insert_text; @@ -240,13 +248,60 @@ struct ScriptCodeCompletionOption { RES icon; Variant default_value; Vector<Pair<int, int>> matches; + int location = LOCATION_OTHER; ScriptCodeCompletionOption() {} - ScriptCodeCompletionOption(const String &p_text, Kind p_kind) { + ScriptCodeCompletionOption(const String &p_text, Kind p_kind, int p_location = LOCATION_OTHER) { display = p_text; insert_text = p_text; kind = p_kind; + location = p_location; + } +}; + +const int KIND_COUNT = 10; +const ScriptCodeCompletionOption::Kind KIND_SORT_ORDER[KIND_COUNT] = { + ScriptCodeCompletionOption::Kind::KIND_VARIABLE, + ScriptCodeCompletionOption::Kind::KIND_MEMBER, + ScriptCodeCompletionOption::Kind::KIND_FUNCTION, + ScriptCodeCompletionOption::Kind::KIND_ENUM, + ScriptCodeCompletionOption::Kind::KIND_SIGNAL, + ScriptCodeCompletionOption::Kind::KIND_CONSTANT, + ScriptCodeCompletionOption::Kind::KIND_CLASS, + ScriptCodeCompletionOption::Kind::KIND_NODE_PATH, + ScriptCodeCompletionOption::Kind::KIND_FILE_PATH, + ScriptCodeCompletionOption::Kind::KIND_PLAIN_TEXT, +}; + +struct ScriptCodeCompletionOptionCompare { + _FORCE_INLINE_ bool operator()(const ScriptCodeCompletionOption &l, const ScriptCodeCompletionOption &r) const { + if (l.location == r.location) { + // If locations are same, sort on kind + if (l.kind == r.kind) { + // If kinds are same, sort alphanumeric + return l.display < r.display; + } + + // Sort kinds based on the const sorting array defined above. Lower index = higher priority. + int l_index = -1; + int r_index = -1; + for (int i = 0; i < KIND_COUNT; i++) { + const ScriptCodeCompletionOption::Kind kind = KIND_SORT_ORDER[i]; + l_index = kind == l.kind ? i : l_index; + r_index = kind == r.kind ? i : r_index; + + if (l_index != -1 && r_index != -1) { + return l_index < r_index; + } + } + + // This return should never be hit unless something goes wrong. + // l and r should always have a Kind which is in the sort order array. + return l.display < r.display; + } + + return l.location < r.location; } }; |