FindInFilesWidget.cpp 5.9 KB

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