mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:17:34 +00:00
HackStudio/LanguageServers: Move some components out of Cpp
This makes them available for use by other language servers. Also as a bonus, update the Shell language server to discover some symbols and add go-to-definition functionality :^)
This commit is contained in:
parent
0d17cf121c
commit
e59a631511
20 changed files with 400 additions and 318 deletions
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "AutoCompleteEngine.h"
|
||||
|
||||
namespace LanguageServers::Cpp {
|
||||
|
||||
AutoCompleteEngine::AutoCompleteEngine(ClientConnection& connection, const FileDB& filedb)
|
||||
: m_connection(connection)
|
||||
, m_filedb(filedb)
|
||||
{
|
||||
}
|
||||
|
||||
AutoCompleteEngine::~AutoCompleteEngine()
|
||||
{
|
||||
}
|
||||
void AutoCompleteEngine::set_declarations_of_document(const String& filename, Vector<GUI::AutocompleteProvider::Declaration>&& declarations)
|
||||
{
|
||||
VERIFY(set_declarations_of_document_callback);
|
||||
set_declarations_of_document_callback(m_connection, filename, move(declarations));
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "FileDB.h"
|
||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||
#include <LibGUI/AutocompleteProvider.h>
|
||||
#include <LibGUI/TextPosition.h>
|
||||
|
||||
namespace LanguageServers::Cpp {
|
||||
|
||||
class ClientConnection;
|
||||
|
||||
class AutoCompleteEngine {
|
||||
public:
|
||||
AutoCompleteEngine(ClientConnection&, const FileDB& filedb);
|
||||
virtual ~AutoCompleteEngine();
|
||||
|
||||
virtual Vector<GUI::AutocompleteProvider::Entry> get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position) = 0;
|
||||
|
||||
// TODO: In the future we can pass the range that was edited and only re-parse what we have to.
|
||||
virtual void on_edit([[maybe_unused]] const String& file) {};
|
||||
virtual void file_opened([[maybe_unused]] const String& file) {};
|
||||
|
||||
virtual Optional<GUI::AutocompleteProvider::ProjectLocation> find_declaration_of(const String&, const GUI::TextPosition&) { return {}; };
|
||||
|
||||
public:
|
||||
Function<void(ClientConnection&, String, Vector<GUI::AutocompleteProvider::Declaration>)> set_declarations_of_document_callback;
|
||||
|
||||
protected:
|
||||
const FileDB& filedb() const { return m_filedb; }
|
||||
void set_declarations_of_document(const String&, Vector<GUI::AutocompleteProvider::Declaration>&&);
|
||||
|
||||
private:
|
||||
ClientConnection& m_connection;
|
||||
const FileDB& m_filedb;
|
||||
};
|
||||
}
|
|
@ -1,7 +1,4 @@
|
|||
set(SOURCES
|
||||
AutoCompleteEngine.cpp
|
||||
ClientConnection.cpp
|
||||
FileDB.cpp
|
||||
LexerAutoComplete.cpp
|
||||
ParserAutoComplete.cpp
|
||||
main.cpp
|
||||
|
@ -15,4 +12,4 @@ serenity_bin(CppLanguageServer)
|
|||
|
||||
# We link with LibGUI because we use GUI::TextDocument to update
|
||||
# the content of files according to the edit actions we receive over IPC.
|
||||
target_link_libraries(CppLanguageServer LibIPC LibCpp LibGUI)
|
||||
target_link_libraries(CppLanguageServer LibIPC LibCpp LibGUI LibLanguageServer)
|
||||
|
|
|
@ -1,165 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "ClientConnection.h"
|
||||
#include "LexerAutoComplete.h"
|
||||
#include "ParserAutoComplete.h"
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/TextDocument.h>
|
||||
|
||||
namespace LanguageServers::Cpp {
|
||||
|
||||
static HashMap<int, RefPtr<ClientConnection>> s_connections;
|
||||
|
||||
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
|
||||
: IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), client_id)
|
||||
{
|
||||
s_connections.set(client_id, *this);
|
||||
m_autocomplete_engine = make<ParserAutoComplete>(*this, m_filedb);
|
||||
m_autocomplete_engine->set_declarations_of_document_callback = &ClientConnection::set_declarations_of_document_callback;
|
||||
}
|
||||
|
||||
ClientConnection::~ClientConnection()
|
||||
{
|
||||
}
|
||||
|
||||
void ClientConnection::die()
|
||||
{
|
||||
s_connections.remove(client_id());
|
||||
exit(0);
|
||||
}
|
||||
|
||||
OwnPtr<Messages::LanguageServer::GreetResponse> ClientConnection::handle(const Messages::LanguageServer::Greet& message)
|
||||
{
|
||||
m_filedb.set_project_root(message.project_root());
|
||||
if (unveil(message.project_root().characters(), "r") < 0) {
|
||||
perror("unveil");
|
||||
exit(1);
|
||||
}
|
||||
if (unveil(nullptr, nullptr) < 0) {
|
||||
perror("unveil");
|
||||
exit(1);
|
||||
}
|
||||
return make<Messages::LanguageServer::GreetResponse>();
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
|
||||
{
|
||||
if (m_filedb.is_open(message.file_name())) {
|
||||
return;
|
||||
}
|
||||
m_filedb.add(message.file_name(), message.file().take_fd());
|
||||
m_autocomplete_engine->file_opened(message.file_name());
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
|
||||
{
|
||||
#if CPP_LANGUAGE_SERVER_DEBUG
|
||||
dbgln("InsertText for file: {}", message.file_name());
|
||||
dbgln("Text: {}", message.text());
|
||||
dbgln("[{}:{}]", message.start_line(), message.start_column());
|
||||
#endif
|
||||
m_filedb.on_file_edit_insert_text(message.file_name(), message.text(), message.start_line(), message.start_column());
|
||||
m_autocomplete_engine->on_edit(message.file_name());
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
|
||||
{
|
||||
#if CPP_LANGUAGE_SERVER_DEBUG
|
||||
dbgln("RemoveText for file: {}", message.file_name());
|
||||
dbgln("[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
|
||||
#endif
|
||||
m_filedb.on_file_edit_remove_text(message.file_name(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
|
||||
m_autocomplete_engine->on_edit(message.file_name());
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
|
||||
{
|
||||
#if CPP_LANGUAGE_SERVER_DEBUG
|
||||
dbgln("AutoCompleteSuggestions for: {} {}:{}", message.location().file, message.location().line, message.location().column);
|
||||
#endif
|
||||
|
||||
auto document = m_filedb.get(message.location().file);
|
||||
if (!document) {
|
||||
dbgln("file {} has not been opened", message.location().file);
|
||||
return;
|
||||
}
|
||||
|
||||
GUI::TextPosition autocomplete_position = { (size_t)message.location().line, (size_t)max(message.location().column, message.location().column - 1) };
|
||||
Vector<GUI::AutocompleteProvider::Entry> suggestions = m_autocomplete_engine->get_suggestions(message.location().file, autocomplete_position);
|
||||
post_message(Messages::LanguageClient::AutoCompleteSuggestions(move(suggestions)));
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::SetFileContent& message)
|
||||
{
|
||||
auto document = m_filedb.get(message.file_name());
|
||||
if (!document) {
|
||||
dbgln("file {} has not been opened", message.file_name());
|
||||
return;
|
||||
}
|
||||
auto content = message.content();
|
||||
document->set_text(content.view());
|
||||
m_autocomplete_engine->on_edit(message.file_name());
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::SetAutoCompleteMode& message)
|
||||
{
|
||||
#ifdef CPP_LANGUAGE_SERVER_DEBUG
|
||||
dbgln("SetAutoCompleteMode: {}", message.mode());
|
||||
#endif
|
||||
if (message.mode() == "Parser")
|
||||
m_autocomplete_engine = make<ParserAutoComplete>(*this, m_filedb);
|
||||
else
|
||||
m_autocomplete_engine = make<LexerAutoComplete>(*this, m_filedb);
|
||||
}
|
||||
|
||||
void ClientConnection::handle(const Messages::LanguageServer::FindDeclaration& message)
|
||||
{
|
||||
dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "FindDeclaration: {} {}:{}", message.location().file, message.location().line, message.location().column);
|
||||
auto document = m_filedb.get(message.location().file);
|
||||
if (!document) {
|
||||
dbgln("file {} has not been opened", message.location().file);
|
||||
return;
|
||||
}
|
||||
|
||||
GUI::TextPosition identifier_position = { (size_t)message.location().line, (size_t)message.location().column };
|
||||
auto location = m_autocomplete_engine->find_declaration_of(message.location().file, identifier_position);
|
||||
if (!location.has_value()) {
|
||||
dbgln("could not find declaration");
|
||||
return;
|
||||
}
|
||||
dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "declaration location: {} {}:{}", location.value().file, location.value().line, location.value().column);
|
||||
post_message(Messages::LanguageClient::DeclarationLocation(GUI::AutocompleteProvider::ProjectLocation { location.value().file, location.value().line, location.value().column }));
|
||||
}
|
||||
|
||||
void ClientConnection::set_declarations_of_document_callback(ClientConnection& instance, const String& filename, Vector<GUI::AutocompleteProvider::Declaration>&& declarations)
|
||||
{
|
||||
instance.post_message(Messages::LanguageClient::DeclarationsInDocument(filename, move(declarations)));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -26,43 +26,33 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "AutoCompleteEngine.h"
|
||||
#include "FileDB.h"
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||
#include <LibIPC/ClientConnection.h>
|
||||
|
||||
#include <DevTools/HackStudio/LanguageServers/LanguageClientEndpoint.h>
|
||||
#include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
|
||||
#include "LexerAutoComplete.h"
|
||||
#include "ParserAutoComplete.h"
|
||||
#include <DevTools/HackStudio/LanguageServers/ClientConnection.h>
|
||||
|
||||
namespace LanguageServers::Cpp {
|
||||
|
||||
class ClientConnection final
|
||||
: public IPC::ClientConnection<LanguageClientEndpoint, LanguageServerEndpoint>
|
||||
, public LanguageServerEndpoint {
|
||||
class ClientConnection final : public LanguageServers::ClientConnection {
|
||||
C_OBJECT(ClientConnection);
|
||||
|
||||
public:
|
||||
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
|
||||
~ClientConnection() override;
|
||||
ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
|
||||
: LanguageServers::ClientConnection(move(socket), client_id)
|
||||
{
|
||||
m_autocomplete_engine = make<ParserAutoComplete>(*this, m_filedb);
|
||||
m_autocomplete_engine->set_declarations_of_document_callback = &ClientConnection::set_declarations_of_document_callback;
|
||||
}
|
||||
|
||||
virtual void die() override;
|
||||
virtual ~ClientConnection() override = default;
|
||||
|
||||
private:
|
||||
virtual OwnPtr<Messages::LanguageServer::GreetResponse> handle(const Messages::LanguageServer::Greet&) override;
|
||||
virtual void handle(const Messages::LanguageServer::FileOpened&) override;
|
||||
virtual void handle(const Messages::LanguageServer::FileEditInsertText&) override;
|
||||
virtual void handle(const Messages::LanguageServer::FileEditRemoveText&) override;
|
||||
virtual void handle(const Messages::LanguageServer::SetFileContent&) override;
|
||||
virtual void handle(const Messages::LanguageServer::AutoCompleteSuggestions&) override;
|
||||
virtual void handle(const Messages::LanguageServer::SetAutoCompleteMode&) override;
|
||||
virtual void handle(const Messages::LanguageServer::FindDeclaration&) override;
|
||||
|
||||
static void set_declarations_of_document_callback(ClientConnection&, const String&, Vector<GUI::AutocompleteProvider::Declaration>&&);
|
||||
|
||||
FileDB m_filedb;
|
||||
OwnPtr<AutoCompleteEngine> m_autocomplete_engine;
|
||||
virtual void handle(const Messages::LanguageServer::SetAutoCompleteMode& message) override
|
||||
{
|
||||
dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "SetAutoCompleteMode: {}", message.mode());
|
||||
if (message.mode() == "Parser")
|
||||
m_autocomplete_engine = make<ParserAutoComplete>(*this, m_filedb);
|
||||
else
|
||||
m_autocomplete_engine = make<LexerAutoComplete>(*this, m_filedb);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,151 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "FileDB.h"
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibCore/File.h>
|
||||
|
||||
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 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()));
|
||||
}
|
||||
|
||||
bool FileDB::is_open(String file_name) const
|
||||
{
|
||||
return m_open_files.contains(to_absolute_path(file_name));
|
||||
}
|
||||
|
||||
bool FileDB::add(const String& file_name, int fd)
|
||||
{
|
||||
auto document = create_from_fd(fd);
|
||||
if (!document)
|
||||
return false;
|
||||
|
||||
m_open_files.set(to_absolute_path(file_name), document.release_nonnull());
|
||||
return true;
|
||||
}
|
||||
|
||||
String FileDB::to_absolute_path(const String& file_name) const
|
||||
{
|
||||
if (LexicalPath { file_name }.is_absolute()) {
|
||||
return file_name;
|
||||
}
|
||||
VERIFY(!m_project_root.is_null());
|
||||
return LexicalPath { String::formatted("{}/{}", m_project_root, file_name) }.string();
|
||||
}
|
||||
|
||||
RefPtr<GUI::TextDocument> FileDB::create_from_filesystem(const String& file_name) const
|
||||
{
|
||||
auto file = Core::File::open(to_absolute_path(file_name), Core::IODevice::ReadOnly);
|
||||
if (file.is_error()) {
|
||||
dbgln("failed to create document for {} from filesystem", file_name);
|
||||
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 {};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void FileDB::on_file_edit_insert_text(const String& file_name, const String& inserted_text, size_t start_line, size_t start_column)
|
||||
{
|
||||
auto document = get(file_name);
|
||||
if (!document) {
|
||||
dbgln("file {} has not been opened", file_name);
|
||||
return;
|
||||
}
|
||||
GUI::TextPosition start_position { start_line, start_column };
|
||||
document->insert_at(start_position, inserted_text, &s_default_document_client);
|
||||
|
||||
#if FILE_CONTENT_DEBUG
|
||||
dbgln("{}", document->text());
|
||||
#endif
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
auto document = get(file_name);
|
||||
if (!document) {
|
||||
dbgln("file {} has not been opened", file_name);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
#if FILE_CONTENT_DEBUG
|
||||
dbgln("{}", document->text());
|
||||
#endif
|
||||
}
|
|
@ -1,55 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibGUI/TextDocument.h>
|
||||
|
||||
class FileDB final {
|
||||
public:
|
||||
RefPtr<const GUI::TextDocument> get(const String& file_name) const;
|
||||
RefPtr<GUI::TextDocument> get(const String& file_name);
|
||||
bool add(const String& file_name, int fd);
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
HashMap<String, NonnullRefPtr<GUI::TextDocument>> m_open_files;
|
||||
String m_project_root;
|
||||
};
|
|
@ -26,10 +26,10 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "AutoCompleteEngine.h"
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||
#include <DevTools/HackStudio/LanguageServers/AutoCompleteEngine.h>
|
||||
#include <LibCpp/Lexer.h>
|
||||
#include <LibGUI/TextPosition.h>
|
||||
|
||||
|
|
|
@ -27,12 +27,12 @@
|
|||
#include "ParserAutoComplete.h"
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h>
|
||||
#include <LibCpp/AST.h>
|
||||
#include <LibCpp/Lexer.h>
|
||||
#include <LibCpp/Parser.h>
|
||||
#include <LibCpp/Preprocessor.h>
|
||||
#include <LibRegex/Regex.h>
|
||||
#include <Userland/DevTools/HackStudio/LanguageServers/ClientConnection.h>
|
||||
|
||||
namespace LanguageServers::Cpp {
|
||||
|
||||
|
|
|
@ -26,12 +26,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "AutoCompleteEngine.h"
|
||||
#include "FileDB.h"
|
||||
#include <AK/Function.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <DevTools/HackStudio/AutoCompleteResponse.h>
|
||||
#include <DevTools/HackStudio/LanguageServers/AutoCompleteEngine.h>
|
||||
#include <DevTools/HackStudio/LanguageServers/FileDB.h>
|
||||
#include <LibCpp/AST.h>
|
||||
#include <LibCpp/Parser.h>
|
||||
#include <LibCpp/Preprocessor.h>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue