Quellcode durchsuchen

HackStudio: Sort ClassView entries

Itamar vor 4 Jahren
Ursprung
Commit
e5f84b53d8
1 geänderte Dateien mit 21 neuen und 6 gelöschten Zeilen
  1. 21 6
      Userland/DevTools/HackStudio/ClassViewWidget.cpp

+ 21 - 6
Userland/DevTools/HackStudio/ClassViewWidget.cpp

@@ -27,7 +27,10 @@
 #include "ClassViewWidget.h"
 #include "HackStudio.h"
 #include "ProjectDeclarations.h"
+#include <AK/BinarySearch.h>
+#include <AK/StdLibExtras.h>
 #include <LibGUI/BoxLayout.h>
+#include <string.h>
 
 namespace HackStudio {
 
@@ -127,6 +130,22 @@ ClassViewModel::ClassViewModel()
     });
 }
 
+static ClassViewNode& add_child_node(NonnullOwnPtrVector<ClassViewNode>& children, NonnullOwnPtr<ClassViewNode>&& node_ptr, ClassViewNode* parent, const GUI::AutocompleteProvider::Declaration* declaration)
+{
+    node_ptr->parent = parent;
+    node_ptr->declaration = declaration;
+
+    size_t inserted_index = 0;
+    ClassViewNode& node = *node_ptr;
+    children.insert_before_matching(
+        move(node_ptr), [&node](auto& other_node) {
+            return strncmp(node.name.characters_without_null_termination(), other_node->name.characters_without_null_termination(), min(node.name.length(), other_node->name.length())) < 0;
+        },
+        0, &inserted_index);
+
+    return children.at(inserted_index);
+}
+
 void ClassViewModel::add_declaration(const GUI::AutocompleteProvider::Declaration& decl)
 {
     ClassViewNode* parent = nullptr;
@@ -160,9 +179,7 @@ void ClassViewModel::add_declaration(const GUI::AutocompleteProvider::Declaratio
                 continue;
             }
 
-            parent->children.append(make<ClassViewNode>(scope));
-            parent->children.last().parent = parent;
-            parent = &parent->children.last();
+            parent = &add_child_node(parent->children, make<ClassViewNode>(scope), parent, nullptr);
         }
     }
 
@@ -184,9 +201,7 @@ void ClassViewModel::add_declaration(const GUI::AutocompleteProvider::Declaratio
         }
     }
     if (!already_exists) {
-        children_of_parent->append(make<ClassViewNode>(decl.name));
-        children_of_parent->last().declaration = &decl;
-        children_of_parent->last().parent = parent;
+        add_child_node(*children_of_parent, make<ClassViewNode>(decl.name), parent, &decl);
     }
 }