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