Ver Fonte

HackStudio: Store known symbol declarations in ProjectDeclarations

They were previously stored inside the Locator widget. I moved them to
the globally visible ProjectDeclarations class so they can also be used
by other widgets.
Itamar há 4 anos atrás
pai
commit
667926df73

BIN
Base/res/icons/hackstudio/Member.png


BIN
Base/res/icons/hackstudio/Namespace.png


+ 1 - 0
Userland/DevTools/HackStudio/CMakeLists.txt

@@ -31,6 +31,7 @@ set(SOURCES
     LanguageClient.cpp
     Locator.cpp
     Project.cpp
+    ProjectDeclarations.cpp
     ProjectFile.cpp
     ProjectTemplate.cpp
     TerminalWrapper.cpp

+ 2 - 2
Userland/DevTools/HackStudio/LanguageClient.cpp

@@ -26,7 +26,7 @@
 
 #include "LanguageClient.h"
 #include "HackStudio.h"
-#include "Locator.h"
+#include "ProjectDeclarations.h"
 #include <AK/String.h>
 #include <AK/Vector.h>
 #include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
@@ -122,7 +122,7 @@ HashMap<String, NonnullOwnPtr<ServerConnectionWrapper>> ServerConnectionInstance
 
 void ServerConnection::handle(const Messages::LanguageClient::DeclarationsInDocument& message)
 {
-    locator().set_declared_symbols(message.filename(), message.declarations());
+    ProjectDeclarations::the().set_declared_symbols(message.filename(), message.declarations());
 }
 
 void LanguageClient::search_declaration(const String& path, size_t line, size_t column)

+ 8 - 28
Userland/DevTools/HackStudio/Locator.cpp

@@ -27,6 +27,7 @@
 #include "Locator.h"
 #include "HackStudio.h"
 #include "Project.h"
+#include "ProjectDeclarations.h"
 #include <LibGUI/AutocompleteProvider.h>
 #include <LibGUI/BoxLayout.h>
 #include <LibGUI/FileIconProvider.h>
@@ -85,23 +86,9 @@ public:
             if (index.column() == Column::Filename)
                 return suggestion.as_symbol_declaration.value().position.file;
             if (index.column() == Column::Icon) {
-                static GUI::Icon struct_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Struct.png"));
-                static GUI::Icon class_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Class.png"));
-                static GUI::Icon function_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Function.png"));
-                static GUI::Icon variable_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Variable.png"));
-                static GUI::Icon preprocessor_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Preprocessor.png"));
-                switch (suggestion.as_symbol_declaration.value().type) {
-                case GUI::AutocompleteProvider::DeclarationType::Struct:
-                    return struct_icon;
-                case GUI::AutocompleteProvider::DeclarationType::Class:
-                    return class_icon;
-                case GUI::AutocompleteProvider::DeclarationType::Function:
-                    return function_icon;
-                case GUI::AutocompleteProvider::DeclarationType::Variable:
-                    return variable_icon;
-                case GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition:
-                    return preprocessor_icon;
-                }
+                auto icon = ProjectDeclarations::get_icon_for(suggestion.as_symbol_declaration.value().type);
+                if (icon.has_value())
+                    return icon.value();
                 return {};
             }
         }
@@ -226,12 +213,10 @@ void Locator::update_suggestions()
             suggestions.append(LocatorSuggestionModel::Suggestion::create_filename(file.name()));
     });
 
-    for (auto& item : m_document_to_declarations) {
-        for (auto& decl : item.value) {
-            if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive) || decl.scope.contains(typed_text, CaseSensitivity::CaseInsensitive))
-                suggestions.append((LocatorSuggestionModel::Suggestion::create_symbol_declaration(decl)));
-        }
-    }
+    ProjectDeclarations::the().for_each_declared_symbol([&suggestions, &typed_text](auto& decl) {
+        if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive) || decl.scope.contains(typed_text, CaseSensitivity::CaseInsensitive))
+            suggestions.append((LocatorSuggestionModel::Suggestion::create_symbol_declaration(decl)));
+    });
 
     dbgln("I have {} suggestion(s):", suggestions.size());
     // Limit the debug logging otherwise this can be very slow for large projects
