mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:47:44 +00:00
HackStudio: Move "find in files" widget to its own file/class
Instead of clogging up main.cpp with find-in-files functionality, put it in a FindInFilesWidget class in a separate file.
This commit is contained in:
parent
2d460b504f
commit
2260190f39
4 changed files with 118 additions and 83 deletions
90
DevTools/HackStudio/FindInFilesWidget.cpp
Normal file
90
DevTools/HackStudio/FindInFilesWidget.cpp
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
#include "FindInFilesWidget.h"
|
||||||
|
#include "Project.h"
|
||||||
|
#include <LibGUI/GBoxLayout.h>
|
||||||
|
#include <LibGUI/GButton.h>
|
||||||
|
#include <LibGUI/GListView.h>
|
||||||
|
#include <LibGUI/GTextBox.h>
|
||||||
|
|
||||||
|
extern void open_file(const String&);
|
||||||
|
extern OwnPtr<Project> g_project;
|
||||||
|
|
||||||
|
struct FilenameAndLineNumber {
|
||||||
|
String filename;
|
||||||
|
int line_number { -1 };
|
||||||
|
};
|
||||||
|
|
||||||
|
class SearchResultsModel final : public GModel {
|
||||||
|
public:
|
||||||
|
explicit SearchResultsModel(const Vector<FilenameAndLineNumber>&& matches)
|
||||||
|
: m_matches(move(matches))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_matches.size(); }
|
||||||
|
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return 1; }
|
||||||
|
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
||||||
|
{
|
||||||
|
if (role == Role::Display) {
|
||||||
|
auto& match = m_matches.at(index.row());
|
||||||
|
return String::format("%s:%d", match.filename.characters(), match.line_number);
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
virtual void update() override {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector<FilenameAndLineNumber> m_matches;
|
||||||
|
};
|
||||||
|
|
||||||
|
static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
|
||||||
|
{
|
||||||
|
Vector<FilenameAndLineNumber> matches;
|
||||||
|
g_project->for_each_text_file([&](auto& file) {
|
||||||
|
auto matches_in_file = file.find(text);
|
||||||
|
for (int match : matches_in_file) {
|
||||||
|
matches.append({ file.name(), match });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return adopt(*new SearchResultsModel(move(matches)));
|
||||||
|
}
|
||||||
|
|
||||||
|
FindInFilesWidget::FindInFilesWidget(GWidget* parent)
|
||||||
|
: GWidget(parent)
|
||||||
|
{
|
||||||
|
set_layout(make<GBoxLayout>(Orientation::Vertical));
|
||||||
|
m_textbox = GTextBox::construct(this);
|
||||||
|
m_textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
||||||
|
m_textbox->set_preferred_size(0, 20);
|
||||||
|
m_button = GButton::construct("Find in files", this);
|
||||||
|
m_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
||||||
|
m_button->set_preferred_size(0, 20);
|
||||||
|
|
||||||
|
m_result_view = GListView::construct(this);
|
||||||
|
|
||||||
|
m_result_view->on_activation = [this](auto& index) {
|
||||||
|
auto match_string = m_result_view->model()->data(index).to_string();
|
||||||
|
auto parts = match_string.split(':');
|
||||||
|
ASSERT(parts.size() == 2);
|
||||||
|
bool ok;
|
||||||
|
int line_number = parts[1].to_int(ok);
|
||||||
|
ASSERT(ok);
|
||||||
|
open_file(parts[0]);
|
||||||
|
m_textbox->set_cursor(line_number - 1, 0);
|
||||||
|
m_textbox->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);
|
||||||
|
}
|
22
DevTools/HackStudio/FindInFilesWidget.h
Normal file
22
DevTools/HackStudio/FindInFilesWidget.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibGUI/GWidget.h>
|
||||||
|
|
||||||
|
class GButton;
|
||||||
|
class GListView;
|
||||||
|
class GTextBox;
|
||||||
|
|
||||||
|
class FindInFilesWidget final : public GWidget {
|
||||||
|
C_OBJECT(FindInFilesWidget)
|
||||||
|
public:
|
||||||
|
virtual ~FindInFilesWidget() override {}
|
||||||
|
|
||||||
|
void focus_textbox_and_select_all();
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit FindInFilesWidget(GWidget* parent);
|
||||||
|
|
||||||
|
RefPtr<GTextBox> m_textbox;
|
||||||
|
RefPtr<GButton> m_button;
|
||||||
|
RefPtr<GListView> m_result_view;
|
||||||
|
};
|
|
@ -4,6 +4,7 @@ OBJS = \
|
||||||
Project.o \
|
Project.o \
|
||||||
TextDocument.o \
|
TextDocument.o \
|
||||||
TerminalWrapper.o \
|
TerminalWrapper.o \
|
||||||
|
FindInFilesWidget.o \
|
||||||
main.o
|
main.o
|
||||||
|
|
||||||
APP = HackStudio
|
APP = HackStudio
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
#include "FindInFilesWidget.h"
|
||||||
#include "Project.h"
|
#include "Project.h"
|
||||||
#include "TerminalWrapper.h"
|
#include "TerminalWrapper.h"
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <LibGUI/GAboutDialog.h>
|
#include <LibGUI/GAboutDialog.h>
|
||||||
#include <LibGUI/GAction.h>
|
#include <LibGUI/GAction.h>
|
||||||
#include <LibGUI/GApplication.h>
|
#include <LibGUI/GApplication.h>
|
||||||
#include <LibGUI/GButton.h>
|
|
||||||
#include <LibGUI/GBoxLayout.h>
|
#include <LibGUI/GBoxLayout.h>
|
||||||
|
#include <LibGUI/GButton.h>
|
||||||
#include <LibGUI/GInputBox.h>
|
#include <LibGUI/GInputBox.h>
|
||||||
#include <LibGUI/GListView.h>
|
#include <LibGUI/GListView.h>
|
||||||
#include <LibGUI/GMenu.h>
|
#include <LibGUI/GMenu.h>
|
||||||
|
@ -27,12 +28,10 @@ OwnPtr<Project> g_project;
|
||||||
RefPtr<GWindow> g_window;
|
RefPtr<GWindow> g_window;
|
||||||
RefPtr<GListView> g_project_list_view;
|
RefPtr<GListView> g_project_list_view;
|
||||||
RefPtr<GTextEditor> g_text_editor;
|
RefPtr<GTextEditor> g_text_editor;
|
||||||
RefPtr<GTextBox> g_find_in_files_textbox;
|
|
||||||
|
|
||||||
static void build(TerminalWrapper&);
|
static void build(TerminalWrapper&);
|
||||||
static void run(TerminalWrapper&);
|
static void run(TerminalWrapper&);
|
||||||
static NonnullRefPtr<GWidget> build_find_in_files_widget();
|
void open_file(const String&);
|
||||||
static void open_file(const String&);
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
@ -75,7 +74,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
auto tab_widget = GTabWidget::construct(inner_splitter);
|
auto tab_widget = GTabWidget::construct(inner_splitter);
|
||||||
|
|
||||||
auto find_in_files_widget = build_find_in_files_widget();
|
auto find_in_files_widget = FindInFilesWidget::construct(nullptr);
|
||||||
tab_widget->add_widget("Find in files", find_in_files_widget);
|
tab_widget->add_widget("Find in files", find_in_files_widget);
|
||||||
|
|
||||||
auto terminal_wrapper = TerminalWrapper::construct(nullptr);
|
auto terminal_wrapper = TerminalWrapper::construct(nullptr);
|
||||||
|
@ -116,8 +115,7 @@ int main(int argc, char** argv)
|
||||||
auto edit_menu = make<GMenu>("Edit");
|
auto edit_menu = make<GMenu>("Edit");
|
||||||
edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
|
edit_menu->add_action(GAction::create("Find in files...", { Mod_Ctrl | Mod_Shift, Key_F }, [&](auto&) {
|
||||||
tab_widget->set_active_widget(find_in_files_widget);
|
tab_widget->set_active_widget(find_in_files_widget);
|
||||||
g_find_in_files_textbox->select_all();
|
find_in_files_widget->focus_textbox_and_select_all();
|
||||||
g_find_in_files_textbox->set_focus(true);
|
|
||||||
}));
|
}));
|
||||||
menubar->add_menu(move(edit_menu));
|
menubar->add_menu(move(edit_menu));
|
||||||
|
|
||||||
|
@ -156,82 +154,6 @@ void run(TerminalWrapper& wrapper)
|
||||||
wrapper.run_command("make run");
|
wrapper.run_command("make run");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FilenameAndLineNumber {
|
|
||||||
String filename;
|
|
||||||
int line_number { -1 };
|
|
||||||
};
|
|
||||||
|
|
||||||
class SearchResultsModel final : public GModel {
|
|
||||||
public:
|
|
||||||
explicit SearchResultsModel(const Vector<FilenameAndLineNumber>&& matches)
|
|
||||||
: m_matches(move(matches))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_matches.size(); }
|
|
||||||
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return 1; }
|
|
||||||
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
|
||||||
{
|
|
||||||
if (role == Role::Display) {
|
|
||||||
auto& match = m_matches.at(index.row());
|
|
||||||
return String::format("%s:%d", match.filename.characters(), match.line_number);
|
|
||||||
}
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
virtual void update() override {}
|
|
||||||
|
|
||||||
private:
|
|
||||||
Vector<FilenameAndLineNumber> m_matches;
|
|
||||||
};
|
|
||||||
|
|
||||||
static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
|
|
||||||
{
|
|
||||||
Vector<FilenameAndLineNumber> matches;
|
|
||||||
g_project->for_each_text_file([&](auto& file) {
|
|
||||||
auto matches_in_file = file.find(text);
|
|
||||||
for (int match : matches_in_file) {
|
|
||||||
matches.append({ file.name(), match });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return adopt(*new SearchResultsModel(move(matches)));
|
|
||||||
}
|
|
||||||
|
|
||||||
NonnullRefPtr<GWidget> build_find_in_files_widget()
|
|
||||||
{
|
|
||||||
auto widget = GWidget::construct();
|
|
||||||
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
|
|
||||||
g_find_in_files_textbox = GTextBox::construct(widget);
|
|
||||||
g_find_in_files_textbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
||||||
g_find_in_files_textbox->set_preferred_size(0, 20);
|
|
||||||
auto button = GButton::construct("Find in files", widget);
|
|
||||||
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
|
||||||
button->set_preferred_size(0, 20);
|
|
||||||
|
|
||||||
auto result_view = GListView::construct(widget);
|
|
||||||
|
|
||||||
result_view->on_activation = [result_view](auto& index) {
|
|
||||||
auto match_string = result_view->model()->data(index).to_string();
|
|
||||||
auto parts = match_string.split(':');
|
|
||||||
ASSERT(parts.size() == 2);
|
|
||||||
bool ok;
|
|
||||||
int line_number = parts[1].to_int(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
open_file(parts[0]);
|
|
||||||
g_text_editor->set_cursor(line_number - 1, 0);
|
|
||||||
g_text_editor->set_focus(true);
|
|
||||||
};
|
|
||||||
|
|
||||||
button->on_click = [result_view = result_view.ptr()](auto&) {
|
|
||||||
auto results_model = find_in_files(g_find_in_files_textbox->text());
|
|
||||||
result_view->set_model(results_model);
|
|
||||||
};
|
|
||||||
g_find_in_files_textbox->on_return_pressed = [button = button.ptr()] {
|
|
||||||
button->click();
|
|
||||||
};
|
|
||||||
return widget;
|
|
||||||
}
|
|
||||||
|
|
||||||
void open_file(const String& filename)
|
void open_file(const String& filename)
|
||||||
{
|
{
|
||||||
auto file = CFile::construct(filename);
|
auto file = CFile::construct(filename);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue