diff --git a/spec/app/cursor-spec.coffee b/spec/app/cursor-spec.coffee index be118af12..2dcf91af8 100644 --- a/spec/app/cursor-spec.coffee +++ b/spec/app/cursor-spec.coffee @@ -12,7 +12,7 @@ describe "Cursor", -> editor = new Editor editor.enableKeymap() editor.setBuffer(buffer) - cursor = editor.find('.cursor').view() + cursor = editor.getCursors()[0] describe "adding and removing of the idle class", -> it "removes the idle class while moving, then adds it back when it stops", -> @@ -43,3 +43,45 @@ describe "Cursor", -> cursor.setScreenPosition([1,30]) expect(cursor.isOnEOL()).toBeTruthy() + describe "vertical auto scroll", -> + beforeEach -> + editor.attachToDom() + editor.focus() + editor.vScrollMargin = 2 + + it "only attempts to scroll when a cursor is visible", -> + setEditorWidthInChars(editor, 20) + setEditorHeightInChars(editor, 10) + editor.setCursorBufferPosition([11,0]) + editor.addCursorAtBufferPosition([0,0]) + editor.addCursorAtBufferPosition([6,50]) + window.advanceClock() + + offscreenScrollHandler = spyOn(editor.getCursors()[0], 'autoScrollVertically') + onscreenScrollHandler = spyOn(editor.getCursors()[1], 'autoScrollVertically') + anotherOffscreenScrollHandler = spyOn(editor.getCursors()[2], 'autoScrollVertically') + + editor.moveCursorRight() + window.advanceClock() + expect(offscreenScrollHandler).not.toHaveBeenCalled() + expect(onscreenScrollHandler).toHaveBeenCalled() + expect(anotherOffscreenScrollHandler).not.toHaveBeenCalled() + + it "only attempts to scroll once when multiple cursors are visible", -> + setEditorWidthInChars(editor, 20) + setEditorHeightInChars(editor, 10) + editor.setCursorBufferPosition([11,0]) + editor.addCursorAtBufferPosition([0,0]) + editor.addCursorAtBufferPosition([6,0]) + window.advanceClock() + + offscreenScrollHandler = spyOn(editor.getCursors()[0], 'autoScrollVertically') + onscreenScrollHandler = spyOn(editor.getCursors()[1], 'autoScrollVertically') + anotherOnscreenScrollHandler = spyOn(editor.getCursors()[2], 'autoScrollVertically') + + editor.moveCursorRight() + window.advanceClock() + expect(offscreenScrollHandler).not.toHaveBeenCalled() + expect(onscreenScrollHandler).toHaveBeenCalled() + expect(anotherOnscreenScrollHandler).not.toHaveBeenCalled() + diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 9c1a3542a..8fc17791a 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -262,10 +262,12 @@ describe "Editor", -> expect(editor.scroller.scrollTop()).toBe(0) it "reduces scroll margins when there isn't enough height to maintain them and scroll smoothly", -> - editor.height(editor.lineHeight * 5) + setEditorHeightInChars(editor, 5) + + _.times 3, -> + editor.moveCursorDown() + window.advanceClock() - _.times 3, -> editor.moveCursorDown() - window.advanceClock() expect(editor.scroller.scrollTop()).toBe(editor.lineHeight) editor.moveCursorUp() @@ -1920,8 +1922,8 @@ describe "Editor", -> it "returns correct bounds based on scroll position", -> expect(editor.bounds()).toEqual [[0,0], [10, 10]] - editor.scrollTop(editor.lineHeight * 1) - editor.horizontalScroller.scrollLeft(editor.charWidth * 1) + editor.scroller.scrollTop(editor.lineHeight * 1) + editor.scroller.scrollLeft(editor.charWidth * 1) expect(editor.bounds()).toEqual [[1,1], [11, 11]] describe "screenPositionInBounds(screenPosition)", -> diff --git a/src/app/cursor.coffee b/src/app/cursor.coffee index 950ed0a9f..c7e54aa60 100644 --- a/src/app/cursor.coffee +++ b/src/app/cursor.coffee @@ -132,11 +132,21 @@ class Cursor extends View @setBufferPosition @editor.getEofPosition() updateAppearance: -> - position = @editor.pixelPositionForScreenPosition(@getScreenPosition()) + screenPosition = @getScreenPosition() + position = @editor.pixelPositionForScreenPosition(screenPosition) @css(position) + + if @editor.getCursors().length == 1 or @editor.screenPositionInBounds(screenPosition) + @autoScroll(position) + + autoScroll: (position) -> + return if @editor._autoScrolling + + @editor._autoScrolling = true _.defer => - @autoScrollVertically(position) - @autoScrollHorizontally(position) + @editor._autoScrolling = false + @autoScrollVertically(position) + @autoScrollHorizontally(position) autoScrollVertically: (position) -> linesInView = @editor.scroller.height() / @editor.lineHeight diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 5aaf0876f..6a9e6d2e4 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -163,10 +163,10 @@ class Editor extends View @parents('#root-view').view() bounds: -> - rows = @height() / @lineHeight - columns = @horizontalScroller.width() / @charWidth + rows = @scroller.height() / @lineHeight + columns = @scroller.width() / @charWidth - start = new Point(@scrollTop() / @lineHeight, @horizontalScroller.scrollLeft() / @charWidth) + start = new Point(@scroller.scrollTop() / @lineHeight, @scroller.scrollLeft() / @charWidth) end = new Point(start.row + rows, start.column + columns) new Range(start, end)