2021-02-06 15:58:02 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-06 15:58:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FileDB.h"
|
|
|
|
|
|
|
|
#include <AK/LexicalPath.h>
|
2021-05-08 12:27:12 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2021-02-06 15:58:02 +02:00
|
|
|
#include <LibCore/File.h>
|
|
|
|
|
2021-03-02 09:40:10 +03:30
|
|
|
namespace LanguageServers {
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
RefPtr<const GUI::TextDocument> FileDB::get(const String& filename) const
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
auto absolute_path = to_absolute_path(filename);
|
2021-02-06 15:58:02 +02:00
|
|
|
auto document_optional = m_open_files.get(absolute_path);
|
|
|
|
if (!document_optional.has_value())
|
2021-03-05 20:55:17 +02:00
|
|
|
return nullptr;
|
2021-02-06 15:58:02 +02:00
|
|
|
|
2021-05-08 12:27:12 +03:00
|
|
|
return adopt_ref(*document_optional.value());
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
RefPtr<GUI::TextDocument> FileDB::get(const String& filename)
|
2021-03-05 20:55:17 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
auto document = reinterpret_cast<const FileDB*>(this)->get(filename);
|
2021-03-05 20:55:17 +02:00
|
|
|
if (document.is_null())
|
|
|
|
return nullptr;
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*const_cast<GUI::TextDocument*>(document.leak_ref()));
|
2021-03-05 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
RefPtr<const GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& filename) const
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
auto absolute_path = to_absolute_path(filename);
|
2021-03-05 20:55:17 +02:00
|
|
|
auto document = get(absolute_path);
|
|
|
|
if (document)
|
|
|
|
return document;
|
|
|
|
return create_from_filesystem(absolute_path);
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
RefPtr<GUI::TextDocument> FileDB::get_or_create_from_filesystem(const String& filename)
|
2021-03-05 20:55:17 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
auto document = reinterpret_cast<const FileDB*>(this)->get_or_create_from_filesystem(filename);
|
2021-03-05 20:55:17 +02:00
|
|
|
if (document.is_null())
|
|
|
|
return nullptr;
|
2021-04-23 16:46:57 +02:00
|
|
|
return adopt_ref(*const_cast<GUI::TextDocument*>(document.leak_ref()));
|
2021-03-05 20:55:17 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
bool FileDB::is_open(const String& filename) const
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
return m_open_files.contains(to_absolute_path(filename));
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
bool FileDB::add(const String& filename, int fd)
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
|
|
|
auto document = create_from_fd(fd);
|
|
|
|
if (!document)
|
|
|
|
return false;
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
m_open_files.set(to_absolute_path(filename), document.release_nonnull());
|
2021-02-06 15:58:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
String FileDB::to_absolute_path(const String& filename) const
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
if (LexicalPath { filename }.is_absolute()) {
|
|
|
|
return filename;
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_project_root.is_null());
|
2021-04-29 21:46:15 +02:00
|
|
|
return LexicalPath { String::formatted("{}/{}", m_project_root, filename) }.string();
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
RefPtr<GUI::TextDocument> FileDB::create_from_filesystem(const String& filename) const
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
auto file = Core::File::open(to_absolute_path(filename), Core::IODevice::ReadOnly);
|
2021-02-06 15:58:02 +02:00
|
|
|
if (file.is_error()) {
|
2021-04-29 21:46:15 +02:00
|
|
|
dbgln("failed to create document for {} from filesystem", filename);
|
2021-02-06 15:58:02 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return create_from_file(*file.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GUI::TextDocument> FileDB::create_from_fd(int fd) const
|
|
|
|
{
|
|
|
|
auto file = Core::File::construct();
|
|
|
|
if (!file->open(fd, Core::IODevice::ReadOnly, Core::File::ShouldCloseFileDescriptor::Yes)) {
|
|
|
|
errno = file->error();
|
|
|
|
perror("open");
|
|
|
|
dbgln("Failed to open project file");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return create_from_file(*file);
|
|
|
|
}
|
|
|
|
|
|
|
|
class DefaultDocumentClient final : public GUI::TextDocument::Client {
|
|
|
|
public:
|
|
|
|
virtual ~DefaultDocumentClient() override = default;
|
|
|
|
virtual void document_did_append_line() override {};
|
|
|
|
virtual void document_did_insert_line(size_t) override {};
|
|
|
|
virtual void document_did_remove_line(size_t) override {};
|
|
|
|
virtual void document_did_remove_all_lines() override {};
|
|
|
|
virtual void document_did_change() override {};
|
|
|
|
virtual void document_did_set_text() override {};
|
|
|
|
virtual void document_did_set_cursor(const GUI::TextPosition&) override {};
|
2021-05-08 13:16:37 +02:00
|
|
|
virtual void document_did_update_undo_stack() override { }
|
2021-02-06 15:58:02 +02:00
|
|
|
|
|
|
|
virtual bool is_automatic_indentation_enabled() const override { return false; }
|
|
|
|
virtual int soft_tab_width() const override { return 4; }
|
|
|
|
};
|
|
|
|
static DefaultDocumentClient s_default_document_client;
|
|
|
|
|
|
|
|
RefPtr<GUI::TextDocument> FileDB::create_from_file(Core::File& file) const
|
|
|
|
{
|
|
|
|
auto content = file.read_all();
|
|
|
|
StringView content_view(content);
|
|
|
|
auto document = GUI::TextDocument::create(&s_default_document_client);
|
|
|
|
document->set_text(content_view);
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
void FileDB::on_file_edit_insert_text(const String& filename, const String& inserted_text, size_t start_line, size_t start_column)
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-04-29 21:46:15 +02:00
|
|
|
VERIFY(is_open(filename));
|
|
|
|
auto document = get(filename);
|
2021-03-05 20:55:17 +02:00
|
|
|
VERIFY(document);
|
2021-02-06 15:58:02 +02:00
|
|
|
GUI::TextPosition start_position { start_line, start_column };
|
|
|
|
document->insert_at(start_position, inserted_text, &s_default_document_client);
|
|
|
|
|
2021-03-02 09:40:10 +03:30
|
|
|
dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
void FileDB::on_file_edit_remove_text(const String& filename, size_t start_line, size_t start_column, size_t end_line, size_t end_column)
|
2021-02-06 15:58:02 +02:00
|
|
|
{
|
2021-03-05 20:55:17 +02:00
|
|
|
// TODO: If file is not open - need to get its contents
|
|
|
|
// Otherwise- somehow verify that respawned language server is synced with all file contents
|
2021-04-29 21:46:15 +02:00
|
|
|
VERIFY(is_open(filename));
|
|
|
|
auto document = get(filename);
|
2021-03-05 20:55:17 +02:00
|
|
|
VERIFY(document);
|
2021-02-06 15:58:02 +02:00
|
|
|
GUI::TextPosition start_position { start_line, start_column };
|
|
|
|
GUI::TextRange range {
|
|
|
|
GUI::TextPosition { start_line, start_column },
|
|
|
|
GUI::TextPosition { end_line, end_column }
|
|
|
|
};
|
|
|
|
|
|
|
|
document->remove(range);
|
2021-03-02 09:40:10 +03:30
|
|
|
dbgln_if(FILE_CONTENT_DEBUG, "{}", document->text());
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:55:17 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
bool FileDB::add(const String& filename, const String& content)
|
2021-03-05 20:55:17 +02:00
|
|
|
{
|
|
|
|
auto document = create_with_content(content);
|
|
|
|
if (!document) {
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:46:15 +02:00
|
|
|
m_open_files.set(to_absolute_path(filename), document.release_nonnull());
|
2021-03-05 20:55:17 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|