From eae80ca46daeefc683291e1e19720f41dd993468 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Wed, 12 Nov 2014 11:50:22 -0800 Subject: [PATCH] Add a check for negative bounds when translating the overlay --- spec/text-editor-component-spec.coffee | 51 ++++++++++++++++++++++++++ src/overlay-manager.coffee | 6 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/spec/text-editor-component-spec.coffee b/spec/text-editor-component-spec.coffee index 41eaafece..ba95f371c 100644 --- a/spec/text-editor-component-spec.coffee +++ b/spec/text-editor-component-spec.coffee @@ -1319,6 +1319,57 @@ describe "TextEditorComponent", -> expect(overlay.style.left).toBe position.left + 'px' expect(overlay.style.top).toBe position.top - itemHeight + 'px' + describe "when the editor is very small", -> + beforeEach -> + gutterWidth = componentNode.querySelector('.gutter').offsetWidth + windowWidth = gutterWidth + 6 * editor.getDefaultCharWidth() + windowHeight = 6 * editor.getLineHeightInPixels() + + wrapperNode.style.width = windowWidth + 'px' + wrapperNode.style.height = windowHeight + 'px' + + component.measureHeightAndWidth() + nextAnimationFrame() + + it "does not flip horizontally and force the overlay to have a negative left", -> + marker = editor.displayBuffer.markBufferRange([[0, 2], [0, 2]], invalidate: 'never') + decoration = editor.decorateMarker(marker, {type: 'overlay', item}) + nextAnimationFrame() + + position = editor.pixelPositionForBufferPosition([0, 2]) + + overlay = component.getTopmostDOMNode().querySelector('atom-overlay') + expect(overlay.style.left).toBe position.left + 'px' + expect(overlay.style.top).toBe position.top + editor.getLineHeightInPixels() + 'px' + + editor.insertText('a') + nextAnimationFrame() + + position = editor.pixelPositionForBufferPosition([0, 3]) + + expect(overlay.style.left).toBe position.left + 'px' + expect(overlay.style.top).toBe position.top + editor.getLineHeightInPixels() + 'px' + + it "does not flip vertically and force the overlay to have a negative top", -> + marker = editor.displayBuffer.markBufferRange([[1, 0], [1, 0]], invalidate: 'never') + decoration = editor.decorateMarker(marker, {type: 'overlay', item}) + nextAnimationFrame() + + position = editor.pixelPositionForBufferPosition([1, 0]) + + overlay = component.getTopmostDOMNode().querySelector('atom-overlay') + expect(overlay.style.left).toBe position.left + 'px' + expect(overlay.style.top).toBe position.top + editor.getLineHeightInPixels() + 'px' + + editor.insertNewline() + nextAnimationFrame() + + position = editor.pixelPositionForBufferPosition([2, 0]) + + expect(overlay.style.left).toBe position.left + 'px' + expect(overlay.style.top).toBe position.top + editor.getLineHeightInPixels() + 'px' + + describe "when editor scroll position is not 0", -> it "flips horizontally when near the right edge", -> editor.setScrollLeft(2 * editor.getDefaultCharWidth()) diff --git a/src/overlay-manager.coffee b/src/overlay-manager.coffee index 62b4780e4..aee83008d 100644 --- a/src/overlay-manager.coffee +++ b/src/overlay-manager.coffee @@ -36,10 +36,12 @@ class OverlayManager itemHeight = item.offsetHeight left = pixelPosition.left - left -= itemWidth if left + itemWidth - editor.getScrollLeft() > editor.getWidth() + if left + itemWidth - editor.getScrollLeft() > editor.getWidth() and left - itemWidth >= editor.getScrollLeft() + left -= itemWidth top = pixelPosition.top + lineHeightInPixels - top -= itemHeight + lineHeightInPixels if top + itemHeight - editor.getScrollTop() > editor.getHeight() + if top + itemHeight - editor.getScrollTop() > editor.getHeight() and top - itemHeight - lineHeightInPixels >= editor.getScrollTop() + top -= itemHeight + lineHeightInPixels overlay.style.top = top + 'px' overlay.style.left = left + 'px'