@@ -257,9 +242,4 @@ void Locator::update_suggestions()
     dbgln("Popup rect: {}", m_popup_window->rect());
     m_popup_window->show();
 }
-void Locator::set_declared_symbols(const String& filename, const Vector<GUI::AutocompleteProvider::Declaration>& declarations)
-{
-    m_document_to_declarations.set(filename, declarations);
-}
-
 }

+ 0 - 2
Userland/DevTools/HackStudio/Locator.h

@@ -39,7 +39,6 @@ public:
 
     void open();
     void close();
-    void set_declared_symbols(const String& filename, const Vector<GUI::AutocompleteProvider::Declaration>&);
 
 private:
     void update_suggestions();
@@ -50,7 +49,6 @@ private:
     RefPtr<GUI::TextBox> m_textbox;
     RefPtr<GUI::Window> m_popup_window;
     RefPtr<GUI::TableView> m_suggestion_view;
-    HashMap<String, Vector<GUI::AutocompleteProvider::Declaration>> m_document_to_declarations;
 };
 
 }

+ 68 - 0
Userland/DevTools/HackStudio/ProjectDeclarations.cpp

@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "ProjectDeclarations.h"
+
+HackStudio::ProjectDeclarations& HackStudio::ProjectDeclarations::the()
+{
+    static ProjectDeclarations s_instance;
+    return s_instance;
+}
+void HackStudio::ProjectDeclarations::set_declared_symbols(const String& filename, const Vector<GUI::AutocompleteProvider::Declaration>& declarations)
+{
+    m_document_to_declarations.set(filename, declarations);
+    if (on_update)
+        on_update();
+}
+
+Optional<GUI::Icon> HackStudio::ProjectDeclarations::get_icon_for(GUI::AutocompleteProvider::DeclarationType type)
+{
+    static GUI::Icon struct_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Struct.png"));
+    static GUI::Icon class_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Class.png"));
+    static GUI::Icon function_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Function.png"));
+    static GUI::Icon variable_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Variable.png"));
+    static GUI::Icon preprocessor_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Preprocessor.png"));
+    static GUI::Icon member_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Member.png"));
+    static GUI::Icon namespace_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Namespace.png"));
+    switch (type) {
+    case GUI::AutocompleteProvider::DeclarationType::Struct:
+        return struct_icon;
+    case GUI::AutocompleteProvider::DeclarationType::Class:
+        return class_icon;
+    case GUI::AutocompleteProvider::DeclarationType::Function:
+        return function_icon;
+    case GUI::AutocompleteProvider::DeclarationType::Variable:
+        return variable_icon;
+    case GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition:
+        return preprocessor_icon;
+    case GUI::AutocompleteProvider::DeclarationType::Member:
+        return member_icon;
+    case GUI::AutocompleteProvider::DeclarationType::Namespace:
+        return namespace_icon;
+    default:
+        return {};
+    }
+}

+ 67 - 0
Userland/DevTools/HackStudio/ProjectDeclarations.h

@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+#include <AK/HashMap.h>
+#include <AK/Noncopyable.h>
+#include <AK/String.h>
+#include <LibGUI/AutocompleteProvider.h>
+#include <LibGUI/Icon.h>
+
+namespace HackStudio {
+
+class ProjectDeclarations {
+    AK_MAKE_NONCOPYABLE(ProjectDeclarations);
+
+public:
+    static ProjectDeclarations& the();
+    template<typename Func>
+    void for_each_declared_symbol(Func);
+
+    void set_declared_symbols(const String& filename, const Vector<GUI::AutocompleteProvider::Declaration>&);
+
+    static Optional<GUI::Icon> get_icon_for(GUI::AutocompleteProvider::DeclarationType);
+
+    Function<void()> on_update = nullptr;
+
+private:
+    ProjectDeclarations() = default;
+    HashMap<String, Vector<GUI::AutocompleteProvider::Declaration>> m_document_to_declarations;
+};
+
+template<typename Func>
+void ProjectDeclarations::for_each_declared_symbol(Func f)
+{
+    for (auto& item : m_document_to_declarations) {
+        for (auto& decl : item.value) {
+            f(decl);
+        }
+    }
+}
+
+}