summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorItamar <itamar8910@gmail.com>2021-03-05 20:55:17 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-06 09:40:33 +0100
commit6f7ef4ec65e0695c782e8378ed331585b23dbfd8 (patch)
treef883a959229de489c02ef96f971513f48d86af6c /Userland
parent684cc5f0274f5cd1f5ce2bd169057a53e9f723b8 (diff)
downloadserenity-6f7ef4ec65e0695c782e8378ed331585b23dbfd8.zip
LanguageServers: Tweak FileDB API
- FileDB::get() now returns nullptr if the file is not in the FileDB - Added FileDB::get_or_create_from_filesystem() - Added FileDB::add() version that receives that file's content as a parameter
Diffstat (limited to 'Userland')
-rw-r--r--Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp2
-rw-r--r--Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp59
-rw-r--r--Userland/DevTools/HackStudio/LanguageServers/FileDB.h6
3 files changed, 54 insertions, 13 deletions
diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp
index 60638cb216..56107d7d92 100644
--- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp
+++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/ParserAutoComplete.cpp
@@ -60,7 +60,7 @@ const ParserAutoComplete::DocumentData* ParserAutoComplete::get_document_data(co
OwnPtr<ParserAutoComplete::DocumentData> ParserAutoComplete::create_document_data_for(const String& file)
{
- auto document = filedb().get(file);
+ auto document = filedb().get_or_create_from_filesystem(file);
if (!document)
return {};
auto content = document->text();
diff --git a/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp b/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp
index 6eb93fe0a6..8ef796b609 100644
--- a/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp
+++ b/Userland/DevTools/HackStudio/LanguageServers/FileDB.cpp
@@ -36,18 +36,37 @@ RefPtr<const GUI::TextDocument> FileDB::get(const String& file_name) const
auto absolute_path = to_absolute_path(file_name);
auto document_optional = m_open_files.get(absolute_path);
if (!document_optional.has_value())
- return create_from_filesystem(absolute_path);
+ return nullptr;
return document_optional.value();
}
RefPtr<GUI::TextDocument> FileDB::get(const String& file_name)
{
+ auto document = reinterpret_cast<const FileDB*>(this)->get(file_name);
+ if (document.is_null())
+ return nullptr;
+ return adopt(*const_cast<GUI::TextDocument*>(document.leak_ref()));
+}
+
+RefPtr<const GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& file_name) const
+{
auto absolute_path = to_absolute_path(file_name);
- return adopt(*const_cast<GUI::TextDocument*>(reinterpret_cast<const FileDB*>(this)->get(absolute_path).leak_ref()));
+ auto document = get(absolute_path);
+ if (document)
+ return document;
+ return create_from_filesystem(absolute_path);
}
-bool FileDB::is_open(String file_name) const
+RefPtr<GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& file_name)
+{
+ auto document = reinterpret_cast<const FileDB*>(this)->get_or_create_from_filesystem(file_name);
+ if (document.is_null())
+ return nullptr;
+ return adopt(*const_cast<GUI::TextDocument*>(document.leak_ref()));
+}
+
+bool FileDB::is_open(const String& file_name) const
{
return m_open_files.contains(to_absolute_path(file_name));
}
@@ -120,11 +139,9 @@ RefPtr<GUI::TextDocument> FileDB::create_from_file(Core::File& file) const
void FileDB::on_file_edit_insert_text(const String& file_name, const String& inserted_text, size_t start_line, size_t start_column)
{
+ VERIFY(is_open(file_name));
auto document = get(file_name);
- if (!document) {
- dbgln("file {} has not been opened", file_name);
- return;
- }
+ VERIFY(document);
GUI::TextPosition start_position { start_line, start_column };
document->insert_at(start_position, inserted_text, &s_default_document_client);
@@ -133,11 +150,11 @@ void FileDB::on_file_edit_insert_text(const String& file_name, const String& ins
void FileDB::on_file_edit_remove_text(const String& file_name, size_t start_line, size_t start_column, size_t end_line, size_t end_column)
{
+ // TODO: If file is not open - need to get its contents
+ // Otherwise- somehow verify that respawned language server is synced with all file contents
+ VERIFY(is_open(file_name));
auto document = get(file_name);
- if (!document) {
- dbgln("file {} has not been opened", file_name);
- return;
- }
+ VERIFY(document);
GUI::TextPosition start_position { start_line, start_column };
GUI::TextRange range {
GUI::TextPosition { start_line, start_column },
@@ -148,4 +165,24 @@ void FileDB::on_file_edit_remove_text(const String& file_name, size_t start_line
dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
}
+RefPtr<GUI::TextDocument> FileDB::create_with_content(const String& content)
+{
+ StringView content_view(content);
+ auto document = GUI::TextDocument::create(&s_default_document_client);
+ document->set_text(content_view);
+ return document;
+}
+
+bool FileDB::add(const String& file_name, const String& content)
+{
+ auto document = create_with_content(content);
+ if (!document) {
+ VERIFY_NOT_REACHED();
+ return false;
+ }
+
+ m_open_files.set(to_absolute_path(file_name), document.release_nonnull());
+ return true;
+}
+
}
diff --git a/Userland/DevTools/HackStudio/LanguageServers/FileDB.h b/Userland/DevTools/HackStudio/LanguageServers/FileDB.h
index 0e3f751a80..e29280ad00 100644
--- a/Userland/DevTools/HackStudio/LanguageServers/FileDB.h
+++ b/Userland/DevTools/HackStudio/LanguageServers/FileDB.h
@@ -37,19 +37,23 @@ class FileDB final {
public:
RefPtr<const GUI::TextDocument> get(const String& file_name) const;
RefPtr<GUI::TextDocument> get(const String& file_name);
+ RefPtr<const GUI::TextDocument> get_or_create_from_filesystem(const String& file_name) const;
+ RefPtr<GUI::TextDocument> get_or_create_from_filesystem(const String& file_name);
bool add(const String& file_name, int fd);
+ bool add(const String& file_name, const String& content);
void set_project_root(const String& root_path) { m_project_root = root_path; }
void on_file_edit_insert_text(const String& file_name, const String& inserted_text, size_t start_line, size_t start_column);
void on_file_edit_remove_text(const String& file_name, size_t start_line, size_t start_column, size_t end_line, size_t end_column);
String to_absolute_path(const String& file_name) const;
- bool is_open(String file_name) const;
+ bool is_open(const String& file_name) const;
private:
RefPtr<GUI::TextDocument> create_from_filesystem(const String& file_name) const;
RefPtr<GUI::TextDocument> create_from_fd(int fd) const;
RefPtr<GUI::TextDocument> create_from_file(Core::File&) const;
+ static RefPtr<GUI::TextDocument> create_with_content(const String&);
private:
HashMap<String, NonnullRefPtr<GUI::TextDocument>> m_open_files;