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

Method execute

internal/format/span.go:224–331  ·  view source on GitHub ↗
(s *formattingScanner)

Source from the content-addressed store, hash-verified

222}
223
224func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange {
225 w.formattingScanner = s
226 w.indentationOnLastIndentedLine = -1
227 w.lastIndentedLine = -1
228 opt := GetFormatCodeSettingsFromContext(w.ctx)
229 w.formattingContext = NewFormattingContext(w.sourceFile, w.requestKind, opt)
230 // formatting context is used by rules provider
231 w.visitor = ast.NewNodeVisitor(func(child *ast.Node) *ast.Node {
232 if child == nil {
233 return child
234 }
235 w.processChildNode(w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, child, -1, w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, false, false)
236 return child
237 }, &ast.NodeFactory{}, ast.NodeVisitorHooks{
238 VisitNodes: func(nodes *ast.NodeList, v *ast.NodeVisitor) *ast.NodeList {
239 if nodes == nil {
240 return nodes
241 }
242 w.processChildNodes(w.visitingNode, w.visitingIndenter, w.visitingNodeStartLine, w.visitingUndecoratedNodeStartLine, nodes, w.visitingNode, w.visitingNodeStartLine, w.visitingIndenter)
243 return nodes
244 },
245 })
246
247 w.formattingScanner.advance()
248
249 if w.formattingScanner.isOnToken() {
250 startLine := scanner.GetECMALineOfPosition(w.sourceFile, withTokenStart(w.enclosingNode, w.sourceFile).Pos())
251 undecoratedStartLine := startLine
252 if ast.HasDecorators(w.enclosingNode) {
253 undecoratedStartLine = scanner.GetECMALineOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(w.enclosingNode, w.sourceFile))
254 }
255
256 w.processNode(w.enclosingNode, w.enclosingNode, startLine, undecoratedStartLine, w.initialIndentation, w.delta)
257 }
258
259 // Leading trivia items get attached to and processed with the token that proceeds them. If the
260 // range ends in the middle of some leading trivia, the token that proceeds them won't be in the
261 // range and thus won't get processed. So we process those remaining trivia items here.
262 remainingTrivia := w.formattingScanner.getCurrentLeadingTrivia()
263 if len(remainingTrivia) > 0 {
264 indentation := w.initialIndentation
265 if NodeWillIndentChild(w.formattingContext.Options, w.enclosingNode, nil, w.sourceFile, false) {
266 indentation += opt.IndentSize // !!! TODO: nil check???
267 }
268
269 w.indentTriviaItems(remainingTrivia, indentation, true, func(item TextRangeWithKind) {
270 startLine, startChar := scanner.GetECMALineAndByteOffsetOfPosition(w.sourceFile, item.Loc.Pos())
271 w.processRange(item, startLine, startChar, w.enclosingNode, w.enclosingNode, nil)
272 w.insertIndentation(item.Loc.Pos(), indentation, false)
273 })
274
275 if opt.TrimTrailingWhitespace.IsTrue() {
276 w.trimTrailingWhitespacesForRemainingRange(remainingTrivia)
277 }
278 }
279
280 if w.previousRange != NewTextRangeWithKind(0, 0, 0) && w.formattingScanner.getTokenFullStart() >= w.originalRange.End() {
281 // Formatting edits happen by looking at pairs of contiguous tokens (see `processPair`),

Callers 1

newFormattingScannerFunction · 0.80

Calls 15

processChildNodeMethod · 0.95
processChildNodesMethod · 0.95
processNodeMethod · 0.95
indentTriviaItemsMethod · 0.95
processRangeMethod · 0.95
insertIndentationMethod · 0.95
processPairMethod · 0.95
NewNodeVisitorFunction · 0.92
GetECMALineOfPositionFunction · 0.92
HasDecoratorsFunction · 0.92

Tested by

no test coverage detected