mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 17:55:08 +00:00
HackStudio: Use GTextDoument::find_all() to implement find-in-files
This fixes the bug seen in my monthly OS update video, where we'd look through a stale copy of each file, instead of the potentially edited version in the GTextDocument. Search results are now also represented as a full GTextRange, and when you jump to a search result, we select the whole matching range. :^)
This commit is contained in:
parent
b8bf998b61
commit
390b219cd1
3 changed files with 24 additions and 62 deletions
|
@ -9,14 +9,14 @@ extern GTextEditor& current_editor();
|
|||
extern void open_file(const String&);
|
||||
extern OwnPtr<Project> g_project;
|
||||
|
||||
struct FilenameAndLineNumber {
|
||||
struct FilenameAndRange {
|
||||
String filename;
|
||||
int line_number { -1 };
|
||||
GTextRange range;
|
||||
};
|
||||
|
||||
class SearchResultsModel final : public GModel {
|
||||
public:
|
||||
explicit SearchResultsModel(const Vector<FilenameAndLineNumber>&& matches)
|
||||
explicit SearchResultsModel(const Vector<FilenameAndRange>&& matches)
|
||||
: m_matches(move(matches))
|
||||
{
|
||||
}
|
||||
|
@ -27,23 +27,29 @@ public:
|
|||
{
|
||||
if (role == Role::Display) {
|
||||
auto& match = m_matches.at(index.row());
|
||||
return String::format("%s:%d", match.filename.characters(), match.line_number);
|
||||
return String::format("%s: (%d,%d - %d,%d)",
|
||||
match.filename.characters(),
|
||||
match.range.start().line() + 1,
|
||||
match.range.start().column(),
|
||||
match.range.end().line() + 1,
|
||||
match.range.end().column());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
virtual void update() override {}
|
||||
virtual GModelIndex index(int row, int column = 0, const GModelIndex& = GModelIndex()) const override { return create_index(row, column, &m_matches.at(row)); }
|
||||
|
||||
private:
|
||||
Vector<FilenameAndLineNumber> m_matches;
|
||||
Vector<FilenameAndRange> m_matches;
|
||||
};
|
||||
|
||||
static RefPtr<SearchResultsModel> find_in_files(const StringView& text)
|
||||
{
|
||||
Vector<FilenameAndLineNumber> matches;
|
||||
Vector<FilenameAndRange> 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 });
|
||||
auto matches_in_file = file.document().find_all(text);
|
||||
for (auto& range : matches_in_file) {
|
||||
matches.append({ file.name(), range });
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -63,15 +69,10 @@ FindInFilesWidget::FindInFilesWidget(GWidget* parent)
|
|||
|
||||
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]);
|
||||
current_editor().set_cursor(line_number - 1, 0);
|
||||
m_result_view->on_activation = [](auto& index) {
|
||||
auto& match = *(const FilenameAndRange*)index.internal_data();
|
||||
open_file(match.filename);
|
||||
current_editor().set_selection(match.range);
|
||||
current_editor().set_focus(true);
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue