提交 e573984e 编写于 作者: A Alex Dima

Fixes #1386: Guess insertSpaces:false if more lines are indented with tabs

上级 ccc0b4ac
......@@ -472,7 +472,7 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo
throw new Error('TextModel.guessIndentation: Model is disposed');
}
var i:number,
let i:number,
len:number,
factors = this._extractIndentationFactors(),
linesWithIndentationCount = factors.linesWithIndentationCount,
......@@ -481,29 +481,12 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo
relativeSpaceCounts = factors.relativeSpaceCounts;
// Count the absolute number of times tabs or spaces have been used as indentation
var linesIndentedWithSpaces = 0;
let linesIndentedWithSpaces = 0;
for (i = 1, len = absoluteSpaceCounts.length; i < len; i++) {
linesIndentedWithSpaces += (absoluteSpaceCounts[i] || 0);
}
// Give preference to spaces over tabs (when evidence is the same)
// or when there are not enough clues (too little indentation in the file)
if (linesIndentedWithTabs >= linesIndentedWithSpaces) {
return {
insertSpaces: true,
tabSize: defaultTabSize
};
}
if (linesWithIndentationCount < 6 && linesIndentedWithTabs > 0) {
// Making a guess with 6 indented lines, of which tabs are used besides spaces is very difficult
return {
insertSpaces: true,
tabSize: defaultTabSize
};
}
var candidate:number,
let candidate:number,
candidateScore:number,
penalization:number,
m:number,
......@@ -528,18 +511,18 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo
scores[candidate] = candidateScore / (1 + penalization);
}
// console.log('----------');
// console.log('linesWithIndentationCount: ', linesWithIndentationCount);
// console.log('linesIndentedWithTabs: ', linesIndentedWithTabs);
// console.log('absoluteSpaceCounts: ', absoluteSpaceCounts);
// console.log('relativeSpaceCounts: ', relativeSpaceCounts);
// console.log('=> linesIndentedWithSpaces: ', linesIndentedWithSpaces);
// console.log('=> scores: ', scores);
// console.log('----------');
// console.log('linesWithIndentationCount: ', linesWithIndentationCount);
// console.log('linesIndentedWithTabs: ', linesIndentedWithTabs);
// console.log('absoluteSpaceCounts: ', absoluteSpaceCounts);
// console.log('relativeSpaceCounts: ', relativeSpaceCounts);
// console.log('=> linesIndentedWithSpaces: ', linesIndentedWithSpaces);
// console.log('=> scores: ', scores);
var bestCandidate = defaultTabSize,
let bestCandidate = defaultTabSize,
bestCandidateScore = 0;
var allowedGuesses = [2, 4, 6, 8];
let allowedGuesses = [2, 4, 6, 8];
for (i = 0; i < allowedGuesses.length; i++) {
candidate = allowedGuesses[i];
......@@ -549,8 +532,15 @@ export class TextModel extends OrderGuaranteeEventEmitter implements EditorCommo
bestCandidateScore = candidateScore;
}
}
let insertSpaces = true;
if (linesIndentedWithTabs > linesIndentedWithSpaces) {
// More lines indented with tabs
insertSpaces = false;
}
return {
insertSpaces: true,
insertSpaces: insertSpaces,
tabSize: bestCandidate
};
}
......
......@@ -15,15 +15,11 @@ function testGuessIndentation(expectedInsertSpaces:boolean, expectedTabSize:numb
m.dispose();
assert.equal(r.insertSpaces, expectedInsertSpaces, msg);
if (expectedInsertSpaces) {
assert.equal(r.tabSize, expectedTabSize, msg);
} else {
assert.equal(r.tabSize, 1337, msg);
}
assert.equal(r.tabSize, expectedTabSize, msg);
}
function guessesTabs(text:string[], msg?:string): void {
testGuessIndentation(false, 0, text, msg);
function guessesTabs(expectedTabSize:number, text:string[], msg?:string): void {
testGuessIndentation(false, expectedTabSize, text, msg);
}
function guessesSpaces(expectedTabSize:number, text:string[], msg?:string): void {
......@@ -63,25 +59,110 @@ suite('Editor Model - TextModel', () => {
test('guess indentation 1', () => {
// Defaults to tabs
guessesSpaces(1337, [
'x',
'x',
'x',
'x',
'x',
'x',
'x'
]);
], 'no clues');
// Gives preference to tabs
guessesSpaces(1337, [
guessesTabs(1337, [
'\tx',
'x',
'x',
'x',
'x',
'x',
'x'
]);
guessesSpaces(1337, [
], 'no spaces, 1xTAB');
guessesSpaces(2, [
' x',
'x',
'x',
'x',
'x',
'x',
'x'
], '1x2');
guessesTabs(1337, [
'\tx',
'\tx',
'\tx',
'\tx',
'\tx',
'\tx',
'\tx'
], '7xTAB');
guessesSpaces(2, [
'\tx',
' x',
'\tx',
' x',
'\tx',
' x',
'\tx',
' x',
], '4x2, 4xTAB');
guessesTabs(1337, [
'\tx',
' x',
'\tx',
' x',
'\tx',
' x',
'\tx',
' x'
]);
guessesSpaces(1337, [
], '4x1, 4xTAB');
guessesTabs(2, [
'\tx',
'\tx',
' x',
'\tx',
' x',
'\tx',
' x',
'\tx',
' x',
], '4x2, 5xTAB');
guessesTabs(2, [
'\tx',
'\tx',
'x',
'\tx',
'x',
'\tx',
'x',
'\tx',
' x',
], '1x2, 5xTAB');
guessesTabs(4, [
'\tx',
'\tx',
'x',
'\tx',
' x'
]);
'x',
'\tx',
'x',
'\tx',
' x',
], '1x4, 5xTAB');
guessesTabs(2, [
'\tx',
'\tx',
'x',
'\tx',
'x',
'\tx',
' x',
'\tx',
' x',
], '1x2, 1x4, 5xTAB');
guessesSpaces(1337, [
'x',
......@@ -98,74 +179,156 @@ suite('Editor Model - TextModel', () => {
' ',
' ',
' ',
' ',
' ',
' ',
' ',
], 'whitespace lines don\'t count');
guessesSpaces(4, [
'x',
' x',
' x',
' x'
], 'odd number is not allowed: 2x3, 1x4');
' x',
'x',
' x',
' x',
' x',
'x',
' x',
' x',
' x',
], 'odd number is not allowed: 6x3, 3x4');
guessesSpaces(4, [
'x',
' x',
' x',
' x'
], 'odd number is not allowed: 2x5, 1x4');
' x',
'x',
' x',
' x',
' x',
'x',
' x',
' x',
' x',
], 'odd number is not allowed: 6x5, 3x4');
guessesSpaces(4, [
'x',
' x',
' x',
' x'
], 'odd number is not allowed: 2x7, 1x4');
' x',
'x',
' x',
' x',
' x',
'x',
' x',
' x',
' x',
], 'odd number is not allowed: 6x7, 3x4');
guessesSpaces(2, [
'x',
' x',
' x',
' x',
' x'
], '4x2');
' x',
'x',
' x',
' x',
' x',
' x',
], '8x2');
guessesSpaces(2, [
'x',
' x',
' x',
], '2x2');
'x',
' x',
' x',
'x',
' x',
' x',
'x',
' x',
' x',
], '8x2');
guessesSpaces(2, [
'x',
' x',
' x',
], '1x2, 1x4');
'x',
' x',
' x',
'x',
' x',
' x',
'x',
' x',
' x',
], '4x2, 4x4');
guessesSpaces(2, [
'x',
' x',
' x',
' x',
], '2x2, 1x4');
'x',
' x',
' x',
' x',
'x',
' x',
' x',
' x',
], '6x2, 3x4');
guessesSpaces(2, [
'x',
' x',
' x',
' x',
' x',
], '2x2, 2x4');
'x',
' x',
' x',
' x',
' x',
], '4x2, 4x4');
guessesSpaces(2, [
'x',
' x',
' x',
' x',
], '1x2, 2x4');
'x',
' x',
' x',
' x',
], '2x2, 4x4');
guessesSpaces(4, [
'x',
' x',
' x',
], '2x4');
'x',
' x',
' x',
'x',
' x',
' x',
'x',
' x',
' x',
], '8x4');
guessesSpaces(2, [
'x',
' x',
' x',
' x',
' x',
], '1x2, 2x4, 1x6');
'x',
' x',
' x',
' x',
' x',
], '2x2, 4x4, 2x6');
guessesSpaces(2, [
'x',
' x',
......@@ -182,7 +345,13 @@ suite('Editor Model - TextModel', () => {
' x',
' x',
' x',
], '3x4, 1x5, 1x8');
'x',
' x',
' x',
' x',
' x',
' x',
], '6x4, 2x5, 2x8');
guessesSpaces(4, [
'x',
' x',
......@@ -200,7 +369,14 @@ suite('Editor Model - TextModel', () => {
' x',
' x',
' x',
], '3x4, 1x5, 2x8');
'x',
'x',
' x',
' x',
' x',
' x',
' x',
], '6x4, 2x5, 4x8');
guessesSpaces(4, [
'x',
' x',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册