@@ -24,6 +24,14 @@ export enum SepticTokenType {
2424 eof = "eof" ,
2525}
2626
27+ const indentsObjectDeclaration = 2 ;
28+ const startObjectName = 17 ;
29+ const spacesBetweenValues = 2 ;
30+ const spacesBetweenIntValues = 6 ;
31+ const indentsAttributeValuesStart = 17 ;
32+ const maxNumberAttrValuesPerLine = 5 ;
33+ const indentsAttributesDelimiter = 14 ;
34+
2735export type SepticToken = IToken < SepticTokenType > ;
2836
2937export class SepticBase {
@@ -185,6 +193,28 @@ export class SepticObject extends SepticBase {
185193 }
186194 return { algExpr : expr , positionsMap : positionsMap } ;
187195 }
196+
197+ toString ( ) : string {
198+ const identifierStr = this . identifier ? ` ${ this . identifier . name } ` : "" ;
199+ const objectTypeFormatted =
200+ " " . repeat ( indentsObjectDeclaration ) + this . type + ":" ;
201+ const indentsName = Math . max (
202+ startObjectName - objectTypeFormatted . length - 1 ,
203+ 2 ,
204+ ) ;
205+ const objectDeclartionFormatted =
206+ objectTypeFormatted + " " . repeat ( indentsName ) + identifierStr ;
207+ return (
208+ objectDeclartionFormatted +
209+ "\n" +
210+ this . attributes
211+ . map ( ( attr ) => {
212+ return attr . toString ( ) ;
213+ } )
214+ . join ( "\n" ) +
215+ "\n"
216+ ) ;
217+ }
188218}
189219
190220export class SepticAttribute extends SepticBase {
@@ -201,6 +231,10 @@ export class SepticAttribute extends SepticBase {
201231 this . values . push ( value ) ;
202232 }
203233
234+ setValues ( values : SepticAttributeValue [ ] ) {
235+ this . values = values ;
236+ }
237+
204238 updateEnd ( ) : void {
205239 if ( this . values . length ) {
206240 this . end = this . values [ this . values . length - 1 ] ! . end ;
@@ -261,6 +295,62 @@ export class SepticAttribute extends SepticBase {
261295 isKey ( key : string ) {
262296 return key === this . key ;
263297 }
298+
299+ toString ( ) : string {
300+ const indentsKey = Math . max (
301+ indentsAttributesDelimiter - this . key . length ,
302+ 0 ,
303+ ) ;
304+ const attrDefFormatted = " " . repeat ( indentsKey ) + this . key + "= " ;
305+ if ( this . getType ( ) === SepticValueTypes . stringList ) {
306+ return (
307+ attrDefFormatted +
308+ formatStringList ( this . values . map ( ( val ) => val . value ) )
309+ ) ;
310+ } else if ( this . getType ( ) === SepticValueTypes . numericList ) {
311+ return (
312+ attrDefFormatted +
313+ formatNumericList ( this . values . map ( ( val ) => val . value ) )
314+ ) ;
315+ } else if ( this . getValues ( ) . length > 1 ) {
316+ return (
317+ attrDefFormatted +
318+ formatList ( this . values . map ( ( val ) => val . value ) )
319+ ) ;
320+ } else {
321+ return attrDefFormatted + ( this . values [ 0 ] ?. value || "" ) ;
322+ }
323+ }
324+ }
325+
326+ function formatStringList ( values : string [ ] ) : string {
327+ let formatted = `${ values . length - 1 } ` ;
328+ values . slice ( 1 ) . forEach ( ( val , ind ) => {
329+ if ( ind % maxNumberAttrValuesPerLine === 0 ) {
330+ formatted += "\n" + " " . repeat ( indentsAttributeValuesStart ) + val ;
331+ } else {
332+ formatted += " " . repeat ( spacesBetweenValues ) + val ;
333+ }
334+ } ) ;
335+ return formatted ;
336+ }
337+
338+ function formatNumericList ( values : string [ ] ) : string {
339+ let formatted = `${ values . length - 1 } ` ;
340+ values . slice ( 1 ) . forEach ( ( val , ind ) => {
341+ const spaces = Math . max ( spacesBetweenIntValues - val . length , 1 ) ;
342+ formatted +=
343+ ind === 0 ? " " . repeat ( spaces - 1 ) + val : " " . repeat ( spaces ) + val ;
344+ } ) ;
345+ return formatted ;
346+ }
347+
348+ function formatList ( values : string [ ] ) : string {
349+ let formatted = `${ values . length - 1 } ` ;
350+ values . slice ( 1 ) . forEach ( ( val ) => {
351+ formatted += " " . repeat ( spacesBetweenValues ) + val ;
352+ } ) ;
353+ return formatted ;
264354}
265355
266356export class SepticIdentifier extends SepticBase {
0 commit comments