Adjust heuristic for preferring 2 over 4 for indentation size

This commit is contained in:
Dmitriy Vasyura
2025-12-18 22:12:16 -08:00
parent d6e971b2df
commit b014124875
2 changed files with 73 additions and 8 deletions

View File

@@ -192,10 +192,7 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de
// Guess tabSize only if inserting spaces... // Guess tabSize only if inserting spaces...
if (insertSpaces) { if (insertSpaces) {
let tabSizeScore = (insertSpaces ? 0 : 0.1 * linesCount); let tabSizeScore = 0;
// console.log("score threshold: " + tabSizeScore);
ALLOWED_TAB_SIZE_GUESSES.forEach((possibleTabSize) => { ALLOWED_TAB_SIZE_GUESSES.forEach((possibleTabSize) => {
const possibleTabSizeScore = spacesDiffCount[possibleTabSize]; const possibleTabSizeScore = spacesDiffCount[possibleTabSize];
if (possibleTabSizeScore > tabSizeScore) { if (possibleTabSizeScore > tabSizeScore) {
@@ -204,14 +201,14 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de
} }
}); });
// Let a tabSize of 2 win even if it is not the maximum // Let a tabSize of 2 win over 4 only if it has at least 2/3 of the occurrences of 4
// (only in case 4 was guessed) // This helps detect 2-space indentation in cases like YAML files where there might be
if (tabSize === 4 && spacesDiffCount[4] > 0 && spacesDiffCount[2] > 0 && spacesDiffCount[2] >= spacesDiffCount[4] / 2) { // some 4-space diffs from deeper nesting, while still preferring 4 when it's clearly predominant
if (tabSize === 4 && spacesDiffCount[4] > 0 && spacesDiffCount[2] > 0 && spacesDiffCount[2] >= spacesDiffCount[4] * 2 / 3) {
tabSize = 2; tabSize = 2;
} }
} }
// console.log('--------------------------'); // console.log('--------------------------');
// console.log('linesIndentedWithTabsCount: ' + linesIndentedWithTabsCount + ', linesIndentedWithSpacesCount: ' + linesIndentedWithSpacesCount); // console.log('linesIndentedWithTabsCount: ' + linesIndentedWithTabsCount + ', linesIndentedWithSpacesCount: ' + linesIndentedWithSpacesCount);
// console.log('spacesDiffCount: ' + spacesDiffCount); // console.log('spacesDiffCount: ' + spacesDiffCount);

View File

@@ -708,6 +708,74 @@ suite('Editor Model - TextModel', () => {
]); ]);
}); });
test('issue #65668: YAML file indented with 2 spaces', () => {
// Full YAML file from the issue - should detect as 2 spaces
assertGuess(true, 2, [
'version: 2',
'',
'jobs:',
' build:',
' docker:',
' - circleci/golang:1.11',
'',
' environment:',
' TEST_RESULTS: /tmp/test-results',
'',
' steps:',
' - checkout',
' - run: mkdir -p $TEST_RESULTS',
'',
' - restore_cache:',
' keys:',
' - v1-pkg-cache',
'',
' - run:',
' name: dep ensure',
' command: dep ensure -v',
'',
' - run:',
' name: Run unit tests',
' command: |',
' trap "go-junit-report <${TEST_RESULTS}/go-test.out > ${TEST_RESULTS}/go-test-report.xml" EXIT',
' go test -v ./... | tee ${TEST_RESULTS}/go-test.out',
'',
' - run:',
' name: Build',
' command: go build -v',
'',
' - save_cache:',
' key: v1-pkg-cache',
' paths:',
' - "/go/pkg"',
'',
' - store_artifacts:',
' path: /tmp/test-results',
' destination: raw-test-output',
'',
' - store_test_results:',
' path: /tmp/test-results',
]);
});
test('issue #249040: 4-space indent should win over 2-space when predominant', () => {
// File with mostly 4-space indents but some 2-space indents should detect as 4 spaces
assertGuess(true, 4, [
'function foo() {',
' let a = 1;',
' let b = 2;',
' if (true) {',
' console.log(a);',
' console.log(b);',
' }',
' const obj = {',
' x: 1,', // 2-space indent here
' y: 2', // 2-space indent here
' };',
' return obj;',
'}',
]);
});
test('validatePosition', () => { test('validatePosition', () => {
const m = createTextModel('line one\nline two'); const m = createTextModel('line one\nline two');