mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:58:11 +00:00
HackStudio: Add C++ Language Server
The language server keeps track of the content of currently edited files by receiving updates about edit actions. Also, C++ autocompletion is no longer tied to HackStudio itself and moved to be part of the language server.
This commit is contained in:
parent
bf53d7ff64
commit
863f14788f
18 changed files with 451 additions and 21 deletions
|
@ -141,3 +141,11 @@ StdIO=/dev/tty1
|
||||||
Environment=TERM=xterm
|
Environment=TERM=xterm
|
||||||
KeepAlive=1
|
KeepAlive=1
|
||||||
BootModes=text
|
BootModes=text
|
||||||
|
|
||||||
|
[CppLanguageServer]
|
||||||
|
Socket=/tmp/portal/language/cpp
|
||||||
|
SocketPermissions=660
|
||||||
|
Lazy=1
|
||||||
|
User=anon
|
||||||
|
MultiInstance=1
|
||||||
|
AcceptSocketConnections=1
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
add_subdirectory(LanguageServers)
|
||||||
|
add_subdirectory(LanguageClients)
|
||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
AutoCompleteBox.cpp
|
AutoCompleteBox.cpp
|
||||||
CodeDocument.cpp
|
CodeDocument.cpp
|
||||||
CppAutoComplete.cpp
|
|
||||||
CursorTool.cpp
|
CursorTool.cpp
|
||||||
Debugger/BacktraceModel.cpp
|
Debugger/BacktraceModel.cpp
|
||||||
Debugger/DebugInfoWidget.cpp
|
Debugger/DebugInfoWidget.cpp
|
||||||
|
@ -33,3 +35,4 @@ set(SOURCES
|
||||||
|
|
||||||
serenity_bin(HackStudio)
|
serenity_bin(HackStudio)
|
||||||
target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell)
|
target_link_libraries(HackStudio LibWeb LibMarkdown LibGUI LibGfx LibCore LibVT LibDebug LibX86 LibDiff LibShell)
|
||||||
|
add_dependencies(HackStudio CppLanguageServer)
|
||||||
|
|
1
DevTools/HackStudio/LanguageClients/CMakeLists.txt
Normal file
1
DevTools/HackStudio/LanguageClients/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
add_subdirectory(Cpp)
|
4
DevTools/HackStudio/LanguageClients/Cpp/CMakeLists.txt
Normal file
4
DevTools/HackStudio/LanguageClients/Cpp/CMakeLists.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
set(GENERATED_SOURCES
|
||||||
|
../../LanguageServers/Cpp/CppLanguageServerEndpoint.h
|
||||||
|
../../LanguageServers/Cpp/CppLanguageClientEndpoint.h
|
||||||
|
)
|
60
DevTools/HackStudio/LanguageClients/Cpp/ServerConnection.h
Normal file
60
DevTools/HackStudio/LanguageClients/Cpp/ServerConnection.h
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/LexicalPath.h>
|
||||||
|
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageClientEndpoint.h>
|
||||||
|
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageServerEndpoint.h>
|
||||||
|
#include <LibIPC/ServerConnection.h>
|
||||||
|
|
||||||
|
namespace LanguageClients {
|
||||||
|
namespace Cpp {
|
||||||
|
|
||||||
|
class ServerConnection : public IPC::ServerConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>
|
||||||
|
, public CppLanguageClientEndpoint {
|
||||||
|
C_OBJECT(ServerConnection)
|
||||||
|
public:
|
||||||
|
virtual void handshake() override
|
||||||
|
{
|
||||||
|
auto response = send_sync<Messages::CppLanguageServer::Greet>(m_project_path.string());
|
||||||
|
set_my_client_id(response->client_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
ServerConnection(const String& project_path)
|
||||||
|
: IPC::ServerConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>(*this, "/tmp/portal/language/cpp")
|
||||||
|
, m_project_path(project_path)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void handle(const Messages::CppLanguageClient::Dummy&) override { }
|
||||||
|
|
||||||
|
LexicalPath m_project_path;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
1
DevTools/HackStudio/LanguageServers/CMakeLists.txt
Normal file
1
DevTools/HackStudio/LanguageServers/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
add_subdirectory(Cpp)
|
|
@ -24,17 +24,18 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "CppAutoComplete.h"
|
#include "AutoComplete.h"
|
||||||
#include <AK/HashTable.h>
|
#include <AK/HashTable.h>
|
||||||
#include <LibCpp/Lexer.h>
|
#include <LibCpp/Lexer.h>
|
||||||
|
|
||||||
// #define DEBUG_AUTOCOMPLETE
|
// #define DEBUG_AUTOCOMPLETE
|
||||||
|
|
||||||
namespace HackStudio {
|
namespace LanguageServers::Cpp {
|
||||||
Vector<String> CppAutoComplete::get_suggestions(const String& code, GUI::TextPosition autocomplete_position)
|
|
||||||
|
Vector<String> AutoComplete::get_suggestions(const String& code, GUI::TextPosition autocomplete_position)
|
||||||
{
|
{
|
||||||
auto lines = code.split('\n', true);
|
auto lines = code.split('\n', true);
|
||||||
Cpp::Lexer lexer(code);
|
Lexer lexer(code);
|
||||||
auto tokens = lexer.lex();
|
auto tokens = lexer.lex();
|
||||||
|
|
||||||
auto index_of_target_token = token_in_position(tokens, autocomplete_position);
|
auto index_of_target_token = token_in_position(tokens, autocomplete_position);
|
||||||
|
@ -52,12 +53,12 @@ Vector<String> CppAutoComplete::get_suggestions(const String& code, GUI::TextPos
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
|
|
||||||
String CppAutoComplete::text_of_token(const Vector<String> lines, const Cpp::Token& token)
|
String AutoComplete::text_of_token(const Vector<String> lines, const Cpp::Token& token)
|
||||||
{
|
{
|
||||||
return lines[token.m_start.line].substring(token.m_start.column, token.m_end.column - token.m_start.column + 1);
|
return lines[token.m_start.line].substring(token.m_start.column, token.m_end.column - token.m_start.column + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<size_t> CppAutoComplete::token_in_position(const Vector<Cpp::Token>& tokens, GUI::TextPosition position)
|
Optional<size_t> AutoComplete::token_in_position(const Vector<Cpp::Token>& tokens, GUI::TextPosition position)
|
||||||
{
|
{
|
||||||
for (size_t token_index = 0; token_index < tokens.size(); ++token_index) {
|
for (size_t token_index = 0; token_index < tokens.size(); ++token_index) {
|
||||||
auto& token = tokens[token_index];
|
auto& token = tokens[token_index];
|
||||||
|
@ -70,7 +71,7 @@ Optional<size_t> CppAutoComplete::token_in_position(const Vector<Cpp::Token>& to
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String> CppAutoComplete::identifier_prefixes(const Vector<String> lines, const Vector<Cpp::Token>& tokens, size_t target_token_index)
|
Vector<String> AutoComplete::identifier_prefixes(const Vector<String> lines, const Vector<Cpp::Token>& tokens, size_t target_token_index)
|
||||||
{
|
{
|
||||||
auto partial_input = text_of_token(lines, tokens[target_token_index]);
|
auto partial_input = text_of_token(lines, tokens[target_token_index]);
|
||||||
Vector<String> suggestions;
|
Vector<String> suggestions;
|
||||||
|
@ -89,4 +90,5 @@ Vector<String> CppAutoComplete::identifier_prefixes(const Vector<String> lines,
|
||||||
}
|
}
|
||||||
return suggestions;
|
return suggestions;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
}
|
|
@ -31,10 +31,13 @@
|
||||||
#include <LibCpp/Lexer.h>
|
#include <LibCpp/Lexer.h>
|
||||||
#include <LibGUI/TextPosition.h>
|
#include <LibGUI/TextPosition.h>
|
||||||
|
|
||||||
namespace HackStudio {
|
namespace LanguageServers::Cpp {
|
||||||
class CppAutoComplete {
|
|
||||||
|
using namespace ::Cpp;
|
||||||
|
|
||||||
|
class AutoComplete {
|
||||||
public:
|
public:
|
||||||
CppAutoComplete() = delete;
|
AutoComplete() = delete;
|
||||||
|
|
||||||
static Vector<String> get_suggestions(const String& code, GUI::TextPosition autocomplete_position);
|
static Vector<String> get_suggestions(const String& code, GUI::TextPosition autocomplete_position);
|
||||||
|
|
||||||
|
@ -43,4 +46,5 @@ private:
|
||||||
static String text_of_token(const Vector<String> lines, const Cpp::Token&);
|
static String text_of_token(const Vector<String> lines, const Cpp::Token&);
|
||||||
static Vector<String> identifier_prefixes(const Vector<String> lines, const Vector<Cpp::Token>&, size_t target_token_index);
|
static Vector<String> identifier_prefixes(const Vector<String> lines, const Vector<Cpp::Token>&, size_t target_token_index);
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
}
|
16
DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt
Normal file
16
DevTools/HackStudio/LanguageServers/Cpp/CMakeLists.txt
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
compile_ipc(CppLanguageServer.ipc CppLanguageServerEndpoint.h)
|
||||||
|
compile_ipc(CppLanguageClient.ipc CppLanguageClientEndpoint.h)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
ClientConnection.cpp
|
||||||
|
main.cpp
|
||||||
|
CppLanguageServerEndpoint.h
|
||||||
|
CppLanguageClientEndpoint.h
|
||||||
|
AutoComplete.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
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)
|
188
DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp
Normal file
188
DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.cpp
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
/*
|
||||||
|
* 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 "AutoComplete.h"
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
|
#include <LibGUI/TextDocument.h>
|
||||||
|
|
||||||
|
// #define DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
// #define DEBUG_FILE_CONTENT
|
||||||
|
|
||||||
|
namespace LanguageServers::Cpp {
|
||||||
|
|
||||||
|
static HashMap<int, RefPtr<ClientConnection>> s_connections;
|
||||||
|
|
||||||
|
ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
|
||||||
|
: IPC::ClientConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>(*this, move(socket), client_id)
|
||||||
|
{
|
||||||
|
s_connections.set(client_id, *this);
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientConnection::~ClientConnection()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientConnection::die()
|
||||||
|
{
|
||||||
|
s_connections.remove(client_id());
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
OwnPtr<Messages::CppLanguageServer::GreetResponse> ClientConnection::handle(const Messages::CppLanguageServer::Greet& message)
|
||||||
|
{
|
||||||
|
m_project_root = LexicalPath(message.project_root());
|
||||||
|
#ifdef DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
dbg() << "project_root: " << m_project_root.string();
|
||||||
|
#endif
|
||||||
|
return make<Messages::CppLanguageServer::GreetResponse>(client_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
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 true; }
|
||||||
|
virtual int soft_tab_width() const { return 4; }
|
||||||
|
};
|
||||||
|
|
||||||
|
static DefaultDocumentClient s_default_document_client;
|
||||||
|
|
||||||
|
void ClientConnection::handle(const Messages::CppLanguageServer::FileOpened& message)
|
||||||
|
{
|
||||||
|
LexicalPath file_path(String::format("%s/%s", m_project_root.string().characters(), message.file_name().characters()));
|
||||||
|
#ifdef DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
dbg() << "FileOpened: " << file_path.string();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto file = Core::File::construct(file_path.string());
|
||||||
|
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||||
|
errno = file->error();
|
||||||
|
perror("open");
|
||||||
|
dbg() << "Failed to open project file: " << file_path.string();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto content = file->read_all();
|
||||||
|
StringView content_view(content);
|
||||||
|
auto document = GUI::TextDocument::create(&s_default_document_client);
|
||||||
|
document->set_text(content_view);
|
||||||
|
m_open_files.set(message.file_name(), document);
|
||||||
|
#ifdef DEBUG_FILE_CONTENT
|
||||||
|
dbg() << document->text();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientConnection::handle(const Messages::CppLanguageServer::FileEditInsertText& message)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
dbg() << "InsertText for file: " << message.file_name();
|
||||||
|
dbg() << "Text: " << message.text();
|
||||||
|
dbg() << "[" << message.start_line() << ":" << message.start_column() << "]";
|
||||||
|
#endif
|
||||||
|
auto document = document_for(message.file_name());
|
||||||
|
if (!document) {
|
||||||
|
dbg() << "file " << message.file_name() << " has not been opened";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
|
||||||
|
document->insert_at(start_position, message.text(), &s_default_document_client);
|
||||||
|
#ifdef DEBUG_FILE_CONTENT
|
||||||
|
dbg() << document->text();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientConnection::handle(const Messages::CppLanguageServer::FileEditRemoveText& message)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
dbg() << "RemoveText for file: " << message.file_name();
|
||||||
|
dbg() << "[" << message.start_line() << ":" << message.start_column() << " - " << message.end_line() << ":" << message.end_column() << "]";
|
||||||
|
#endif
|
||||||
|
auto document = document_for(message.file_name());
|
||||||
|
if (!document) {
|
||||||
|
dbg() << "file " << message.file_name() << " has not been opened";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
GUI::TextPosition start_position { (size_t)message.start_line(), (size_t)message.start_column() };
|
||||||
|
GUI::TextRange range {
|
||||||
|
GUI::TextPosition { (size_t)message.start_line(),
|
||||||
|
(size_t)message.start_column() },
|
||||||
|
GUI::TextPosition { (size_t)message.end_line(),
|
||||||
|
(size_t)message.end_column() }
|
||||||
|
};
|
||||||
|
|
||||||
|
document->remove(range);
|
||||||
|
#ifdef DEBUG_FILE_CONTENT
|
||||||
|
dbg() << document->text();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: The work we do here could be taxing and block the client for a significant time.
|
||||||
|
// Would should turn this to an async IPC endpoint and report the reuslts back in a separate Server->Client message.
|
||||||
|
OwnPtr<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse> ClientConnection::handle(const Messages::CppLanguageServer::AutoCompleteSuggestions& message)
|
||||||
|
{
|
||||||
|
#ifdef DEBUG_CPP_LANGUAGE_SERVER
|
||||||
|
dbg() << "AutoCompleteSuggestions for: " << message.file_name() << " " << message.cursor_line() << ":" << message.cursor_column();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
auto document = document_for(message.file_name());
|
||||||
|
if (!document) {
|
||||||
|
dbg() << "file " << message.file_name() << " has not been opened";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector<String> suggestions = AutoComplete::get_suggestions(document->text(), { (size_t)message.cursor_line(), (size_t)message.cursor_column() });
|
||||||
|
return make<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse>(suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
|
RefPtr<GUI::TextDocument> ClientConnection::document_for(const String& file_name)
|
||||||
|
{
|
||||||
|
auto document_optional = m_open_files.get(file_name);
|
||||||
|
if (!document_optional.has_value())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return document_optional.value();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientConnection::handle(const Messages::CppLanguageServer::SetFileContent& message)
|
||||||
|
{
|
||||||
|
auto document = document_for(message.file_name());
|
||||||
|
if (!document) {
|
||||||
|
dbg() << "file " << message.file_name() << " has not been opened";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto content = message.content();
|
||||||
|
document->set_text(content.view());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
63
DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h
Normal file
63
DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/LexicalPath.h>
|
||||||
|
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageClientEndpoint.h>
|
||||||
|
#include <DevTools/HackStudio/LanguageServers/Cpp/CppLanguageServerEndpoint.h>
|
||||||
|
#include <LibGUI/TextDocument.h>
|
||||||
|
#include <LibIPC/ClientConnection.h>
|
||||||
|
|
||||||
|
namespace LanguageServers::Cpp {
|
||||||
|
|
||||||
|
class ClientConnection final
|
||||||
|
: public IPC::ClientConnection<CppLanguageClientEndpoint, CppLanguageServerEndpoint>
|
||||||
|
, public CppLanguageServerEndpoint {
|
||||||
|
C_OBJECT(ClientConnection);
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ClientConnection(NonnullRefPtr<Core::LocalSocket>, int client_id);
|
||||||
|
~ClientConnection() override;
|
||||||
|
|
||||||
|
virtual void die() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual OwnPtr<Messages::CppLanguageServer::GreetResponse> handle(const Messages::CppLanguageServer::Greet&) override;
|
||||||
|
virtual void handle(const Messages::CppLanguageServer::FileOpened&) override;
|
||||||
|
virtual void handle(const Messages::CppLanguageServer::FileEditInsertText&) override;
|
||||||
|
virtual void handle(const Messages::CppLanguageServer::FileEditRemoveText&) override;
|
||||||
|
virtual void handle(const Messages::CppLanguageServer::SetFileContent&) override;
|
||||||
|
virtual OwnPtr<Messages::CppLanguageServer::AutoCompleteSuggestionsResponse> handle(const Messages::CppLanguageServer::AutoCompleteSuggestions&) override;
|
||||||
|
|
||||||
|
RefPtr<GUI::TextDocument> document_for(const String& file_name);
|
||||||
|
|
||||||
|
LexicalPath m_project_root;
|
||||||
|
HashMap<String, NonnullRefPtr<GUI::TextDocument>> m_open_files;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
endpoint CppLanguageClient = 8002
|
||||||
|
{
|
||||||
|
Dummy() =|
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
endpoint CppLanguageServer = 8001
|
||||||
|
{
|
||||||
|
Greet(String project_root) => (i32 client_id)
|
||||||
|
|
||||||
|
FileOpened(String file_name) =|
|
||||||
|
FileEditInsertText(String file_name, String text, i32 start_line, i32 start_column) =|
|
||||||
|
FileEditRemoveText(String file_name, i32 start_line, i32 start_column, i32 end_line, i32 end_column) =|
|
||||||
|
SetFileContent(String file_name, String content) =|
|
||||||
|
|
||||||
|
AutoCompleteSuggestions(String file_name, i32 cursor_line, i32 cursor_column) => (Vector<String> suggestions)
|
||||||
|
}
|
51
DevTools/HackStudio/LanguageServers/Cpp/main.cpp
Normal file
51
DevTools/HackStudio/LanguageServers/Cpp/main.cpp
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
* 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 <AK/LexicalPath.h>
|
||||||
|
#include <DevTools/HackStudio/LanguageServers/Cpp/ClientConnection.h>
|
||||||
|
#include <LibCore/EventLoop.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
|
#include <LibCore/LocalServer.h>
|
||||||
|
#include <LibIPC/ClientConnection.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(int, char**)
|
||||||
|
{
|
||||||
|
Core::EventLoop event_loop;
|
||||||
|
if (pledge("stdio unix rpath", nullptr) < 0) {
|
||||||
|
perror("pledge");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto socket = Core::LocalSocket::take_over_accepted_socket_from_system_server();
|
||||||
|
IPC::new_client_connection<LanguageServers::Cpp::ClientConnection>(socket.release_nonnull(), 1);
|
||||||
|
if (pledge("stdio rpath", nullptr) < 0) {
|
||||||
|
perror("pledge");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return event_loop.exec();
|
||||||
|
}
|
|
@ -39,6 +39,9 @@ public:
|
||||||
|
|
||||||
String action_text() const { return m_action_text; }
|
String action_text() const { return m_action_text; }
|
||||||
|
|
||||||
|
virtual bool is_insert_text() const { return false; }
|
||||||
|
virtual bool is_remove_text() const { return false; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Command() { }
|
Command() { }
|
||||||
void set_action_text(const String& text) { m_action_text = text; }
|
void set_action_text(const String& text) { m_action_text = text; }
|
||||||
|
|
|
@ -282,6 +282,18 @@ void TextDocument::set_all_cursors(const TextPosition& position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String TextDocument::text() const
|
||||||
|
{
|
||||||
|
StringBuilder builder;
|
||||||
|
for (size_t i = 0; i < line_count(); ++i) {
|
||||||
|
auto& line = this->line(i);
|
||||||
|
builder.append(line.view());
|
||||||
|
if (i != line_count() - 1)
|
||||||
|
builder.append('\n');
|
||||||
|
}
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
String TextDocument::text_in_range(const TextRange& a_range) const
|
String TextDocument::text_in_range(const TextRange& a_range) const
|
||||||
{
|
{
|
||||||
auto range = a_range.normalized();
|
auto range = a_range.normalized();
|
||||||
|
|
|
@ -103,6 +103,7 @@ public:
|
||||||
|
|
||||||
void update_views(Badge<TextDocumentLine>);
|
void update_views(Badge<TextDocumentLine>);
|
||||||
|
|
||||||
|
String text() const;
|
||||||
String text_in_range(const TextRange&) const;
|
String text_in_range(const TextRange&) const;
|
||||||
|
|
||||||
Vector<TextRange> find_all(const StringView& needle) const;
|
Vector<TextRange> find_all(const StringView& needle) const;
|
||||||
|
@ -210,6 +211,9 @@ public:
|
||||||
InsertTextCommand(TextDocument&, const String&, const TextPosition&);
|
InsertTextCommand(TextDocument&, const String&, const TextPosition&);
|
||||||
virtual void undo() override;
|
virtual void undo() override;
|
||||||
virtual void redo() override;
|
virtual void redo() override;
|
||||||
|
virtual bool is_insert_text() const override { return true; }
|
||||||
|
const String& text() const { return m_text; }
|
||||||
|
const TextRange& range() const { return m_range; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_text;
|
String m_text;
|
||||||
|
@ -221,6 +225,8 @@ public:
|
||||||
RemoveTextCommand(TextDocument&, const String&, const TextRange&);
|
RemoveTextCommand(TextDocument&, const String&, const TextRange&);
|
||||||
virtual void undo() override;
|
virtual void undo() override;
|
||||||
virtual void redo() override;
|
virtual void redo() override;
|
||||||
|
virtual bool is_remove_text() const override { return true; }
|
||||||
|
const TextRange& range() const { return m_range; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_text;
|
String m_text;
|
||||||
|
|
|
@ -1254,14 +1254,7 @@ bool TextEditor::write_to_file(const StringView& path)
|
||||||
|
|
||||||
String TextEditor::text() const
|
String TextEditor::text() const
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
return document().text();
|
||||||
for (size_t i = 0; i < line_count(); ++i) {
|
|
||||||
auto& line = this->line(i);
|
|
||||||
builder.append(line.view());
|
|
||||||
if (i != line_count() - 1)
|
|
||||||
builder.append('\n');
|
|
||||||
}
|
|
||||||
return builder.to_string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextEditor::clear()
|
void TextEditor::clear()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue