mirror of
https://github.com/RGBCube/serenity
synced 2025-05-16 02:44:58 +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
|
@ -6,45 +6,11 @@ const GTextDocument& ProjectFile::document() const
|
|||
{
|
||||
if (!m_document) {
|
||||
m_document = GTextDocument::create(nullptr);
|
||||
m_document->set_text(contents());
|
||||
auto file = CFile::construct(m_name);
|
||||
if (!file->open(CFile::ReadOnly)) {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
m_document->set_text(file->read_all());
|
||||
}
|
||||
return *m_document;
|
||||
}
|
||||
|
||||
const ByteBuffer& ProjectFile::contents() const
|
||||
{
|
||||
if (m_contents.is_null()) {
|
||||
auto file = CFile::construct(m_name);
|
||||
if (file->open(CFile::ReadOnly))
|
||||
m_contents = file->read_all();
|
||||
}
|
||||
return m_contents;
|
||||
}
|
||||
|
||||
Vector<int> ProjectFile::find(const StringView& needle) const
|
||||
{
|
||||
// NOTE: This forces us to load the contents if we hadn't already.
|
||||
contents();
|
||||
|
||||
Vector<int> matching_line_numbers;
|
||||
|
||||
String needle_as_string(needle);
|
||||
|
||||
int line_index = 0;
|
||||
int start_of_line = 0;
|
||||
for (int i = 0; i < m_contents.size(); ++i) {
|
||||
char ch = m_contents[i];
|
||||
if (ch == '\n') {
|
||||
// FIXME: Please come back here and do this the good boy way.
|
||||
String line(StringView(m_contents.data() + start_of_line, i - start_of_line));
|
||||
auto* found = strstr(line.characters(), needle_as_string.characters());
|
||||
if (found)
|
||||
matching_line_numbers.append(line_index + 1);
|
||||
++line_index;
|
||||
start_of_line = i + 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return matching_line_numbers;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue