2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-10-23 22:13:08 +03:00
|
|
|
#include "FindInFilesWidget.h"
|
2020-08-10 23:28:38 +03:00
|
|
|
#include "HackStudio.h"
|
2019-10-23 22:13:08 +03:00
|
|
|
#include "Project.h"
|
2019-12-11 00:07:52 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-02-06 22:33:02 +03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
#include <LibGUI/Button.h>
|
|
|
|
#include <LibGUI/TableView.h>
|
|
|
|
#include <LibGUI/TextBox.h>
|
2022-04-09 10:28:38 +03:00
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
2019-10-23 22:13:08 +03:00
|
|
|
|
2020-08-17 16:22:30 +03:00
|
|
|
namespace HackStudio {
|
|
|
|
|
2019-12-11 00:07:52 +03:00
|
|
|
struct Match {
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString filename;
|
2020-02-02 17:07:41 +03:00
|
|
|
GUI::TextRange range;
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString text;
|
2019-10-23 22:13:08 +03:00
|
|
|
};
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class SearchResultsModel final : public GUI::Model {
|
2019-10-23 22:13:08 +03:00
|
|
|
public:
|
2019-12-11 00:07:52 +03:00
|
|
|
enum Column {
|
|
|
|
Filename,
|
|
|
|
Location,
|
|
|
|
MatchedText,
|
|
|
|
__Count
|
|
|
|
};
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
explicit SearchResultsModel(Vector<Match> const&& matches)
|
2019-10-23 22:13:08 +03:00
|
|
|
: m_matches(move(matches))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
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; }
|
2019-12-11 00:07:52 +03:00
|
|
|
|
2023-06-13 18:30:15 +03:00
|
|
|
virtual ErrorOr<String> column_name(int column) const override
|
2019-12-11 00:07:52 +03:00
|
|
|
{
|
|
|
|
switch (column) {
|
|
|
|
case Column::Filename:
|
2023-08-07 12:12:38 +03:00
|
|
|
return "Filename"_string;
|
2019-12-11 00:07:52 +03:00
|
|
|
case Column::Location:
|
2023-08-08 05:26:17 +03:00
|
|
|
return "#"_string;
|
2019-12-11 00:07:52 +03:00
|
|
|
case Column::MatchedText:
|
2023-08-08 05:26:17 +03:00
|
|
|
return "Text"_string;
|
2019-12-11 00:07:52 +03:00
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-12-11 00:07:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-16 17:00:07 +03:00
|
|
|
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override
|
2019-10-23 22:13:08 +03:00
|
|
|
{
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == GUI::ModelRole::TextAlignment)
|
2020-05-21 20:36:09 +03:00
|
|
|
return Gfx::TextAlignment::CenterLeft;
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == GUI::ModelRole::Font) {
|
2020-05-21 19:56:52 +03:00
|
|
|
if (index.column() == Column::MatchedText)
|
2020-12-29 20:25:13 +03:00
|
|
|
return Gfx::FontDatabase::default_fixed_width_font();
|
2020-05-21 19:56:52 +03:00
|
|
|
return {};
|
|
|
|
}
|
2020-08-16 17:00:07 +03:00
|
|
|
if (role == GUI::ModelRole::Display) {
|
2019-10-23 22:13:08 +03:00
|
|
|
auto& match = m_matches.at(index.row());
|
2019-12-11 00:07:52 +03:00
|
|
|
switch (index.column()) {
|
|
|
|
case Column::Filename:
|
|
|
|
return match.filename;
|
|
|
|
case Column::Location:
|
|
|
|
return (int)match.range.start().line();
|
|
|
|
case Column::MatchedText:
|
|
|
|
return match.text;
|
|
|
|
}
|
2019-10-23 22:13:08 +03:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2019-12-11 00:07:52 +03:00
|
|
|
|
2020-12-10 20:35:59 +03:00
|
|
|
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));
|
|
|
|
}
|
2019-10-23 22:13:08 +03:00
|
|
|
|
|
|
|
private:
|
2019-12-11 00:07:52 +03:00
|
|
|
Vector<Match> m_matches;
|
2019-10-23 22:13:08 +03:00
|
|
|
};
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
static RefPtr<SearchResultsModel> find_in_files(StringView text)
|
2019-10-23 22:13:08 +03:00
|
|
|
{
|
2019-12-11 00:07:52 +03:00
|
|
|
Vector<Match> matches;
|
2020-09-19 17:25:05 +03:00
|
|
|
project().for_each_text_file([&](auto& file) {
|
2019-11-01 21:22:42 +03:00
|
|
|
auto matches_in_file = file.document().find_all(text);
|
|
|
|
for (auto& range : matches_in_file) {
|
2019-12-11 00:07:52 +03:00
|
|
|
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);
|
2023-12-16 17:19:34 +03:00
|
|
|
matches.append({ file.name(), range, builder.to_byte_string() });
|
2019-10-23 22:13:08 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-23 17:46:57 +03:00
|
|
|
return adopt_ref(*new SearchResultsModel(move(matches)));
|
2019-10-23 22:13:08 +03:00
|
|
|
}
|
|
|
|
|
2020-02-23 12:57:42 +03:00
|
|
|
FindInFilesWidget::FindInFilesWidget()
|
2019-10-23 22:13:08 +03:00
|
|
|
{
|
2020-03-04 11:43:54 +03:00
|
|
|
set_layout<GUI::VerticalBoxLayout>();
|
2020-04-01 12:28:16 +03:00
|
|
|
|
|
|
|
auto& top_container = add<Widget>();
|
|
|
|
top_container.set_layout<GUI::HorizontalBoxLayout>();
|
2022-02-24 13:55:22 +03:00
|
|
|
top_container.set_fixed_height(22);
|
2020-04-01 12:28:16 +03:00
|
|
|
|
|
|
|
m_textbox = top_container.add<GUI::TextBox>();
|
|
|
|
|
2023-08-07 12:12:38 +03:00
|
|
|
m_button = top_container.add<GUI::Button>("Find"_string);
|
2023-05-22 20:07:09 +03:00
|
|
|
m_button->set_fixed_width(50);
|
2019-10-23 22:13:08 +03:00
|
|
|
|
2020-02-23 12:57:42 +03:00
|
|
|
m_result_view = add<GUI::TableView>();
|
2019-10-23 22:13:08 +03:00
|
|
|
|
2019-11-01 21:22:42 +03:00
|
|
|
m_result_view->on_activation = [](auto& index) {
|
2019-12-11 00:07:52 +03:00
|
|
|
auto& match = *(const Match*)index.internal_data();
|
2019-11-01 21:22:42 +03:00
|
|
|
open_file(match.filename);
|
|
|
|
current_editor().set_selection(match.range);
|
2019-10-27 14:55:10 +03:00
|
|
|
current_editor().set_focus(true);
|
2019-10-23 22:13:08 +03:00
|
|
|
};
|
|
|
|
|
2020-05-12 21:30:33 +03:00
|
|
|
m_button->on_click = [this](auto) {
|
2019-10-23 22:13:08 +03:00
|
|
|
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);
|
|
|
|
}
|
2021-08-02 21:26:20 +03:00
|
|
|
void FindInFilesWidget::reset()
|
|
|
|
{
|
|
|
|
m_result_view->set_model(nullptr);
|
|
|
|
}
|
2020-08-17 16:22:30 +03:00
|
|
|
|
|
|
|
}
|