Skip to content

Commit 3443df4

Browse files
committed
chore: Add push_back to the LSP vector types
1 parent 588336a commit 3443df4

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ class Vector<std::variant<Ts...>> final : public LSPObject {
193193
details::try_emplace(result, repr_->at(index));
194194
return result;
195195
}
196+
197+
template <typename T>
198+
[[nodiscard]] auto emplace_back() -> std::variant<Ts...> {
199+
std::variant<Ts...> result;
200+
details::TryEmplace<T>{}(result, repr_->emplace_back());
201+
return result;
202+
}
203+
204+
template <std::derived_from<LSPObject> T>
205+
[[nodiscard]] auto emplace_back() -> T {
206+
return T(repr_->emplace_back());
207+
}
196208
};
197209
198210
template <typename Key, typename Value>
@@ -272,6 +284,7 @@ export function gen_fwd_h({ model, outputDirectory }: { model: MetaModel; output
272284
emit(`#pragma once`);
273285
emit();
274286
emit(`#include <nlohmann/json.hpp>`);
287+
emit(`#include <concepts>`);
275288
emit(`#include <variant>`);
276289
emit();
277290
emit(`namespace cxx::lsp {`);

src/lsp/cxx/lsp/fwd.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#pragma once
2222

23+
#include <concepts>
2324
#include <nlohmann/json.hpp>
2425
#include <variant>
2526

@@ -637,6 +638,18 @@ class Vector<std::variant<Ts...>> final : public LSPObject {
637638
details::try_emplace(result, repr_->at(index));
638639
return result;
639640
}
641+
642+
template <typename T>
643+
[[nodiscard]] auto emplace_back() -> std::variant<Ts...> {
644+
std::variant<Ts...> result;
645+
details::TryEmplace<T>{}(result, repr_->emplace_back());
646+
return result;
647+
}
648+
649+
template <std::derived_from<LSPObject> T>
650+
[[nodiscard]] auto emplace_back() -> T {
651+
return T(repr_->emplace_back());
652+
}
640653
};
641654

642655
template <typename Key, typename Value>

src/lsp/tests/test_types.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,27 @@ TEST(LSP, StringArrayProperty) {
119119
ASSERT_EQ(textDocumentContentRegistrationOptions.schemes().at(0), "file");
120120
ASSERT_EQ(textDocumentContentRegistrationOptions.schemes().at(1), "http");
121121
}
122+
123+
TEST(LSP, VariantArrayProperty) {
124+
auto storage = json::object();
125+
126+
NotebookDocumentSyncRegistrationOptions
127+
notebookDocumentSyncRegistrationOptions{storage};
128+
129+
auto notebookSelector =
130+
notebookDocumentSyncRegistrationOptions.notebookSelector();
131+
132+
ASSERT_TRUE(notebookSelector.empty());
133+
134+
auto item = notebookSelector.emplace_back<NotebookDocumentFilterWithCells>();
135+
136+
ASSERT_FALSE(item.notebook().has_value());
137+
138+
item.notebook("a_notebook");
139+
140+
ASSERT_TRUE(item.notebook().has_value());
141+
142+
ASSERT_EQ(std::get<std::string>(*item.notebook()), "a_notebook");
143+
144+
ASSERT_EQ(notebookSelector.size(), 1);
145+
}

0 commit comments

Comments
 (0)