ShellComprehensionEngine.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ShellComprehensionEngine.h"
  7. #include <AK/Assertions.h>
  8. #include <AK/HashTable.h>
  9. #include <LibRegex/Regex.h>
  10. namespace CodeComprehension::Shell {
  11. RefPtr<::Shell::Shell> ShellComprehensionEngine::s_shell {};
  12. ShellComprehensionEngine::ShellComprehensionEngine(FileDB const& filedb)
  13. : CodeComprehensionEngine(filedb, true)
  14. {
  15. }
  16. ShellComprehensionEngine::DocumentData const& ShellComprehensionEngine::get_or_create_document_data(String const& file)
  17. {
  18. auto absolute_path = filedb().to_absolute_path(file);
  19. if (!m_documents.contains(absolute_path)) {
  20. set_document_data(absolute_path, create_document_data_for(absolute_path));
  21. }
  22. return get_document_data(absolute_path);
  23. }
  24. ShellComprehensionEngine::DocumentData const& ShellComprehensionEngine::get_document_data(String const& file) const
  25. {
  26. auto absolute_path = filedb().to_absolute_path(file);
  27. auto document_data = m_documents.get(absolute_path);
  28. VERIFY(document_data.has_value());
  29. return *document_data.value();
  30. }
  31. OwnPtr<ShellComprehensionEngine::DocumentData> ShellComprehensionEngine::create_document_data_for(String const& file)
  32. {
  33. auto document = filedb().get_or_read_from_filesystem(file);
  34. if (!document.has_value())
  35. return {};
  36. auto content = document.value();
  37. auto document_data = make<DocumentData>(move(content), file);
  38. for (auto& path : document_data->sourced_paths())
  39. get_or_create_document_data(path);
  40. update_declared_symbols(*document_data);
  41. return document_data;
  42. }
  43. void ShellComprehensionEngine::set_document_data(String const& file, OwnPtr<DocumentData>&& data)
  44. {
  45. m_documents.set(filedb().to_absolute_path(file), move(data));
  46. }
  47. ShellComprehensionEngine::DocumentData::DocumentData(String&& _text, String _filename)
  48. : filename(move(_filename))
  49. , text(move(_text))
  50. , node(parse())
  51. {
  52. }
  53. Vector<String> const& ShellComprehensionEngine::DocumentData::sourced_paths() const
  54. {
  55. if (all_sourced_paths.has_value())
  56. return all_sourced_paths.value();
  57. struct : public ::Shell::AST::NodeVisitor {
  58. void visit(const ::Shell::AST::CastToCommand* node) override
  59. {
  60. auto& inner = node->inner();
  61. if (inner->is_list()) {
  62. if (auto* list = dynamic_cast<const ::Shell::AST::ListConcatenate*>(inner.ptr())) {
  63. auto& entries = list->list();
  64. if (entries.size() == 2 && entries.first()->is_bareword() && static_ptr_cast<::Shell::AST::BarewordLiteral>(entries.first())->text() == "source") {
  65. auto& filename = entries[1];
  66. if (filename->would_execute())
  67. return;
  68. auto name_list = const_cast<::Shell::AST::Node*>(filename.ptr())->run(nullptr)->resolve_as_list(nullptr);
  69. StringBuilder builder;
  70. builder.join(' ', name_list);
  71. sourced_files.set(builder.build());
  72. }
  73. }
  74. }
  75. ::Shell::AST::NodeVisitor::visit(node);
  76. }
  77. HashTable<String> sourced_files;
  78. } visitor;
  79. node->visit(visitor);
  80. Vector<String> sourced_paths;
  81. for (auto& entry : visitor.sourced_files)
  82. sourced_paths.append(move(entry));
  83. all_sourced_paths = move(sourced_paths);
  84. return all_sourced_paths.value();
  85. }
  86. NonnullRefPtr<::Shell::AST::Node> ShellComprehensionEngine::DocumentData::parse() const
  87. {
  88. ::Shell::Parser parser { text };
  89. if (auto node = parser.parse())
  90. return node.release_nonnull();
  91. return ::Shell::AST::make_ref_counted<::Shell::AST::SyntaxError>(::Shell::AST::Position {}, "Unable to parse file");
  92. }
  93. size_t ShellComprehensionEngine::resolve(ShellComprehensionEngine::DocumentData const& document, const GUI::TextPosition& position)
  94. {
  95. size_t offset = 0;
  96. if (position.line() > 0) {
  97. auto first = true;
  98. size_t line = 0;
  99. for (auto& line_view : document.text.split_limit('\n', position.line() + 1, SplitBehavior::KeepEmpty)) {
  100. if (line == position.line())
  101. break;
  102. if (first)
  103. first = false;
  104. else
  105. ++offset; // For the newline.
  106. offset += line_view.length();
  107. ++line;
  108. }
  109. }
  110. offset += position.column() + 1;
  111. return offset;
  112. }
  113. Vector<CodeComprehension::AutocompleteResultEntry> ShellComprehensionEngine::get_suggestions(String const& file, const GUI::TextPosition& position)
  114. {
  115. dbgln_if(SH_LANGUAGE_SERVER_DEBUG, "ShellComprehensionEngine position {}:{}", position.line(), position.column());
  116. auto const& document = get_or_create_document_data(file);
  117. size_t offset_in_file = resolve(document, position);
  118. ::Shell::AST::HitTestResult hit_test = document.node->hit_test_position(offset_in_file);
  119. if (!hit_test.matching_node) {
  120. dbgln_if(SH_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", position.line(), position.column());
  121. return {};
  122. }
  123. auto completions = const_cast<::Shell::AST::Node*>(document.node.ptr())->complete_for_editor(shell(), offset_in_file, hit_test);
  124. Vector<CodeComprehension::AutocompleteResultEntry> entries;
  125. for (auto& completion : completions)
  126. entries.append({ completion.text_string, completion.input_offset });
  127. return entries;
  128. }
  129. void ShellComprehensionEngine::on_edit(String const& file)
  130. {
  131. set_document_data(file, create_document_data_for(file));
  132. }
  133. void ShellComprehensionEngine::file_opened([[maybe_unused]] String const& file)
  134. {
  135. set_document_data(file, create_document_data_for(file));
  136. }
  137. Optional<CodeComprehension::ProjectLocation> ShellComprehensionEngine::find_declaration_of(String const& filename, const GUI::TextPosition& identifier_position)
  138. {
  139. dbgln_if(SH_LANGUAGE_SERVER_DEBUG, "find_declaration_of({}, {}:{})", filename, identifier_position.line(), identifier_position.column());
  140. auto const& document = get_or_create_document_data(filename);
  141. auto position = resolve(document, identifier_position);
  142. auto result = document.node->hit_test_position(position);
  143. if (!result.matching_node) {
  144. dbgln_if(SH_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
  145. return {};
  146. }
  147. if (!result.matching_node->is_bareword()) {
  148. dbgln_if(SH_LANGUAGE_SERVER_DEBUG, "no bareword at position {}:{}", identifier_position.line(), identifier_position.column());
  149. return {};
  150. }
  151. auto name = static_ptr_cast<::Shell::AST::BarewordLiteral>(result.matching_node)->text();
  152. auto& declarations = all_declarations();
  153. for (auto& entry : declarations) {
  154. for (auto& declaration : entry.value) {
  155. if (declaration.name == name)
  156. return declaration.position;
  157. }
  158. }
  159. return {};
  160. }
  161. void ShellComprehensionEngine::update_declared_symbols(DocumentData const& document)
  162. {
  163. struct Visitor : public ::Shell::AST::NodeVisitor {
  164. explicit Visitor(String const& filename)
  165. : filename(filename)
  166. {
  167. }
  168. void visit(const ::Shell::AST::VariableDeclarations* node) override
  169. {
  170. for (auto& entry : node->variables()) {
  171. auto literal = entry.name->leftmost_trivial_literal();
  172. if (!literal)
  173. continue;
  174. String name;
  175. if (literal->is_bareword())
  176. name = static_ptr_cast<::Shell::AST::BarewordLiteral>(literal)->text();
  177. if (!name.is_empty()) {
  178. dbgln("Found variable {}", name);
  179. declarations.append({ move(name), { filename, entry.name->position().start_line.line_number, entry.name->position().start_line.line_column }, CodeComprehension::DeclarationType::Variable, {} });
  180. }
  181. }
  182. ::Shell::AST::NodeVisitor::visit(node);
  183. }
  184. void visit(const ::Shell::AST::FunctionDeclaration* node) override
  185. {
  186. dbgln("Found function {}", node->name().name);
  187. declarations.append({ node->name().name, { filename, node->position().start_line.line_number, node->position().start_line.line_column }, CodeComprehension::DeclarationType::Function, {} });
  188. }
  189. String const& filename;
  190. Vector<CodeComprehension::Declaration> declarations;
  191. } visitor { document.filename };
  192. document.node->visit(visitor);
  193. set_declarations_of_document(document.filename, move(visitor.declarations));
  194. }
  195. }