ParserAutoComplete.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "ParserAutoComplete.h"
  27. #include <AK/Assertions.h>
  28. #include <AK/HashTable.h>
  29. #include <LibCpp/AST.h>
  30. #include <LibCpp/Lexer.h>
  31. #include <LibCpp/Parser.h>
  32. #include <LibCpp/Preprocessor.h>
  33. #include <LibRegex/Regex.h>
  34. // #define AUTOCOMPLETE_VERBOSE
  35. #ifdef AUTOCOMPLETE_VERBOSE
  36. # define VERBOSE(fmt, ...) dbgln(fmt, ##__VA_ARGS__)
  37. #else
  38. # define VERBOSE(fmt, ...) \
  39. do { \
  40. } while (0)
  41. #endif
  42. namespace LanguageServers::Cpp {
  43. ParserAutoComplete::ParserAutoComplete(const FileDB& filedb)
  44. : AutoCompleteEngine(filedb)
  45. {
  46. }
  47. const ParserAutoComplete::DocumentData& ParserAutoComplete::get_or_create_document_data(const String& file)
  48. {
  49. auto absolute_path = filedb().to_absolute_path(file);
  50. if (!m_documents.contains(absolute_path)) {
  51. set_document_data(absolute_path, create_document_data_for(absolute_path));
  52. }
  53. return get_document_data(absolute_path);
  54. }
  55. const ParserAutoComplete::DocumentData& ParserAutoComplete::get_document_data(const String& file) const
  56. {
  57. auto absolute_path = filedb().to_absolute_path(file);
  58. auto document_data = m_documents.get(absolute_path);
  59. ASSERT(document_data.has_value());
  60. return *document_data.value();
  61. }
  62. OwnPtr<ParserAutoComplete::DocumentData> ParserAutoComplete::create_document_data_for(const String& file)
  63. {
  64. auto document = filedb().get(file);
  65. ASSERT(document);
  66. auto content = document->text();
  67. auto document_data = make<DocumentData>(document->text());
  68. auto root = document_data->parser.parse();
  69. for (auto& path : document_data->preprocessor.included_paths()) {
  70. get_or_create_document_data(document_path_from_include_path(path));
  71. }
  72. #ifdef DEBUG_AUTOCOMPLETE
  73. root->dump(0);
  74. #endif
  75. return move(document_data);
  76. }
  77. void ParserAutoComplete::set_document_data(const String& file, OwnPtr<DocumentData>&& data)
  78. {
  79. m_documents.set(filedb().to_absolute_path(file), move(data));
  80. }
  81. ParserAutoComplete::DocumentData::DocumentData(String&& _text)
  82. : text(move(_text))
  83. , preprocessor(text.view())
  84. , parser(preprocessor.process().view())
  85. {
  86. }
  87. Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position)
  88. {
  89. Cpp::Position position { autocomplete_position.line(), autocomplete_position.column() > 0 ? autocomplete_position.column() - 1 : 0 };
  90. VERBOSE("ParserAutoComplete position {}:{}", position.line, position.column);
  91. const auto& document = get_or_create_document_data(file);
  92. auto node = document.parser.node_at(position);
  93. if (!node) {
  94. VERBOSE("no node at position {}:{}", position.line, position.column);
  95. return {};
  96. }
  97. if (node->is_identifier()) {
  98. if (is_property(*node)) {
  99. return autocomplete_property(document, (MemberExpression&)(*node->parent()), document.parser.text_of_node(*node));
  100. }
  101. return autocomplete_name(document, *node, document.parser.text_of_node(*node));
  102. }
  103. if (is_empty_property(document, *node, position)) {
  104. ASSERT(node->is_member_expression());
  105. return autocomplete_property(document, (MemberExpression&)(*node), "");
  106. }
  107. String partial_text = String::empty();
  108. auto containing_token = document.parser.token_at(position);
  109. if (containing_token.has_value()) {
  110. partial_text = document.parser.text_of_token(containing_token.value());
  111. }
  112. return autocomplete_name(document, *node, partial_text.view());
  113. }
  114. Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_name(const DocumentData& document, const ASTNode& node, const StringView& partial_text) const
  115. {
  116. const Cpp::ASTNode* current = &node;
  117. NonnullRefPtrVector<Cpp::Declaration> available_declarations;
  118. while (current) {
  119. available_declarations.append(current->declarations());
  120. current = current->parent();
  121. }
  122. available_declarations.append(get_declarations_in_outer_scope_including_headers(document));
  123. Vector<StringView> available_names;
  124. auto add_name = [&available_names](auto& name) {
  125. if (name.is_null() || name.is_empty())
  126. return;
  127. if (!available_names.contains_slow(name))
  128. available_names.append(name);
  129. };
  130. for (auto& decl : available_declarations) {
  131. if (decl.is_variable_or_parameter_declaration()) {
  132. add_name(((Cpp::VariableOrParameterDeclaration&)decl).m_name);
  133. }
  134. if (decl.is_struct_or_class()) {
  135. add_name(((Cpp::StructOrClassDeclaration&)decl).m_name);
  136. }
  137. if (decl.is_function()) {
  138. add_name(((Cpp::FunctionDeclaration&)decl).m_name);
  139. }
  140. }
  141. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  142. for (auto& name : available_names) {
  143. if (name.starts_with(partial_text)) {
  144. suggestions.append({ name.to_string(), partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  145. }
  146. }
  147. return suggestions;
  148. }
  149. Vector<GUI::AutocompleteProvider::Entry> ParserAutoComplete::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const StringView partial_text) const
  150. {
  151. auto type = type_of(document, *parent.m_object);
  152. if (type.is_null()) {
  153. VERBOSE("Could not infer type of object");
  154. return {};
  155. }
  156. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  157. for (auto& prop : properties_of_type(document, type)) {
  158. if (prop.name.starts_with(partial_text)) {
  159. suggestions.append({ prop.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  160. }
  161. }
  162. return suggestions;
  163. }
  164. bool ParserAutoComplete::is_property(const ASTNode& node) const
  165. {
  166. if (!node.parent()->is_member_expression())
  167. return false;
  168. auto& parent = (MemberExpression&)(*node.parent());
  169. return parent.m_property.ptr() == &node;
  170. }
  171. bool ParserAutoComplete::is_empty_property(const DocumentData& document, const ASTNode& node, const Position& autocomplete_position) const
  172. {
  173. if (!node.is_member_expression())
  174. return false;
  175. auto previous_token = document.parser.token_at(autocomplete_position);
  176. if (!previous_token.has_value())
  177. return false;
  178. return previous_token.value().type() == Token::Type::Dot;
  179. }
  180. String ParserAutoComplete::type_of_property(const DocumentData& document, const Identifier& identifier) const
  181. {
  182. auto& parent = (const MemberExpression&)(*identifier.parent());
  183. auto properties = properties_of_type(document, type_of(document, *parent.m_object));
  184. for (auto& prop : properties) {
  185. if (prop.name == identifier.m_name)
  186. return prop.type->m_name;
  187. }
  188. return {};
  189. }
  190. String ParserAutoComplete::type_of_variable(const Identifier& identifier) const
  191. {
  192. const ASTNode* current = &identifier;
  193. while (current) {
  194. for (auto& decl : current->declarations()) {
  195. if (decl.is_variable_or_parameter_declaration()) {
  196. auto& var_or_param = (VariableOrParameterDeclaration&)decl;
  197. if (var_or_param.m_name == identifier.m_name) {
  198. return var_or_param.m_type->m_name;
  199. }
  200. }
  201. }
  202. current = current->parent();
  203. }
  204. return {};
  205. }
  206. String ParserAutoComplete::type_of(const DocumentData& document, const Expression& expression) const
  207. {
  208. if (expression.is_member_expression()) {
  209. auto& member_expression = (const MemberExpression&)expression;
  210. return type_of_property(document, *member_expression.m_property);
  211. }
  212. if (!expression.is_identifier()) {
  213. ASSERT_NOT_REACHED(); // TODO
  214. }
  215. auto& identifier = (const Identifier&)expression;
  216. if (is_property(identifier))
  217. return type_of_property(document, identifier);
  218. return type_of_variable(identifier);
  219. }
  220. Vector<ParserAutoComplete::PropertyInfo> ParserAutoComplete::properties_of_type(const DocumentData& document, const String& type) const
  221. {
  222. auto declarations = get_declarations_in_outer_scope_including_headers(document);
  223. Vector<PropertyInfo> properties;
  224. for (auto& decl : declarations) {
  225. if (!decl.is_struct_or_class())
  226. continue;
  227. auto& struct_or_class = (StructOrClassDeclaration&)decl;
  228. if (struct_or_class.m_name != type)
  229. continue;
  230. for (auto& member : struct_or_class.m_members) {
  231. properties.append({ member.m_name, member.m_type });
  232. }
  233. }
  234. return properties;
  235. }
  236. NonnullRefPtrVector<Declaration> ParserAutoComplete::get_declarations_in_outer_scope_including_headers(const DocumentData& document) const
  237. {
  238. NonnullRefPtrVector<Declaration> declarations;
  239. for (auto& include : document.preprocessor.included_paths()) {
  240. auto included_document = get_document_data(document_path_from_include_path(include));
  241. declarations.append(get_declarations_in_outer_scope_including_headers(included_document));
  242. }
  243. for (auto& decl : document.parser.root_node()->declarations()) {
  244. declarations.append(decl);
  245. }
  246. return declarations;
  247. }
  248. String ParserAutoComplete::document_path_from_include_path(const StringView& include_path) const
  249. {
  250. static Regex<PosixExtended> library_include("<(.+)>");
  251. static Regex<PosixExtended> user_defined_include("\"(.+)\"");
  252. auto document_path_for_library_include = [&](const StringView& include_path) -> String {
  253. RegexResult result;
  254. if (!library_include.search(include_path, result))
  255. return {};
  256. auto path = result.capture_group_matches.at(0).at(0).view.u8view();
  257. return String::formatted("/usr/include/{}", path);
  258. };
  259. auto document_path_for_user_defined_include = [&](const StringView& include_path) -> String {
  260. RegexResult result;
  261. if (!user_defined_include.search(include_path, result))
  262. return {};
  263. return result.capture_group_matches.at(0).at(0).view.u8view();
  264. };
  265. auto result = document_path_for_library_include(include_path);
  266. if (result.is_null())
  267. result = document_path_for_user_defined_include(include_path);
  268. return result;
  269. }
  270. void ParserAutoComplete::on_edit(const String& file)
  271. {
  272. set_document_data(file, create_document_data_for(file));
  273. }
  274. void ParserAutoComplete::file_opened([[maybe_unused]] const String& file)
  275. {
  276. set_document_data(file, create_document_data_for(file));
  277. }
  278. }