MCPcopy Create free account
hub / github.com/microsoft/typescript-go / computeNewText

Method computeNewText

internal/ls/change/trackerimpl.go:57–89  ·  view source on GitHub ↗
(change *trackerEdit, targetSourceFile *ast.SourceFile, sourceFile *ast.SourceFile)

Source from the content-addressed store, hash-verified

55}
56
57func (t *Tracker) computeNewText(change *trackerEdit, targetSourceFile *ast.SourceFile, sourceFile *ast.SourceFile) string {
58 switch change.kind {
59 case trackerEditKindRemove:
60 return ""
61 case trackerEditKindText:
62 return change.NewText
63 }
64
65 pos := int(t.converters.LineAndCharacterToPosition(sourceFile, change.Range.Start))
66 formatNode := func(n *ast.Node) string {
67 return t.getFormattedTextOfNode(n, targetSourceFile, sourceFile, pos, change.options)
68 }
69
70 var text string
71 switch change.kind {
72
73 case trackerEditKindReplaceWithMultipleNodes:
74 if change.options.joiner == "" {
75 change.options.joiner = t.newLine
76 }
77 text = strings.Join(core.Map(change.nodes, func(n *ast.Node) string { return strings.TrimSuffix(formatNode(n), t.newLine) }), change.options.joiner)
78 case trackerEditKindReplaceWithSingleNode:
79 text = formatNode(change.Node)
80 default:
81 panic(fmt.Sprintf("change kind %d should have been handled earlier", change.kind))
82 }
83 // strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
84 noIndent := text
85 if !(change.options.indentation != nil || format.GetLineStartPositionForPosition(pos, targetSourceFile) == pos) {
86 noIndent = strings.TrimLeftFunc(text, unicode.IsSpace)
87 }
88 return change.options.Prefix + noIndent + core.IfElse(strings.HasSuffix(noIndent, change.options.Suffix), "", change.options.Suffix)
89}
90
91/** Note: this may mutate `nodeIn`. */
92func (t *Tracker) getFormattedTextOfNode(nodeIn *ast.Node, targetSourceFile *ast.SourceFile, sourceFile *ast.SourceFile, pos int, options NodeOptions) string {

Callers 1

Calls 7

MapFunction · 0.92
IfElseFunction · 0.92
panicFunction · 0.85
intInterface · 0.50

Tested by

no test coverage detected