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

Function getIndentationForNodeWorker

internal/format/indent.go:279–373  ·  view source on GitHub ↗
(
	current *ast.Node,
	currentStartLine int,
	currentStartCharacter int,
	ignoreActualIndentationRange *core.TextRange,
	indentationDelta int,
	sourceFile *ast.SourceFile,
	isNextChild bool,
	options lsutil.FormatCodeSettings,
)

Source from the content-addressed store, hash-verified

277}
278
279func getIndentationForNodeWorker(
280 current *ast.Node,
281 currentStartLine int,
282 currentStartCharacter int,
283 ignoreActualIndentationRange *core.TextRange,
284 indentationDelta int,
285 sourceFile *ast.SourceFile,
286 isNextChild bool,
287 options lsutil.FormatCodeSettings,
288) int {
289 parent := current.Parent
290
291 // Walk up the tree and collect indentation for parent-child node pairs. Indentation is not added if
292 // * parent and child nodes start on the same line, or
293 // * parent is an IfStatement and child starts on the same line as an 'else clause'.
294 for parent != nil {
295 useActualIndentation := true
296 if ignoreActualIndentationRange != nil {
297 start := scanner.GetTokenPosOfNode(current, sourceFile, false)
298 useActualIndentation = start < ignoreActualIndentationRange.Pos() || start > ignoreActualIndentationRange.End()
299 }
300
301 containingListOrParentStartLine, containingListOrParentStartCharacter := getContainingListOrParentStart(parent, current, sourceFile)
302 parentAndChildShareLine := containingListOrParentStartLine == currentStartLine ||
303 childStartsOnTheSameLineWithElseInIfStatement(parent, current, currentStartLine, sourceFile)
304
305 if useActualIndentation {
306 // check if current node is a list item - if yes, take indentation from it
307 var firstListChild *ast.Node
308 containerList := GetContainingList(current, sourceFile)
309 if containerList != nil {
310 firstListChild = core.FirstOrNil(containerList.Nodes)
311 }
312 // A list indents its children if the children begin on a later line than the list itself:
313 //
314 // f1( L0 - List start
315 // { L1 - First child start: indented, along with all other children
316 // prop: 0
317 // },
318 // {
319 // prop: 1
320 // }
321 // )
322 //
323 // f2({ L0 - List start and first child start: children are not indented.
324 // prop: 0 Object properties are indented only one level, because the list
325 // }, { itself contributes nothing.
326 // prop: 1 L3 - The indentation of the second object literal is best understood by
327 // }) looking at the relationship between the list and *first* list item.
328 var listIndentsChild bool
329 if firstListChild != nil {
330 listLine := getStartLineForNode(firstListChild, sourceFile)
331 listIndentsChild = listLine > containingListOrParentStartLine
332 }
333 actualIndentation := getActualIndentationForListItem(current, sourceFile, options, listIndentsChild)
334 if actualIndentation != -1 {
335 return actualIndentation + indentationDelta
336 }

Callers 2

GetIndentationForNodeFunction · 0.85
getSmartIndentFunction · 0.85

Tested by

no test coverage detected