11import type { SnapshotNode } from './snapshot.ts' ;
2- import {
3- buildTextPreview ,
4- extractReadableText ,
5- isLargeTextSurface ,
6- trimText ,
7- } from './text-surface.ts' ;
2+ import { buildTextPreview , describeTextSurface , trimText } from './text-surface.ts' ;
83
94type SnapshotDisplayLine = {
105 node : SnapshotNode ;
@@ -56,10 +51,11 @@ export function formatSnapshotLine(
5651 options : SnapshotLineFormatOptions = { } ,
5752) : string {
5853 const type = normalizedType ?? formatRole ( node . type ?? 'Element' ) ;
59- const label = resolveDisplayLabel ( node , type , options ) ;
54+ const textSurface = describeTextSurface ( node , type ) ;
55+ const label = resolveDisplayLabel ( node , type , options , textSurface ) ;
6056 const indent = ' ' . repeat ( depth ) ;
6157 const ref = node . ref ? `@${ node . ref } ` : '' ;
62- const metadata = buildLineMetadata ( node , type , options ) ;
58+ const metadata = buildLineMetadata ( node , type , options , textSurface ) ;
6359 const metadataText = metadata . map ( ( entry ) => ` [${ entry } ]` ) . join ( '' ) ;
6460 const textPart = label ? ` "${ label } "` : '' ;
6561 if ( hiddenGroup ) {
@@ -188,22 +184,23 @@ function resolveDisplayLabel(
188184 node : SnapshotNode ,
189185 type : string ,
190186 options : SnapshotLineFormatOptions ,
187+ textSurface : { text : string ; isLargeSurface : boolean ; shouldSummarize : boolean } ,
191188) : string {
192189 if ( ! options . summarizeTextSurfaces ) {
193190 return displayLabel ( node , type ) ;
194191 }
195- const text = extractReadableText ( node ) ;
196- if ( ! isLargeTextSurface ( node , type ) || ! shouldSummarizeTextSurface ( text ) ) {
192+ if ( ! textSurface . shouldSummarize ) {
197193 return displayLabel ( node , type ) ;
198194 }
199- const semanticLabel = semanticSurfaceLabel ( node , type , text ) ;
195+ const semanticLabel = semanticSurfaceLabel ( node , type , textSurface . text ) ;
200196 return semanticLabel || displayLabel ( node , type ) ;
201197}
202198
203199function buildLineMetadata (
204200 node : SnapshotNode ,
205201 type : string ,
206202 options : SnapshotLineFormatOptions ,
203+ textSurface : { text : string ; isLargeSurface : boolean ; shouldSummarize : boolean } ,
207204) : string [ ] {
208205 const metadata : string [ ] = [ ] ;
209206 if ( node . enabled === false ) metadata . push ( 'disabled' ) ;
@@ -213,22 +210,14 @@ function buildLineMetadata(
213210 if ( ! options . summarizeTextSurfaces ) {
214211 return metadata ;
215212 }
216- const text = extractReadableText ( node ) ;
217- if ( ! isLargeTextSurface ( node , type ) || ! shouldSummarizeTextSurface ( text ) ) {
213+ if ( ! textSurface . shouldSummarize ) {
218214 return metadata ;
219215 }
220- metadata . push ( `preview:"${ escapePreviewText ( buildTextPreview ( text ) ) } "` ) ;
216+ metadata . push ( `preview:"${ escapePreviewText ( buildTextPreview ( textSurface . text ) ) } "` ) ;
221217 metadata . push ( 'truncated' ) ;
222218 return uniqueMetadata ( metadata ) ;
223219}
224220
225- function shouldSummarizeTextSurface ( text : string ) : boolean {
226- if ( ! text ) {
227- return false ;
228- }
229- return text . length > 80 || / [ \r \n ] / . test ( text ) ;
230- }
231-
232221function semanticSurfaceLabel ( node : SnapshotNode , type : string , text : string ) : string {
233222 const label = trimText ( node . label ) ;
234223 if ( label && label !== text ) {
0 commit comments