1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:27:35 +00:00

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
This commit is contained in:
Itamar 2021-03-05 20:55:17 +02:00 committed by Andreas Kling
parent 684cc5f027
commit 6f7ef4ec65
3 changed files with 55 additions and 14 deletions

View file

@ -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();

View file

@ -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 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 = reinterpret_cast<const FileDB*>(this)->get(file_name);
if (document.is_null())
return nullptr;
return adopt(*const_cast<GUI::TextDocument*>(document.leak_ref()));
}
bool FileDB::is_open(String file_name) const
RefPtr<const GUI::TextDocument> 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<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;
}
}

View file

@ -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;