diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index c516e3e90..97d959b35 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -653,6 +653,11 @@ describe "Editor", -> expect(editor.renderedLines.find(".line:first").text()).toBe buffer.lineForRow(0) expect(editor.renderedLines.find(".line:last").text()).toBe buffer.lineForRow(6) + it "increases the width of the rendered lines element if the max line length changes", -> + widthBefore = editor.renderedLines.width() + buffer.change([[12,0], [12,0]], [1..50].join('')) + expect(editor.renderedLines.width()).toBeGreaterThan widthBefore + describe "when lines are removed", -> beforeEach -> editor.attachToDom(heightInLines: 5) @@ -696,6 +701,11 @@ describe "Editor", -> expect(editor.renderedLines.find(".line:first").text()).toBe buffer.lineForRow(0) expect(editor.renderedLines.find(".line:last").text()).toBe buffer.lineForRow(6) + it "decreases the width of the rendered screen lines if the max line length changes", -> + widthBefore = editor.renderedLines.width() + buffer.delete([[6, 0], [6, Infinity]]) + expect(editor.renderedLines.width()).toBeLessThan widthBefore + describe "when folding leaves less then a screen worth of text (regression)", -> it "renders lines properly", -> editor.lineOverdraw = 1 diff --git a/spec/app/renderer-spec.coffee b/spec/app/renderer-spec.coffee index a064a74b0..61d04b5e1 100644 --- a/spec/app/renderer-spec.coffee +++ b/spec/app/renderer-spec.coffee @@ -600,5 +600,7 @@ describe "Renderer", -> expect(renderer.clipScreenPosition([0, 1], skipAtomicTokens: true)).toEqual [0, tabText.length] expect(renderer.clipScreenPosition([0, tabText.length], skipAtomicTokens: true)).toEqual [0, tabText.length] - describe ".bufferRowsForScreenRows()", -> - it "returns the buffer rows corresponding to each screen row in the given range", -> + describe ".maxLineLength()", -> + it "returns the length of the longest screen line", -> + expect(renderer.maxLineLength()).toBe 65 + diff --git a/src/app/editor.coffee b/src/app/editor.coffee index cd80b42c4..7afa01b7c 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -235,20 +235,11 @@ class Editor extends View @clearRenderedLines() @subscribeToFontSize() @calculateDimensions() - @setSoftWrapColumn() if @softWrap - @prepareForVerticalScrolling() - - # this also renders the visible lines - @setScrollPositionFromActiveEditSession() - - # TODO: The redundant assignment of scrollLeft below is needed because the - # lines weren't rendered when we called - # setScrollPositionFromActiveEditSession above. Remove this when we fix that - # problem by setting the width of the lines container based on the max line - # length - - @scrollView.scrollLeft(@getActiveEditSession().scrollLeft ? 0) @hiddenInput.width(@charWidth) + @setSoftWrapColumn() if @softWrap + @prepareForScrolling() + @setScrollPositionFromActiveEditSession() # this also renders the visible lines + $(window).on "resize.editor#{@id}", => @updateRenderedLines() @focus() if @isFocused @trigger 'editor-open', [this] @@ -264,10 +255,17 @@ class Editor extends View @compositeSelection.mergeIntersectingSelections({reverse}) @syncCursorAnimations() - prepareForVerticalScrolling: -> - linesHeight = @lineHeight * @screenLineCount() - @verticalScrollbarContent.height(linesHeight) - @renderedLines.css('padding-bottom', linesHeight) + prepareForScrolling: -> + @adjustHeightOfRenderedLines() + @adjustWidthOfRenderedLines() + + adjustHeightOfRenderedLines: -> + heightOfRenderedLines = @lineHeight * @screenLineCount() + @verticalScrollbarContent.height(heightOfRenderedLines) + @renderedLines.css('padding-bottom', heightOfRenderedLines) + + adjustWidthOfRenderedLines: -> + @renderedLines.width(@charWidth * @maxScreenLineLength()) scrollTop: (scrollTop, options) -> return @cachedScrollTop or 0 unless scrollTop? @@ -314,6 +312,9 @@ class Editor extends View screenLineCount: -> @renderer.lineCount() + maxScreenLineLength: -> + @renderer.maxLineLength() + getLastScreenRow: -> @screenLineCount() - 1 @@ -333,11 +334,9 @@ class Editor extends View @buffer.on "path-change.editor#{@id}", => @trigger 'editor-path-change' @renderer = new Renderer(@buffer, { softWrapColumn: @calcSoftWrapColumn(), tabText: @tabText }) - if @attached - @prepareForVerticalScrolling() - @renderLines() - + @prepareForScrolling() if @attached @loadEditSessionForBuffer(@buffer) + @renderLines() if @attached @buffer.on "change.editor#{@id}", (e) => @handleBufferChange(e) @renderer.on 'change', (e) => @handleRendererChange(e) @@ -452,6 +451,7 @@ class Editor extends View if @attached @verticalScrollbarContent.height(@lineHeight * @screenLineCount()) + @adjustWidthOfRenderedLines() return if oldScreenRange.start.row > @lastRenderedScreenRow diff --git a/src/app/line-map.coffee b/src/app/line-map.coffee index 3e12a71f2..a06c1254b 100644 --- a/src/app/line-map.coffee +++ b/src/app/line-map.coffee @@ -54,6 +54,13 @@ class LineMap lastScreenRow: -> @screenLineCount() - 1 + maxScreenLineLength: -> + maxLength = 0 + @traverseByDelta 'screenDelta', [0, 0], [@lastScreenRow(), 0], ({lineFragment}) -> + length = lineFragment.text.length + maxLength = length if length > maxLength + maxLength + screenPositionForBufferPosition: (bufferPosition, options) -> @translatePosition('bufferDelta', 'screenDelta', bufferPosition, options) diff --git a/src/app/renderer.coffee b/src/app/renderer.coffee index b5c5e70c7..57fa12de0 100644 --- a/src/app/renderer.coffee +++ b/src/app/renderer.coffee @@ -150,6 +150,9 @@ class Renderer lineCount: -> @lineMap.screenLineCount() + maxLineLength: -> + @lineMap.maxScreenLineLength() + screenPositionForBufferPosition: (position, options) -> @lineMap.screenPositionForBufferPosition(position, options)