Skip to content

Commit 2e51658

Browse files
committed
bpp-lsp: Remove dependency on libfrozen
We can use a simple std::array and linear searches, libfrozen was overkill for this [notest]
1 parent bca9e49 commit 2e51658

6 files changed

Lines changed: 52 additions & 36 deletions

File tree

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ jobs:
7474
run: |
7575
if [ "${{ matrix.language }}" = "c-cpp" ]; then
7676
apt-get update
77-
apt-get install build-essential flex bison pandoc nlohmann-json3-dev libfrozen-dev libutfcpp-dev -y
77+
apt-get install build-essential flex bison pandoc nlohmann-json3-dev libutfcpp-dev -y
7878
fi
7979
8080
# Add any setup steps before running the `github/codeql-action/init` action.

.github/workflows/jekyll-gh-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
if: steps.code_changed.outputs.code == 'true'
7676
run: |
7777
apt-get update
78-
apt-get install build-essential flex bison doxygen graphviz libfrozen-dev libutfcpp-dev -y
78+
apt-get install build-essential flex bison doxygen graphviz libutfcpp-dev -y
7979
- name: Run tests and generate test stats SVG
8080
if: steps.code_changed.outputs.code == 'true'
8181
run: |

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ Pre-built packages are available for **amd64** and **arm64**. The .debs can also
6868

6969
Language server-specific prerequisites:
7070
- `nlohmann-json3-dev`
71-
- `libfrozen-dev`
7271

7372
Optional:
7473
- `pandoc` and `perl` for building the documentation
@@ -78,7 +77,7 @@ On Debian-based systems, you can install the prerequisites with:
7877

7978
```bash
8079
$ sudo apt update
81-
$ sudo apt install build-essential flex bison libutfcpp-dev libfrozen-dev pandoc perl debhelper nlohmann-json3-dev
80+
$ sudo apt install build-essential flex bison libutfcpp-dev pandoc perl debhelper nlohmann-json3-dev
8281
```
8382

8483
#### Building

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Source: bpp
22
Priority: optional
33
Maintainer: rail5 <andrew@rail5.org>
44
Homepage: https://bpp.sh
5-
Build-Depends: debhelper (>= 10), pandoc, flex, bison, perl, nlohmann-json3-dev, libfrozen-dev, libutfcpp-dev
5+
Build-Depends: debhelper (>= 10), pandoc, flex, bison, perl, nlohmann-json3-dev, libutfcpp-dev
66
Standards-Version: 4.7.0
77
Section: utils
88

