@@ -28,7 +28,6 @@ type NLPToken struct {
2828type Sequence struct {
2929 Definition `mapstructure:",squash"`
3030 Tokens []NLPToken
31- history []int
3231 Ignorecase bool
3332 needsTagging bool
3433}
@@ -134,7 +133,7 @@ func tokensMatch(token NLPToken, word tag.Token) bool {
134133 return true
135134}
136135
137- func sequenceMatches (idx int , chk Sequence , target NLPToken , words []tag.Token ) ([]string , int ) {
136+ func sequenceMatches (idx int , chk Sequence , target NLPToken , words []tag.Token , history [] int ) ([]string , int ) {
138137 var text []string
139138
140139 toks := chk .Tokens
@@ -144,7 +143,7 @@ func sequenceMatches(idx int, chk Sequence, target NLPToken, words []tag.Token)
144143 index := 0
145144
146145 for jdx , tok := range words {
147- if tokensMatch (target , tok ) && ! core .IntInSlice (jdx , chk . history ) {
146+ if tokensMatch (target , tok ) && ! core .IntInSlice (jdx , history ) {
148147 index = jdx
149148 // We've found our context.
150149 //
@@ -213,21 +212,42 @@ func sequenceMatches(idx int, chk Sequence, target NLPToken, words []tag.Token)
213212}
214213
215214func stepsToString (steps []string ) string {
216- s := ""
217- for _ , step := range steps {
218- if strings .HasPrefix (step , "'" ) {
219- s += step
220- } else {
221- s += " " + step
215+ var sb strings.Builder
216+
217+ for i , step := range steps {
218+ switch {
219+ case step == "." || step == "," || step == ":" || step == ";" || step == "!" || step == "?" || step == "'" || step == `"` || step == ")" :
220+ // No space before punctuation or closing parenthesis
221+ sb .WriteString (step )
222+ case step == "(" :
223+ // No space before or after an opening parenthesis
224+ if i > 0 && sb .Len () > 0 {
225+ lastChar := sb .String ()[sb .Len ()- 1 ]
226+ if lastChar != ' ' {
227+ sb .WriteString (" " )
228+ }
229+ }
230+ sb .WriteString (step )
231+ case strings .HasPrefix (step , "'" ):
232+ // If the step starts with an apostrophe, attach it without space
233+ sb .WriteString (step )
234+ default :
235+ // Otherwise, add space before the word
236+ if sb .Len () > 0 {
237+ sb .WriteString (" " )
238+ }
239+ sb .WriteString (step )
222240 }
223241 }
224- return strings .Trim (s , " " )
242+
243+ return strings .TrimSpace (sb .String ())
225244}
226245
227246// Run looks for the user-defined sequence of tokens.
228247func (s Sequence ) Run (blk nlp.Block , f * core.File , _ * core.Config ) ([]core.Alert , error ) {
229248 var alerts []core.Alert
230249 var offset []string
250+ var history []int
231251
232252 // This is *always* sentence-scoped.
233253 words := nlp .TextToTokens (blk .Text , & f .NLP )
@@ -238,8 +258,8 @@ func (s Sequence) Run(blk nlp.Block, f *core.File, _ *core.Config) ([]core.Alert
238258 // We're looking for our "anchor" ...
239259 for _ , loc := range tok .re .FindAllStringIndex (txt , - 1 ) {
240260 // These are all possible violations in `txt`:
241- steps , index := sequenceMatches (idx , s , tok , words )
242- s . history = append (s . history , index ) //nolint:staticcheck
261+ steps , index := sequenceMatches (idx , s , tok , words , history )
262+ history = append (history , index )
243263
244264 if len (steps ) > 0 {
245265 seq := stepsToString (steps )
0 commit comments