Locator.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Locator.h"
  8. #include "HackStudio.h"
  9. #include "Project.h"
  10. #include "ProjectDeclarations.h"
  11. #include <LibGUI/AutocompleteProvider.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/FileIconProvider.h>
  14. #include <LibGUI/TableView.h>
  15. #include <LibGUI/TextBox.h>
  16. #include <LibGUI/Window.h>
  17. namespace HackStudio {
  18. class LocatorSuggestionModel final : public GUI::Model {
  19. public:
  20. struct Suggestion {
  21. static Suggestion create_filename(DeprecatedString const& filename);
  22. static Suggestion create_symbol_declaration(CodeComprehension::Declaration const&);
  23. bool is_filename() const { return as_filename.has_value(); }
  24. bool is_symbol_declaration() const { return as_symbol_declaration.has_value(); }
  25. Optional<DeprecatedString> as_filename;
  26. Optional<CodeComprehension::Declaration> as_symbol_declaration;
  27. };
  28. explicit LocatorSuggestionModel(Vector<Suggestion>&& suggestions)
  29. : m_suggestions(move(suggestions))
  30. {
  31. }
  32. enum Column {
  33. Icon,
  34. Name,
  35. Filename,
  36. __Column_Count,
  37. };
  38. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_suggestions.size(); }
  39. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Column_Count; }
  40. virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
  41. {
  42. auto& suggestion = m_suggestions.at(index.row());
  43. if (role != GUI::ModelRole::Display)
  44. return {};
  45. if (suggestion.is_filename()) {
  46. if (index.column() == Column::Name)
  47. return suggestion.as_filename.value();
  48. if (index.column() == Column::Filename)
  49. return "";
  50. if (index.column() == Column::Icon)
  51. return GUI::FileIconProvider::icon_for_path(suggestion.as_filename.value());
  52. }
  53. if (suggestion.is_symbol_declaration()) {
  54. if (index.column() == Column::Name) {
  55. if (!suggestion.as_symbol_declaration.value().scope.is_empty())
  56. return suggestion.as_symbol_declaration.value().name;
  57. return DeprecatedString::formatted("{}::{}", suggestion.as_symbol_declaration.value().scope, suggestion.as_symbol_declaration.value().name);
  58. }
  59. if (index.column() == Column::Filename)
  60. return suggestion.as_symbol_declaration.value().position.file;
  61. if (index.column() == Column::Icon) {
  62. auto icon = ProjectDeclarations::get_icon_for(suggestion.as_symbol_declaration.value().type);
  63. if (icon.has_value())
  64. return icon.value();
  65. return {};
  66. }
  67. }
  68. return {};
  69. }
  70. Vector<Suggestion> const& suggestions() const { return m_suggestions; }
  71. private:
  72. Vector<Suggestion> m_suggestions;
  73. };
  74. LocatorSuggestionModel::Suggestion LocatorSuggestionModel::Suggestion::create_filename(DeprecatedString const& filename)
  75. {
  76. LocatorSuggestionModel::Suggestion s;
  77. s.as_filename = filename;
  78. return s;
  79. }
  80. LocatorSuggestionModel::Suggestion LocatorSuggestionModel::Suggestion::create_symbol_declaration(CodeComprehension::Declaration const& decl)
  81. {
  82. LocatorSuggestionModel::Suggestion s;
  83. s.as_symbol_declaration = decl;
  84. return s;
  85. }
  86. Locator::Locator(Core::EventReceiver* parent)
  87. {
  88. set_layout<GUI::VerticalBoxLayout>();
  89. set_fixed_height(22);
  90. m_textbox = add<GUI::TextBox>();
  91. m_textbox->on_change = [this] {
  92. update_suggestions();
  93. };
  94. m_textbox->on_escape_pressed = [this] {
  95. m_popup_window->hide();
  96. m_textbox->set_focus(false);
  97. };
  98. m_textbox->on_up_pressed = [this] {
  99. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  100. if (new_index.is_valid())
  101. new_index = m_suggestion_view->model()->index(new_index.row() - 1);
  102. else
  103. new_index = m_suggestion_view->model()->index(0);
  104. if (m_suggestion_view->model()->is_within_range(new_index)) {
  105. m_suggestion_view->selection().set(new_index);
  106. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  107. }
  108. };
  109. m_textbox->on_down_pressed = [this] {
  110. GUI::ModelIndex new_index = m_suggestion_view->selection().first();
  111. if (new_index.is_valid())
  112. new_index = m_suggestion_view->model()->index(new_index.row() + 1);
  113. else
  114. new_index = m_suggestion_view->model()->index(0);
  115. if (m_suggestion_view->model()->is_within_range(new_index)) {
  116. m_suggestion_view->selection().set(new_index);
  117. m_suggestion_view->scroll_into_view(new_index, Orientation::Vertical);
  118. }
  119. };
  120. m_textbox->on_return_pressed = [this] {
  121. auto selected_index = m_suggestion_view->selection().first();
  122. if (!selected_index.is_valid())
  123. return;
  124. open_suggestion(selected_index);
  125. };
  126. m_textbox->on_focusout = [&]() {
  127. close();
  128. };
  129. m_popup_window = GUI::Window::construct(parent);
  130. m_popup_window->set_window_type(GUI::WindowType::Popup);
  131. m_popup_window->set_rect(0, 0, 500, 200);
  132. m_suggestion_view = m_popup_window->set_main_widget<GUI::TableView>();
  133. m_suggestion_view->set_column_headers_visible(false);
  134. m_suggestion_view->on_activation = [this](auto& index) {
  135. open_suggestion(index);
  136. };
  137. }
  138. void Locator::open_suggestion(const GUI::ModelIndex& index)
  139. {
  140. auto& model = reinterpret_cast<LocatorSuggestionModel&>(*m_suggestion_view->model());
  141. auto suggestion = model.suggestions()[index.row()];
  142. if (suggestion.is_filename()) {
  143. auto filename = suggestion.as_filename.value();
  144. open_file(filename);
  145. }
  146. if (suggestion.is_symbol_declaration()) {
  147. auto position = suggestion.as_symbol_declaration.value().position;
  148. open_file(position.file, position.line, position.column);
  149. }
  150. close();
  151. }
  152. void Locator::open()
  153. {
  154. m_textbox->set_focus(true);
  155. if (!m_textbox->text().is_empty()) {
  156. m_textbox->select_all();
  157. m_popup_window->show();
  158. }
  159. }
  160. void Locator::close()
  161. {
  162. m_popup_window->hide();
  163. }
  164. void Locator::update_suggestions()
  165. {
  166. auto typed_text = m_textbox->text();
  167. Vector<LocatorSuggestionModel::Suggestion> suggestions;
  168. project().for_each_text_file([&](auto& file) {
  169. if (file.name().contains(typed_text, CaseSensitivity::CaseInsensitive))
  170. suggestions.append(LocatorSuggestionModel::Suggestion::create_filename(file.name()));
  171. });
  172. ProjectDeclarations::the().for_each_declared_symbol([&suggestions, &typed_text](auto& decl) {
  173. if (decl.name.contains(typed_text, CaseSensitivity::CaseInsensitive) || decl.scope.contains(typed_text, CaseSensitivity::CaseInsensitive))
  174. suggestions.append((LocatorSuggestionModel::Suggestion::create_symbol_declaration(decl)));
  175. });
  176. dbgln("I have {} suggestion(s):", suggestions.size());
  177. // Limit the debug logging otherwise this can be very slow for large projects
  178. if (suggestions.size() < 100) {
  179. for (auto& s : suggestions) {
  180. if (s.is_filename())
  181. dbgln(" {}", s.as_filename.value());
  182. if (s.is_symbol_declaration())
  183. dbgln(" {} ({})", s.as_symbol_declaration.value().name, s.as_symbol_declaration.value().position.file);
  184. }
  185. }
  186. bool has_suggestions = !suggestions.is_empty();
  187. m_suggestion_view->set_model(adopt_ref(*new LocatorSuggestionModel(move(suggestions))));
  188. if (!has_suggestions)
  189. m_suggestion_view->selection().clear();
  190. else
  191. m_suggestion_view->selection().set(m_suggestion_view->model()->index(0));
  192. m_popup_window->move_to(screen_relative_rect().top_left().translated(0, -m_popup_window->height()));
  193. dbgln("Popup rect: {}", m_popup_window->rect());
  194. m_popup_window->show();
  195. }
  196. }