From 667926df73881abfdaf4ef827ab90cb788de75ab Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 9 Apr 2021 08:49:48 +0300 Subject: [PATCH] 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. --- Base/res/icons/hackstudio/Member.png | Bin 0 -> 2143 bytes Base/res/icons/hackstudio/Namespace.png | Bin 0 -> 2014 bytes Userland/DevTools/HackStudio/CMakeLists.txt | 1 + .../DevTools/HackStudio/LanguageClient.cpp | 4 +- Userland/DevTools/HackStudio/Locator.cpp | 36 +++------- Userland/DevTools/HackStudio/Locator.h | 2 - .../HackStudio/ProjectDeclarations.cpp | 68 ++++++++++++++++++ .../DevTools/HackStudio/ProjectDeclarations.h | 67 +++++++++++++++++ 8 files changed, 146 insertions(+), 32 deletions(-) create mode 100644 Base/res/icons/hackstudio/Member.png create mode 100644 Base/res/icons/hackstudio/Namespace.png create mode 100644 Userland/DevTools/HackStudio/ProjectDeclarations.cpp create mode 100644 Userland/DevTools/HackStudio/ProjectDeclarations.h diff --git a/Base/res/icons/hackstudio/Member.png b/Base/res/icons/hackstudio/Member.png new file mode 100644 index 0000000000000000000000000000000000000000..e79338fd077cdaf249c3ef940c2b14085d0ba1ef GIT binary patch literal 2143 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s-U8_PON`ey06$*;-(=u~X z6-p`#QWa7wGSe6sDsHU}k1Ttm!Sgp=bT11FN9R+6d6%tUJpGC#E*LB|r- zyo}>a`c?m*?=!#N^H`Hc*W|8?kGM2drhi;e7GHVyWLv2{bG5C_nc26w+co>|y~3}B$E|D=4zfhd^UK-2*Y&fanR$ra{xjuwtV${xet#>; zNU+;e;of}X{GR&uxIJI9@9n8~{9}2C`?7Vn${ms2?A<{#C+^z)<*m8fWy4?J^x8S5 zKQ4UrspU*l(H0}m4R+lbP4bT;!@M)PN>)CI*nCxNi&v+NgN(?Ez-5Um*0+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h{l@%B7_`E6-EvTRF?`r_;IAsh`Et*WOB)Tm4DAjr-RrqwAS{3azd> zj!hh_ydeRttt@M;G}Vl@%>VL%-@2|dhHIx*!jlLGUa6Gh{QubZ?>6n8ESwi)!TDwH zjc@KUFYdG6^1c1fZrj^$oDb^R)_qyG{~A-8oo9a8)*FHbJ9uMa%1u-GcidrYoA+vy zqV=Tt%K~(m-lu=@tyC!DZn<{np4D%QT82$mLVvMYSg^QxPuctKReRAz`($kES`4*%!p z$p5>4cX#8(6&)7h1|XniY5b?Yf@9Io*WPSwdV*|ha+*Rs52hw1{P;Gj@!|FKw*UTq z1_q~Y3(K<=I8V7N@z2)k)QA5+E!o@{S&euEE++k`FJxq%yh)mwSy}mCON*$%W+Mak z^$NC9o~$BEkJuZFtehgWzg%;|nvd@5nGSJJ*}=fDgV)RQ_wI8U3=9kmp00i_>zopr E0AU{F6aWAK literal 0 HcmV?d00001 diff --git a/Base/res/icons/hackstudio/Namespace.png b/Base/res/icons/hackstudio/Namespace.png new file mode 100644 index 0000000000000000000000000000000000000000..133208960479d9e584b502f5aec6dc2f54ad4253 GIT binary patch literal 2014 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s-s;WXFN`ey06$*;-(=u~X z6-p`#QWa7wGSe6sDsF8J&n`1o=lOeHYgfxe2aainKUgfce(|X~u|dC1)$bCYSq2No z!fMuSEC2twdnUeO#vF4$lOHesHXe5sw{+w<{&h*<<%e#z@qMayUYvcGutF!QdM}&( zSJA3t74v@Cyj6Mew0U7f`ti!!6PGFS6*q2u6m#Oz)&qgd7Mk!z&ePug`tKa=-x6OQ zi=DdVIAc~pt?&9dq2C^P%4QvJU)4W>S>8`@`z6`jnaT6jpQk?mcX!P~p7}eXHXUDa z`03KO@tZi;I^V6{xN>2k*@A7;+-vxmPds3<={Mf?^~S|LooC&i{!G4i^V*Wbymz8! znHccPmYaMief3ths&2b;{yS@rd53Kc{yvzvYp=omijdOvg*7c(>!Y<7ZmoK&XW6g& zd*{ln!bZ%e!p`|M8J=BSvY08-y;Fnn<)nifi=I4oIVsY~n5699DrD+h$}!WZrX(iv zqEY&Sd!>vKF6Xv2zpGk!ENihY!~9&fP@OGzX3gApA;P@xs}bv(7!K!|2|Wf+Pbb}z z{1TbD_PNl+nU>Dt2W%?5YQadyEejt^SfTjm^ya?nsd!zk@@ymF>R>%W52 zd<%4sao*D2>3Q08zjRSV^aGxv6Bi$z3INQbrlCq@$f3;!_m@CvCqcnKI-1tKWsQ zudE6VT))b3@v?+wZlRhB0!}=dMO!R-Rh49uI}De#mYf&m}I=RC$rrSU&|S)f42RdsMyy!XM%!Ei*1j%^2DSGdQB4hGtO@2%bM$(?7QLk z@^@~hD%zYmqPlnb6|UsDuUU6%pUIU;wE?!jHs10{J9fTp!;&f6O76KzhTNH?^YV(` zOuejMd!v_qFO0obc=YVO;OaX||9`5wcdAS;+}`HZvX;1jW7j6ghh)jS&(0Kyw2Yl@ zI`Nda-P>IAvXg6U{~Gz7oO5}Po95)cuYXQW{~gBs?w_gk+MAR1i7u~ur~9M$u3vZj z-&gyO=3b86S?Hr3aQ0iB{A$l0@=7hS#n#7AWaK0a7z zfA4PfoxRL0ZmpSN2}~DCZu_VUCz);6`nFH0L7+*(Jtl3Mp}MDLMc?}BA3v(ZA4lGM z{!gYh>t0fWzskJSwoKuC0ss74Y|m>SacG~^`e^xahNrkn-IHCH9+{tWoz6N>($M&2 z1XeDejZ@Vr@9j-`4*8_v#M){`?)AQ#|Gr$B7)>zifIc z=TCJDU*T7~j#>0soAoKb+I7st_qlq_`hD*wF4Oe6e8KZT75^E!juG_?UyH?F4_{D<-_@=TBO;uUb_ #include #include @@ -122,7 +122,7 @@ HashMap> 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) diff --git a/Userland/DevTools/HackStudio/Locator.cpp b/Userland/DevTools/HackStudio/Locator.cpp index 91ae7e5de9..8144695c10 100644 --- a/Userland/DevTools/HackStudio/Locator.cpp +++ b/Userland/DevTools/HackStudio/Locator.cpp @@ -27,6 +27,7 @@ #include "Locator.h" #include "HackStudio.h" #include "Project.h" +#include "ProjectDeclarations.h" #include #include #include @@ -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& declarations) -{ - m_document_to_declarations.set(filename, declarations); -} - } diff --git a/Userland/DevTools/HackStudio/Locator.h b/Userland/DevTools/HackStudio/Locator.h index 7d8ae8ac85..5b2e365107 100644 --- a/Userland/DevTools/HackStudio/Locator.h +++ b/Userland/DevTools/HackStudio/Locator.h @@ -39,7 +39,6 @@ public: void open(); void close(); - void set_declared_symbols(const String& filename, const Vector&); private: void update_suggestions(); @@ -50,7 +49,6 @@ private: RefPtr m_textbox; RefPtr m_popup_window; RefPtr m_suggestion_view; - HashMap> m_document_to_declarations; }; } diff --git a/Userland/DevTools/HackStudio/ProjectDeclarations.cpp b/Userland/DevTools/HackStudio/ProjectDeclarations.cpp new file mode 100644 index 0000000000..02a8f19790 --- /dev/null +++ b/Userland/DevTools/HackStudio/ProjectDeclarations.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2021, Itamar S. + * 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& declarations) +{ + m_document_to_declarations.set(filename, declarations); + if (on_update) + on_update(); +} + +Optional 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 {}; + } +} diff --git a/Userland/DevTools/HackStudio/ProjectDeclarations.h b/Userland/DevTools/HackStudio/ProjectDeclarations.h new file mode 100644 index 0000000000..93494530f5 --- /dev/null +++ b/Userland/DevTools/HackStudio/ProjectDeclarations.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021, Itamar S. + * 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 +#include +#include +#include +#include +#include + +namespace HackStudio { + +class ProjectDeclarations { + AK_MAKE_NONCOPYABLE(ProjectDeclarations); + +public: + static ProjectDeclarations& the(); + template + void for_each_declared_symbol(Func); + + void set_declared_symbols(const String& filename, const Vector&); + + static Optional get_icon_for(GUI::AutocompleteProvider::DeclarationType); + + Function on_update = nullptr; + +private: + ProjectDeclarations() = default; + HashMap> m_document_to_declarations; +}; + +template +void ProjectDeclarations::for_each_declared_symbol(Func f) +{ + for (auto& item : m_document_to_declarations) { + for (auto& decl : item.value) { + f(decl); + } + } +} + +}