Skip to content

Commit 6277711

Browse files
authored
Rename line maps with ECMA/LSP prefix (#1786)
1 parent 6524ca6 commit 6277711

35 files changed

+148
-156
lines changed

internal/ast/ast.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10724,10 +10724,10 @@ type SourceFile struct {
1072410724
ClassifiableNames collections.Set[string]
1072510725
PatternAmbientModules []*PatternAmbientModule
1072610726

10727-
// Fields set by LineMap
10727+
// Fields set by ECMALineMap
1072810728

10729-
lineMapMu sync.RWMutex
10730-
lineMap []core.TextPos
10729+
ecmaLineMapMu sync.RWMutex
10730+
ecmaLineMap []core.TextPos
1073110731

1073210732
// Fields set by language service
1073310733

@@ -10862,17 +10862,17 @@ func (f *NodeFactory) UpdateSourceFile(node *SourceFile, statements *StatementLi
1086210862
return node.AsNode()
1086310863
}
1086410864

10865-
func (node *SourceFile) LineMap() []core.TextPos {
10866-
node.lineMapMu.RLock()
10867-
lineMap := node.lineMap
10868-
node.lineMapMu.RUnlock()
10865+
func (node *SourceFile) ECMALineMap() []core.TextPos {
10866+
node.ecmaLineMapMu.RLock()
10867+
lineMap := node.ecmaLineMap
10868+
node.ecmaLineMapMu.RUnlock()
1086910869
if lineMap == nil {
10870-
node.lineMapMu.Lock()
10871-
defer node.lineMapMu.Unlock()
10872-
lineMap = node.lineMap
10870+
node.ecmaLineMapMu.Lock()
10871+
defer node.ecmaLineMapMu.Unlock()
10872+
lineMap = node.ecmaLineMap
1087310873
if lineMap == nil {
10874-
lineMap = core.ComputeLineStarts(node.Text())
10875-
node.lineMap = lineMap
10874+
lineMap = core.ComputeECMALineStarts(node.Text())
10875+
node.ecmaLineMap = lineMap
1087610876
}
1087710877
}
1087810878
return lineMap
@@ -11054,7 +11054,7 @@ func getDeclarationName(declaration *Node) string {
1105411054

1105511055
type SourceFileLike interface {
1105611056
Text() string
11057-
LineMap() []core.TextPos
11057+
ECMALineMap() []core.TextPos
1105811058
}
1105911059

1106011060
type CommentRange struct {

internal/astnav/tokens_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func tsGetTouchingPropertyName(t testing.TB, fileText string, positions []int) [
240240
}
241241

242242
func writeRangeDiff(output *strings.Builder, file *ast.SourceFile, diff tokenDiff, rng core.TextRange, position int) {
243-
lines := file.LineMap()
243+
lines := file.ECMALineMap()
244244

245245
tsTokenPos := position
246246
goTokenPos := position

internal/checker/grammarchecks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,8 @@ func (c *Checker) checkGrammarArrowFunction(node *ast.Node, file *ast.SourceFile
814814
}
815815

816816
equalsGreaterThanToken := arrowFunc.EqualsGreaterThanToken
817-
startLine, _ := scanner.GetLineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
818-
endLine, _ := scanner.GetLineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
817+
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
818+
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
819819
return startLine != endLine && c.grammarErrorOnNode(equalsGreaterThanToken, diagnostics.Line_terminator_not_permitted_before_arrow)
820820
}
821821

internal/compiler/program.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,10 @@ func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFi
10741074
// Build map of directives by line number
10751075
directivesByLine := make(map[int]ast.CommentDirective)
10761076
for _, directive := range sourceFile.CommentDirectives {
1077-
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
1077+
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
10781078
directivesByLine[line] = directive
10791079
}
1080-
lineStarts := scanner.GetLineStarts(sourceFile)
1080+
lineStarts := scanner.GetECMALineStarts(sourceFile)
10811081
filtered := make([]*ast.Diagnostic, 0, len(diags))
10821082
for _, diagnostic := range diags {
10831083
ignoreDiagnostic := false
@@ -1225,7 +1225,7 @@ func (p *Program) getDiagnosticsHelper(ctx context.Context, sourceFile *ast.Sour
12251225
func (p *Program) LineCount() int {
12261226
var count int
12271227
for _, file := range p.files {
1228-
count += len(file.LineMap())
1228+
count += len(file.ECMALineMap())
12291229
}
12301230
return count
12311231
}

internal/core/core.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,12 @@ func Coalesce[T *U, U any](a T, b T) T {
362362
}
363363
}
364364

365-
func ComputeLineStarts(text string) []TextPos {
365+
func ComputeECMALineStarts(text string) []TextPos {
366366
result := make([]TextPos, 0, strings.Count(text, "\n")+1)
367-
return slices.AppendSeq(result, ComputeLineStartsSeq(text))
367+
return slices.AppendSeq(result, ComputeECMALineStartsSeq(text))
368368
}
369369

370-
func ComputeLineStartsSeq(text string) iter.Seq[TextPos] {
370+
func ComputeECMALineStartsSeq(text string) iter.Seq[TextPos] {
371371
return func(yield func(TextPos) bool) {
372372
textLen := TextPos(len(text))
373373
var pos TextPos

internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func FormatDiagnosticWithColorAndContext(output io.Writer, diagnostic *ast.Diagn
8484
}
8585

8686
func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, length int, squiggleColor string, indent string, formatOpts *FormattingOptions) {
87-
firstLine, firstLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start)
88-
lastLine, lastLineChar := scanner.GetLineAndCharacterOfPosition(sourceFile, start+length)
87+
firstLine, firstLineChar := scanner.GetECMALineAndCharacterOfPosition(sourceFile, start)
88+
lastLine, lastLineChar := scanner.GetECMALineAndCharacterOfPosition(sourceFile, start+length)
8989
if length == 0 {
9090
lastLineChar++ // When length is zero, squiggle the character right after the start position.
9191
}
9292

93-
lastLineOfFile, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
93+
lastLineOfFile, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
9494

9595
hasMoreThanFiveLines := lastLine-firstLine >= 4
9696
gutterWidth := len(strconv.Itoa(lastLine + 1))
@@ -113,10 +113,10 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
113113
i = lastLine - 1
114114
}
115115

116-
lineStart := scanner.GetPositionOfLineAndCharacter(sourceFile, i, 0)
116+
lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, i, 0)
117117
var lineEnd int
118118
if i < lastLineOfFile {
119-
lineEnd = scanner.GetPositionOfLineAndCharacter(sourceFile, i+1, 0)
119+
lineEnd = scanner.GetECMAPositionOfLineAndCharacter(sourceFile, i+1, 0)
120120
} else {
121121
lineEnd = sourceFile.Loc.End()
122122
}
@@ -216,7 +216,7 @@ func writeWithStyleAndReset(output io.Writer, text string, formatStyle string) {
216216
}
217217

218218
func WriteLocation(output io.Writer, file *ast.SourceFile, pos int, formatOpts *FormattingOptions, writeWithStyleAndReset FormattedWriter) {
219-
firstLine, firstChar := scanner.GetLineAndCharacterOfPosition(file, pos)
219+
firstLine, firstChar := scanner.GetECMALineAndCharacterOfPosition(file, pos)
220220
var relativeFileName string
221221
if formatOpts != nil {
222222
relativeFileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)
@@ -357,7 +357,7 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
357357
if file == nil || len(fileErrors) == 0 {
358358
return ""
359359
}
360-
line, _ := scanner.GetLineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
360+
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
361361
fileName := file.FileName()
362362
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
363363
fileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)
@@ -378,7 +378,7 @@ func WriteFormatDiagnostics(output io.Writer, diagnostics []*ast.Diagnostic, for
378378

379379
func WriteFormatDiagnostic(output io.Writer, diagnostic *ast.Diagnostic, formatOpts *FormattingOptions) {
380380
if diagnostic.File() != nil {
381-
line, character := scanner.GetLineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
381+
line, character := scanner.GetECMALineAndCharacterOfPosition(diagnostic.File(), diagnostic.Loc().Pos())
382382
fileName := diagnostic.File().FileName()
383383
relativeFileName := tspath.ConvertToRelativePath(fileName, formatOpts.ComparePathsOptions)
384384
fmt.Fprintf(output, "%s(%d,%d): ", relativeFileName, line+1, character+1)

internal/format/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,18 @@ func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position
146146
}
147147

148148
func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
149-
line, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, position)
149+
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
150150
if line == 0 {
151151
return nil
152152
}
153153
// get start position for the previous line
154-
startPos := int(scanner.GetLineStarts(sourceFile)[line-1])
154+
startPos := int(scanner.GetECMALineStarts(sourceFile)[line-1])
155155
// After the enter key, the cursor is now at a new line. The new line may or may not contain non-whitespace characters.
156156
// If the new line has only whitespaces, we won't want to format this line, because that would remove the indentation as
157157
// trailing whitespaces. So the end of the formatting span should be the later one between:
158158
// 1. the end of the previous line
159159
// 2. the last non-whitespace character in the current line
160-
endOfFormatSpan := scanner.GetEndLinePosition(sourceFile, line)
160+
endOfFormatSpan := scanner.GetECMAEndLinePosition(sourceFile, line)
161161
for endOfFormatSpan > startPos {
162162
ch, s := utf8.DecodeRuneInString(sourceFile.Text()[endOfFormatSpan:])
163163
if s == 0 || stringutil.IsWhiteSpaceSingleLine(ch) { // on multibyte character keep backing up

internal/format/indent.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func GetIndentationForNode(n *ast.Node, ignoreActualIndentationRange *core.TextRange, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
16-
startline, startpos := scanner.GetLineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
16+
startline, startpos := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
1717
return getIndentationForNodeWorker(n, startline, startpos, ignoreActualIndentationRange /*indentationDelta*/, 0, sourceFile /*isNextChild*/, false, options)
1818
}
1919

@@ -100,7 +100,7 @@ func getIndentationForNodeWorker(
100100
parent = current.Parent
101101

102102
if useTrueStart {
103-
currentStartLine, currentStartCharacter = scanner.GetLineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false))
103+
currentStartLine, currentStartCharacter = scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(current, sourceFile, false))
104104
} else {
105105
currentStartLine = containingListOrParentStartLine
106106
currentStartCharacter = containingListOrParentStartCharacter
@@ -131,7 +131,7 @@ func isArgumentAndStartLineOverlapsExpressionBeingCalled(parent *ast.Node, child
131131
return false
132132
}
133133
expressionOfCallExpressionEnd := parent.Expression().End()
134-
expressionOfCallExpressionEndLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, expressionOfCallExpressionEnd)
134+
expressionOfCallExpressionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, expressionOfCallExpressionEnd)
135135
return expressionOfCallExpressionEndLine == childStartLine
136136
}
137137

@@ -166,7 +166,7 @@ func getActualIndentationForListStartLine(list *ast.NodeList, sourceFile *ast.So
166166
if list == nil {
167167
return -1
168168
}
169-
line, char := scanner.GetLineAndCharacterOfPosition(sourceFile, list.Loc.Pos())
169+
line, char := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Loc.Pos())
170170
return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options)
171171
}
172172

@@ -185,7 +185,7 @@ func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile *
185185
continue
186186
}
187187
// skip list items that ends on the same line with the current list element
188-
prevEndLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, list.Nodes[i].End())
188+
prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Nodes[i].End())
189189
if prevEndLine != line {
190190
return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options)
191191
}
@@ -196,7 +196,7 @@ func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile *
196196
}
197197

