Skip to content

Commit caa2977

Browse files
authored
Don't use file GetOrCreateToken for change tracker (#2320)
1 parent b0fed9f commit caa2977

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

internal/ast/ast.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10940,6 +10940,8 @@ func (node *SourceFile) BindOnce(bind func()) {
1094010940
})
1094110941
}
1094210942

10943+
// Gets a token from the file's token cache, or creates it if it does not already exist.
10944+
// This function should NOT be used for creating synthetic tokens that are not in the file in the first place.
1094310945
func (node *SourceFile) GetOrCreateToken(
1094410946
kind Kind,
1094510947
pos int,

internal/ls/change/tracker.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ func (t *Tracker) InsertNodeBefore(sourceFile *ast.SourceFile, before *ast.Node,
178178
// InsertModifierBefore inserts a modifier token (like 'type') before a node with a trailing space.
179179
func (t *Tracker) InsertModifierBefore(sourceFile *ast.SourceFile, modifier ast.Kind, before *ast.Node) {
180180
pos := astnav.GetStartOfNode(before, sourceFile, false)
181-
token := sourceFile.GetOrCreateToken(modifier, pos, pos, before.Parent, ast.TokenFlagsNone)
181+
token := t.NewToken(modifier)
182+
token.Loc = core.NewTextRange(pos, pos)
183+
token.Parent = before.Parent
182184
t.InsertNodeAt(sourceFile, core.TextPos(pos), token, NodeOptions{Suffix: " "})
183185
}
184186

@@ -260,9 +262,12 @@ func (t *Tracker) endPosForInsertNodeAfter(sourceFile *ast.SourceFile, after *as
260262
// check if previous statement ends with semicolon
261263
// if not - insert semicolon to preserve the code from changing the meaning due to ASI
262264
endPos := t.converters.PositionToLineAndCharacter(sourceFile, core.TextPos(after.End()))
265+
semicolon := t.NewToken(ast.KindSemicolonToken)
266+
semicolon.Loc = core.NewTextRange(after.End(), after.End())
267+
semicolon.Parent = after.Parent
263268
t.ReplaceRange(sourceFile,
264269
lsproto.Range{Start: endPos, End: endPos},
265-
sourceFile.GetOrCreateToken(ast.KindSemicolonToken, after.End(), after.End(), after.Parent, ast.TokenFlagsNone),
270+
semicolon,
266271
NodeOptions{},
267272
)
268273
}
@@ -347,7 +352,10 @@ func (t *Tracker) InsertNodeInListAfter(sourceFile *ast.SourceFile, after *ast.N
347352

348353
// insert separator immediately following the 'after' node to preserve comments in trailing trivia
349354
// !!! formatcontext
350-
t.ReplaceRange(sourceFile, lsproto.Range{Start: end, End: end}, sourceFile.GetOrCreateToken(separator, after.End(), after.End()+len(separatorString), after.Parent, ast.TokenFlagsNone), NodeOptions{})
355+
separatorToken := t.NewToken(separator)
356+
separatorToken.Loc = core.NewTextRange(after.End(), after.End()+len(separatorString))
357+
separatorToken.Parent = after.Parent
358+
t.ReplaceRange(sourceFile, lsproto.Range{Start: end, End: end}, separatorToken, NodeOptions{})
351359
// use the same indentation as 'after' item
352360
indentation := format.FindFirstNonWhitespaceColumn(afterStartLinePosition, afterStart, sourceFile, t.formatSettings)
353361
// insert element before the line break on the line that contains 'after' element

0 commit comments

Comments
 (0)