Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEROM22 committed Dec 17, 2024
1 parent b6fcfc3 commit 1fd3c7a
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 193 deletions.
75 changes: 40 additions & 35 deletions packages/typedoc-signature/lib/internal/concise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ import {
isUnionType,
} from "@onlyoffice/typedoc-util-is-type"
import {type JSONOutput as J} from "typedoc"
import {type Context} from "./context.ts"
import {type Formatter} from "./formatter.ts"

export interface ContextFormat {
context: Context
formatter: Formatter
export interface Context {
f: Formatter
trailOf(t: Declaration | Fragment): FlatTrail | undefined
reflectionOf(t: FlatTrail): J.Reflection | undefined
idOf(id: number): number | undefined
}

export type FlatTrail = number[]

export function classDeclaration(r: J.Reflection, cf: ContextFormat): Signature {
export function classDeclaration(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isClassReflection(r)) {
Expand Down Expand Up @@ -77,7 +75,7 @@ export function constructorDeclaration(r: J.Reflection): Signature {
return s
}

export function enumMemberReflection(r: J.Reflection, cf: ContextFormat): Signature {
export function enumMemberReflection(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isEnumMemberReflection(r)) {
Expand All @@ -98,7 +96,7 @@ export function enumMemberReflection(r: J.Reflection, cf: ContextFormat): Signat
return s
}

export function enumReflection(r: J.Reflection, cf: ContextFormat): Signature {
export function enumReflection(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isEnumReflection(r)) {
Expand Down Expand Up @@ -137,7 +135,7 @@ export function functionsDeclaration(f: J.Reflection): Signature {
return s
}

export function interfaceReflection(r: J.Reflection, cf: ContextFormat): Signature {
export function interfaceReflection(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isInterfaceReflection(r)) {
Expand Down Expand Up @@ -170,7 +168,7 @@ export function methodDeclaration(m: J.Reflection): Signature {
return s
}

export function propertyReflection(r: J.Reflection, cf: ContextFormat): Signature {
export function propertyReflection(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isPropertyReflection(r)) {
Expand All @@ -192,7 +190,7 @@ export function propertyReflection(r: J.Reflection, cf: ContextFormat): Signatur
return s
}

export function typeAliasReflection(r: J.Reflection, cf: ContextFormat): Signature {
export function typeAliasReflection(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isTypeAliasReflection(r)) {
Expand All @@ -208,7 +206,7 @@ export function typeAliasReflection(r: J.Reflection, cf: ContextFormat): Signatu
return s
}

export function variableDeclaration(r: J.Reflection, cf: ContextFormat): Signature {
export function variableDeclaration(cf: Context, r: J.Reflection): Signature {
const s: Signature = []

if (!isVariableReflection(r)) {
Expand Down Expand Up @@ -267,7 +265,7 @@ export function value(p: J.Reflection): Signature {
return s
}

export function type(t: J.SomeType, cf: ContextFormat): Signature {
export function type(t: J.SomeType, cf: Context): Signature {
if (isArrayType(t)) {
return arrayType(t, cf)
}
Expand Down Expand Up @@ -295,7 +293,7 @@ export function type(t: J.SomeType, cf: ContextFormat): Signature {
return []
}

export function arrayType(a: J.ArrayType, cf: ContextFormat): Signature {
export function arrayType(a: J.ArrayType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand Down Expand Up @@ -347,7 +345,7 @@ export function literalType(l: J.LiteralType): Signature {
return s
}

export function referenceType(r: J.ReferenceType, cf: ContextFormat): Signature {
export function referenceType(r: J.ReferenceType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand All @@ -356,26 +354,26 @@ export function referenceType(r: J.ReferenceType, cf: ContextFormat): Signature
}

if (r.typeArguments) {
if (s.length === 0) {
t = new TypeToken()
t.text = r.name
s.push(t)
}

t = new TextToken()
t.text = "<"
s.push(t)

if (r.typeArguments) {
for (const a of r.typeArguments) {
const b = type(a, cf)
cf.formatter.preprocess(b)
s.push(...b)
for (const a of r.typeArguments) {
const b = type(a, cf)
cf.f.preprocess(b)
s.push(...b)

t = new TextToken()
t.text = ", "
s.push(t)
}
s.pop()
} else {
t = new TypeToken()
t.text = "unknown"
t = new TextToken()
t.text = ", "
s.push(t)
}
s.pop()

t = new TextToken()
t.text = ">"
Expand All @@ -399,7 +397,7 @@ export function referenceType(r: J.ReferenceType, cf: ContextFormat): Signature
return s
}

export function reflectionType(r: J.ReflectionType, cf: ContextFormat): Signature {
export function reflectionType(r: J.ReflectionType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand Down Expand Up @@ -460,7 +458,7 @@ export function reflectionType(r: J.ReflectionType, cf: ContextFormat): Signatur
return s
}

export function templateLiteralType(tt: J.TemplateLiteralType, cf: ContextFormat): Signature {
export function templateLiteralType(tt: J.TemplateLiteralType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand All @@ -482,7 +480,7 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: ContextFormat
s.push(t)

const b = type(e, cf)
cf.formatter.preprocess(b)
cf.f.preprocess(b)
s.push(...b)

t = new TextToken()
Expand All @@ -502,7 +500,7 @@ export function templateLiteralType(tt: J.TemplateLiteralType, cf: ContextFormat
return s
}

export function tupleType(tt: J.TupleType, cf: ContextFormat): Signature {
export function tupleType(tt: J.TupleType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand Down Expand Up @@ -531,7 +529,7 @@ export function tupleType(tt: J.TupleType, cf: ContextFormat): Signature {
return s
}

export function unionType(u: J.UnionType, cf: ContextFormat): Signature {
export function unionType(u: J.UnionType, cf: Context): Signature {
const s: Signature = []
let t: Token

Expand Down Expand Up @@ -564,23 +562,29 @@ export function unionType(u: J.UnionType, cf: ContextFormat): Signature {
return s
}

export function fragment(f: Fragment[], cf: ContextFormat): void {
export function fragment(f: Fragment[], cf: Context): void {
for (const e of f) {
const ft = cf.trailOf(e)
if (ft) {
const t = cf.reflectionOf(ft)
if (!t) {
console.log(`Reflection for fragment ${e.name} not found`)
continue
}
if (!isParameterReflection(t)) {
return
}
if (t.type) {
const b = type(t.type, cf)
e.signature.concise.push(...b)
}
} else {
console.log(`Trail for fragment ${e.name} not found`)
}
}
}

export function returns(r: (J.SomeType | undefined), cf: ContextFormat): Signature {
export function returns(cf: Context, r: (J.SomeType | undefined)): Signature {
const s: Signature = []

if (r && r.type) {
Expand All @@ -595,7 +599,7 @@ export function returns(r: (J.SomeType | undefined), cf: ContextFormat): Signatu
return s
}

function reference(t: number, n: string, cf: ContextFormat): Token | Reference {
function reference(t: number, n: string, cf: Context): Token | Reference {
let r: Token | Reference
const id = cf.idOf(t)

Expand All @@ -605,6 +609,7 @@ function reference(t: number, n: string, cf: ContextFormat): Token | Reference {
r.token = new TypeToken()
r.token.text = n
} else {
console.log(`Reflection id not found for ${n} typedoc reference id = ${t}`)
r = new TypeToken()
r.text = n
}
Expand Down
19 changes: 5 additions & 14 deletions packages/typedoc-signature/lib/internal/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import {NoopToken, Reference, type Signature, TextToken, type Token} from "@only
import {IndentToken, NewlineToken} from "./tokens.ts"

export class Formatter {
l: number
n: string

constructor(l = 100, n = "\n") {
this.l = l
this.n = n
}
i = " "
l = 100
n = "\n"

preprocess(s: Signature): void {
let l = 0
Expand All @@ -30,7 +26,7 @@ export class Formatter {

// if the length is less than 100 and there are no newlines in the signature of the child elements
// remove indents and newlines
if (l < 100 && !f) {
if (l < this.l && !f) {
const ts: Signature = []

for (const t of s) {
Expand All @@ -55,12 +51,7 @@ export class Formatter {
t.text = this.n
}
if (e instanceof IndentToken) {
let ci = 0
const i = /_INDENT-(\d+)_/.exec(e.text)
if (i) {
ci = Number.parseInt(i[1])
}
t.text = " ".repeat(ci)
t.text = this.i.repeat(e.indent)
}
ts.push(t)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {IndentToken, NewlineToken} from "./tokens.ts"

export class Context {
export class State {
m = 2
i = 0

constructor(m = 2) {
this.m = m
nt(): NewlineToken {
return new NewlineToken()
}

it(): IndentToken {
return new IndentToken(this.i)
}

in(): void {
Expand All @@ -15,12 +19,4 @@ export class Context {
out(): void {
this.i -= this.m
}

nt(): NewlineToken {
return new NewlineToken()
}

it(i = this.i): IndentToken {
return new IndentToken(i)
}
}
4 changes: 3 additions & 1 deletion packages/typedoc-signature/lib/internal/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export class NewlineToken extends TextToken {

export class IndentToken extends TextToken {
processed = false
text = "_INDENT_"
indent: number
constructor(i = 0) {
super()
this.text = `_INDENT-${i}_`
this.indent = i
}
}
Loading

0 comments on commit 1fd3c7a

Please sign in to comment.