summaryrefslogtreecommitdiff
path: root/DevTools/HackStudio/ProjectFile.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-11-01 18:40:32 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-01 21:31:06 +0100
commitb81f6f2c43b04a221a74880a58b1ced0cbad4a25 (patch)
treecb0d11ead67f8dc253e6a72154bb25d2effb9507 /DevTools/HackStudio/ProjectFile.cpp
parent45e0c841ad0b04cdc80e9641560a2506deeec7e6 (diff)
downloadserenity-b81f6f2c43b04a221a74880a58b1ced0cbad4a25.zip
HackStudio: Rename TextDocument => ProjectFile
TextDocument was not the right name, and got even more confusing with the addition of GTextDocument in LibGUI.
Diffstat (limited to 'DevTools/HackStudio/ProjectFile.cpp')
-rw-r--r--DevTools/HackStudio/ProjectFile.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/DevTools/HackStudio/ProjectFile.cpp b/DevTools/HackStudio/ProjectFile.cpp
new file mode 100644
index 0000000000..9c95356106
--- /dev/null
+++ b/DevTools/HackStudio/ProjectFile.cpp
@@ -0,0 +1,50 @@
+#include "ProjectFile.h"
+#include <LibCore/CFile.h>
+#include <string.h>
+
+const GTextDocument& ProjectFile::document() const
+{
+ if (!m_document) {
+ m_document = GTextDocument::create(nullptr);
+ m_document->set_text(contents());
+ }
+ 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;
+}