diff --git a/lib/codemirror.js b/lib/codemirror.js
index e9cfd9de9a23c3d479347789703d889cbc0b04d6..1ed13911947ca7d0b11a13d6fb1c36a7d012d9df 100644
--- a/lib/codemirror.js
+++ b/lib/codemirror.js
@@ -2032,16 +2032,19 @@
     var cm = op.cm, display = cm.display;
     if (op.updatedDisplay) updateHeightsInViewport(cm);
 
+    op.barMeasure = measureForScrollbars(cm);
+
     // If the max line changed since it was last measured, measure it,
     // and ensure the document's width matches it.
     // updateDisplay_W2 will use these properties to do the actual resizing
     if (display.maxLineChanged && !cm.options.lineWrapping) {
-      op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left;
+      op.adjustWidthTo = measureChar(cm, display.maxLine, display.maxLine.text.length).left + 3;
       op.maxScrollLeft = Math.max(0, display.sizer.offsetLeft + op.adjustWidthTo +
                                   scrollerCutOff - display.scroller.clientWidth);
+      var wDiff = op.adjustWidthTo - parseInt(display.sizer.style.minWidth || "0");
+      op.barMeasure.scrollWidth += wDiff;
     }
 
-    op.barMeasure = measureForScrollbars(cm);
     if (op.updatedDisplay || op.selectionChanged)
       op.newSelectionNodes = drawSelection(cm);
   }