Locator.cpp 8.0 KB

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