mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
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.
This commit is contained in:
parent
b87791cada
commit
667926df73
Notes:
sideshowbarker
2024-07-18 20:25:59 +09:00
Author: https://github.com/itamar8910 Commit: https://github.com/SerenityOS/serenity/commit/667926df738 Pull-request: https://github.com/SerenityOS/serenity/pull/6216
8 changed files with 146 additions and 32 deletions
BIN
Base/res/icons/hackstudio/Member.png
Normal file
BIN
Base/res/icons/hackstudio/Member.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
BIN
Base/res/icons/hackstudio/Namespace.png
Normal file
BIN
Base/res/icons/hackstudio/Namespace.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2 KiB |
|
@ -31,6 +31,7 @@ set(SOURCES
|
|||
LanguageClient.cpp
|
||||
Locator.cpp
|
||||
Project.cpp
|
||||
ProjectDeclarations.cpp
|
||||
ProjectFile.cpp
|
||||
ProjectTemplate.cpp
|
||||
TerminalWrapper.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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
Userland/DevTools/HackStudio/ProjectDeclarations.cpp
Normal file
68
Userland/DevTools/HackStudio/ProjectDeclarations.cpp
Normal file
|
@ -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
Userland/DevTools/HackStudio/ProjectDeclarations.h
Normal file
67
Userland/DevTools/HackStudio/ProjectDeclarations.h
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue