ShellComprehensionEngine.h 1.9 KB

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