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

Method addRegionOutliningSpans

internal/ls/folding.go:101–144  ·  view source on GitHub ↗
(ctx context.Context, sourceFile *ast.SourceFile)

Source from the content-addressed store, hash-verified

99}
100
101func (l *LanguageService) addRegionOutliningSpans(ctx context.Context, sourceFile *ast.SourceFile) []*lsproto.FoldingRange {
102 regions := make([]*lsproto.FoldingRange, 0, 40)
103 out := make([]*lsproto.FoldingRange, 0, 40)
104 lineStarts := scanner.GetECMALineStarts(sourceFile)
105 for _, currentLineStart := range lineStarts {
106 lineEnd := getLineEndOfPosition(sourceFile, int(currentLineStart))
107 lineText := sourceFile.Text()[currentLineStart:lineEnd]
108 result := parseRegionDelimiter(lineText)
109 if result == nil || isInComment(sourceFile, int(currentLineStart), astnav.GetTokenAtPosition(sourceFile, int(currentLineStart))) != nil {
110 continue
111 }
112
113 if result.isStart {
114 commentStart := l.createLspPosition(strings.Index(sourceFile.Text()[currentLineStart:lineEnd], "//")+int(currentLineStart), sourceFile)
115 foldingRangeKindRegion := lsproto.FoldingRangeKindRegion
116 region := &lsproto.FoldingRange{
117 StartLine: commentStart.Line,
118 StartCharacter: &commentStart.Character,
119 Kind: &foldingRangeKindRegion,
120 }
121 if supportsCollapsedText(ctx) {
122 collapsedText := "#region"
123 if result.name != "" {
124 collapsedText = result.name
125 }
126 region.CollapsedText = &collapsedText
127 }
128 // Our spans start out with some initial data.
129 // On every `#endregion`, we'll come back to these `FoldingRange`s
130 // and fill in their EndLine/EndCharacter.
131 regions = append(regions, region)
132 } else {
133 if len(regions) > 0 {
134 region := regions[len(regions)-1]
135 regions = regions[:len(regions)-1]
136 endingPosition := l.createLspPosition(lineEnd, sourceFile)
137 region.EndLine = endingPosition.Line
138 region.EndCharacter = &endingPosition.Character
139 out = append(out, region)
140 }
141 }
142 }
143 return out
144}
145
146func visitNode(ctx context.Context, n *ast.Node, depthRemaining int, sourceFile *ast.SourceFile, l *LanguageService) []*lsproto.FoldingRange {
147 if n.Flags&ast.NodeFlagsReparsed != 0 || depthRemaining == 0 || ctx.Err() != nil {

Callers 1

ProvideFoldingRangeMethod · 0.95

Calls 12

createLspPositionMethod · 0.95
GetECMALineStartsFunction · 0.92
GetTokenAtPositionFunction · 0.92
getLineEndOfPositionFunction · 0.85
parseRegionDelimiterFunction · 0.85
isInCommentFunction · 0.85
supportsCollapsedTextFunction · 0.85
lenFunction · 0.85
TextMethod · 0.65
makeFunction · 0.50
intInterface · 0.50
appendFunction · 0.50

Tested by

no test coverage detected