Skip to content

Commit

Permalink
Link to known types
Browse files Browse the repository at this point in the history
  • Loading branch information
Frixuu committed Jul 23, 2024
1 parent edb38e3 commit f6b2455
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 11 deletions.
48 changes: 47 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions docs/.vitepress/theme/Signature.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<script setup lang="ts">
const props = defineProps(["name", "typeParams", "args", "returnType"]);
import TypeRef from './TypeRef.vue';
const props = defineProps<{name: string, returnType: any}>();
</script>

<template>
<span>
<slot name="pre"></slot>
<span class="code">
<strong class="name">{{ name }}</strong>: <span class="type">{{ returnType }}</span>
<strong class="name">{{ name }}</strong>:
<span class="type" v-if="returnType"><TypeRef :type="returnType" /></span>
<slot></slot>
</span>
<slot name="post"></slot>
Expand Down
33 changes: 33 additions & 0 deletions docs/.vitepress/theme/TypeRef.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script setup lang="ts">
import { withBase } from 'vitepress';
interface Path {
pack: string[];
module: string;
name: string;
}
interface RuntimeType {
path: Path;
typeParams: RuntimeType[];
}
const props = defineProps<{type: RuntimeType}>();
</script>

<template>
<span>
<template v-if="type.path.pack[0] == 'minetest'">
<a :href="withBase(['reference', ...type.path.pack, type.path.name]
.reduce((curr, next) => `${curr}/${next}`, '') + '.html')">
{{type.path.name}}
</a>
</template>
<template v-else>{{type.path.name}}</template>
<template v-if="type.typeParams.length > 0">&lt;<span v-for="(param, index) in type.typeParams" :key="index">
<TypeRef :type="param" /><span v-if="index !== type.typeParams.length - 1">, </span>
</span>&gt;</template>
</span>
</template>
20 changes: 15 additions & 5 deletions scripts/generate-class-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { abort, assertHaxeExists, decodeUtf8, encodeUtf8, invokeHaxe, mustArray,
import { path, xml } from "./deps.ts";
import { Context } from "./haxe/documentation/context.ts";
import { renderType } from "./haxe/documentation/render.ts";
import { Class, Interface, Method, Path, Property, Type } from "./haxe/types.ts";
import { Class, GenericRuntimeType, Interface, Method, Path, Property, Type } from "./haxe/types.ts";

await assertHaxeExists();

Expand Down Expand Up @@ -89,11 +89,21 @@ for (const typeNode of document["~children"] as XmlNode[]) {
const method = new Method(memberName);
member = method;
} else {
const property = new Property(memberName);
const typeNode = (memberNode["~children"] as XmlNode[]).find(child => child["~name"].length == 1);
if (typeNode && typeNode["@path"]) {
property.type = Path.fromDotPath(typeNode["@path"]! as string);

function selectTypeNodes(node: XmlNode): XmlNode[] {
return (node["~children"] as XmlNode[]).filter(child => child["~name"].length == 1);
}

function buildType(node: XmlNode): GenericRuntimeType {
const path = Path.fromDotPath(node["@path"] as string || "Dynamic");
const type = new GenericRuntimeType(path, []);
for (const childNode of selectTypeNodes(node)) {
type.typeParams.push(buildType(childNode));
}
return type;
}

const property = new Property(memberName, buildType(selectTypeNodes(memberNode).at(0)!));
member = property;
}
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/haxe/documentation/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function renderInterfaceTypeInfo(ctx: Context, iface: Interface): string {
}

function renderPropertySignature(property: Property): string {
let markdown = `<Signature name="${property.name}" returnType="${property.type}">`;
let markdown = `<Signature name="${property.name}" :returnType='${JSON.stringify(property.type)}'>`;
markdown += "</Signature>";
return markdown;
}
Expand Down
15 changes: 13 additions & 2 deletions scripts/haxe/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export class Path {
}
}

export class RuntimeType {
constructor() { }
}

export class GenericRuntimeType extends RuntimeType {
constructor(public path: Path, public typeParams: RuntimeType[]) {
super();
}
}

export interface Documentible {
documentation?: string;
}
Expand Down Expand Up @@ -167,9 +177,10 @@ export abstract class Member implements Documentible {
}

export class Property extends Member {
type?: Path;
constructor(name: string) {
type: RuntimeType;
constructor(name: string, type: RuntimeType) {
super(name);
this.type = type;
}
}

Expand Down

0 comments on commit f6b2455

Please sign in to comment.