Skip to content

Commit 1155cdf

Browse files
committed
Fix microsoft#62143. Indentation guess may not be too eager with alignment.
1 parent 6e49853 commit 1155cdf

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

src/vs/editor/common/model/indentationGuesser.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,19 @@ export function guessIndentation(source: ITextBuffer, defaultTabSize: number, de
158158
spacesDiff(previousLineText, previousLineIndentation, currentLineText, currentLineIndentation, tmp);
159159

160160
if (tmp.looksLikeAlignment) {
161-
// skip this line entirely
162-
continue;
161+
// if defaultInsertSpaces === true && the spaces count == tabSize, we may want to count it as valid indentation
162+
//
163+
// - item1
164+
// - item2
165+
//
166+
// otherwise skip this line entirely
167+
//
168+
// const a = 1,
169+
// b = 2;
170+
171+
if (!(defaultInsertSpaces && defaultTabSize === tmp.spacesDiff)) {
172+
continue;
173+
}
163174
}
164175

165176
let currentSpacesDiff = tmp.spacesDiff;

src/vs/editor/test/common/model/textModel.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,46 @@ suite('Editor Model - TextModel', () => {
608608
]);
609609
});
610610

611+
test('issue #62143: Broken indentation detection', () => {
612+
// works before the fix
613+
assertGuess(true, 2, [
614+
'x',
615+
'x',
616+
' x',
617+
' x'
618+
]);
619+
620+
// works before the fix
621+
assertGuess(true, 2, [
622+
'x',
623+
' - item2',
624+
' - item3'
625+
]);
626+
627+
// works before the fix
628+
testGuessIndentation(true, 2, true, 2, [
629+
'x x',
630+
' x',
631+
' x',
632+
]);
633+
634+
// fails before the fix
635+
// empty space inline breaks the indentation guess
636+
testGuessIndentation(true, 2, true, 2, [
637+
'x x',
638+
' x',
639+
' x',
640+
' x'
641+
]);
642+
643+
testGuessIndentation(true, 2, true, 2, [
644+
'<!--test1.md -->',
645+
'- item1',
646+
' - item2',
647+
' - item3'
648+
]);
649+
});
650+
611651
test('validatePosition', () => {
612652

613653
let m = TextModel.createFromString('line one\nline two');

0 commit comments

Comments
 (0)