Locator.cpp 9.2 KB

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