198198
func findColumnForFirstNonWhitespaceCharacterInLine(line int, char int, sourceFile *ast.SourceFile, options *FormatCodeSettings) int {
199-
lineStart := scanner.GetPositionOfLineAndCharacter(sourceFile, line, 0)
199+
lineStart := scanner.GetECMAPositionOfLineAndCharacter(sourceFile, line, 0)
200200
return FindFirstNonWhitespaceColumn(lineStart, lineStart+char, sourceFile, options)
201201
}
202202

@@ -247,7 +247,7 @@ func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.
247247
}
248248

249249
func getStartLineAndCharacterForNode(n *ast.Node, sourceFile *ast.SourceFile) (line int, character int) {
250-
return scanner.GetLineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
250+
return scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
251251
}
252252

253253
func GetContainingList(node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
@@ -356,7 +356,7 @@ func getContainingListOrParentStart(parent *ast.Node, child *ast.Node, sourceFil
356356
} else {
357357
startPos = scanner.GetTokenPosOfNode(parent, sourceFile, false)
358358
}
359-
return scanner.GetLineAndCharacterOfPosition(sourceFile, startPos)
359+
return scanner.GetECMALineAndCharacterOfPosition(sourceFile, startPos)
360360
}
361361

362362
func isControlFlowEndingStatement(kind ast.Kind, parentKind ast.Kind) bool {
@@ -439,8 +439,8 @@ func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *
439439
return rangeIsOnOneLine(child.Loc, sourceFile)
440440
}
441441
if parent.Kind == ast.KindBinaryExpression && sourceFile != nil && childKind == ast.KindJsxElement {
442-
parentStartLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos()))
443-
childStartLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos()))
442+
parentStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos()))
443+
childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos()))
444444
return parentStartLine != childStartLine
445445
}
446446
if parent.Kind != ast.KindBinaryExpression {
@@ -516,7 +516,7 @@ func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *
516516
// branch beginning on the line that the whenTrue branch ends.
517517
func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
518518
if parent.Kind == ast.KindConditionalExpression && (child == parent.AsConditionalExpression().WhenTrue || child == parent.AsConditionalExpression().WhenFalse) {
519-
conditionEndLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End())
519+
conditionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End())
520520
if child == parent.AsConditionalExpression().WhenTrue {
521521
return childStartLine == conditionEndLine
522522
} else {
@@ -528,7 +528,7 @@ func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast
528528
// 0 L2: indented two stops, one because whenTrue was indented
529529
// ); and one because of the parentheses spanning multiple lines
530530
trueStartLine, _ := getStartLineAndCharacterForNode(parent.AsConditionalExpression().WhenTrue, sourceFile)
531-
trueEndLine, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
531+
trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
532532
return conditionEndLine == trueStartLine && trueEndLine == childStartLine
533533
}
534534
}
@@ -550,7 +550,7 @@ func argumentStartsOnSameLineAsPreviousArgument(parent *ast.Node, child *ast.Nod
550550
}
551551

552552
previousNode := parent.Arguments()[currentIndex-1]
553-
lineOfPreviousNode, _ := scanner.GetLineAndCharacterOfPosition(sourceFile, previousNode.End())
553+
lineOfPreviousNode, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, previousNode.End())
554554
if childStartLine == lineOfPreviousNode {
555555
return true
556556
}

internal/format/rulecontext.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ func isSemicolonDeletionContext(context *formattingContext) bool {
584584
nextTokenStart = scanner.GetTokenPosOfNode(nextRealToken, context.SourceFile, false)
585585
}
586586

587-
startLine, _ := scanner.GetLineAndCharacterOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos())
588-
endLine, _ := scanner.GetLineAndCharacterOfPosition(context.SourceFile, nextTokenStart)
587+
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos())
588+
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, nextTokenStart)
589589
if startLine == endLine {
590590
return nextTokenKind == ast.KindCloseBraceToken || nextTokenKind == ast.KindEndOfFile
591591
}

0 commit comments

Comments
 (0)