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
2 changes: 1 addition & 1 deletion packages/cxx-gen-lsp/src/MetaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export function toCppType(type: Type): string {
return `std::tuple<${type.items.map(toCppType).join(", ")}>`;

case "or":
return `std::variant<std::monostate, ${type.items.map(toCppType).join(", ")}>`;
return `std::variant<${type.items.map(toCppType).join(", ")}>`;

case "and":
return `std::tuple<${type.items.map(toCppType).join(", ")}>`;
Expand Down
15 changes: 6 additions & 9 deletions packages/cxx-gen-lsp/src/gen_fwd_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ using Pattern = std::string;

class LSPObject {
public:
LSPObject() = default;
explicit LSPObject(json& repr): repr_(&repr) {}

[[nodiscard]] explicit operator bool() const { return repr_ != nullptr; }
[[nodiscard]] operator const json&() const { return *repr_; }
[[nodiscard]] auto get() const -> json& { return *repr_; }

Expand All @@ -45,7 +47,7 @@ class Vector final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_array(); }
[[nodiscard]] explicit operator bool() const { return repr_ && repr_->is_array(); }
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(int index) const -> const T& { return repr_->at(index); }
Expand All @@ -71,11 +73,6 @@ auto try_emplace(std::variant<Ts...>& result, json& value) -> bool {
return (details::TryEmplace<Ts>{}(result, value) || ...);
}

template <>
struct TryEmplace<std::monostate> {
auto operator()(auto&, const json&) const -> bool { return false; }
};

template <>
struct TryEmplace<std::nullptr_t> {
auto operator()(auto& result, json& value) const -> bool {
Expand Down Expand Up @@ -169,7 +166,7 @@ class Vector<std::variant<Ts...>> final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_array(); }
[[nodiscard]] explicit operator bool() const { return repr_ && repr_->is_array(); }
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(int index) const -> std::variant<Ts...> {
Expand All @@ -184,7 +181,7 @@ class Map final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_object(); }
[[nodiscard]] explicit operator bool() const { return repr_ && repr_->is_object(); }
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(const Key& key) const -> const Value& { return repr_->at(key); }
Expand All @@ -195,7 +192,7 @@ class Map<Key, std::variant<Ts...>> final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_object(); }
[[nodiscard]] explicit operator bool() const { return repr_ && repr_->is_object(); }
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }

Expand Down
24 changes: 13 additions & 11 deletions packages/cxx-gen-lsp/src/gen_types_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,17 @@ class TypeGenerator {
}

case "array":
this.emit(`assert(value.is_array());`);
this.emit(`if (value.is_null()) value = json::array();`);
this.emit(`return ${propertyType}(value);`);
return;

case "map":
this.emit(`assert(value.is_object());`);
this.emit(`if (value.is_null()) value = json::object();`);
this.emit(`return ${propertyType}(value);`);
return;

