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

Function getSmartIndent

internal/format/indent.go:238–277  ·  view source on GitHub ↗
(sourceFile *ast.SourceFile, position int, precedingToken *ast.Node, lineAtPosition int, assumeNewLineBeforeCloseBrace bool, options lsutil.FormatCodeSettings)

Source from the content-addressed store, hash-verified

236}
237
238func getSmartIndent(sourceFile *ast.SourceFile, position int, precedingToken *ast.Node, lineAtPosition int, assumeNewLineBeforeCloseBrace bool, options lsutil.FormatCodeSettings) int {
239 // try to find node that can contribute to indentation and includes 'position' starting from 'precedingToken'
240 // if such node is found - compute initial indentation for 'position' inside this node
241 var previous *ast.Node
242 current := precedingToken
243
244 for current != nil {
245 if lsutil.PositionBelongsToNode(current, position, sourceFile) && ShouldIndentChildNode(options, current, previous, sourceFile, true) {
246 currentStartLine, currentStartChar := getStartLineAndCharacterForNode(current, sourceFile)
247 ntk := nextTokenIsCurlyBraceOnSameLineAsCursor(precedingToken, current, lineAtPosition, sourceFile)
248 var indentationDelta int
249 if ntk != nextTokenKindUnknown {
250 // handle cases when codefix is about to be inserted before the close brace
251 if assumeNewLineBeforeCloseBrace && ntk == nextTokenKindCloseBrace {
252 indentationDelta = options.IndentSize
253 }
254 // else 0
255 } else {
256 if lineAtPosition != currentStartLine {
257 indentationDelta = options.IndentSize
258 }
259 }
260 return getIndentationForNodeWorker(current, currentStartLine, currentStartChar, nil, indentationDelta, sourceFile, true, options)
261 }
262
263 // check if current node is a list item - if yes, take indentation from it
264 // do not consider parent-child line sharing yet:
265 // function foo(a
266 // | preceding node 'a' does share line with its parent but indentation is expected
267 actualIndentation := getActualIndentationForListItem(current, sourceFile, options, true /*listIndentsChild*/)
268 if actualIndentation != -1 {
269 return actualIndentation
270 }
271
272 previous = current
273 current = current.Parent
274 }
275 // no parent was found - return the base indentation of the SourceFile
276 return options.BaseIndentSize
277}
278
279func getIndentationForNodeWorker(
280 current *ast.Node,

Callers 1

GetIndentationFunction · 0.85

Tested by

no test coverage detected