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