be more cautious when detecting indentation of mixed files

pull/39/head^2
nightwing 2015-02-25 15:29:00 +04:00
rodzic 584e691a3f
commit 372cdf2545
2 zmienionych plików z 24 dodań i 15 usunięć

Wyświetl plik

@ -46,9 +46,10 @@ exports.$detectIndentation = function(lines, fallback) {
if (!/^\s*[^*+\-\s]/.test(line))
continue;
if (line[0] == "\t")
if (line[0] == "\t") {
tabIndents++;
prevSpaces = -Number.MAX_VALUE;
} else {
var spaces = line.match(/^ */)[0].length;
if (spaces && line[spaces] != "\t") {
var diff = spaces - prevSpaces;
@ -58,7 +59,7 @@ exports.$detectIndentation = function(lines, fallback) {
stats[spaces] = (stats[spaces] || 0) + 1;
}
prevSpaces = spaces;
}
// ignore lines ending with backslash
while (i < max && line[line.length - 1] == "\\")
line = lines[i++];
@ -81,7 +82,7 @@ exports.$detectIndentation = function(lines, fallback) {
spaceIndents = score;
score = stats[1] ? 0.9 : 0.8;
if (!stats.length)
score = 0
score = 0;
} else
score /= spaceIndents;
@ -95,9 +96,11 @@ exports.$detectIndentation = function(lines, fallback) {
if (first.score && first.score > 1.4)
var tabLength = first.length;
if (tabIndents > spaceIndents + 1)
if (tabIndents > spaceIndents + 1) {
if (tabLength == 1 || spaceIndents < tabIndents / 4 || first.score < 1.8)
tabLength = undefined;
return {ch: "\t", length: tabLength};
}
if (spaceIndents > tabIndents + 1)
return {ch: " ", length: tabLength};
};

Wyświetl plik

@ -32,6 +32,12 @@ module.exports = {
s.insert({row: 0, column: 0}, " ");
indent = whitespace.$detectIndentation(s.doc.$lines);
assert.equal(indent.ch, "\t");
assert.equal(indent.length, undefined);
s.doc.removeInLine(0, 0, 1);
s.insert({row: 0, column: 0}, "x\n y\n");
indent = whitespace.$detectIndentation(s.doc.$lines);
assert.equal(indent.ch, "\t");
assert.equal(indent.length, 4);
s.setValue("");