ParserAutoComplete.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "AutoCompleteEngine.h"
  28. #include "FileDB.h"
  29. #include <AK/Function.h>
  30. #include <AK/String.h>
  31. #include <AK/Vector.h>
  32. #include <DevTools/HackStudio/AutoCompleteResponse.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. DocumentData(String&& text, const String& filename);
  49. String filename;
  50. String text;
  51. Preprocessor preprocessor;
  52. Parser parser;
  53. };
  54. Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const StringView partial_text) const;
  55. Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const StringView& partial_text) const;
  56. String type_of(const DocumentData&, const Expression&) const;
  57. String type_of_property(const DocumentData&, const Identifier&) const;
  58. String type_of_variable(const Identifier&) const;
  59. bool is_property(const ASTNode&) const;
  60. bool is_empty_property(const DocumentData&, const ASTNode&, const Position& autocomplete_position) const;
  61. RefPtr<Declaration> find_declaration_of(const DocumentData&, const ASTNode&) const;
  62. NonnullRefPtrVector<Declaration> get_available_declarations(const DocumentData&, const ASTNode&) const;
  63. struct PropertyInfo {
  64. StringView name;
  65. RefPtr<Type> type;
  66. };
  67. Vector<PropertyInfo> properties_of_type(const DocumentData& document, const String& type) const;
  68. NonnullRefPtrVector<Declaration> get_declarations_in_outer_scope_including_headers(const DocumentData& document) const;
  69. const DocumentData& get_document_data(const String& file) const;
  70. const DocumentData& get_or_create_document_data(const String& file);
  71. void set_document_data(const String& file, OwnPtr<DocumentData>&& data);
  72. OwnPtr<DocumentData> create_document_data_for(const String& file);
  73. String document_path_from_include_path(const StringView& include_path) const;
  74. void update_declared_symbols(const DocumentData&);
  75. GUI::AutocompleteProvider::DeclarationType type_of_declaration(const Declaration&);
  76. HashMap<String, OwnPtr<DocumentData>> m_documents;
  77. };
  78. }