fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 51 lines 1.5 kB view raw
1import type { AnalysisContext, Node } from '@hey-api/codegen-core'; 2 3import type { py } from '../../ts-python'; 4import type { BaseCtor, MixinCtor } from './types'; 5 6export interface LayoutMethods extends Node { 7 /** Computes whether output should be multiline based on layout setting and element count. */ 8 $multiline(count: number): boolean; 9 /** Sets automatic line output with optional threshold (default: 3). */ 10 auto(threshold?: number): this; 11 /** Sets single line output. */ 12 inline(): this; 13 /** Sets multi line output. */ 14 pretty(): this; 15} 16 17export function LayoutMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 18 abstract class Layout extends Base { 19 protected static readonly DEFAULT_THRESHOLD = 3; 20 protected layout: boolean | number | undefined; 21 22 override analyze(ctx: AnalysisContext): void { 23 super.analyze(ctx); 24 } 25 26 protected auto(threshold: number = Layout.DEFAULT_THRESHOLD): this { 27 this.layout = threshold; 28 return this; 29 } 30 31 protected inline(): this { 32 this.layout = false; 33 return this; 34 } 35 36 protected pretty(): this { 37 this.layout = true; 38 return this; 39 } 40 41 protected $multiline(count: number): boolean { 42 if (this.layout === undefined) { 43 this.layout = Layout.DEFAULT_THRESHOLD; 44 } 45 if (count === 0) return false; 46 return typeof this.layout === 'number' ? count >= this.layout : this.layout; 47 } 48 } 49 50 return Layout as unknown as MixinCtor<TBase, LayoutMethods>; 51}