Skip to content

Commit 914d8f8

Browse files
committed
bpp-lsp: Use libfrozen-dev to make request/notification handler maps constexpr
1 parent f4f95f9 commit 914d8f8

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Source: bpp
22
Priority: optional
33
Maintainer: rail5 <[email protected]>
4-
Build-Depends: debhelper (>= 10), pandoc, antlr4, libantlr4-runtime-dev, perl, nlohmann-json3-dev
4+
Build-Depends: debhelper (>= 10), pandoc, antlr4, libantlr4-runtime-dev, perl, nlohmann-json3-dev, libfrozen-dev
55
Standards-Version: 4.6.2
66
Section: utils
77

src/lsp/BashppServer.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
std::mutex bpp::BashppServer::output_mutex;
1313
std::mutex bpp::BashppServer::log_mutex;
1414

15+
const frozen::unordered_map<frozen::string, bpp::BashppServer::RequestHandler, 8> bpp::BashppServer::request_handlers = {
16+
{frozen::string("initialize"), &BashppServer::handleInitialize},
17+
{frozen::string("textDocument/definition"), &BashppServer::handleDefinition},
18+
{frozen::string("textDocument/completion"), &BashppServer::handleCompletion},
19+
{frozen::string("textDocument/hover"), &BashppServer::handleHover},
20+
{frozen::string("textDocument/documentSymbol"), &BashppServer::handleDocumentSymbol},
21+
{frozen::string("textDocument/rename"), &BashppServer::handleRename},
22+
{frozen::string("textDocument/references"), &BashppServer::handleReferences},
23+
{frozen::string("shutdown"), &BashppServer::shutdown}
24+
};
25+
26+
const frozen::unordered_map<frozen::string, bpp::BashppServer::NotificationHandler, 4> bpp::BashppServer::notification_handlers = {
27+
{"textDocument/didOpen", &BashppServer::handleDidOpen},
28+
{"textDocument/didChange", &BashppServer::handleDidChange},
29+
{"workspace/didChangeWatchedFiles", &BashppServer::handleDidChangeWatchedFiles},
30+
{"textDocument/didClose", &BashppServer::handleDidClose}
31+
};
32+
1533
bpp::BashppServer::BashppServer() {
1634
log("Bash++ Language Server initialized.");
1735
log("Using ", thread_pool.getThreadCount(), " threads for processing requests.");
@@ -232,9 +250,9 @@ void bpp::BashppServer::processRequest(const GenericRequestMessage& request) {
232250
GenericResponseMessage response;
233251
response.id = request.id;
234252
std::function<GenericResponseMessage(const GenericRequestMessage&)> request_handler = invalidRequestHandler;
235-
auto it = request_handlers.find(request.method);
253+
auto it = request_handlers.find(frozen::string(request.method.c_str()));
236254
if (it != request_handlers.end()) {
237-
request_handler = it->second; // Set the handler to the appropriate function
255+
request_handler = std::bind(it->second, this, std::placeholders::_1); // Bind the method to the current instance
238256
} else {
239257
log("No handler found for request method: ", request.method);
240258
}
@@ -254,9 +272,9 @@ void bpp::BashppServer::processRequest(const GenericRequestMessage& request) {
254272

255273
void bpp::BashppServer::processNotification(const GenericNotificationMessage& notification) {
256274
std::function<void(const GenericNotificationMessage&)> notification_handler = invalidNotificationHandler;
257-
auto it = notification_handlers.find(notification.method);
275+
auto it = notification_handlers.find(frozen::string(notification.method.c_str()));
258276
if (it != notification_handlers.end()) {
259-
notification_handler = it->second; // Set the handler to the appropriate function
277+
notification_handler = std::bind(it->second, this, std::placeholders::_1); // Bind the method to the current instance
260278
} else {
261279
log("No handler found for notification method: ", notification.method);
262280
}

src/lsp/BashppServer.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include <nlohmann/json.hpp>
1818
#include <unistd.h>
1919

20+
#include <frozen/string.h>
21+
#include <frozen/unordered_map.h>
22+
2023
#include "ThreadPool.h"
2124
#include "ProgramPool.h"
2225

@@ -74,32 +77,19 @@ class BashppServer {
7477
static std::mutex output_mutex; // Mutex for thread-safe output
7578
static std::mutex log_mutex; // Mutex for thread-safe logging
7679

77-
// TODO(@rail5): When Debian 13 is released, use libfrozen-dev to make these maps constexpr
80+
using RequestHandler = GenericResponseMessage (BashppServer::*)(const GenericRequestMessage&);
81+
using NotificationHandler = void (BashppServer::*)(const GenericNotificationMessage&);
7882
/**
7983
* @brief Maps request types to the functions that handle them.
8084
*
8185
*/
82-
const std::unordered_map<std::string, std::function<GenericResponseMessage(const GenericRequestMessage& )>> request_handlers = {
83-
{"initialize", std::bind(&BashppServer::handleInitialize, this, std::placeholders::_1)},
84-
{"textDocument/definition", std::bind(&BashppServer::handleDefinition, this, std::placeholders::_1)},
85-
{"textDocument/completion", std::bind(&BashppServer::handleCompletion, this, std::placeholders::_1)},
86-
{"textDocument/hover", std::bind(&BashppServer::handleHover, this, std::placeholders::_1)},
87-
{"textDocument/documentSymbol", std::bind(&BashppServer::handleDocumentSymbol, this, std::placeholders::_1)},
88-
{"textDocument/rename", std::bind(&BashppServer::handleRename, this, std::placeholders::_1)},
89-
{"textDocument/references", std::bind(&BashppServer::handleReferences, this, std::placeholders::_1)},
90-
{"shutdown", std::bind(&BashppServer::shutdown, this, std::placeholders::_1)}
91-
};
86+
static const frozen::unordered_map<frozen::string, RequestHandler, 8> request_handlers;
9287

9388
/**
9489
* @brief Maps notification types to the functions that handle them.
9590
*
9691
*/
97-
const std::unordered_map<std::string, std::function<void(const GenericNotificationMessage& )>> notification_handlers = {
98-
{"textDocument/didOpen", std::bind(&BashppServer::handleDidOpen, this, std::placeholders::_1)},
99-
{"textDocument/didChange", std::bind(&BashppServer::handleDidChange, this, std::placeholders::_1)},
100-
{"workspace/didChangeWatchedFiles", std::bind(&BashppServer::handleDidChangeWatchedFiles, this, std::placeholders::_1)},
101-
{"textDocument/didClose", std::bind(&BashppServer::handleDidClose, this, std::placeholders::_1)}
102-
};
92+
static const frozen::unordered_map<frozen::string, NotificationHandler, 4> notification_handlers;
10393

10494
static const GenericResponseMessage invalidRequestHandler(const GenericRequestMessage& request);
10595
static void invalidNotificationHandler(const GenericNotificationMessage& request);

0 commit comments

Comments
 (0)