From b84837942040ea6fce38fff9aae71eea1c839e5f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 11 Dec 2012 15:54:01 -0800 Subject: [PATCH] Support scrolling to buffer and screen positions --- spec/app/editor-spec.coffee | 12 ++++++------ src/app/editor.coffee | 15 ++++++++++++--- .../outline-view/src/outline-view.coffee | 4 +--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index 355f234bd..34b78d86f 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -1005,12 +1005,12 @@ describe "Editor", -> editor.addCursorAtBufferPosition([6,50]) [cursor1, cursor2] = editor.getCursors() - spyOn(editor, 'scrollTo') + spyOn(editor, 'scrollToPixelPosition') cursor1.setScreenPosition([10, 10]) - expect(editor.scrollTo).not.toHaveBeenCalled() + expect(editor.scrollToPixelPosition).not.toHaveBeenCalled() cursor2.setScreenPosition([11, 11]) - expect(editor.scrollTo).toHaveBeenCalled() + expect(editor.scrollToPixelPosition).toHaveBeenCalled() describe "when the last cursor exceeds the upper or lower scroll margins", -> describe "when the editor is taller than twice the vertical scroll margin", -> @@ -1396,7 +1396,7 @@ describe "Editor", -> describe "when lines are added", -> beforeEach -> editor.attachToDom(heightInLines: 5) - spyOn(editor, "scrollTo") + spyOn(editor, "scrollToPixelPosition") describe "when the change precedes the first rendered row", -> it "inserts and removes rendered lines to account for upstream change", -> @@ -1448,7 +1448,7 @@ describe "Editor", -> describe "when lines are removed", -> beforeEach -> editor.attachToDom(heightInLines: 5) - spyOn(editor, "scrollTo") + spyOn(editor, "scrollToPixelPosition") it "sets the rendered screen line's width to either the max line length or the scollView's width (whichever is greater)", -> maxLineLength = editor.maxScreenLineLength() @@ -1614,7 +1614,7 @@ describe "Editor", -> describe "when lines are inserted", -> it "re-renders the correct line number range in the gutter", -> - spyOn(editor, 'scrollTo') + spyOn(editor, 'scrollToPixelPosition') editor.scrollTop(3 * editor.lineHeight) expect(editor.gutter.find('.line-number:first').text()).toBe '2' expect(editor.gutter.find('.line-number:last').text()).toBe '11' diff --git a/src/app/editor.coffee b/src/app/editor.coffee index ad40e03ac..5fd94b04c 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -497,7 +497,13 @@ class Editor extends View scrollToBottom: -> @scrollBottom(@screenLineCount() * @lineHeight) - scrollTo: (pixelPosition, options) -> + scrollToBufferPosition: (bufferPosition, options) -> + @scrollToPixelPosition(@pixelPositionForBufferPosition(bufferPosition), options) + + scrollToScreenPosition: (screenPosition, options) -> + @scrollToPixelPosition(@pixelPositionForScreenPosition(screenPosition), options) + + scrollToPixelPosition: (pixelPosition, options) -> return unless @attached @scrollVertically(pixelPosition, options) @scrollHorizontally(pixelPosition) @@ -800,11 +806,11 @@ class Editor extends View autoscroll: (options={}) -> for cursorView in @getCursorViews() when cursorView.needsAutoscroll() - @scrollTo(cursorView.getPixelPosition()) unless options.suppressAutoScroll + @scrollToPixelPosition(cursorView.getPixelPosition()) unless options.suppressAutoScroll cursorView.autoscrolled() for selectionView in @getSelectionViews() when selectionView.needsAutoscroll() - @scrollTo(selectionView.getCenterPixelPosition(), center: true) + @scrollToPixelPosition(selectionView.getCenterPixelPosition(), center: true) selectionView.autoscrolled() updateRenderedLines: -> @@ -1025,6 +1031,9 @@ class Editor extends View @renderedLines.find('.line').each (n) -> console.log n, $(this).text() + pixelPositionForBufferPosition: (position) -> + @pixelPositionForScreenPosition(@screenPositionForBufferPosition(position)) + pixelPositionForScreenPosition: (position) -> position = Point.fromObject(position) { top: position.row * @lineHeight, left: position.column * @charWidth } diff --git a/src/extensions/outline-view/src/outline-view.coffee b/src/extensions/outline-view/src/outline-view.coffee index f4c08ec4b..2b9b688ba 100644 --- a/src/extensions/outline-view/src/outline-view.coffee +++ b/src/extensions/outline-view/src/outline-view.coffee @@ -51,9 +51,7 @@ class OutlineView extends SelectList confirmed : ({position, name}) -> @cancel() editor = @rootView.getActiveEditor() - screenPosition = editor.screenPositionForBufferPosition(position) - pixelPosition = editor.pixelPositionForScreenPosition(screenPosition) - editor.scrollTo(pixelPosition, center: true) + editor.scrollToBufferPosition(position, center: true) editor.setCursorBufferPosition(position) cancelled: ->