src/lsp/BashppServer.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,22 @@ void bpp::BashppServer::sendNotification(const GenericNotificationMessage& notif
139139
void bpp::BashppServer::processRequest(const GenericRequestMessage& request) {
140140
GenericResponseMessage response;
141141
response.id = request.id;
142-
std::function<GenericResponseMessage(const GenericRequestMessage&)> request_handler = invalidRequestHandler;
143-
const auto* it = request_handlers.find(frozen::string(request.method));
144-
if (it != request_handlers.end()) {
145-
request_handler = std::bind(it->second, this, std::placeholders::_1); // Bind the method to the current instance
146-
} else {
142+
143+
const auto* it = std::find_if(request_handlers.begin(), request_handlers.end(),
144+
[&request](const RequestHandlerEntry& entry) {
145+
return entry.method_name == request.method;
146+
}
147+
);
148+
149+
if (it == request_handlers.end()) {
147150
log("No handler found for request method: ", request.method);
151+
response = invalidRequestHandler(request);
152+
sendResponse(response);
153+
return;
148154
}
149155

150156
try {
151-
response = request_handler(request);
157+
response = (this->*it->handler)(request);
152158
} catch (const std::exception& e) {
153159
log("Error handling request: ", e.what());
154160
ResponseError err;
@@ -157,20 +163,25 @@ void bpp::BashppServer::processRequest(const GenericRequestMessage& request) {
157163
err.data = e.what();
158164
response.error = err;
159165
}
166+
160167
sendResponse(response);
161168
}
162169

163170
void bpp::BashppServer::processNotification(const GenericNotificationMessage& notification) {
164-
std::function<void(const GenericNotificationMessage&)> notification_handler = invalidNotificationHandler;
165-
const auto* it = notification_handlers.find(frozen::string(notification.method));
166-
if (it != notification_handlers.end()) {
167-
notification_handler = std::bind(it->second, this, std::placeholders::_1); // Bind the method to the current instance
168-
} else {
171+
const auto* it = std::find_if(notification_handlers.begin(), notification_handlers.end(),
172+
[&notification](const NotificationHandlerEntry& entry) {
173+
return entry.method_name == notification.method;
174+
}
175+
);
176+
177+
if (it == notification_handlers.end()) {
169178
log("No handler found for notification method: ", notification.method);
179+
invalidNotificationHandler(notification);
180+
return;
170181
}
171182

172183
try {
173-
notification_handler(notification);
184+
(this->*it->handler)(notification);
174185
} catch (const std::exception& e) {
175186
log("Error handling notification: ", e.what());
176187
}

src/lsp/BashppServer.h

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

20-
#include <frozen/string.h>
21-
#include <frozen/unordered_map.h>
22-
2320
#include "ThreadPool.h"
2421
#include "ProgramPool.h"
2522

@@ -276,33 +273,42 @@ class BashppServer {
276273
using RequestHandler = GenericResponseMessage (BashppServer::*)(const GenericRequestMessage&);
277274
using NotificationHandler = void (BashppServer::*)(const GenericNotificationMessage&);
278275

276+
struct RequestHandlerEntry {
277+
std::string_view method_name;
278+
RequestHandler handler;
279+
};
280+
struct NotificationHandlerEntry {
281+
std::string_view method_name;
282+
NotificationHandler handler;
283+
};
284+
279285
/**
280286
* @brief Maps request types to the functions that handle them.
281287
*
282288
*/
283-
static constexpr frozen::unordered_map<frozen::string, RequestHandler, 8> request_handlers = {
284-
{"initialize", &BashppServer::handleInitialize},
285-
{"textDocument/definition", &BashppServer::handleDefinition},
286-
{"textDocument/completion", &BashppServer::handleCompletion},
287-
{"textDocument/hover", &BashppServer::handleHover},
289+
static constexpr std::array<RequestHandlerEntry, 8> request_handlers = {{
290+
{"initialize", &BashppServer::handleInitialize},
291+
{"textDocument/definition", &BashppServer::handleDefinition},
292+
{"textDocument/completion", &BashppServer::handleCompletion},
293+
{"textDocument/hover", &BashppServer::handleHover},
288294
{"textDocument/documentSymbol", &BashppServer::handleDocumentSymbol},
289-
{"textDocument/rename", &BashppServer::handleRename},
290-
{"textDocument/references", &BashppServer::handleReferences},
291-
{"shutdown", &BashppServer::shutdown}
292-
};
295+
{"textDocument/rename", &BashppServer::handleRename},
296+
{"textDocument/references", &BashppServer::handleReferences},
297+
{"shutdown", &BashppServer::shutdown}
298+
}};
293299

294300
/**
295301
* @brief Maps notification types to the functions that handle them.
296302
*
297303
*/
298-
static constexpr frozen::unordered_map<frozen::string, NotificationHandler, 6> notification_handlers = {
299-
{"textDocument/didOpen", &BashppServer::handleDidOpen},
300-
{"textDocument/didChange", &BashppServer::handleDidChange},
304+
static constexpr std::array<NotificationHandlerEntry, 6> notification_handlers = {{
305+
{"textDocument/didOpen", &BashppServer::handleDidOpen},
306+
{"textDocument/didChange", &BashppServer::handleDidChange},
301307
{"workspace/didChangeWatchedFiles", &BashppServer::handleDidChangeWatchedFiles},
302-
{"textDocument/didSave", &BashppServer::handleDidSave},
303-
{"textDocument/didClose", &BashppServer::handleDidClose},
304-
{"exit", &BashppServer::exit}
305-
};
308+
{"textDocument/didSave", &BashppServer::handleDidSave},
309+
{"textDocument/didClose", &BashppServer::handleDidClose},
310+
{"exit", &BashppServer::exit}
311+
}};
306312
};
307313

308314
} // namespace bpp

0 commit comments

Comments
 (0)