@@ -8,28 +8,30 @@ import (
88 "github.com/cluttercode/clutter/pkg/clutter/clutterindex"
99)
1010
11+ type params struct { next , prev , cycle , first , last bool }
12+
1113func ResolveList (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index ) ([]* clutterindex.Entry , error ) {
12- return resolve (z , what , index , false , false )
14+ return resolve (z , what , index , params {} )
1315}
1416
15- func ResolveNext (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index ) ([]* clutterindex.Entry , error ) {
16- return resolve (z , what , index , true , false )
17+ func ResolveNext (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index , cycle bool ) ([]* clutterindex.Entry , error ) {
18+ return resolve (z , what , index , params { next : true , cycle : cycle } )
1719}
1820
19- func ResolvePrev (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index ) ([]* clutterindex.Entry , error ) {
20- return resolve (z , what , index , false , true )
21+ func ResolvePrev (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index , cycle bool ) ([]* clutterindex.Entry , error ) {
22+ return resolve (z , what , index , params { prev : true , cycle : cycle } )
2123}
2224
23- func resolve (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index , next , prev bool ) ([]* clutterindex.Entry , error ) {
24- if next && prev {
25+ func resolve (z * zap.SugaredLogger , what * clutterindex.Entry , index * clutterindex.Index , p params ) ([]* clutterindex.Entry , error ) {
26+ if p . next && p . prev {
2527 z .Panic ("prev and next are mutually exclusive" )
2628 }
2729
2830 matcher := func (ent * clutterindex.Entry ) bool { return what .Name == ent .Name && ent .IsReferredBy (what ) }
2931
3032 if _ , search := what .IsSearch (); search {
31- if prev || next {
32- prev , next = false , false
33+ if p . prev || p . next {
34+ p . prev , p . next = false , false
3335 z .Warn ("--next and --prev are ignored when resolving a search tag" )
3436 }
3537
@@ -56,7 +58,7 @@ func resolve(z *zap.SugaredLogger, what *clutterindex.Entry, index *clutterindex
5658 return nil
5759 }
5860
59- if prev {
61+ if p . prev {
6062 if ent .Loc == what .Loc {
6163 if hold == nil {
6264 z .Debugw ("found what, but nothing held" )
@@ -65,7 +67,7 @@ func resolve(z *zap.SugaredLogger, what *clutterindex.Entry, index *clutterindex
6567
6668 z .Debugw ("found what, emit held" , "ent" , hold )
6769
68- fmt . Println ( hold . String () )
70+ ents = append ( ents , hold )
6971
7072 return clutterindex .ErrStop
7173 }
@@ -76,7 +78,7 @@ func resolve(z *zap.SugaredLogger, what *clutterindex.Entry, index *clutterindex
7678 return nil
7779 }
7880
79- if next {
81+ if p . next {
8082 if hold == nil {
8183 if ent .Loc == what .Loc {
8284 hold = ent
@@ -95,11 +97,27 @@ func resolve(z *zap.SugaredLogger, what *clutterindex.Entry, index *clutterindex
9597
9698 ents = append (ents , ent )
9799
100+ if p .first {
101+ return clutterindex .ErrStop
102+ }
103+
98104 return nil
99105 },
100106 ); err != nil {
101107 return nil , fmt .Errorf ("filter: %w" , err )
102108 }
103109
110+ if p .cycle && len (ents ) == 0 {
111+ if p .next {
112+ return resolve (z , what , index , params {first : true })
113+ } else if p .prev {
114+ return resolve (z , what , index , params {last : true })
115+ }
116+ }
117+
118+ if p .last && len (ents ) > 0 {
119+ return []* clutterindex.Entry {ents [len (ents )- 1 ]}, nil
120+ }
121+
104122 return ents , nil
105123}
0 commit comments