MCPcopy Index your code
hub / github.com/microsoft/typescript-go / writeCommentRangeWorker

Method writeCommentRangeWorker

internal/printer/printer.go:645–733  ·  view source on GitHub ↗
(text string, lineMap []core.TextPos, kind ast.Kind, loc core.TextRange)

Source from the content-addressed store, hash-verified

643}
644
645func (p *Printer) writeCommentRangeWorker(text string, lineMap []core.TextPos, kind ast.Kind, loc core.TextRange) {
646 if kind == ast.KindMultiLineCommentTrivia {
647 indentSize := GetDefaultIndentSize()
648 firstLine := scanner.ComputeLineOfPosition(lineMap, loc.Pos())
649 lineCount := len(lineMap)
650 firstCommentLineIndent := -1
651 pos := loc.Pos()
652 currentLine := firstLine
653 for ; pos < loc.End(); currentLine++ {
654 var nextLineStart int
655 if currentLine+1 == lineCount {
656 nextLineStart = len(text) + 1
657 } else {
658 nextLineStart = int(lineMap[currentLine+1])
659 }
660
661 if pos != loc.Pos() {
662 // If we are not emitting first line, we need to write the spaces to adjust the alignment
663 if firstCommentLineIndent == -1 {
664 firstCommentLineIndent = calculateIndent(text, int(lineMap[firstLine]), loc.Pos())
665 }
666
667 // These are number of spaces writer is going to write at current indent
668 currentWriterIndentSpacing := p.writer.GetIndent() * indentSize
669
670 // Number of spaces we want to be writing
671 // eg: Assume writer indent
672 // module m {
673 // /* starts at character 9 this is line 1
674 // * starts at character pos 4 line --1 = 8 - 8 + 3
675 // More left indented comment */ --2 = 8 - 8 + 2
676 // class c { }
677 // }
678 // module m {
679 // /* this is line 1 -- Assume current writer indent 8
680 // * line --3 = 8 - 4 + 5
681 // More right indented comment */ --4 = 8 - 4 + 11
682 // class c { }
683 // }
684 spacesToEmit := currentWriterIndentSpacing - firstCommentLineIndent + calculateIndent(text, pos, nextLineStart)
685 if spacesToEmit > 0 {
686 numberOfSingleSpacesToEmit := spacesToEmit % indentSize
687 indentSizeSpaceString := getIndentString((spacesToEmit-numberOfSingleSpacesToEmit)/indentSize, indentSize)
688
689 // Write indent size string ( in eg 1: = "", 2: "" , 3: string with 8 spaces 4: string with 12 spaces
690 p.writer.RawWrite(indentSizeSpaceString)
691
692 // Emit the single spaces (in eg: 1: 3 spaces, 2: 2 spaces, 3: 1 space, 4: 3 spaces)
693 for numberOfSingleSpacesToEmit > 0 {
694 p.writer.RawWrite(" ")
695 numberOfSingleSpacesToEmit--
696 }
697 } else {
698 // No spaces to emit write empty string
699 p.writer.RawWrite("")
700 }
701 }
702

Callers 2

writeCommentRangeMethod · 0.95

Calls 15

writeCommentMethod · 0.95
writeLineMethod · 0.95
ComputeLineOfPositionFunction · 0.92
IsLineBreakFunction · 0.92
GetDefaultIndentSizeFunction · 0.85
lenFunction · 0.85
calculateIndentFunction · 0.85
getIndentStringFunction · 0.85
minFunction · 0.85
PosMethod · 0.65
EndMethod · 0.65
GetIndentMethod · 0.65

Tested by

no test coverage detected