Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <LibCodeComprehension/CodeComprehensionEngine.h>
10#include <Shell/Shell.h>
11
12namespace CodeComprehension::Shell {
13
14class ShellComprehensionEngine : public CodeComprehensionEngine {
15public:
16 ShellComprehensionEngine(FileDB const& filedb);
17 virtual Vector<CodeComprehension::AutocompleteResultEntry> get_suggestions(DeprecatedString const& file, const GUI::TextPosition& position) override;
18 virtual void on_edit(DeprecatedString const& file) override;
19 virtual void file_opened([[maybe_unused]] DeprecatedString const& file) override;
20 virtual Optional<CodeComprehension::ProjectLocation> find_declaration_of(DeprecatedString const& filename, const GUI::TextPosition& identifier_position) override;
21
22private:
23 struct DocumentData {
24 DocumentData(DeprecatedString&& text, DeprecatedString filename);
25 DeprecatedString filename;
26 DeprecatedString text;
27 NonnullRefPtr<::Shell::AST::Node> node;
28
29 Vector<DeprecatedString> const& sourced_paths() const;
30
31 private:
32 NonnullRefPtr<::Shell::AST::Node> parse() const;
33
34 mutable Optional<Vector<DeprecatedString>> all_sourced_paths {};
35 };
36
37 DocumentData const& get_document_data(DeprecatedString const& file) const;
38 DocumentData const& get_or_create_document_data(DeprecatedString const& file);
39 void set_document_data(DeprecatedString const& file, OwnPtr<DocumentData>&& data);
40
41 OwnPtr<DocumentData> create_document_data_for(DeprecatedString const& file);
42 void update_declared_symbols(DocumentData const&);
43
44 static size_t resolve(ShellComprehensionEngine::DocumentData const& document, const GUI::TextPosition& position);
45
46 ::Shell::Shell& shell()
47 {
48 if (s_shell)
49 return *s_shell;
50 s_shell = ::Shell::Shell::construct();
51 return *s_shell;
52 }
53
54 HashMap<DeprecatedString, OwnPtr<DocumentData>> m_documents;
55 static RefPtr<::Shell::Shell> s_shell;
56};
57}