ParserAutoComplete.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/String.h>
  30. #include <AK/Vector.h>
  31. #include <DevTools/HackStudio/AutoCompleteResponse.h>
  32. #include <LibCpp/AST.h>
  33. #include <LibCpp/Parser.h>
  34. #include <LibCpp/Preprocessor.h>
  35. #include <LibGUI/TextPosition.h>
  36. namespace LanguageServers::Cpp {
  37. using namespace ::Cpp;
  38. class ParserAutoComplete : public AutoCompleteEngine {
  39. public:
  40. ParserAutoComplete(const FileDB& filedb);
  41. virtual Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) override;
  42. virtual void on_edit(const String& file) override;
  43. virtual void file_opened([[maybe_unused]] const String& file) override;
  44. private:
  45. struct DocumentData {
  46. DocumentData(String&& text);
  47. String text;
  48. Preprocessor preprocessor;
  49. Parser parser;
  50. };
  51. Vector<GUI::AutocompleteProvider::Entry> autocomplete_property(const DocumentData&, const MemberExpression&, const StringView partial_text) const;
  52. Vector<GUI::AutocompleteProvider::Entry> autocomplete_name(const DocumentData&, const ASTNode&, const StringView& partial_text) const;
  53. String type_of(const DocumentData&, const Expression&) const;
  54. String type_of_property(const DocumentData&, const Identifier&) const;
  55. String type_of_variable(const Identifier&) const;
  56. bool is_property(const ASTNode&) const;
  57. bool is_empty_property(const DocumentData&, const ASTNode&, const Position& autocomplete_position) const;
  58. struct PropertyInfo {
  59. StringView name;
  60. RefPtr<Type> type;
  61. };
  62. Vector<PropertyInfo> properties_of_type(const DocumentData& document, const String& type) const;
  63. NonnullRefPtrVector<Declaration> get_declarations_in_outer_scope_including_headers(const DocumentData& document) const;
  64. const DocumentData& get_document_data(const String& file) const;
  65. const DocumentData& get_or_create_document_data(const String& file);
  66. void set_document_data(const String& file, OwnPtr<DocumentData>&& data);
  67. OwnPtr<DocumentData> create_document_data_for(const String& file);
  68. String document_path_from_include_path(const StringView& include_path) const;
  69. HashMap<String, OwnPtr<DocumentData>> m_documents;
  70. };
  71. }