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