ProjectDeclarations.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ProjectDeclarations.h"
  8. #include "HackStudio.h"
  9. namespace HackStudio {
  10. ProjectDeclarations::ProjectDeclarations()
  11. : m_declarations_model(adopt_ref(*new DeclarationsModel({})))
  12. {
  13. }
  14. ProjectDeclarations& ProjectDeclarations::the()
  15. {
  16. static ProjectDeclarations s_instance;
  17. return s_instance;
  18. }
  19. void ProjectDeclarations::set_declared_symbols(ByteString const& filename, Vector<CodeComprehension::Declaration> const& declarations)
  20. {
  21. m_document_to_declarations.set(filename, declarations);
  22. // FIXME: Partially invalidate the model instead of fully rebuilding it.
  23. update_declarations_model();
  24. if (on_update)
  25. on_update();
  26. }
  27. Optional<GUI::Icon> ProjectDeclarations::get_icon_for(CodeComprehension::DeclarationType type)
  28. {
  29. static GUI::Icon struct_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Struct.png"sv).release_value_but_fixme_should_propagate_errors());
  30. static GUI::Icon class_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Class.png"sv).release_value_but_fixme_should_propagate_errors());
  31. static GUI::Icon function_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Function.png"sv).release_value_but_fixme_should_propagate_errors());
  32. static GUI::Icon variable_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Variable.png"sv).release_value_but_fixme_should_propagate_errors());
  33. static GUI::Icon preprocessor_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Preprocessor.png"sv).release_value_but_fixme_should_propagate_errors());
  34. static GUI::Icon member_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Member.png"sv).release_value_but_fixme_should_propagate_errors());
  35. static GUI::Icon namespace_icon(Gfx::Bitmap::load_from_file("/res/icons/hackstudio/Namespace.png"sv).release_value_but_fixme_should_propagate_errors());
  36. switch (type) {
  37. case CodeComprehension::DeclarationType::Struct:
  38. return struct_icon;
  39. case CodeComprehension::DeclarationType::Class:
  40. return class_icon;
  41. case CodeComprehension::DeclarationType::Function:
  42. return function_icon;
  43. case CodeComprehension::DeclarationType::Variable:
  44. return variable_icon;
  45. case CodeComprehension::DeclarationType::PreprocessorDefinition:
  46. return preprocessor_icon;
  47. case CodeComprehension::DeclarationType::Member:
  48. return member_icon;
  49. case CodeComprehension::DeclarationType::Namespace:
  50. return namespace_icon;
  51. default:
  52. return {};
  53. }
  54. }
  55. void ProjectDeclarations::update_declarations_model()
  56. {
  57. Vector<Declaration> declarations;
  58. project().for_each_text_file([&](auto& file) {
  59. declarations.append(Declaration::create_filename(file.name()));
  60. });
  61. for_each_declared_symbol([&declarations](auto& decl) {
  62. declarations.append((Declaration::create_symbol_declaration(decl)));
  63. });
  64. m_declarations_model->set_declarations(move(declarations));
  65. }
  66. }