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

Method indentMultilineComment

internal/format/span.go:927–986  ·  view source on GitHub ↗
(commentRange core.TextRange, indentation int, firstLineIsIndented bool, indentFinalLine bool)

Source from the content-addressed store, hash-verified

925}
926
927func (w *formatSpanWorker) indentMultilineComment(commentRange core.TextRange, indentation int, firstLineIsIndented bool, indentFinalLine bool) {
928 // split comment in lines
929 startLine := scanner.GetECMALineOfPosition(w.sourceFile, commentRange.Pos())
930 endLine := scanner.GetECMALineOfPosition(w.sourceFile, commentRange.End())
931
932 if startLine == endLine {
933 if !firstLineIsIndented {
934 // treat as single line comment
935 w.insertIndentation(commentRange.Pos(), indentation, false)
936 }
937 return
938 }
939
940 parts := make([]core.TextRange, 0, strings.Count(w.sourceFile.Text()[commentRange.Pos():commentRange.End()], "\n"))
941 startPos := commentRange.Pos()
942 for line := startLine; line < endLine; line++ {
943 endOfLine := scanner.GetECMAEndLinePosition(w.sourceFile, line)
944 parts = append(parts, core.NewTextRange(startPos, endOfLine))
945 startPos = int(scanner.GetECMALineStarts(w.sourceFile)[line+1])
946 }
947
948 if indentFinalLine {
949 parts = append(parts, core.NewTextRange(startPos, commentRange.End()))
950 }
951
952 if len(parts) == 0 {
953 return
954 }
955
956 startLinePos := int(scanner.GetECMALineStarts(w.sourceFile)[startLine])
957
958 nonWhitespaceInFirstPartCharacter, nonWhitespaceInFirstPartColumn := findFirstNonWhitespaceCharacterAndColumn(startLinePos, parts[0].Pos(), w.sourceFile, w.formattingContext.Options)
959
960 startIndex := 0
961
962 if firstLineIsIndented {
963 startIndex = 1
964 startLine++
965 }
966
967 // shift all parts on the delta size
968 delta := indentation - nonWhitespaceInFirstPartColumn
969 for i := startIndex; i < len(parts); i++ {
970 startLinePos := int(scanner.GetECMALineStarts(w.sourceFile)[startLine])
971 nonWhitespaceCharacter := nonWhitespaceInFirstPartCharacter
972 nonWhitespaceColumn := nonWhitespaceInFirstPartColumn
973 if i != 0 {
974 nonWhitespaceCharacter, nonWhitespaceColumn = findFirstNonWhitespaceCharacterAndColumn(parts[i].Pos(), parts[i].End(), w.sourceFile, w.formattingContext.Options)
975 }
976 newIndentation := nonWhitespaceColumn + delta
977 if newIndentation > 0 {
978 indentationString := getIndentationString(newIndentation, w.formattingContext.Options)
979 w.recordReplace(startLinePos, nonWhitespaceCharacter, indentationString)
980 } else {
981 w.recordDelete(startLinePos, nonWhitespaceCharacter)
982 }
983
984 startLine++

Callers 1

indentTriviaItemsMethod · 0.95

Calls 15

insertIndentationMethod · 0.95
recordReplaceMethod · 0.95
recordDeleteMethod · 0.95
GetECMALineOfPositionFunction · 0.92
GetECMAEndLinePositionFunction · 0.92
NewTextRangeFunction · 0.92
GetECMALineStartsFunction · 0.92
lenFunction · 0.85
getIndentationStringFunction · 0.85
PosMethod · 0.65
EndMethod · 0.65

Tested by

no test coverage detected