@@ -339,6 +339,112 @@ export class SNBTTag {
339339 return this . value ? 'true' : 'false'
340340 }
341341 }
342+ toHighlightString (
343+ highlighters : Record < string , ( string ) => string> ,
344+ exclude_type ?: true
345+ ) {
346+ function NO_SYNTAX_HIGHLIGHTER ( s ) {
347+ return s
348+ }
349+ function getFormatter ( name ) {
350+ return name in highlighters
351+ ? highlighters [ name ]
352+ : NO_SYNTAX_HIGHLIGHTER
353+ }
354+ const STRING_FORMATTER = getFormatter ( 'string' )
355+ const NUMBER_FORMATTER = getFormatter ( 'number' )
356+ const BOOLEAN_FORMATTER = getFormatter ( 'boolean' )
357+ const BRACKET_FORMATTER = getFormatter ( 'brackets' )
358+ const ARRAY_FORMATTER = getFormatter ( 'list' )
359+ const TYPE_BYTE = getFormatter ( 'type_byte' )
360+ const TYPE_SHORT = getFormatter ( 'type_short' )
361+ const TYPE_INT = getFormatter ( 'type_int_list' )
362+ switch ( this . type ) {
363+ case SNBTTagType . END :
364+ throw new Error ( 'Cannot convert END tag to string' )
365+ case SNBTTagType . BYTE :
366+ return (
367+ NUMBER_FORMATTER ( this . value . toString ( ) ) +
368+ TYPE_BYTE ( exclude_type ? '' : 'b' )
369+ )
370+ case SNBTTagType . SHORT :
371+ return (
372+ NUMBER_FORMATTER ( this . value . toString ( ) ) +
373+ TYPE_SHORT ( exclude_type ? '' : 's' )
374+ )
375+ case SNBTTagType . INT :
376+ return NUMBER_FORMATTER ( this . value . toString ( ) )
377+ case SNBTTagType . LONG :
378+ return NUMBER_FORMATTER ( this . value . toString ( ) )
379+ case SNBTTagType . FLOAT :
380+ return NUMBER_FORMATTER ( this . value . toString ( ) )
381+ case SNBTTagType . DOUBLE :
382+ return NUMBER_FORMATTER (
383+ this . value . toString ( ) + Number . isInteger ( this . value )
384+ ? '.0'
385+ : ''
386+ )
387+ case SNBTTagType . STRING :
388+ return STRING_FORMATTER ( SNBTUtil . stringify ( this . value ) )
389+ case SNBTTagType . COMPOUND :
390+ let entries = Object . entries ( this . value as Record < any , SNBTTag > )
391+ if ( ! entries . length ) return BRACKET_FORMATTER ( `{}` )
392+ return (
393+ BRACKET_FORMATTER ( '{' ) +
394+ '\n' +
395+ entries
396+ . map ( ( [ key , value ] ) =>
397+ `${ key } :${ value . toHighlightString ( highlighters ) } `
398+ . split ( '\n' )
399+ . map ( ( _ ) => ` ${ _ } ` )
400+ . join ( '\n' )
401+ )
402+ . join ( ',\n' ) +
403+ '\n' +
404+ BRACKET_FORMATTER ( '}' )
405+ )
406+ case SNBTTagType . INT_ARRAY : {
407+ let items = this . value . map ( ( item ) =>
408+ item . toHighlightString ( highlighters )
409+ )
410+ let combined = items . join ( ',' )
411+ let isIndented = items . indexOf ( '\n' ) > - 1
412+ if ( combined . length > 16 ) isIndented = true
413+ if ( isIndented ) {
414+ return `${ ARRAY_FORMATTER ( '[' ) } ${ TYPE_INT ( 'I' ) } ;\n${ items
415+ . map ( ( _ ) => ` ${ _ } ` )
416+ . join ( ',\n' ) } \n${ ARRAY_FORMATTER ( ']' ) } `
417+ }
418+ return (
419+ `${ ARRAY_FORMATTER ( '[' ) } ${ TYPE_INT ( 'I' ) } ;` +
420+ combined +
421+ ARRAY_FORMATTER ( ']' )
422+ )
423+ }
424+ case SNBTTagType . BYTE_ARRAY :
425+ case SNBTTagType . LIST :
426+ case SNBTTagType . LONG_ARRAY : {
427+ let items = this . value . map ( ( item ) =>
428+ item . toHighlightString ( highlighters )
429+ )
430+ let combined = items . join ( ', ' )
431+ let isIndented = combined . indexOf ( '\n' ) > - 1
432+ if ( combined . length > 16 ) isIndented = true
433+ if ( isIndented ) {
434+ return `${ ARRAY_FORMATTER ( '[' ) } \n${ items
435+ . map ( ( _ ) =>
436+ _ . split ( '\n' )
437+ . map ( ( i ) => ` ${ i } ` )
438+ . join ( '\n' )
439+ )
440+ . join ( ',\n' ) } \n${ ARRAY_FORMATTER ( ']' ) } `
441+ }
442+ return ARRAY_FORMATTER ( '[' ) + combined + ARRAY_FORMATTER ( ']' )
443+ }
444+ case SNBTTagType . BOOLEAN :
445+ return BOOLEAN_FORMATTER ( this . value ? 'true' : 'false' )
446+ }
447+ }
342448 // clone the tag taking into account the type
343449 // the LIST, INT_ARRAY, BYTE_ARRAY, and LONG_ARRAY clone each item into a new list,
344450 // the COMPOUND copies each entry and makes a new compound
0 commit comments