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