FindInFilesWidget.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "FindInFilesWidget.h"
  27. #include "HackStudio.h"
  28. #include "Project.h"
  29. #include <AK/StringBuilder.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/TableView.h>
  33. #include <LibGUI/TextBox.h>
  34. struct Match {
  35. String filename;
  36. GUI::TextRange range;
  37. String text;
  38. };
  39. class SearchResultsModel final : public GUI::Model {
  40. public:
  41. enum Column {
  42. Filename,
  43. Location,
  44. MatchedText,
  45. __Count
  46. };
  47. explicit SearchResultsModel(const Vector<Match>&& matches)
  48. : m_matches(move(matches))
  49. {
  50. }
  51. virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_matches.size(); }
  52. virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; }
  53. virtual String column_name(int column) const override
  54. {
  55. switch (column) {
  56. case Column::Filename:
  57. return "Filename";
  58. case Column::Location:
  59. return "#";
  60. case Column::MatchedText:
  61. return "Text";
  62. default:
  63. ASSERT_NOT_REACHED();
  64. }
  65. }
  66. virtual GUI::Variant data(const GUI::ModelIndex& index, Role role = Role::Display) const override
  67. {
  68. if (role == Role::TextAlignment)
  69. return Gfx::TextAlignment::CenterLeft;
  70. if (role == Role::Font) {
  71. if (index.column() == Column::MatchedText)
  72. return Gfx::Font::default_fixed_width_font();
  73. return {};
  74. }
  75. if (role == Role::Display) {
  76. auto& match = m_matches.at(index.row());
  77. switch (index.column()) {
  78. case Column::Filename:
  79. return match.filename;
  80. case Column::Location:
  81. return (int)match.range.start().line();
  82. case Column::MatchedText:
  83. return match.text;
  84. }
  85. }
  86. return {};
  87. }
  88. virtual void update() override { }
  89. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override { return create_index(row, column, &m_matches.at(row)); }
  90. private:
  91. Vector<Match> m_matches;
  92. };
  93. static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
  94. {
  95. Vector<Match> matches;
  96. g_project->for_each_text_file([&](auto& file) {
  97. auto matches_in_file = file.document().find_all(text);
  98. for (auto& range : matches_in_file) {
  99. auto whole_line_range = file.document().range_for_entire_line(range.start().line());
  100. auto whole_line_containing_match = file.document().text_in_range(whole_line_range);
  101. auto left_part = whole_line_containing_match.substring(0, range.start().column());
  102. auto right_part = whole_line_containing_match.substring(range.end().column(), whole_line_containing_match.length() - range.end().column());
  103. StringBuilder builder;
  104. builder.append(left_part);
  105. builder.append(0x01);
  106. builder.append(file.document().text_in_range(range));
  107. builder.append(0x02);
  108. builder.append(right_part);
  109. matches.append({ file.name(), range, builder.to_string() });
  110. }
  111. });
  112. return adopt(*new SearchResultsModel(move(matches)));
  113. }
  114. FindInFilesWidget::FindInFilesWidget()
  115. {
  116. set_layout<GUI::VerticalBoxLayout>();
  117. auto& top_container = add<Widget>();
  118. top_container.set_layout<GUI::HorizontalBoxLayout>();
  119. top_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  120. top_container.set_preferred_size(0, 20);
  121. m_textbox = top_container.add<GUI::TextBox>();
  122. m_textbox->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  123. m_button = top_container.add<GUI::Button>("Find in files");
  124. m_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  125. m_button->set_preferred_size(100, 0);
  126. m_result_view = add<GUI::TableView>();
  127. m_result_view->on_activation = [](auto& index) {
  128. auto& match = *(const Match*)index.internal_data();
  129. open_file(match.filename);
  130. current_editor().set_selection(match.range);
  131. current_editor().set_focus(true);
  132. };
  133. m_button->on_click = [this](auto) {
  134. auto results_model = find_in_files(m_textbox->text());
  135. m_result_view->set_model(results_model);
  136. };
  137. m_textbox->on_return_pressed = [this] {
  138. m_button->click();
  139. };
  140. }
  141. void FindInFilesWidget::focus_textbox_and_select_all()
  142. {
  143. m_textbox->select_all();
  144. m_textbox->set_focus(true);
  145. }