Bug 998718 - Indentation detection is wrong when there's a majority of outliers. r=msucan
authorHeather Arthur <fayearthur@gmail.com>
Tue, 22 Apr 2014 15:14:00 -0400
changeset 198772 4fc153a9d91b143575c8a785eacda7f037d9fbcf
parent 198771 76604cbff5ab3ef8cde1ae8625324d45a195c50c
child 198773 4af2b4c97db50b3db7d9834d5e7fbb81c9d95d6d
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmsucan
bugs998718
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 998718 - Indentation detection is wrong when there's a majority of outliers. r=msucan
browser/devtools/sourceeditor/editor.js
browser/devtools/sourceeditor/test/browser_detectindent.js
--- a/browser/devtools/sourceeditor/editor.js
+++ b/browser/devtools/sourceeditor/editor.js
@@ -1031,39 +1031,42 @@ function detectIndentation(ed) {
       return;
     }
     if (width > 1) {
       total++;
     }
 
     // see how much this line is offset from the line above it
     let indent = Math.abs(width - last);
-    if (indent > 1) {
+    if (indent > 1 && indent <= 8) {
       spaces[indent] = (spaces[indent] || 0) + 1;
     }
     last = width;
   });
 
   // this file is not indented at all
   if (total == 0) {
     return null;
   }
 
   // mark as tabs if they start more than half the lines
   if (tabs >= total / 2) {
     return { tabs: true };
   }
 
   // find most frequent non-zero width difference between adjacent lines
-  let freqIndent = null, max = 0;
+  let freqIndent = null, max = 1;
   for (let width in spaces) {
     width = parseInt(width, 10);
     let tally = spaces[width];
     if (tally > max) {
       max = tally;
       freqIndent = width;
     }
   }
+  if (!freqIndent) {
+    return null;
+  }
 
   return { tabs: false, spaces: freqIndent };
 }
 
 module.exports = Editor;
--- a/browser/devtools/sourceeditor/test/browser_detectindent.js
+++ b/browser/devtools/sourceeditor/test/browser_detectindent.js
@@ -52,26 +52,39 @@ const TABS_CODE = [
 "\tbackground: blue;",
 "}",
 "",
 "span {",
 "\tpadding-left: 10px;",
 "}"
 ].join("\n");
 
+const NONE_CODE = [
+"var x = 0;",
+"           // stray thing",
+"var y = 9;",
+"    ",
+""
+].join("\n");
 
 function test() {
   waitForExplicitFinish();
 
   setup((ed, win) => {
     is(ed.getOption("indentUnit"), 2,
        "2 spaces before code added");
     is(ed.getOption("indentWithTabs"), false,
        "spaces is default");
 
+    ed.setText(NONE_CODE);
+    is(ed.getOption("indentUnit"), 2,
+       "2 spaces after un-detectable code");
+    is(ed.getOption("indentWithTabs"), false,
+       "spaces still set after un-detectable code");
+
     ed.setText(FOUR_SPACES_CODE);
     is(ed.getOption("indentUnit"), 4,
        "4 spaces detected in 4 space code");
     is(ed.getOption("indentWithTabs"), false,
        "spaces detected in 4 space code");
 
     ed.setText(TWO_SPACES_CODE);
     is(ed.getOption("indentUnit"), 2,