Skip to content

Commit c8953bf

Browse files
committed
chore: Clean up the LSP requests
1 parent 3aea810 commit c8953bf

File tree

7 files changed

+979
-2617
lines changed

7 files changed

+979
-2617
lines changed

packages/cxx-gen-lsp/src/gen_fwd_h.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class LSPRequest : public LSPObject {
4646
public:
4747
using LSPObject::LSPObject;
4848
49-
[[nodiscard]] auto method() const -> std::string {
50-
return repr_->at("method").get<std::string>();
51-
}
49+
[[nodiscard]] auto id() const -> std::optional<std::variant<long, std::string>>;
50+
[[nodiscard]] auto method() const -> std::string;
5251
};
5352
5453
class LSPResponse : public LSPObject {

packages/cxx-gen-lsp/src/gen_requests_cc.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,34 @@ class RequestGenerator {
4242
genTypes() {
4343
this.begin();
4444

45+
this.emit();
46+
this.emit(`auto LSPRequest::id() const -> std::optional<std::variant<long, std::string>> {`);
47+
this.emit(` if (!repr_->contains("id")) return std::nullopt;`);
48+
this.emit(` const auto& id = repr_->at("id");`);
49+
this.emit(` if (id.is_string()) return id.get<std::string>();`);
50+
this.emit(` return id.get<long>();`);
51+
this.emit(`}`);
52+
this.emit();
53+
this.emit(`auto LSPRequest::method() const -> std::string {`);
54+
this.emit(` return repr_->at("method");`);
55+
this.emit(`}`);
56+
4557
const requestsAndNotifications = [...this.model.requests, ...this.model.notifications];
4658

4759
requestsAndNotifications.forEach((request) => {
4860
const { typeName } = request;
4961
this.emit();
50-
this.emit(`auto ${typeName}::method() const -> std::string {`);
51-
this.emit(` return repr_->at("method");`);
52-
this.emit(`}`);
53-
this.emit();
5462
this.emit(`auto ${typeName}::method(std::string method) -> ${typeName}& {`);
5563
this.emit(` (*repr_)["method"] = std::move(method);`);
5664
this.emit(` return *this;`);
5765
this.emit(`}`);
5866
this.emit();
59-
this.emit(`auto ${typeName}::id() const -> std::variant<long, std::string> {`);
60-
this.emit(` const auto& id = repr_->at("id");`);
61-
this.emit(` if (id.is_string()) return id.get<std::string>();`);
62-
this.emit(` return id.get<long>();`);
63-
this.emit(`}`);
64-
this.emit();
65-
this.emit(`auto ${typeName}::id(long id) -> ${typeName}& {`);
66-
this.emit(` (*repr_)["id"] = id;`);
67-
this.emit(` return *this;`);
68-
this.emit(`}`);
69-
this.emit();
70-
this.emit(`auto ${typeName}::id(std::string id) -> ${typeName}& {`);
71-
this.emit(` (*repr_)["id"] = std::move(id);`);
67+
this.emit(`auto ${typeName}::id(std::variant<long, std::string> id) -> ${typeName}& {`);
68+
this.emit(` if (std::holds_alternative<long>(id)) {`);
69+
this.emit(` (*repr_)["id"] = std::get<long>(id);`);
70+
this.emit(` } else {`);
71+
this.emit(` (*repr_)["id"] = std::get<std::string>(id);`);
72+
this.emit(` }`);
7273
this.emit(` return *this;`);
7374
this.emit(`}`);
7475

@@ -91,12 +92,6 @@ class RequestGenerator {
9192
const responseTypeName = typeName.replace(/Request$/, "Response");
9293
const resultTypeName = toCppType(request.result);
9394

94-
this.emit();
95-
this.emit(`auto ${responseTypeName}::id() const -> std::variant<long, std::string> {`);
96-
this.emit(` const auto& id = repr_->at("id");`);
97-
this.emit(` if (id.is_string()) return id.get<std::string>();`);
98-
this.emit(` return id.get<long>();`);
99-
this.emit(`}`);
10095
this.emit();
10196
this.emit(`auto ${responseTypeName}::id(long id) -> ${responseTypeName}& {`);
10297
this.emit(` (*repr_)["id"] = id;`);

packages/cxx-gen-lsp/src/gen_requests_h.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ export function gen_requests_h({ model, outputDirectory }: { model: MetaModel; o
6666
emit(`class ${typeName} final : public LSPRequest {`);
6767
emit(`public:`);
6868
emit(` using LSPRequest::LSPRequest;`);
69+
emit(` using LSPRequest::id;`);
70+
emit(` using LSPRequest::method;`);
6971
emit();
70-
emit(` [[nodiscard]] auto method() const -> std::string;`);
7172
emit(` auto method(std::string method) -> ${typeName}&;`);
72-
emit();
73-
emit(` [[nodiscard]] auto id() const -> std::variant<long, std::string>;`);
74-
emit(` auto id(long id) -> ${typeName}&;`);
75-
emit(` auto id(std::string id) -> ${typeName}&;`);
73+
emit(` auto id(std::variant<long, std::string> id) -> ${typeName}&;`);
7674

7775
if (request.params) {
7876
const paramsType = toCppType(request.params);

src/frontend/cxx/lsp_server.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,20 @@ class Server {
391391
//
392392
void operator()(const LSPRequest& request) {
393393
std::cerr << "Request: " << request.method() << "\n";
394+
395+
if (request.id().has_value()) {
396+
// send an empty response.
397+
json storage;
398+
LSPObject result(storage);
399+
sendToClient(result, request.id());
400+
}
394401
}
395402
};
396403

397404
int startServer(const CLI& cli) {
398405
Server server;
399-
server.start();
400-
return 0;
406+
auto exitCode = server.start();
407+
return exitCode;
401408
}
402409

403410
} // namespace cxx::lsp

src/lsp/cxx/lsp/fwd.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,9 +645,9 @@ class LSPRequest : public LSPObject {
645645
public:
646646
using LSPObject::LSPObject;
647647

648-
[[nodiscard]] auto method() const -> std::string {
649-
return repr_->at("method").get<std::string>();
650-
}
648+
[[nodiscard]] auto id() const
649+
-> std::optional<std::variant<long, std::string>>;
650+
[[nodiscard]] auto method() const -> std::string;
651651
};
652652

653653
class LSPResponse : public LSPObject {

0 commit comments

Comments
 (0)