ParserAutoComplete.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Function.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. #include <DevTools/HackStudio/AutoCompleteResponse.h>
  31. #include <DevTools/HackStudio/LanguageServers/AutoCompleteEngine.h>
  32. #include <DevTools/HackStudio/LanguageServers/FileDB.h>
  33. #include <LibCpp/AST.h>
  34. #include <LibCpp/Parser.h>
  35. #include <LibCpp/Preprocessor.h>
  36. #include <LibGUI/TextPosition.h>
  37. namespace LanguageServers::Cpp {
  38. using namespace ::Cpp;
  39. class ParserAutoComplete : public AutoCompleteEngine {
  40. public:
  41. ParserAutoComplete(ClientConnection&, const FileDB& filedb);
  42. virtual Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) override;
  43. virtual void on_edit(const String& file) override;
  44. virtual void file_opened([[maybe_unused]] const String& file) override;
  45. virtual Optional<GUI::AutocompleteProvider::ProjectLocation> find_declaration_of(const String& file_name, const GUI::TextPosition& identifier_position) override;
  46. private:
  47. struct DocumentData {
  48. const String& filename() const { return m_filename; }
  49. const String& text() const { return m_text; }
  50. const Preprocessor& preprocessor() const
  51. {
  52. VERIFY(m_preprocessor);
  53. return *m_preprocessor;
  54. }
  55. Preprocessor& preprocessor()
  56. {
  57. VERIFY(m_preprocessor);
  58. return *m_preprocessor;
  59. }
  60. const Parser& parser() const
  61. {
  62. VERIFY(m_parser);
  63. return *m_parser;
  64. }
  65. Parser& parser()
  66. {
  67. VERIFY(m_parser);
  68. return *m_parser;
  69. }
  70. String m_filename;
  71. String m_text;
  72. OwnPtr<Preprocessor> m_preprocessor;
  73. OwnPtr<Parser> m_parser;
  74. };
  75. Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const String partial_text) const;
  76. Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const String& partial_text) const;
  77. String type_of(const DocumentData&, const Expression&) const;
  78. String type_of_property(const DocumentData&, const Identifier&) const;
  79. String type_of_variable(const Identifier&) const;
  80. bool is_property(const ASTNode&) const;
  81. bool is_empty_property(const DocumentData&, const ASTNode&, const Position& autocomplete_position) const;
  82. RefPtr<Declaration> find_declaration_of(const DocumentData&, const ASTNode&) const;
  83. NonnullRefPtrVector<Declaration> get_available_declarations(const DocumentData&, const ASTNode&) const;
  84. struct PropertyInfo {
  85. StringView name;
  86. RefPtr<Type> type;
  87. };
  88. Vector<PropertyInfo> properties_of_type(const DocumentData& document, const String& type) const;
  89. NonnullRefPtrVector<Declaration> get_global_declarations_including_headers(const DocumentData& document) const;
  90. NonnullRefPtrVector<Declaration> get_global_declarations(const ASTNode& node) const;
  91. const DocumentData* get_document_data(const String& file) const;
  92. const DocumentData* get_or_create_document_data(const String& file);
  93. void set_document_data(const String& file, OwnPtr<DocumentData>&& data);
  94. OwnPtr<DocumentData> create_document_data_for(const String& file);
  95. String document_path_from_include_path(const StringView& include_path) const;
  96. void update_declared_symbols(const DocumentData&);
  97. GUI::AutocompleteProvider::DeclarationType type_of_declaration(const Declaration&);
  98. String scope_of_declaration(const Declaration&);
  99. Optional<GUI::AutocompleteProvider::ProjectLocation> find_preprocessor_definition(const DocumentData&, const GUI::TextPosition&);
  100. OwnPtr<DocumentData> create_document_data(String&& text, const String& filename);
  101. HashMap<String, OwnPtr<DocumentData>> m_documents;
  102. };
  103. }