1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "FindInFilesWidget.h"
#include "HackStudio.h"
#include "Project.h"
#include <AK/StringBuilder.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/TableView.h>
#include <LibGUI/TextBox.h>
#include <LibGfx/FontDatabase.h>
namespace HackStudio {
struct Match {
String filename;
GUI::TextRange range;
String text;
};
class SearchResultsModel final : public GUI::Model {
public:
enum Column {
Filename,
Location,
MatchedText,
__Count
};
explicit SearchResultsModel(const Vector<Match>&& matches)
: m_matches(move(matches))
{
}
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_matches.size(); }
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; }
virtual String column_name(int column) const override
{
switch (column) {
case Column::Filename:
return "Filename";
case Column::Location:
return "#";
case Column::MatchedText:
return "Text";
default:
VERIFY_NOT_REACHED();
}
}
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
{
if (role == GUI::ModelRole::TextAlignment)
return Gfx::TextAlignment::CenterLeft;
if (role == GUI::ModelRole::Font) {
if (index.column() == Column::MatchedText)
return Gfx::FontDatabase::default_fixed_width_font();
return {};
}
if (role == GUI::ModelRole::Display) {
auto& match = m_matches.at(index.row());
switch (index.column()) {
case Column::Filename:
return match.filename;
case Column::Location:
return (int)match.range.start().line();
case Column::MatchedText:
return match.text;
}
}
return {};
}
virtual void update() override { }
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override
{
if (row < 0 || row >= (int)m_matches.size())
return {};
if (column < 0 || column >= Column::__Count)
return {};
return create_index(row, column, &m_matches.at(row));
}
private:
Vector<Match> m_matches;
};
static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
{
Vector<Match> matches;
project().for_each_text_file([&](auto& file) {
auto matches_in_file = file.document().find_all(text);
for (auto& range : matches_in_file) {
auto whole_line_range = file.document().range_for_entire_line(range.start().line());
auto whole_line_containing_match = file.document().text_in_range(whole_line_range);
auto left_part = whole_line_containing_match.substring(0, range.start().column());
auto right_part = whole_line_containing_match.substring(range.end().column(), whole_line_containing_match.length() - range.end().column());
StringBuilder builder;
builder.append(left_part);
builder.append(0x01);
builder.append(file.document().text_in_range(range));
builder.append(0x02);
builder.append(right_part);
matches.append({ file.name(), range, builder.to_string() });
}
});
return adopt_ref(*new SearchResultsModel(move(matches)));
}
FindInFilesWidget::FindInFilesWidget()
{
set_layout<GUI::VerticalBoxLayout>();
auto& top_container = add<Widget>();
top_container.set_layout<GUI::HorizontalBoxLayout>();
top_container.set_fixed_height(20);
m_textbox = top_container.add<GUI::TextBox>();
m_button = top_container.add<GUI::Button>("Find in files");
m_button->set_fixed_width(100);
m_result_view = add<GUI::TableView>();
m_result_view->on_activation = [](auto& index) {
auto& match = *(const Match*)index.internal_data();
open_file(match.filename);
current_editor().set_selection(match.range);
current_editor().set_focus(true);
};
m_button->on_click = [this](auto) {
auto results_model = find_in_files(m_textbox->text());
m_result_view->set_model(results_model);
};
m_textbox->on_return_pressed = [this] {
m_button->click();
};
}
void FindInFilesWidget::focus_textbox_and_select_all()
{
m_textbox->select_all();
m_textbox->set_focus(true);
}
}
|