@@ -32,6 +32,10 @@ type lineGroup struct {
3232
3333 // The actual content of the lineGroup.
3434 lineGroupContent
35+
36+ // Track which methods are used during sorting so we can filter debugging
37+ // output to just the parts that are relevant.
38+ access accessRecorder
3539}
3640
3741var compareLineGroups = comparingFunc ((* lineGroup ).commentOnly , falseFirst ()).
@@ -50,6 +54,13 @@ type lineGroupContent struct {
5054 lines []string
5155}
5256
57+ type accessRecorder struct {
58+ commentOnly bool
59+ regexTokens []regexTokenAccessRecorder
60+ joinedLines bool
61+ joinedComment bool
62+ }
63+
5364// groupLines splits lines into one or more lineGroups based on the provided options.
5465func groupLines (lines []string , metadata blockMetadata ) []* lineGroup {
5566 var groups []* lineGroup
@@ -113,7 +124,6 @@ func groupLines(lines []string, metadata blockMetadata) []*lineGroup {
113124 commentRange = indexRange {}
114125 lineRange = indexRange {}
115126 block = codeBlock {}
116- log .Printf ("%#v" , groups [len (groups )- 1 ])
117127 }
118128 for i , l := range lines {
119129 if metadata .opts .Block && ! lineRange .empty () && block .expectsContinuation () {
@@ -331,6 +341,7 @@ func findQuote(s string, i int) string {
331341}
332342
333343func (lg * lineGroup ) append (s string ) {
344+ lg .access = accessRecorder {}
334345 lg .lines [len (lg .lines )- 1 ] = lg .lines [len (lg .lines )- 1 ] + s
335346}
336347
@@ -339,42 +350,53 @@ func (lg *lineGroup) hasSuffix(s string) bool {
339350}
340351
341352func (lg * lineGroup ) trimSuffix (s string ) {
353+ lg .access = accessRecorder {}
342354 lg .lines [len (lg .lines )- 1 ] = strings .TrimSuffix (lg .lines [len (lg .lines )- 1 ], s )
343355}
344356
345357func (lg * lineGroup ) commentOnly () bool {
358+ lg .access .commentOnly = true
346359 return len (lg .lines ) == 0
347360}
348361
349362func (lg * lineGroup ) regexTokens () []regexToken {
350363 // TODO: jfaer - Should we match regexes on the original content?
351- regexMatches := lg .opts .matchRegexes (lg .joinedLines ())
364+ regexMatches := lg .opts .matchRegexes (lg .internalJoinedLines ())
352365 ret := make ([]regexToken , len (regexMatches ))
366+ if lg .access .regexTokens == nil {
367+ lg .access .regexTokens = make ([]regexTokenAccessRecorder , len (regexMatches ))
368+ }
353369 for i , match := range regexMatches {
354370 if match == nil {
355371 // Regex did not match.
356372 continue
357373 }
358374
359375 ret [i ] = make (regexToken , len (match ))
376+ if lg .access .regexTokens [i ] == nil {
377+ lg .access .regexTokens [i ] = make (regexTokenAccessRecorder , len (match ))
378+ }
360379 for j , s := range match {
361- prefixOrder := lg .prefixOrder
380+ order := lg .prefixOrder
362381 if j != 0 {
363382 // Only try to match PrefixOrder on the first capture group in a regex.
364383 // TODO: jfaer - Should this just be the first capture group in the first regex match?
365- prefixOrder = nil
384+ order = func () * prefixOrder { return nil }
366385 }
367386 ret [i ][j ] = & captureGroupToken {
368387 opts : & lg .opts ,
369- prefixOrder : prefixOrder ,
388+ prefixOrder : order ,
370389 raw : s ,
390+ access : & lg .access .regexTokens [i ][j ],
371391 }
372392 }
373393 }
374394 return ret
375395}
376396
377- func (lg * lineGroup ) joinedLines () string {
397+ // internalJoinedLines calculates the same thing as joinedLines, except it
398+ // doesn't record that it was used in the accessRecorder.
399+ func (lg * lineGroup ) internalJoinedLines () string {
378400 if len (lg .lines ) == 0 {
379401 return ""
380402 }
@@ -394,23 +416,58 @@ func (lg *lineGroup) joinedLines() string {
394416 return s .String ()
395417}
396418
419+ func (lg * lineGroup ) joinedLines () string {
420+ lg .access .joinedLines = true
421+ return lg .internalJoinedLines ()
422+ }
423+
397424func (lg * lineGroup ) joinedComment () string {
425+ lg .access .joinedComment = true
398426 if len (lg .comment ) == 0 {
399427 return ""
400428 }
401429 return strings .Join (lg .comment , "\n " )
402430}
403431
404- func (lg * lineGroup ) GoString () string {
405- var comment strings.Builder
406- for _ , c := range lg .comment {
407- comment .WriteString (fmt .Sprintf (" %#v\n " , c ))
432+ func (lg * lineGroup ) DebugString () string {
433+ var s strings.Builder
434+ s .WriteString ("LineGroup{\n " )
435+ if len (lg .comment ) > 0 {
436+ s .WriteString ("comment=\n " )
437+ for _ , c := range lg .comment {
438+ fmt .Fprintf (& s , " %#v\n " , c )
439+ }
408440 }
409- var lines strings.Builder
410- for _ , l := range lg .lines {
411- lines .WriteString (fmt .Sprintf (" %#v\n " , l ))
441+ if len (lg .lines ) > 0 {
442+ s .WriteString ("lines=\n " )
443+ for _ , l := range lg .lines {
444+ fmt .Fprintf (& s , " %#v\n " , l )
445+ }
412446 }
413- return fmt .Sprintf ("LineGroup{\n comment=\n %slines=\n %s}" , comment .String (), lines .String ())
447+ if lg .access .commentOnly {
448+ fmt .Fprintf (& s , "commentOnly=%t\n " , lg .commentOnly ())
449+ }
450+ if lg .access .regexTokens != nil {
451+ for i , regex := range lg .regexTokens () {
452+ if regex .wasUsed () {
453+ fmt .Fprintf (& s , "regex[%d]=%s\n " , i , regex .DebugString ())
454+ }
455+ }
456+ }
457+ if lg .access .joinedLines {
458+ if len (lg .lines ) > 1 {
459+ // Only print the joinedLines when they're meaningfully different from the
460+ // raw lines above.
461+ fmt .Fprintf (& s , "joinedLines=%#v\n " , lg .joinedLines ())
462+ } else if ! lg .access .joinedComment {
463+ s .WriteString ("linesTiebreaker=true\n " )
464+ }
465+ }
466+ if lg .access .joinedComment {
467+ s .WriteString ("commentTiebreaker=true\n " )
468+ }
469+ s .WriteString ("}" )
470+ return s .String ()
414471}
415472
416473func (lg * lineGroup ) allLines () []string {
@@ -426,22 +483,66 @@ func (lg *lineGroup) String() string {
426483
427484type regexToken []* captureGroupToken
428485
486+ type regexTokenAccessRecorder []captureGroupTokenAccessRecorder
487+
488+ func (t regexToken ) wasUsed () bool {
489+ if t == nil {
490+ // Report that the regex didn't match.
491+ return true
492+ }
493+ for _ , cg := range t {
494+ if cg .wasUsed () {
495+ return true
496+ }
497+ }
498+ return false
499+ }
500+
501+ func (t regexToken ) DebugString () string {
502+ if t == nil {
503+ return "<did not match>"
504+ }
505+
506+ captureGroups := make ([]string , len (t ))
507+ for i , cg := range t {
508+ if cg .wasUsed () {
509+ captureGroups [i ] = cg .DebugString ()
510+ } else {
511+ captureGroups [i ] = "<unused>"
512+ }
513+ }
514+
515+ if len (captureGroups ) == 1 {
516+ return captureGroups [0 ]
517+ }
518+ return fmt .Sprintf ("%v" , captureGroups )
519+ }
520+
429521type captureGroupToken struct {
430522 opts * blockOptions
431523 prefixOrder func () * prefixOrder
432524
433525 raw string
526+
527+ access * captureGroupTokenAccessRecorder
528+ }
529+
530+ type captureGroupTokenAccessRecorder struct {
531+ prefix bool
532+ transform bool
434533}
435534
436535func (t * captureGroupToken ) prefix () orderedPrefix {
437- if t .prefixOrder == nil {
536+ ord := t .prefixOrder ()
537+ if ord == nil {
438538 return orderedPrefix {}
439539 }
440-
441- return t . prefixOrder () .match (t .raw )
540+ t . access . prefix = true
541+ return ord .match (t .raw )
442542}
443543
444544func (t * captureGroupToken ) transform () numericTokens {
545+ t .access .transform = true
445546 // Combinations of switches (for example, case-insensitive and numeric
446547 // ordering) which must be applied to create a single comparison key,
447548 // otherwise a sub-ordering can preempt a total ordering:
@@ -466,3 +567,28 @@ func (t *captureGroupToken) transform() numericTokens {
466567 }
467568 return t .opts .maybeParseNumeric (s )
468569}
570+
571+ func (t captureGroupToken ) wasUsed () bool {
572+ return t .access .prefix || t .access .transform
573+ }
574+
575+ func (t captureGroupToken ) DebugString () string {
576+ var s []string
577+ if t .access .prefix {
578+ s = append (s , fmt .Sprintf ("prefix:%q" , t .prefix ().prefix ))
579+ }
580+ if t .access .transform {
581+ var tokens strings.Builder
582+ if len (s ) > 0 {
583+ tokens .WriteString ("tokens:" )
584+ }
585+ fmt .Fprintf (& tokens , "%s" , t .transform ().DebugString ())
586+ s = append (s , tokens .String ())
587+ }
588+
589+ ret := strings .Join (s , " " )
590+ if len (s ) > 1 {
591+ ret = "[" + ret + "]"
592+ }
593+ return ret
594+ }
0 commit comments