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
20 changes: 20 additions & 0 deletions packages/cxx-gen-lsp/src/MetaModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ export function isRequest(request: Request | Notification): request is Request {
return "result" in request;
}

export function isStringLike(type: Type, typeAliasByName: Map<string, TypeAlias>): boolean {
switch (type.kind) {
case "base":
return type.name === "string";

case "reference": {
if (typeAliasByName.has(type.name)) {
return isStringLike(typeAliasByName.get(type.name)!.type, typeAliasByName);
}
return false;
}

case "stringLiteral":
return true;

default:
return false;
} // switch
}

export function getEnumBaseType(enumeration: Enumeration) {
switch (enumeration.type.name) {
case "integer":
Expand Down
20 changes: 0 additions & 20 deletions packages/cxx-gen-lsp/src/gen_requests_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,6 @@ class RequestGenerator {
return propertyType;
}

isStringLike(type: Type): boolean {
switch (type.kind) {
case "base":
return type.name === "string";

case "reference": {
if (this.typeAliasByName.has(type.name)) {
return this.isStringLike(this.typeAliasByName.get(type.name)!.type);
}
return false;
}

case "stringLiteral":
return true;

default:
return false;
} // switch
}

begin() {
this.emit(copyrightHeader);
this.emit();
Expand Down
29 changes: 7 additions & 22 deletions packages/cxx-gen-lsp/src/gen_types_cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ class TypeGenerator {
return propertyType;
}

isStringLike(type: Type): boolean {
switch (type.kind) {
case "base":
return type.name === "string";

case "reference": {
if (this.typeAliasByName.has(type.name)) {
return this.isStringLike(this.typeAliasByName.get(type.name)!.type);
}
return false;
}

case "stringLiteral":
return true;

default:
return false;
} // switch
}

begin() {
this.emit(copyrightHeader);
this.emit();
Expand Down Expand Up @@ -212,7 +192,8 @@ class TypeGenerator {
} // switch

this.emit();
this.emit(`lsp_runtime_error("${structure.name}::${property.name}: not implement yet");`);

this.emit(`lsp_runtime_error("${structure.name}::${property.name}: not implemented yet");`);
}

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

case "array":
this.emit(`(*repr_)["${property.name}"] = std::move(${value});`);
return;

case "or":
if (!this.generatePropertySetterOr({ structure, property, value })) break;
return;
Expand All @@ -370,7 +355,7 @@ class TypeGenerator {
break;
} // switch

this.emit(`lsp_runtime_error("${typeName}::${property.name}: not implement yet");`);
this.emit(`lsp_runtime_error("${typeName}::${property.name}: not implemented yet");`);
}

generatePropertySetterReference({
Expand Down
30 changes: 29 additions & 1 deletion packages/cxx-gen-lsp/src/gen_types_h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// SOFTWARE.

import * as path from "node:path";
import { getStructureProperties, MetaModel, Property, toCppType } from "./MetaModel.js";
import { getStructureProperties, isStringLike, MetaModel, Property, toCppType, Type } from "./MetaModel.js";
import { writeFileSync } from "node:fs";
import { copyrightHeader } from "./copyrightHeader.js";

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

const typeAliasByName = new Map(model.typeAliases.map((t) => [t.name, t]));

const isOrType = (type: Type) => {
if (type.kind === "or") {
return true;
} else if (type.kind === "reference" && typeAliasByName.has(type.name)) {
return isOrType(typeAliasByName.get(type.name)!.type);
}
return false;
};

model.structures.forEach((structure) => {
const typeName = structure.name;
emit();
Expand All @@ -63,6 +74,14 @@ export function gen_types_h({ model, outputDirectory }: { model: MetaModel; outp
const returnType = getReturnType(property);
emit();
emit(` [[nodiscard ]]auto ${propertyName}() const -> ${returnType};`);
if (property.optional || property.type.kind === "or") {
emit();
emit(`template <typename T>`);
emit(`[[nodiscard]] auto ${propertyName}() -> T {`);
emit(` auto& value = (*repr_)["${propertyName}"];`);
emit(` return T(value);`);
emit(`}`);
}
});

emit();
Expand All @@ -71,6 +90,15 @@ export function gen_types_h({ model, outputDirectory }: { model: MetaModel; outp
const argumentType = getReturnType(property);
emit();
emit(` auto ${propertyName}(${argumentType} ${propertyName}) -> ${typeName}&;`);

if (property.type.kind === "array" && isStringLike(property.type.element, typeAliasByName)) {
emit();
emit(` auto ${propertyName}(std::vector<std::string> ${propertyName}) -> ${typeName}& {`);
emit(` auto& value = (*repr_)["${propertyName}"];`);
emit(` value = std::move(${propertyName});`);
emit(` return *this;`);
emit(` }`);
}
});

emit(`};`);
Expand Down
Loading