From 6f7ef4ec65e0695c782e8378ed331585b23dbfd8 Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 5 Mar 2021 20:55:17 +0200 Subject: [PATCH] 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 --- .../Cpp/ParserAutoComplete.cpp | 2 +- .../HackStudio/LanguageServers/FileDB.cpp | 61 +++++++++++++++---- .../HackStudio/LanguageServers/FileDB.h | 6 +- 3 files changed, 55 insertions(+), 14 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::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 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 FileDB::get(const String& file_name) { - auto absolute_path = to_absolute_path(file_name); - return adopt(*const_cast(reinterpret_cast(this)->get(absolute_path).leak_ref())); + auto document = reinterpret_cast(this)->get(file_name); + if (document.is_null()) + return nullptr; + return adopt(*const_cast(document.leak_ref())); } -bool FileDB::is_open(String file_name) const +RefPtr FileDB::get_or_create_from_filesystem(const String& file_name) const +{ + auto absolute_path = to_absolute_path(file_name); + auto document = get(absolute_path); + if (document) + return document; + return create_from_filesystem(absolute_path); +} + +RefPtr FileDB::get_or_create_from_filesystem(const String& file_name) +{ + auto document = reinterpret_cast(this)->get_or_create_from_filesystem(file_name); + if (document.is_null()) + return nullptr; + return adopt(*const_cast(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 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 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 get(const String& file_name) const; RefPtr get(const String& file_name); + RefPtr get_or_create_from_filesystem(const String& file_name) const; + RefPtr 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 create_from_filesystem(const String& file_name) const; RefPtr create_from_fd(int fd) const; RefPtr create_from_file(Core::File&) const; + static RefPtr create_with_content(const String&); private: HashMap> m_open_files;