@@ -13,29 +13,30 @@ type TextRangeWithKind struct {
13
13
Kind ast.Kind
14
14
}
15
15
16
- func NewTextRangeWithKind (pos int , end int , kind ast.Kind ) * TextRangeWithKind {
17
- return & TextRangeWithKind {
16
+ func NewTextRangeWithKind (pos int , end int , kind ast.Kind ) TextRangeWithKind {
17
+ return TextRangeWithKind {
18
18
Loc : core .NewTextRange (pos , end ),
19
19
Kind : kind ,
20
20
}
21
21
}
22
22
23
23
type tokenInfo struct {
24
- leadingTrivia []* TextRangeWithKind
25
- token * TextRangeWithKind
26
- trailingTrivia []* TextRangeWithKind
24
+ leadingTrivia []TextRangeWithKind
25
+ token TextRangeWithKind
26
+ trailingTrivia []TextRangeWithKind
27
27
}
28
28
29
29
type formattingScanner struct {
30
- s * scanner.Scanner
31
- startPos int
32
- endPos int
33
- savedPos int
34
- lastTokenInfo * tokenInfo
35
- lastScanAction scanAction
36
- leadingTrivia []* TextRangeWithKind
37
- trailingTrivia []* TextRangeWithKind
38
- wasNewLine bool
30
+ s * scanner.Scanner
31
+ startPos int
32
+ endPos int
33
+ savedPos int
34
+ hasLastTokenInfo bool
35
+ lastTokenInfo tokenInfo
36
+ lastScanAction scanAction
37
+ leadingTrivia []TextRangeWithKind
38
+ trailingTrivia []TextRangeWithKind
39
+ wasNewLine bool
39
40
}
40
41
41
42
func newFormattingScanner (text string , languageVariant core.LanguageVariant , startPos int , endPos int , worker * formatSpanWorker ) []core.TextChange {
@@ -54,14 +55,14 @@ func newFormattingScanner(text string, languageVariant core.LanguageVariant, sta
54
55
55
56
res := worker .execute (fmtScn )
56
57
57
- fmtScn .lastTokenInfo = nil
58
+ fmtScn .hasLastTokenInfo = false
58
59
scan .Reset ()
59
60
60
61
return res
61
62
}
62
63
63
64
func (s * formattingScanner ) advance () {
64
- s .lastTokenInfo = nil
65
+ s .hasLastTokenInfo = false
65
66
isStarted := s .s .TokenFullStart () != s .startPos
66
67
67
68
if isStarted {
@@ -124,7 +125,7 @@ func (s *formattingScanner) shouldRescanJsxText(node *ast.Node) bool {
124
125
if ast .IsJsxText (node ) {
125
126
return true
126
127
}
127
- if ! ast .IsJsxElement (node ) || s .lastTokenInfo == nil {
128
+ if ! ast .IsJsxElement (node ) || s .hasLastTokenInfo == false {
128
129
return false
129
130
}
130
131
@@ -160,14 +161,14 @@ const (
160
161
actionRescanJsxAttributeValue
161
162
)
162
163
163
- func fixTokenKind (tokenInfo * tokenInfo , container * ast.Node ) * tokenInfo {
164
+ func fixTokenKind (tokenInfo tokenInfo , container * ast.Node ) tokenInfo {
164
165
if ast .IsTokenKind (container .Kind ) && tokenInfo .token .Kind != container .Kind {
165
166
tokenInfo .token .Kind = container .Kind
166
167
}
167
168
return tokenInfo
168
169
}
169
170
170
- func (s * formattingScanner ) readTokenInfo (n * ast.Node ) * tokenInfo {
171
+ func (s * formattingScanner ) readTokenInfo (n * ast.Node ) tokenInfo {
171
172
// Debug.assert(isOnToken()); // !!!
172
173
173
174
// normally scanner returns the smallest available token
@@ -190,14 +191,15 @@ func (s *formattingScanner) readTokenInfo(n *ast.Node) *tokenInfo {
190
191
expectedScanAction = actionScan
191
192
}
192
193
193
- if s .lastTokenInfo != nil && expectedScanAction == s .lastScanAction {
194
+ if s .hasLastTokenInfo && expectedScanAction == s .lastScanAction {
194
195
// readTokenInfo was called before with the same expected scan action.
195
196
// No need to re-scan text, return existing 'lastTokenInfo'
196
197
// it is ok to call fixTokenKind here since it does not affect
197
198
// what portion of text is consumed. In contrast rescanning can change it,
198
199
// i.e. for '>=' when originally scanner eats just one character
199
200
// and rescanning forces it to consume more.
200
- return fixTokenKind (s .lastTokenInfo , n )
201
+ s .lastTokenInfo = fixTokenKind (s .lastTokenInfo , n )
202
+ return s .lastTokenInfo
201
203
}
202
204
203
205
if s .s .TokenFullStart () != s .savedPos {
@@ -237,13 +239,15 @@ func (s *formattingScanner) readTokenInfo(n *ast.Node) *tokenInfo {
237
239
}
238
240
}
239
241
240
- s .lastTokenInfo = & tokenInfo {
242
+ s .hasLastTokenInfo = true
243
+ s .lastTokenInfo = tokenInfo {
241
244
leadingTrivia : slices .Clone (s .leadingTrivia ),
242
245
token : token ,
243
246
trailingTrivia : slices .Clone (s .trailingTrivia ),
244
247
}
248
+ s .lastTokenInfo = fixTokenKind (s .lastTokenInfo , n )
245
249
246
- return fixTokenKind ( s .lastTokenInfo , n )
250
+ return s .lastTokenInfo
247
251
}
248
252
249
253
func (s * formattingScanner ) getNextToken (n * ast.Node , expectedScanAction scanAction ) ast.Kind {
@@ -287,7 +291,7 @@ func (s *formattingScanner) getNextToken(n *ast.Node, expectedScanAction scanAct
287
291
return token
288
292
}
289
293
290
- func (s * formattingScanner ) readEOFTokenRange () * TextRangeWithKind {
294
+ func (s * formattingScanner ) readEOFTokenRange () TextRangeWithKind {
291
295
// Debug.assert(isOnEOF()); // !!!
292
296
return NewTextRangeWithKind (
293
297
s .s .TokenFullStart (),
@@ -298,15 +302,15 @@ func (s *formattingScanner) readEOFTokenRange() *TextRangeWithKind {
298
302
299
303
func (s * formattingScanner ) isOnToken () bool {
300
304
current := s .s .Token ()
301
- if s .lastTokenInfo != nil {
305
+ if s .hasLastTokenInfo {
302
306
current = s .lastTokenInfo .token .Kind
303
307
}
304
308
return current != ast .KindEndOfFile && ! ast .IsTrivia (current )
305
309
}
306
310
307
311
func (s * formattingScanner ) isOnEOF () bool {
308
312
current := s .s .Token ()
309
- if s .lastTokenInfo != nil {
313
+ if s .hasLastTokenInfo {
310
314
current = s .lastTokenInfo .token .Kind
311
315
}
312
316
return current == ast .KindEndOfFile
@@ -316,7 +320,7 @@ func (s *formattingScanner) skipToEndOf(r *core.TextRange) {
316
320
s .s .ResetTokenState (r .End ())
317
321
s .savedPos = s .s .TokenFullStart ()
318
322
s .lastScanAction = actionScan
319
- s .lastTokenInfo = nil
323
+ s .hasLastTokenInfo = false
320
324
s .wasNewLine = false
321
325
s .leadingTrivia = nil
322
326
s .trailingTrivia = nil
@@ -326,13 +330,13 @@ func (s *formattingScanner) skipToStartOf(r *core.TextRange) {
326
330
s .s .ResetTokenState (r .Pos ())
327
331
s .savedPos = s .s .TokenFullStart ()
328
332
s .lastScanAction = actionScan
329
- s .lastTokenInfo = nil
333
+ s .hasLastTokenInfo = false
330
334
s .wasNewLine = false
331
335
s .leadingTrivia = nil
332
336
s .trailingTrivia = nil
333
337
}
334
338
335
- func (s * formattingScanner ) getCurrentLeadingTrivia () []* TextRangeWithKind {
339
+ func (s * formattingScanner ) getCurrentLeadingTrivia () []TextRangeWithKind {
336
340
return s .leadingTrivia
337
341
}
338
342
@@ -341,7 +345,7 @@ func (s *formattingScanner) lastTrailingTriviaWasNewLine() bool {
341
345
}
342
346
343
347
func (s * formattingScanner ) getTokenFullStart () int {
344
- if s .lastTokenInfo != nil {
348
+ if s .hasLastTokenInfo {
345
349
return s .lastTokenInfo .token .Loc .Pos ()
346
350
}
347
351
return s .s .TokenFullStart ()
0 commit comments