kopia lustrzana https://github.com/c9/core
Merge pull request +6368 from c9/fix/tab-guess
Be more cautious when detecting indentation of mixed filespull/39/head^2
commit
8a1c216acb
|
@ -46,19 +46,20 @@ exports.$detectIndentation = function(lines, fallback) {
|
||||||
if (!/^\s*[^*+\-\s]/.test(line))
|
if (!/^\s*[^*+\-\s]/.test(line))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (line[0] == "\t")
|
if (line[0] == "\t") {
|
||||||
tabIndents++;
|
tabIndents++;
|
||||||
|
prevSpaces = -Number.MAX_VALUE;
|
||||||
var spaces = line.match(/^ */)[0].length;
|
} else {
|
||||||
if (spaces && line[spaces] != "\t") {
|
var spaces = line.match(/^ */)[0].length;
|
||||||
var diff = spaces - prevSpaces;
|
if (spaces && line[spaces] != "\t") {
|
||||||
if (diff > 0 && !(prevSpaces%diff) && !(spaces%diff))
|
var diff = spaces - prevSpaces;
|
||||||
changes[diff] = (changes[diff] || 0) + 1;
|
if (diff > 0 && !(prevSpaces%diff) && !(spaces%diff))
|
||||||
|
changes[diff] = (changes[diff] || 0) + 1;
|
||||||
stats[spaces] = (stats[spaces] || 0) + 1;
|
|
||||||
|
stats[spaces] = (stats[spaces] || 0) + 1;
|
||||||
|
}
|
||||||
|
prevSpaces = spaces;
|
||||||
}
|
}
|
||||||
prevSpaces = spaces;
|
|
||||||
|
|
||||||
// ignore lines ending with backslash
|
// ignore lines ending with backslash
|
||||||
while (i < max && line[line.length - 1] == "\\")
|
while (i < max && line[line.length - 1] == "\\")
|
||||||
line = lines[i++];
|
line = lines[i++];
|
||||||
|
@ -81,7 +82,7 @@ exports.$detectIndentation = function(lines, fallback) {
|
||||||
spaceIndents = score;
|
spaceIndents = score;
|
||||||
score = stats[1] ? 0.9 : 0.8;
|
score = stats[1] ? 0.9 : 0.8;
|
||||||
if (!stats.length)
|
if (!stats.length)
|
||||||
score = 0
|
score = 0;
|
||||||
} else
|
} else
|
||||||
score /= spaceIndents;
|
score /= spaceIndents;
|
||||||
|
|
||||||
|
@ -95,9 +96,11 @@ exports.$detectIndentation = function(lines, fallback) {
|
||||||
if (first.score && first.score > 1.4)
|
if (first.score && first.score > 1.4)
|
||||||
var tabLength = first.length;
|
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};
|
return {ch: "\t", length: tabLength};
|
||||||
|
}
|
||||||
if (spaceIndents > tabIndents + 1)
|
if (spaceIndents > tabIndents + 1)
|
||||||
return {ch: " ", length: tabLength};
|
return {ch: " ", length: tabLength};
|
||||||
};
|
};
|
||||||
|
|
|
@ -29,7 +29,13 @@ module.exports = {
|
||||||
assert.equal(indent.ch, "\t");
|
assert.equal(indent.ch, "\t");
|
||||||
assert.equal(indent.length, undefined);
|
assert.equal(indent.length, undefined);
|
||||||
|
|
||||||
s.insert({row: 0, column: 0}, " ");
|
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);
|
indent = whitespace.$detectIndentation(s.doc.$lines);
|
||||||
assert.equal(indent.ch, "\t");
|
assert.equal(indent.ch, "\t");
|
||||||
assert.equal(indent.length, 4);
|
assert.equal(indent.length, 4);
|
||||||
|
|
|
@ -632,6 +632,11 @@ define(function(require, exports, module) {
|
||||||
max: "64",
|
max: "64",
|
||||||
position: 100
|
position: 100
|
||||||
},
|
},
|
||||||
|
"Autodetect Tab Size on Load" : {
|
||||||
|
type: "checkbox",
|
||||||
|
path: "project/ace/@guessTabSize",
|
||||||
|
position: 150
|
||||||
|
},
|
||||||
"New File Line Endings" : {
|
"New File Line Endings" : {
|
||||||
type: "dropdown",
|
type: "dropdown",
|
||||||
path: "project/ace/@newLineMode",
|
path: "project/ace/@newLineMode",
|
||||||
|
@ -2052,7 +2057,8 @@ define(function(require, exports, module) {
|
||||||
|
|
||||||
function detectSettingsOnLoad(c9Session) {
|
function detectSettingsOnLoad(c9Session) {
|
||||||
var session = c9Session.session;
|
var session = c9Session.session;
|
||||||
whitespaceUtil.detectIndentation(session);
|
if (settings.get("project/ace/@guessTabSize"))
|
||||||
|
whitespaceUtil.detectIndentation(session);
|
||||||
if (!session.syntax) {
|
if (!session.syntax) {
|
||||||
var syntax = detectSyntax(c9Session);
|
var syntax = detectSyntax(c9Session);
|
||||||
if (syntax)
|
if (syntax)
|
||||||
|
|
Ładowanie…
Reference in New Issue