case "stringLiteral":
this.emit(`if (value.is_null()) value = "${property.type.value}";`);
this.emit(`assert(value.is_string());`);
this.emit(`return value.get<std::string>();`);
return;
Expand All @@ -222,36 +223,43 @@ class TypeGenerator {
throw new Error(`Unexpected null type`);

case "string":
this.emit(`if (value.is_null()) value = "";`);
this.emit(`assert(value.is_string());`);
this.emit(`return value.get<std::string>();`);
return true;

case "integer":
this.emit(`if (value.is_null()) value = 0;`);
this.emit(`assert(value.is_number_integer());`);
this.emit(`return value.get<int>();`);
return true;

case "uinteger":
this.emit(`if (value.is_null()) value = 0;`);
this.emit(`assert(value.is_number_integer());`);
this.emit(`return value.get<long>();`);
return true;

case "decimal":
this.emit(`if (value.is_null()) value = 0.0;`);
this.emit(`assert(value.is_number());`);
this.emit(`return value.get<double>();`);
return true;

case "boolean":
this.emit(`if (value.is_null()) value = false;`);
this.emit(`assert(value.is_boolean());`);
this.emit(`return value.get<bool>();`);
return true;

case "DocumentUri":
this.emit(`if (value.is_null()) value = "";`);
this.emit(`assert(value.is_string());`);
this.emit(`return value.get<std::string>();`);
return true;

case "URI":
this.emit(`if (value.is_null()) value = "";`);
this.emit(`assert(value.is_string());`);
this.emit(`return value.get<std::string>();`);
return true;
Expand Down Expand Up @@ -347,7 +355,7 @@ class TypeGenerator {

switch (property.type.kind) {
case "base":
this.emit(`repr_->emplace("${property.name}", std::move(${value}));`);
this.emit(`(*repr_)["${property.name}"] = std::move(${value});`);
return;

case "reference":
Expand Down Expand Up @@ -380,7 +388,7 @@ class TypeGenerator {

if (enumeration.type.name !== "string") {
const enumBaseType = toCppType(enumeration.type);
this.emit(`repr_->emplace("${property.name}", static_cast<${enumBaseType}>(${value}));`);
this.emit(`(*repr_)["${property.name}"] = static_cast<${enumBaseType}>(${value});`);
return true;
}

Expand All @@ -389,7 +397,7 @@ class TypeGenerator {
}

if (this.structByName.has(property.type.name)) {
this.emit(`repr_->emplace("${property.name}", ${value});`);
this.emit(`(*repr_)["${property.name}"] = ${value};`);
return true;
}

Expand All @@ -407,15 +415,9 @@ class TypeGenerator {
}): boolean {
if (property.type.kind !== "or") return false;

this.emit();
this.emit("// or type");
this.emit();
this.emit(`struct {`);
this.emit(`json* repr_;`);
this.emit();
this.emit(`void operator()(std::monostate) {`);
this.emit(`lsp_runtime_error("monostate is not a valid a property value");`);
this.emit(`}`);

property.type.items.forEach((item) => {
const itemType = this.getPropertyType({ type: item });
Expand Down
58 changes: 30 additions & 28 deletions src/lsp/cxx/lsp/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,10 @@ using Pattern = std::string;

class LSPObject {
public:
LSPObject() = default;
explicit LSPObject(json& repr) : repr_(&repr) {}

[[nodiscard]] explicit operator bool() const { return repr_ != nullptr; }
[[nodiscard]] operator const json&() const { return *repr_; }
[[nodiscard]] auto get() const -> json& { return *repr_; }

Expand All @@ -477,7 +479,9 @@ class Vector final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_array(); }
[[nodiscard]] explicit operator bool() const {
return repr_ && repr_->is_array();
}
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(int index) const -> const T& {
Expand Down Expand Up @@ -505,11 +509,6 @@ auto try_emplace(std::variant<Ts...>& result, json& value) -> bool {
return (details::TryEmplace<Ts>{}(result, value) || ...);
}

template <>
struct TryEmplace<std::monostate> {
auto operator()(auto&, const json&) const -> bool { return false; }
};

template <>
struct TryEmplace<std::nullptr_t> {
auto operator()(auto& result, json& value) const -> bool {
Expand Down Expand Up @@ -603,7 +602,9 @@ class Vector<std::variant<Ts...>> final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_array(); }
[[nodiscard]] explicit operator bool() const {
return repr_ && repr_->is_array();
}
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(int index) const -> std::variant<Ts...> {
Expand All @@ -618,7 +619,9 @@ class Map final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_object(); }
[[nodiscard]] explicit operator bool() const {
return repr_ && repr_->is_object();
}
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }
[[nodiscard]] auto at(const Key& key) const -> const Value& {
Expand All @@ -631,7 +634,9 @@ class Map<Key, std::variant<Ts...>> final : public LSPObject {
public:
using LSPObject::LSPObject;

[[nodiscard]] explicit operator bool() const { return repr_->is_object(); }
[[nodiscard]] explicit operator bool() const {
return repr_ && repr_->is_object();
}
[[nodiscard]] auto size() const -> std::size_t { return repr_->size(); }
[[nodiscard]] auto empty() const -> bool { return repr_->empty(); }

Expand All @@ -645,53 +650,50 @@ class Map<Key, std::variant<Ts...>> final : public LSPObject {
using RegularExpressionEngineKind = std::string;

using NotebookDocumentFilter =
std::variant<std::monostate, NotebookDocumentFilterNotebookType,
std::variant<NotebookDocumentFilterNotebookType,
NotebookDocumentFilterScheme, NotebookDocumentFilterPattern>;

using TextDocumentFilter =
std::variant<std::monostate, TextDocumentFilterLanguage,
TextDocumentFilterScheme, TextDocumentFilterPattern>;
std::variant<TextDocumentFilterLanguage, TextDocumentFilterScheme,
TextDocumentFilterPattern>;

using GlobPattern = std::variant<std::monostate, Pattern, RelativePattern>;
using GlobPattern = std::variant<Pattern, RelativePattern>;

using DocumentFilter = std::variant<std::monostate, TextDocumentFilter,
NotebookCellTextDocumentFilter>;
using DocumentFilter =
std::variant<TextDocumentFilter, NotebookCellTextDocumentFilter>;

using MarkedString =
std::variant<std::monostate, std::string, MarkedStringWithLanguage>;
using MarkedString = std::variant<std::string, MarkedStringWithLanguage>;

using TextDocumentContentChangeEvent =
std::variant<std::monostate, TextDocumentContentChangePartial,
std::variant<TextDocumentContentChangePartial,
TextDocumentContentChangeWholeDocument>;

using WorkspaceDocumentDiagnosticReport =
std::variant<std::monostate, WorkspaceFullDocumentDiagnosticReport,
std::variant<WorkspaceFullDocumentDiagnosticReport,
WorkspaceUnchangedDocumentDiagnosticReport>;

using ChangeAnnotationIdentifier = std::string;

using ProgressToken = std::variant<std::monostate, int, std::string>;
using ProgressToken = std::variant<int, std::string>;

using DocumentSelector = Vector<DocumentFilter>;

using PrepareRenameResult =
std::variant<std::monostate, Range, PrepareRenamePlaceholder,
PrepareRenameDefaultBehavior>;
std::variant<Range, PrepareRenamePlaceholder, PrepareRenameDefaultBehavior>;

using DocumentDiagnosticReport =
std::variant<std::monostate, RelatedFullDocumentDiagnosticReport,
std::variant<RelatedFullDocumentDiagnosticReport,
RelatedUnchangedDocumentDiagnosticReport>;

using InlineValue =
std::variant<std::monostate, InlineValueText, InlineValueVariableLookup,
InlineValueEvaluatableExpression>;
using InlineValue = std::variant<InlineValueText, InlineValueVariableLookup,
InlineValueEvaluatableExpression>;

using DeclarationLink = LocationLink;

using Declaration = std::variant<std::monostate, Location, Vector<Location>>;
using Declaration = std::variant<Location, Vector<Location>>;

using DefinitionLink = LocationLink;

using Definition = std::variant<std::monostate, Location, Vector<Location>>;
using Definition = std::variant<Location, Vector<Location>>;

} // namespace cxx::lsp
Loading