jsonFormatter.ts ×23

Frontier kind: Code frontier

unlabeled · c_06797081cea2

110 tests · 4029 LOC · 20 files · introduces 0 tests · 89 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
23 ranges89 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
547 ranges4029 lines · 20 files · Browse complete extent
All tests (intent)
110 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 89 introduced LOC across 23 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/base/common/jsonFormatter.ts 89 introduced LOC · 23 ranges

Open complete file

55
56 export function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[] {
57 > let initialIndentLevel: number; jsonFormatter.ts
58 > let formatText: string;
59 > let formatTextStart: number;
60 > let rangeStart: number;
61 > let rangeEnd: number;
62 > if (range) {
63 rangeStart = range.offset;
64 rangeEnd = rangeStart + range.length;
74 formatText = documentText.substring(formatTextStart, endOffset);
75 initialIndentLevel = computeIndentLevel(formatText, options);
76 > } else { jsonFormatter.ts
77 formatText = documentText;
78 initialIndentLevel = 0;
81 rangeEnd = documentText.length;
82 }
83 > const eol = getEOL(options, documentText); jsonFormatter.ts
84 >
85 > let lineBreak = false;
86 > let indentLevel = 0;
87 > let indentValue: string;
88 > if (options.insertSpaces) {
89 indentValue = repeat(' ', options.tabSize || 4);
90 > } else { jsonFormatter.ts
91 indentValue = '\t';
92 }
94 > const scanner = createScanner(formatText, false);
95 > let hasError = false;
96 >
97 > function newLineAndIndent(): string {
98 return eol + repeat(indentValue, initialIndentLevel + indentLevel);
99 }
100 > function scanNext(): SyntaxKind { jsonFormatter.ts
101 > let token = scanner.scan();
102 > lineBreak = false;
103 > while (token === SyntaxKind.Trivia || token === SyntaxKind.LineBreakTrivia) {
104 lineBreak = lineBreak || (token === SyntaxKind.LineBreakTrivia);
105 token = scanner.scan();
106 }
107 > hasError = token === SyntaxKind.Unknown || scanner.getTokenError() !== ScanError.None; jsonFormatter.ts
108 > return token;
109 > }
110 > const editOperations: Edit[] = [];
111 > function addEdit(text: string, startOffset: number, endOffset: number) {
112 > if (!hasError && startOffset < rangeEnd && endOffset > rangeStart && documentText.substring(startOffset, endOffset) !== text) {
113 editOperations.push({ offset: startOffset, length: endOffset - startOffset, content: text });
114 }
116 >
117 > let firstToken = scanNext();
118 >
119 > if (firstToken !== SyntaxKind.EOF) {
120 > const firstTokenStart = scanner.getTokenOffset() + formatTextStart;
121 > const initialIndent = repeat(indentValue, initialIndentLevel);
122 > addEdit(initialIndent, formatTextStart, firstTokenStart);
123 > }
124 >
125 > while (firstToken !== SyntaxKind.EOF) {
126 > let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart;
127 > let secondToken = scanNext();
128 >
129 > let replaceContent = '';
130 > while (!lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) {
131 // comments on the same line: keep them on the same line, but ignore them otherwise
132 const commentTokenStart = scanner.getTokenOffset() + formatTextStart;
136 secondToken = scanNext();
137 }
139 > if (secondToken === SyntaxKind.CloseBraceToken) {
140 if (firstToken !== SyntaxKind.OpenBraceToken) {
141 indentLevel--;
142 replaceContent = newLineAndIndent();
143 }
144 > } else if (secondToken === SyntaxKind.CloseBracketToken) { jsonFormatter.ts
145 if (firstToken !== SyntaxKind.OpenBracketToken) {
146 indentLevel--;
147 replaceContent = newLineAndIndent();
148 }
149 > } else { jsonFormatter.ts
150 > switch (firstToken) {
151 > case SyntaxKind.OpenBracketToken:
152 > case SyntaxKind.OpenBraceToken:
153 indentLevel++;
154 replaceContent = newLineAndIndent();
155 break;
156 > case SyntaxKind.CommaToken: jsonFormatter.ts
157 > case SyntaxKind.LineCommentTrivia:
158 replaceContent = newLineAndIndent();
159 break;
160 > case SyntaxKind.BlockCommentTrivia: jsonFormatter.ts
161 if (lineBreak) {
162 replaceContent = newLineAndIndent();
166 }
167 break;
168 > case SyntaxKind.ColonToken: jsonFormatter.ts
169 replaceContent = ' ';
170 break;
171 > case SyntaxKind.StringLiteral: jsonFormatter.ts
172 if (secondToken === SyntaxKind.ColonToken) {
173 replaceContent = '';
174 break;
175 }
176 > // fall through jsonFormatter.ts
177 > case SyntaxKind.NullKeyword:
178 > case SyntaxKind.TrueKeyword:
179 > case SyntaxKind.FalseKeyword:
180 > case SyntaxKind.NumericLiteral:
181 > case SyntaxKind.CloseBraceToken:
182 > case SyntaxKind.CloseBracketToken:
183 if (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia) {
184 replaceContent = ' ';
187 }
188 break;
189 > case SyntaxKind.Unknown: jsonFormatter.ts
190 hasError = true;
191 break;
193 > if (lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) {
194 replaceContent = newLineAndIndent();
195 }
197 > }
198 > const secondTokenStart = scanner.getTokenOffset() + formatTextStart;
199 > addEdit(replaceContent, firstTokenEnd, secondTokenStart);
200 > firstToken = secondToken;
201 > }
202 > return editOperations;
203 > }
204
205 /**
216 }
217
218 > function repeat(s: string, count: number): string { jsonFormatter.ts
219 > let result = '';
220 > for (let i = 0; i < count; i++) {
221 > result += s;
222 > }
223 > return result;
224 > }
225
226 function computeIndentLevel(content: string, options: FormattingOptions): number {
243
244 export function getEOL(options: FormattingOptions, text: string): string {
245 > for (let i = 0; i < text.length; i++) { jsonFormatter.ts
246 > const ch = text.charAt(i);
247 > if (ch === '\r') {
248 if (i + 1 < text.length && text.charAt(i + 1) === '\n') {
249 return '\r\n';
250 }
251 return '\r';
252 > } else if (ch === '\n') { jsonFormatter.ts
253 return '\n';
254 }
256 > return (options && options.eol) || '\n';
257 > }
258
259 export function isEOL(text: string, offset: number) {