Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/cxx-gen-lsp/src/gen_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ class LSPRequest : public LSPObject {
public:
using LSPObject::LSPObject;

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

class LSPResponse : public LSPObject {
Expand Down
41 changes: 18 additions & 23 deletions packages/cxx-gen-lsp/src/gen_requests_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,34 @@ class RequestGenerator {
genTypes() {
this.begin();

this.emit();
this.emit(`auto LSPRequest::id() const -> std::optional<std::variant<long, std::string>> {`);
this.emit(` if (!repr_->contains("id")) return std::nullopt;`);
this.emit(` const auto& id = repr_->at("id");`);
this.emit(` if (id.is_string()) return id.get<std::string>();`);
this.emit(` return id.get<long>();`);
this.emit(`}`);
this.emit();
this.emit(`auto LSPRequest::method() const -> std::string {`);
this.emit(` return repr_->at("method");`);
this.emit(`}`);

const requestsAndNotifications = [...this.model.requests, ...this.model.notifications];

requestsAndNotifications.forEach((request) => {
const { typeName } = request;
this.emit();
this.emit(`auto ${typeName}::method() const -> std::string {`);
this.emit(` return repr_->at("method");`);
this.emit(`}`);
this.emit();
this.emit(`auto ${typeName}::method(std::string method) -> ${typeName}& {`);
this.emit(` (*repr_)["method"] = std::move(method);`);
this.emit(` return *this;`);
this.emit(`}`);
this.emit();
this.emit(`auto ${typeName}::id() const -> std::variant<long, std::string> {`);
this.emit(` const auto& id = repr_->at("id");`);
this.emit(` if (id.is_string()) return id.get<std::string>();`);
this.emit(` return id.get<long>();`);
this.emit(`}`);
this.emit();
this.emit(`auto ${typeName}::id(long id) -> ${typeName}& {`);
this.emit(` (*repr_)["id"] = id;`);
this.emit(` return *this;`);
this.emit(`}`);
this.emit();
this.emit(`auto ${typeName}::id(std::string id) -> ${typeName}& {`);
this.emit(` (*repr_)["id"] = std::move(id);`);
this.emit(`auto ${typeName}::id(std::variant<long, std::string> id) -> ${typeName}& {`);
this.emit(` if (std::holds_alternative<long>(id)) {`);
this.emit(` (*repr_)["id"] = std::get<long>(id);`);
this.emit(` } else {`);
this.emit(` (*repr_)["id"] = std::get<std::string>(id);`);
this.emit(` }`);
this.emit(` return *this;`);
this.emit(`}`);

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

this.emit();
this.emit(`auto ${responseTypeName}::id() const -> std::variant<long, std::string> {`);
this.emit(` const auto& id = repr_->at("id");`);
this.emit(` if (id.is_string()) return id.get<std::string>();`);
this.emit(` return id.get<long>();`);
this.emit(`}`);
this.emit();
this.emit(`auto ${responseTypeName}::id(long id) -> ${responseTypeName}& {`);
this.emit(` (*repr_)["id"] = id;`);
Expand Down
8 changes: 3 additions & 5 deletions packages/cxx-gen-lsp/src/gen_requests_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ export function gen_requests_h({ model, outputDirectory }: { model: MetaModel; o
emit(`class ${typeName} final : public LSPRequest {`);
emit(`public:`);
emit(` using LSPRequest::LSPRequest;`);
emit(` using LSPRequest::id;`);
emit(` using LSPRequest::method;`);
emit();
emit(` [[nodiscard]] auto method() const -> std::string;`);
emit(` auto method(std::string method) -> ${typeName}&;`);
emit();
emit(` [[nodiscard]] auto id() const -> std::variant<long, std::string>;`);
emit(` auto id(long id) -> ${typeName}&;`);
emit(` auto id(std::string id) -> ${typeName}&;`);
emit(` auto id(std::variant<long, std::string> id) -> ${typeName}&;`);

if (request.params) {
const paramsType = toCppType(request.params);
Expand Down
11 changes: 9 additions & 2 deletions src/frontend/cxx/lsp_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,20 @@ class Server {
//
void operator()(const LSPRequest& request) {
std::cerr << "Request: " << request.method() << "\n";

if (request.id().has_value()) {
// send an empty response.
json storage;
LSPObject result(storage);
sendToClient(result, request.id());
}
}
};

int startServer(const CLI& cli) {
Server server;
server.start();
return 0;
auto exitCode = server.start();
return exitCode;
}

} // namespace cxx::lsp
6 changes: 3 additions & 3 deletions src/lsp/cxx/lsp/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ class LSPRequest : public LSPObject {
public:
using LSPObject::LSPObject;

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

class LSPResponse : public LSPObject {
Expand Down
Loading