Skip to content

Commit 9fb46f6

Browse files
committed
chore: Add helpers to simplify access to the LSP types
1 parent 931d20f commit 9fb46f6

File tree

7 files changed

+4914
-443
lines changed

7 files changed

+4914
-443
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ export function isRequest(request: Request | Notification): request is Request {
143143
return "result" in request;
144144
}
145145

146+
export function isStringLike(type: Type, typeAliasByName: Map<string, TypeAlias>): boolean {
147+
switch (type.kind) {
148+
case "base":
149+
return type.name === "string";
150+
151+
case "reference": {
152+
if (typeAliasByName.has(type.name)) {
153+
return isStringLike(typeAliasByName.get(type.name)!.type, typeAliasByName);
154+
}
155+
return false;
156+
}
157+
158+
case "stringLiteral":
159+
return true;
160+
161+
default:
162+
return false;
163+
} // switch
164+
}
165+
146166
export function getEnumBaseType(enumeration: Enumeration) {
147167
switch (enumeration.type.name) {
148168
case "integer":

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,6 @@ class RequestGenerator {
165165
return propertyType;
166166
}
167167

168-
isStringLike(type: Type): boolean {
169-
switch (type.kind) {
170-
case "base":
171-
return type.name === "string";
172-
173-
case "reference": {
174-
if (this.typeAliasByName.has(type.name)) {
175-
return this.isStringLike(this.typeAliasByName.get(type.name)!.type);
176-
}
177-
return false;
178-
}
179-
180-
case "stringLiteral":
181-
return true;
182-
183-
default:
184-
return false;
185-
} // switch
186-
}
187-
188168
begin() {
189169
this.emit(copyrightHeader);
190170
this.emit();

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,6 @@ class TypeGenerator {
7676
return propertyType;
7777
}
7878

79-
isStringLike(type: Type): boolean {
80-
switch (type.kind) {
81-
case "base":
82-
return type.name === "string";
83-
84-
case "reference": {
85-
if (this.typeAliasByName.has(type.name)) {
86-
return this.isStringLike(this.typeAliasByName.get(type.name)!.type);
87-
}
88-
return false;
89-
}
90-
91-
case "stringLiteral":
92-
return true;
93-
94-
default:
95-
return false;
96-
} // switch
97-
}
98-
9979
begin() {
10080
this.emit(copyrightHeader);
10181
this.emit();
@@ -212,7 +192,8 @@ class TypeGenerator {
212192
} // switch
213193

214194
this.emit();
215-
this.emit(`lsp_runtime_error("${structure.name}::${property.name}: not implement yet");`);
195+
196+
this.emit(`lsp_runtime_error("${structure.name}::${property.name}: not implemented yet");`);
216197
}
217198

218199
generatePropertyGetterBase({ property }: { structure: Structure; property: Property }): boolean {
@@ -362,6 +343,10 @@ class TypeGenerator {
362343
if (!this.generatePropertySetterReference({ structure, property, value })) break;
363344
return;
364345

346+
case "array":
347+
this.emit(`(*repr_)["${property.name}"] = std::move(${value});`);
348+
return;
349+
365350
case "or":
366351
if (!this.generatePropertySetterOr({ structure, property, value })) break;
367352
return;
@@ -370,7 +355,7 @@ class TypeGenerator {
370355
break;
371356
} // switch
372357

373-
this.emit(`lsp_runtime_error("${typeName}::${property.name}: not implement yet");`);
358+
this.emit(`lsp_runtime_error("${typeName}::${property.name}: not implemented yet");`);
374359
}
375360

376361
generatePropertySetterReference({

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// SOFTWARE.
2020

2121
import * as path from "node:path";
22-
import { getStructureProperties, MetaModel, Property, toCppType } from "./MetaModel.js";
22+
import { getStructureProperties, isStringLike, MetaModel, Property, toCppType, Type } from "./MetaModel.js";
2323
import { writeFileSync } from "node:fs";
2424
import { copyrightHeader } from "./copyrightHeader.js";
2525

@@ -47,6 +47,17 @@ export function gen_types_h({ model, outputDirectory }: { model: MetaModel; outp
4747
emit(`namespace cxx::lsp {`);
4848
emit();
4949

50+
const typeAliasByName = new Map(model.typeAliases.map((t) => [t.name, t]));
51+
52+
const isOrType = (type: Type) => {
53+
if (type.kind === "or") {
54+
return true;
55+
} else if (type.kind === "reference" && typeAliasByName.has(type.name)) {
56+
return isOrType(typeAliasByName.get(type.name)!.type);
57+
}
58+
return false;
59+
};
60+
5061
model.structures.forEach((structure) => {
5162
const typeName = structure.name;
5263
emit();
@@ -63,6 +74,14 @@ export function gen_types_h({ model, outputDirectory }: { model: MetaModel; outp
6374
const returnType = getReturnType(property);
6475
emit();
6576
emit(` [[nodiscard ]]auto ${propertyName}() const -> ${returnType};`);
77+
if (property.optional || property.type.kind === "or") {
78+
emit();
79+
emit(`template <typename T>`);
80+
emit(`[[nodiscard]] auto ${propertyName}() -> T {`);
81+
emit(` auto& value = (*repr_)["${propertyName}"];`);
82+
emit(` return T(value);`);
83+
emit(`}`);
84+
}
6685
});
6786

6887
emit();
@@ -71,6 +90,15 @@ export function gen_types_h({ model, outputDirectory }: { model: MetaModel; outp
7190
const argumentType = getReturnType(property);
7291
emit();
7392
emit(` auto ${propertyName}(${argumentType} ${propertyName}) -> ${typeName}&;`);
93+
94+
if (property.type.kind === "array" && isStringLike(property.type.element, typeAliasByName)) {
95+
emit();
96+
emit(` auto ${propertyName}(std::vector<std::string> ${propertyName}) -> ${typeName}& {`);
97+
emit(` auto& value = (*repr_)["${propertyName}"];`);
98+
emit(` value = std::move(${propertyName});`);
99+
emit(` return *this;`);
100+
emit(` }`);
101+
}
74102
});
75103

76104
emit(`};`);

0 commit comments

Comments
 (0)