summaryrefslogtreecommitdiff
path: root/Userland/DevTools/HackStudio/LanguageServers/ClientConnection.cpp
blob: e909115888b78bd523cc059e00d92af2215efdd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ClientConnection.h"
#include <AK/Debug.h>
#include <AK/HashMap.h>
#include <LibCore/File.h>
#include <LibGUI/TextDocument.h>

namespace LanguageServers {

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);
}

ClientConnection::~ClientConnection()
{
}

void ClientConnection::die()
{
    s_connections.remove(client_id());
    exit(0);
}

void 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);
    }
}

void ClientConnection::handle(const Messages::LanguageServer::FileOpened& message)
{
    if (m_filedb.is_open(message.filename())) {
        return;
    }
    m_filedb.add(message.filename(), message.file().take_fd());
    m_autocomplete_engine->file_opened(message.filename());
}

void ClientConnection::handle(const Messages::LanguageServer::FileEditInsertText& message)
{
    dbgln_if(LANGUAGE_SERVER_DEBUG, "InsertText for file: {}", message.filename());
    dbgln_if(LANGUAGE_SERVER_DEBUG, "Text: {}", message.text());
    dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{}]", message.start_line(), message.start_column());
    m_filedb.on_file_edit_insert_text(message.filename(), message.text(), message.start_line(), message.start_column());
    m_autocomplete_engine->on_edit(message.filename());
}

void ClientConnection::handle(const Messages::LanguageServer::FileEditRemoveText& message)
{
    dbgln_if(LANGUAGE_SERVER_DEBUG, "RemoveText for file: {}", message.filename());
    dbgln_if(LANGUAGE_SERVER_DEBUG, "[{}:{} - {}:{}]", message.start_line(), message.start_column(), message.end_line(), message.end_column());
    m_filedb.on_file_edit_remove_text(message.filename(), message.start_line(), message.start_column(), message.end_line(), message.end_column());
    m_autocomplete_engine->on_edit(message.filename());
}

void ClientConnection::handle(const Messages::LanguageServer::AutoCompleteSuggestions& message)
{
    dbgln_if(LANGUAGE_SERVER_DEBUG, "AutoCompleteSuggestions for: {} {}:{}", 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 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)
{
    dbgln_if(LANGUAGE_SERVER_DEBUG, "SetFileContent: {}", message.filename());
    auto document = m_filedb.get(message.filename());
    if (!document) {
        m_filedb.add(message.filename(), message.content());
        VERIFY(m_filedb.is_open(message.filename()));
    } else {
        const auto& content = message.content();
        document->set_text(content.view());
    }
    VERIFY(m_filedb.is_open(message.filename()));
    m_autocomplete_engine->on_edit(message.filename());
}

void ClientConnection::handle(const Messages::LanguageServer::FindDeclaration& message)
{
    dbgln_if(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(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)));
}

}