indentationGuesser.ts ×18

Frontier kind: Code frontier

unlabeled · c_c2488da20045

13 tests · 40710 LOC · 238 files · introduces 0 tests · 125 LOC · 2 files

Introduces — evidence that enters the hierarchy at this concept

Code
19 ranges125 lines · 2 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
4956 ranges40710 lines · 238 files · Browse complete extent
All tests (intent)
13 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.

2 files ranked by introduced lines: 125 introduced LOC across 19 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/editor/common/model/indentationGuesser.ts 115 introduced LOC · 18 ranges

Open complete file

7 import { ITextBuffer } from '../model.js';
8
9 > class SpacesDiffResult { indentationGuesser.ts
10 > public spacesDiff: number = 0;
11 > public looksLikeAlignment: boolean = false;
12 }
13
15 * Compute the diff in spaces between two line's indentation.
16 */
17 > function spacesDiff(a: string, aLength: number, b: string, bLength: number, result: SpacesDiffResult): void { indentationGuesser.ts
18 >
19 > result.spacesDiff = 0;
20 > result.looksLikeAlignment = false;
21 >
22 > // This can go both ways (e.g.):
23 > // - a: "\t"
24 > // - b: "\t "
25 > // => This should count 1 tab and 4 spaces
26 >
27 > let i: number;
28 >
29 > for (i = 0; i < aLength && i < bLength; i++) {
30 const aCharCode = a.charCodeAt(i);
31 const bCharCode = b.charCodeAt(i);
35 }
36 }
38 > let aSpacesCnt = 0, aTabsCount = 0;
39 > for (let j = i; j < aLength; j++) {
40 const aCharCode = a.charCodeAt(j);
41 if (aCharCode === CharCode.Space) {
45 }
46 }
48 > let bSpacesCnt = 0, bTabsCount = 0;
49 > for (let j = i; j < bLength; j++) {
50 const bCharCode = b.charCodeAt(j);
51 if (bCharCode === CharCode.Space) {
55 }
56 }
58 > if (aSpacesCnt > 0 && aTabsCount > 0) {
59 return;
60 }
61 > if (bSpacesCnt > 0 && bTabsCount > 0) { indentationGuesser.ts
62 return;
63 }
65 > const tabsDiff = Math.abs(aTabsCount - bTabsCount);
66 > const spacesDiff = Math.abs(aSpacesCnt - bSpacesCnt);
67 >
68 > if (tabsDiff === 0) {
69 > // check if the indentation difference might be caused by alignment reasons
70 > // sometime folks like to align their code, but this should not be used as a hint
71 > result.spacesDiff = spacesDiff;
72 >
73 > if (spacesDiff > 0 && 0 <= bSpacesCnt - 1 && bSpacesCnt - 1 < a.length && bSpacesCnt < b.length) {
74 if (b.charCodeAt(bSpacesCnt) !== CharCode.Space && a.charCodeAt(bSpacesCnt - 1) === CharCode.Space) {
75 if (a.charCodeAt(a.length - 1) === CharCode.Comma) {
104
105 export function guessIndentation(source: ITextBuffer, defaultTabSize: number, defaultInsertSpaces: boolean): IGuessedIndentation {
106 > // Look at most at the first 10k lines indentationGuesser.ts
107 > const linesCount = Math.min(source.getLineCount(), 10000);
108 >
109 > let linesIndentedWithTabsCount = 0; // number of lines that contain at least one tab in indentation
110 > let linesIndentedWithSpacesCount = 0; // number of lines that contain only spaces in indentation
111 >
112 > let previousLineText = ''; // content of latest line that contained non-whitespace chars
113 > let previousLineIndentation = 0; // index at which latest line contained the first non-whitespace char
114 >
115 > const ALLOWED_TAB_SIZE_GUESSES = [2, 4, 6, 8, 3, 5, 7]; // prefer even guesses for `tabSize`, limit to [2, 8].
116 > const MAX_ALLOWED_TAB_SIZE_GUESS = 8; // max(ALLOWED_TAB_SIZE_GUESSES) = 8
117 >
118 > const spacesDiffCount = [0, 0, 0, 0, 0, 0, 0, 0, 0]; // `tabSize` scores
119 > const tmp = new SpacesDiffResult();
120 >
121 > for (let lineNumber = 1; lineNumber <= linesCount; lineNumber++) {
122 > const currentLineLength = source.getLineLength(lineNumber);
123 > const currentLineText = source.getLineContent(lineNumber);
124 >
125 > // if the text buffer is chunk based, so long lines are cons-string, v8 will flattern the string when we check charCode.
126 > // checking charCode on chunks directly is cheaper.
127 > const useCurrentLineText = (currentLineLength <= 65536);
128 >
129 > let currentLineHasContent = false; // does `currentLineText` contain non-whitespace chars
130 > let currentLineIndentation = 0; // index at which `currentLineText` contains the first non-whitespace char
131 > let currentLineSpacesCount = 0; // count of spaces found in `currentLineText` indentation
132 > let currentLineTabsCount = 0; // count of tabs found in `currentLineText` indentation
133 > for (let j = 0, lenJ = currentLineLength; j < lenJ; j++) {
134 > const charCode = (useCurrentLineText ? currentLineText.charCodeAt(j) : source.getLineCharCode(lineNumber, j));
135 >
136 > if (charCode === CharCode.Tab) {
137 currentLineTabsCount++;
138 > } else if (charCode === CharCode.Space) { indentationGuesser.ts
139 currentLineSpacesCount++;
140 > } else { indentationGuesser.ts
141 > // Hit non whitespace character on this line
142 > currentLineHasContent = true;
143 > currentLineIndentation = j;
144 > break;
145 > }
146 > }
147 >
148 > // Ignore empty or only whitespace lines
149 > if (!currentLineHasContent) {
150 continue;
151 }
153 > if (currentLineTabsCount > 0) {
154 linesIndentedWithTabsCount++;
155 > } else if (currentLineSpacesCount > 1) { indentationGuesser.ts
156 linesIndentedWithSpacesCount++;
157 }
159 > spacesDiff(previousLineText, previousLineIndentation, currentLineText, currentLineIndentation, tmp);
160 >
161 > if (tmp.looksLikeAlignment) {
162 // if defaultInsertSpaces === true && the spaces count == tabSize, we may want to count it as valid indentation
163 //
174 }
175 }
177 > const currentSpacesDiff = tmp.spacesDiff;
178 > if (currentSpacesDiff <= MAX_ALLOWED_TAB_SIZE_GUESS) {
179 > spacesDiffCount[currentSpacesDiff]++;
180 > }
181 >
182 > previousLineText = currentLineText;
183 > previousLineIndentation = currentLineIndentation;
184 > }
185 >
186 > let insertSpaces = defaultInsertSpaces;
187 > if (linesIndentedWithTabsCount !== linesIndentedWithSpacesCount) {
188 insertSpaces = (linesIndentedWithTabsCount < linesIndentedWithSpacesCount);
189 }
191 > let tabSize = defaultTabSize;
192 >
193 > // Guess tabSize only if inserting spaces...
194 > if (insertSpaces) {
195 let tabSizeScore = 0;
196 ALLOWED_TAB_SIZE_GUESSES.forEach((possibleTabSize) => {
209 }
210 }
212 > // console.log('--------------------------');
213 > // console.log('linesIndentedWithTabsCount: ' + linesIndentedWithTabsCount + ', linesIndentedWithSpacesCount: ' + linesIndentedWithSpacesCount);
214 > // console.log('spacesDiffCount: ' + spacesDiffCount);
215 > // console.log('tabSize: ' + tabSize + ', tabSizeScore: ' + tabSizeScore);
216 >
217 > return {
218 > insertSpaces: insertSpaces,
219 > tabSize: tabSize
220 > };
221 > }
src/vs/editor/common/model/textModel.ts 10 introduced LOC · 1 range

Open complete file

205 public static resolveOptions(textBuffer: model.ITextBuffer, options: model.ITextModelCreationOptions): model.TextModelResolvedOptions {
206 if (options.detectIndentation) {
207 > const guessedIndentation = guessIndentation(textBuffer, options.tabSize, options.insertSpaces); textModel.ts
208 > return new model.TextModelResolvedOptions({
209 > tabSize: guessedIndentation.tabSize,
210 > indentSize: 'tabSize', // TODO@Alex: guess indentSize independent of tabSize
211 > insertSpaces: guessedIndentation.insertSpaces,
212 > trimAutoWhitespace: options.trimAutoWhitespace,
213 > defaultEOL: options.defaultEOL,
214 > bracketPairColorizationOptions: options.bracketPairColorizationOptions,
215 > });
216 > }
217
218 return new model.TextModelResolvedOptions(options);