mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 15:15:07 +00:00
HackStudio: Start adding a "find in files" function
Projects now contain a set of TextDocument objects. Each TextDocument represents a member file in the project. TextDocuments may not have their file contents loaded at all times, but they will be loaded on demand when calling TextDocument::contents(). "Find in files" works by iterating over the documents in the project and calling find(needle) on each one. The return value from find() is a vector of line numbers where the needle was found. This is obviously going to need a bunch more work. :^)
This commit is contained in:
parent
41289e652f
commit
d3e81d2ba8
6 changed files with 209 additions and 38 deletions
41
DevTools/HackStudio/TextDocument.cpp
Normal file
41
DevTools/HackStudio/TextDocument.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "TextDocument.h"
|
||||
#include <LibCore/CFile.h>
|
||||
#include <string.h>
|
||||
|
||||
const ByteBuffer& TextDocument::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> TextDocument::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