diff --git a/src/vs/editor/common/model/indentationGuesser.ts b/src/vs/editor/common/model/indentationGuesser.ts index ba6a7f64089..b3fb3ce0ac2 100644 --- a/src/vs/editor/common/model/indentationGuesser.ts +++ b/src/vs/editor/common/model/indentationGuesser.ts @@ -192,10 +192,7 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de // Guess tabSize only if inserting spaces... if (insertSpaces) { - let tabSizeScore = (insertSpaces ? 0 : 0.1 * linesCount); - - // console.log("score threshold: " + tabSizeScore); - + let tabSizeScore = 0; ALLOWED_TAB_SIZE_GUESSES.forEach((possibleTabSize) => { const possibleTabSizeScore = spacesDiffCount[possibleTabSize]; 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 - // (only in case 4 was guessed) - if (tabSize === 4 && spacesDiffCount[4] > 0 && spacesDiffCount[2] > 0 && spacesDiffCount[2] >= spacesDiffCount[4] / 2) { + // Let a tabSize of 2 win over 4 only if it has at least 2/3 of the occurrences of 4 + // This helps detect 2-space indentation in cases like YAML files where there might be + // 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; } } - // console.log('--------------------------'); // console.log('linesIndentedWithTabsCount: ' + linesIndentedWithTabsCount + ', linesIndentedWithSpacesCount: ' + linesIndentedWithSpacesCount); // console.log('spacesDiffCount: ' + spacesDiffCount); diff --git a/src/vs/editor/test/common/model/textModel.test.ts b/src/vs/editor/test/common/model/textModel.test.ts index c6c281bfe2b..286389ef0e9 100644 --- a/src/vs/editor/test/common/model/textModel.test.ts +++ b/src/vs/editor/test/common/model/textModel.test.ts @@ -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', () => { const m = createTextModel('line one\nline two');