2020-10-02 03:01:33 +03:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-02 03:01:33 +03:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LanguageClient.h"
|
2021-02-27 09:50:02 +02:00
|
|
|
#include "HackStudio.h"
|
2021-04-09 08:49:48 +03:00
|
|
|
#include "ProjectDeclarations.h"
|
2021-05-17 22:19:50 +02:00
|
|
|
#include "ToDoEntries.h"
|
2020-10-02 03:01:33 +03:30
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Vector.h>
|
2021-01-31 15:10:45 +01:00
|
|
|
#include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
|
2021-02-13 09:47:08 +02:00
|
|
|
#include <LibGUI/Notification.h>
|
2020-10-02 03:01:33 +03:30
|
|
|
|
|
|
|
namespace HackStudio {
|
|
|
|
|
2021-05-02 19:54:34 +02:00
|
|
|
void ServerConnection::auto_complete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>& suggestions)
|
2020-10-02 03:01:33 +03:30
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_current_language_client) {
|
2021-01-23 19:48:42 +02:00
|
|
|
dbgln("Language Server connection has no attached language client");
|
|
|
|
return;
|
|
|
|
}
|
2021-05-02 19:54:34 +02:00
|
|
|
m_current_language_client->provide_autocomplete_suggestions(suggestions);
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
2021-02-27 09:31:05 +02:00
|
|
|
|
2021-05-02 19:54:34 +02:00
|
|
|
void ServerConnection::declaration_location(const GUI::AutocompleteProvider::ProjectLocation& location)
|
2021-02-20 12:27:39 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_current_language_client) {
|
2021-02-20 12:27:39 +02:00
|
|
|
dbgln("Language Server connection has no attached language client");
|
|
|
|
return;
|
|
|
|
}
|
2021-05-02 19:54:34 +02:00
|
|
|
m_current_language_client->declaration_found(location.file, location.line, location.column);
|
2021-02-27 09:31:05 +02:00
|
|
|
}
|
|
|
|
|
2021-02-13 09:47:08 +02:00
|
|
|
void ServerConnection::die()
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
VERIFY(m_wrapper);
|
|
|
|
// Wrapper destructs us here
|
|
|
|
m_wrapper->on_crash();
|
2021-02-13 09:47:08 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 22:12:37 +03:00
|
|
|
void LanguageClient::open_file(const String& path, int fd)
|
2020-10-02 03:01:33 +03:30
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_file_opened(path, fd);
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageClient::set_file_content(const String& path, const String& content)
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_set_file_content(path, content);
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageClient::insert_text(const String& path, const String& text, size_t line, size_t column)
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-03-05 21:01:42 +02:00
|
|
|
// set_active_client();
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_file_edit_insert_text(path, text, line, column);
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageClient::remove_text(const String& path, size_t from_line, size_t from_column, size_t to_line, size_t to_column)
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_file_edit_remove_text(path, from_line, from_column, to_line, to_column);
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageClient::request_autocomplete(const String& path, size_t cursor_line, size_t cursor_column)
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-02-06 15:58:02 +02:00
|
|
|
set_active_client();
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_auto_complete_suggestions(GUI::AutocompleteProvider::ProjectLocation { path, cursor_line, cursor_column });
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void LanguageClient::provide_autocomplete_suggestions(const Vector<GUI::AutocompleteProvider::Entry>& suggestions) const
|
2020-10-02 03:01:33 +03:30
|
|
|
{
|
|
|
|
if (on_autocomplete_suggestions)
|
|
|
|
on_autocomplete_suggestions(suggestions);
|
|
|
|
|
|
|
|
// Otherwise, drop it on the floor :shrug:
|
|
|
|
}
|
|
|
|
|
2021-02-06 15:58:02 +02:00
|
|
|
void LanguageClient::set_active_client()
|
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
if (!m_connection_wrapper.connection())
|
2021-02-13 09:47:08 +02:00
|
|
|
return;
|
2021-03-05 21:01:42 +02:00
|
|
|
m_connection_wrapper.set_active_client(*this);
|
2021-02-13 09:47:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
HashMap<String, NonnullOwnPtr<ServerConnectionWrapper>> ServerConnectionInstances::s_instance_for_language;
|
|
|
|
|
2021-05-02 19:54:34 +02:00
|
|
|
void ServerConnection::declarations_in_document(const String& filename, const Vector<GUI::AutocompleteProvider::Declaration>& declarations)
|
2021-02-13 09:47:08 +02:00
|
|
|
{
|
2021-05-02 19:54:34 +02:00
|
|
|
ProjectDeclarations::the().set_declared_symbols(filename, declarations);
|
2021-03-05 21:01:42 +02:00
|
|
|
}
|
2021-02-13 09:47:08 +02:00
|
|
|
|
2021-05-17 22:19:50 +02:00
|
|
|
void ServerConnection::todo_entries_in_document(const String& filename, const Vector<String>& todo_entries)
|
|
|
|
{
|
|
|
|
ToDoEntries::the().set_entries(filename, move(todo_entries));
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void LanguageClient::search_declaration(const String& path, size_t line, size_t column)
|
|
|
|
{
|
|
|
|
if (!m_connection_wrapper.connection())
|
|
|
|
return;
|
|
|
|
set_active_client();
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection_wrapper.connection()->async_find_declaration(GUI::AutocompleteProvider::ProjectLocation { path, line, column });
|
2021-03-05 21:01:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageClient::declaration_found(const String& file, size_t line, size_t column) const
|
|
|
|
{
|
|
|
|
if (!on_declaration_found) {
|
|
|
|
dbgln("on_declaration_found callback is not set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
on_declaration_found(file, line, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConnectionInstances::set_instance_for_language(const String& language_name, NonnullOwnPtr<ServerConnectionWrapper>&& connection_wrapper)
|
|
|
|
{
|
|
|
|
s_instance_for_language.set(language_name, move(connection_wrapper));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConnectionInstances::remove_instance_for_language(const String& language_name)
|
|
|
|
{
|
|
|
|
s_instance_for_language.remove(language_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerConnectionWrapper* ServerConnectionInstances::get_instance_wrapper(const String& language_name)
|
|
|
|
{
|
|
|
|
if (auto instance = s_instance_for_language.get(language_name); instance.has_value()) {
|
|
|
|
return const_cast<ServerConnectionWrapper*>(instance.value());
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-02-13 09:47:08 +02:00
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void ServerConnectionWrapper::on_crash()
|
|
|
|
{
|
|
|
|
show_crash_notification();
|
|
|
|
m_connection.clear();
|
|
|
|
|
|
|
|
static constexpr int max_crash_frequency_seconds = 3;
|
|
|
|
if (m_last_crash_timer.is_valid() && m_last_crash_timer.elapsed() / 1000 < max_crash_frequency_seconds) {
|
|
|
|
dbgln("LanguageServer crash frequency is too high");
|
|
|
|
m_respawn_allowed = false;
|
|
|
|
|
|
|
|
show_frequenct_crashes_notification();
|
|
|
|
} else {
|
|
|
|
m_last_crash_timer.start();
|
|
|
|
try_respawn_connection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ServerConnectionWrapper::show_frequenct_crashes_notification() const
|
|
|
|
{
|
|
|
|
auto notification = GUI::Notification::construct();
|
|
|
|
notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
|
|
|
|
notification->set_title("LanguageServer Crashes too much!");
|
|
|
|
notification->set_text("LanguageServer aided features will not be available in this session");
|
|
|
|
notification->show();
|
|
|
|
}
|
|
|
|
void ServerConnectionWrapper::show_crash_notification() const
|
|
|
|
{
|
|
|
|
auto notification = GUI::Notification::construct();
|
2021-02-13 09:47:08 +02:00
|
|
|
notification->set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/app-hack-studio.png"));
|
|
|
|
notification->set_title("Oops!");
|
2021-03-05 21:01:42 +02:00
|
|
|
notification->set_text(String::formatted("LanguageServer has crashed"));
|
2021-02-13 09:47:08 +02:00
|
|
|
notification->show();
|
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
ServerConnectionWrapper::ServerConnectionWrapper(const String& language_name, Function<NonnullRefPtr<ServerConnection>()> connection_creator)
|
|
|
|
: m_language(language_from_name(language_name))
|
|
|
|
, m_connection_creator(move(connection_creator))
|
|
|
|
{
|
|
|
|
create_connection();
|
|
|
|
}
|
2021-02-13 09:47:08 +02:00
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void ServerConnectionWrapper::create_connection()
|
2021-02-13 09:47:08 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
VERIFY(m_connection.is_null());
|
|
|
|
m_connection = m_connection_creator();
|
|
|
|
m_connection->set_wrapper(*this);
|
2021-02-13 09:47:08 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
ServerConnection* ServerConnectionWrapper::connection()
|
2021-02-13 09:47:08 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
return m_connection.ptr();
|
2021-02-06 15:58:02 +02:00
|
|
|
}
|
2021-03-05 21:01:42 +02:00
|
|
|
|
|
|
|
void ServerConnectionWrapper::attach(LanguageClient& client)
|
2021-02-27 09:50:02 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
m_connection->m_current_language_client = &client;
|
2021-02-27 09:50:02 +02:00
|
|
|
}
|
2021-02-06 15:58:02 +02:00
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void ServerConnectionWrapper::detach()
|
2021-02-20 12:27:39 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
m_connection->m_current_language_client.clear();
|
2021-02-20 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
2021-03-05 21:01:42 +02:00
|
|
|
void ServerConnectionWrapper::set_active_client(LanguageClient& client)
|
2021-02-20 12:27:39 +02:00
|
|
|
{
|
2021-03-05 21:01:42 +02:00
|
|
|
m_connection->m_current_language_client = &client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerConnectionWrapper::try_respawn_connection()
|
|
|
|
{
|
|
|
|
if (!m_respawn_allowed)
|
2021-02-20 12:27:39 +02:00
|
|
|
return;
|
2021-03-05 21:01:42 +02:00
|
|
|
|
|
|
|
dbgln("Respawning ServerConnection");
|
|
|
|
create_connection();
|
|
|
|
|
|
|
|
// After respawning the language-server, we have to flush the content of the project files
|
|
|
|
// so the server's FileDB will be up-to-date.
|
|
|
|
project().for_each_text_file([this](const ProjectFile& file) {
|
|
|
|
if (file.code_document().language() != m_language)
|
|
|
|
return;
|
2021-05-03 13:33:59 +02:00
|
|
|
m_connection->async_set_file_content(file.code_document().file_path(), file.document().text());
|
2021-03-05 21:01:42 +02:00
|
|
|
});
|
2021-02-20 12:27:39 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 03:01:33 +03:30
|
|
|
}
|