ClassViewWidget.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringView.h>
  8. #include <LibGUI/AutocompleteProvider.h>
  9. #include <LibGUI/TreeView.h>
  10. #include <LibGUI/Widget.h>
  11. namespace HackStudio {
  12. class ClassViewWidget final : public GUI::Widget {
  13. C_OBJECT(ClassViewWidget)
  14. public:
  15. virtual ~ClassViewWidget() override { }
  16. void refresh();
  17. private:
  18. ClassViewWidget();
  19. RefPtr<GUI::TreeView> m_class_tree;
  20. };
  21. // Note: A ClassViewNode stores a raw pointer to the Declaration from ProjectDeclarations and a StringView into its name.
  22. // We should take care to update the ClassViewModel whenever the declarations change, because otherwise we may be holding pointers to freed memory.
  23. // This is currently achieved with the on_update callback of ProjectDeclarations.
  24. struct ClassViewNode {
  25. StringView name;
  26. CodeComprehension::Declaration const* declaration { nullptr };
  27. NonnullOwnPtrVector<ClassViewNode> children;
  28. ClassViewNode* parent { nullptr };
  29. explicit ClassViewNode(StringView name)
  30. : name(name) {};
  31. };
  32. class ClassViewModel final : public GUI::Model {
  33. public:
  34. static RefPtr<ClassViewModel> create();
  35. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
  36. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 1; }
  37. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole role) const override;
  38. virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
  39. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent_index = GUI::ModelIndex()) const override;
  40. private:
  41. explicit ClassViewModel();
  42. void add_declaration(CodeComprehension::Declaration const&);
  43. NonnullOwnPtrVector<ClassViewNode> m_root_scope;
  44. };
  45. }