diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 93460f173..b5858007e 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -724,6 +724,21 @@ describe "DisplayBuffer", -> expect(displayBuffer.clipScreenPosition([0, 1], clip: 'forward')).toEqual [0, tabLength] expect(displayBuffer.clipScreenPosition([0, tabLength], clip: 'forward')).toEqual [0, tabLength] + describe "::screenPositionForPixelPosition(pixelPosition)", -> + it "clips pixel positions above buffer start", -> + displayBuffer.setLineHeightInPixels(20) + expect(displayBuffer.screenPositionForPixelPosition(top: -Infinity, left: -Infinity)).toEqual [0, 0] + expect(displayBuffer.screenPositionForPixelPosition(top: -Infinity, left: Infinity)).toEqual [0, 0] + expect(displayBuffer.screenPositionForPixelPosition(top: -1, left: Infinity)).toEqual [0, 0] + expect(displayBuffer.screenPositionForPixelPosition(top: 0, left: Infinity)).toEqual [0, 29] + + it "clips pixel positions below buffer end", -> + displayBuffer.setLineHeightInPixels(20) + expect(displayBuffer.screenPositionForPixelPosition(top: Infinity, left: -Infinity)).toEqual [12, 2] + expect(displayBuffer.screenPositionForPixelPosition(top: Infinity, left: Infinity)).toEqual [12, 2] + expect(displayBuffer.screenPositionForPixelPosition(top: displayBuffer.getHeight() + 1, left: 0)).toEqual [12, 2] + expect(displayBuffer.screenPositionForPixelPosition(top: displayBuffer.getHeight() - 1, left: 0)).toEqual [12, 0] + describe "::screenPositionForBufferPosition(bufferPosition, options)", -> it "clips the specified buffer position", -> expect(displayBuffer.screenPositionForBufferPosition([0, 2])).toEqual [0, 2] diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index 5f9d2af2f..a5ca4c309 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -1419,6 +1419,22 @@ describe "TextEditorComponent", -> beforeEach -> linesNode = componentNode.querySelector('.lines') + describe "when the mouse is single-clicked above the first line", -> + it "moves the cursor to the start of file buffer position", -> + editor.setText('foo') + editor.setCursorBufferPosition([0, 3]) + height = 4.5 * lineHeightInPixels + wrapperNode.style.height = height + 'px' + wrapperNode.style.width = 10 * charWidth + 'px' + component.measureDimensions() + nextAnimationFrame() + + coordinates = clientCoordinatesForScreenPosition([0, 2]) + coordinates.clientY = -1 + linesNode.dispatchEvent(buildMouseEvent('mousedown', coordinates)) + nextAnimationFrame() + expect(editor.getCursorScreenPosition()).toEqual [0, 0] + describe "when the mouse is single-clicked below the last line", -> it "moves the cursor to the end of file buffer position", -> editor.setText('foo') diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index b60113661..6ec4ce859 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -692,6 +692,7 @@ class DisplayBuffer extends Model targetLeft = pixelPosition.left defaultCharWidth = @defaultCharWidth row = Math.floor(targetTop / @getLineHeightInPixels()) + targetLeft = 0 if row < 0 targetLeft = Infinity if row > @getLastRow() row = Math.min(row, @getLastRow()) row = Math.max(0, row)