Skip to content

Commit 588336a

Browse files
committed
fix: Ownership of vectors of string properties
1 parent baf9573 commit 588336a

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public:
5151
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
5252
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
5353
[[nodiscard]] auto at(int index) const -> const T& { return repr_->at(index); }
54+
55+
template <typename... Args>
56+
void emplace_back(Args&&... args) { repr_->emplace_back(std::forward<Args>(args)...); }
5457
};
5558
5659
namespace details {
@@ -161,6 +164,22 @@ struct TryEmplace<TextDocumentSyncKind> {
161164
162165
} // namespace details
163166
167+
template <>
168+
class Vector<std::string> final : public LSPObject {
169+
public:
170+
using LSPObject::LSPObject;
171+
172+
[[nodiscard]] explicit operator bool() const { return repr_ && repr_->is_array(); }
173+
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
174+
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
175+
[[nodiscard]] auto at(int index) const -> std::string {
176+
return repr_->at(index);
177+
}
178+
179+
template <typename... Args>
180+
void emplace_back(Args&&... args) { repr_->emplace_back(std::forward<Args>(args)...); }
181+
};
182+
164183
template <typename... Ts>
165184
class Vector<std::variant<Ts...>> final : public LSPObject {
166185
public:

src/lsp/cxx/lsp/fwd.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ class Vector final : public LSPObject {
487487
[[nodiscard]] auto at(int index) const -> const T& {
488488
return repr_->at(index);
489489
}
490+
491+
template <typename... Args>
492+
void emplace_back(Args&&... args) {
493+
repr_->emplace_back(std::forward<Args>(args)...);
494+
}
490495
};
491496

492497
namespace details {
@@ -597,6 +602,26 @@ struct TryEmplace<TextDocumentSyncKind> {
597602

598603
} // namespace details
599604

605+
template <>
606+
class Vector<std::string> final : public LSPObject {
607+
public:
608+
using LSPObject::LSPObject;
609+
610+
[[nodiscard]] explicit operator bool() const {
611+
return repr_ && repr_->is_array();
612+
}
613+
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
614+
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
615+
[[nodiscard]] auto at(int index) const -> std::string {
616+
return repr_->at(index);
617+
}
618+
619+
template <typename... Args>
620+
void emplace_back(Args&&... args) {
621+
repr_->emplace_back(std::forward<Args>(args)...);
622+
}
623+
};
624+
600625
template <typename... Ts>
601626
class Vector<std::variant<Ts...>> final : public LSPObject {
602627
public:

src/lsp/tests/test_types.cc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,34 @@ TEST(LSP, StringProperty) {
8888
ASSERT_EQ(range.start().line(), 0);
8989
ASSERT_EQ(range.start().character(), 0);
9090

91+
range.start().line(1);
92+
range.start().character(2);
93+
range.end().line(3);
94+
range.end().character(4);
95+
96+
ASSERT_EQ(range.start().line(), 1);
97+
ASSERT_EQ(range.start().character(), 2);
98+
ASSERT_EQ(range.end().line(), 3);
99+
ASSERT_EQ(range.end().character(), 4);
100+
91101
ASSERT_TRUE(location);
92-
}
102+
}
103+
104+
TEST(LSP, StringArrayProperty) {
105+
json storage = json::object();
106+
107+
auto textDocumentContentRegistrationOptions =
108+
TextDocumentContentRegistrationOptions{storage};
109+
110+
auto schemas = textDocumentContentRegistrationOptions.schemes();
111+
ASSERT_TRUE(schemas.empty());
112+
113+
schemas.emplace_back("file");
114+
schemas.emplace_back("http");
115+
116+
ASSERT_EQ(schemas.at(0), "file");
117+
ASSERT_EQ(schemas.at(1), "http");
118+
119+
ASSERT_EQ(textDocumentContentRegistrationOptions.schemes().at(0), "file");
120+
ASSERT_EQ(textDocumentContentRegistrationOptions.schemes().at(1), "http");
121+
}

0 commit comments

Comments
 (0)