summaryrefslogtreecommitdiff
path: root/editor
diff options
context:
space:
mode:
Diffstat (limited to 'editor')
-rw-r--r--editor/plugins/script_text_editor.cpp3
-rw-r--r--editor/plugins/script_text_editor.h47
2 files changed, 50 insertions, 0 deletions
diff --git a/editor/plugins/script_text_editor.cpp b/editor/plugins/script_text_editor.cpp
index c1b0a32fc7..4626f10b8d 100644
--- a/editor/plugins/script_text_editor.cpp
+++ b/editor/plugins/script_text_editor.cpp
@@ -699,6 +699,9 @@ void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLa
}
String hint;
Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint);
+
+ r_options->sort_custom_inplace<CodeCompletionOptionCompare>();
+
if (err == OK) {
code_editor->get_text_editor()->set_code_hint(hint);
}
diff --git a/editor/plugins/script_text_editor.h b/editor/plugins/script_text_editor.h
index 5c3a66404e..c1c4b0af54 100644
--- a/editor/plugins/script_text_editor.h
+++ b/editor/plugins/script_text_editor.h
@@ -256,4 +256,51 @@ public:
~ScriptTextEditor();
};
+const int KIND_COUNT = 10;
+// The order in which to sort code completion options.
+const ScriptLanguage::CodeCompletionKind KIND_SORT_ORDER[KIND_COUNT] = {
+ ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE,
+ ScriptLanguage::CODE_COMPLETION_KIND_MEMBER,
+ ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION,
+ ScriptLanguage::CODE_COMPLETION_KIND_ENUM,
+ ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL,
+ ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT,
+ ScriptLanguage::CODE_COMPLETION_KIND_CLASS,
+ ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH,
+ ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH,
+ ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT,
+};
+
+// The custom comparer which will sort completion options.
+struct CodeCompletionOptionCompare {
+ _FORCE_INLINE_ bool operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &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 ScriptLanguage::CodeCompletionKind 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;
+ }
+};
+
#endif // SCRIPT_TEXT_EDITOR_H