Skip to content

Commit

Permalink
Rename stable: true to avoidOverlay: false and fix tests
Browse files Browse the repository at this point in the history
As part of the test fixes, I’m honoring the `autoscroll: false` option
in `insertText` and `insertNewline` to avoid inadvertently scrolling the
editor during tests when the editor is modified.
  • Loading branch information
Nathan Sobo committed Nov 28, 2016
1 parent f9ef678 commit f4c45c1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
24 changes: 11 additions & 13 deletions spec/text-editor-presenter-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2501,13 +2501,13 @@ describe "TextEditorPresenter", ->
pixelPosition: {top: 1 * 10, left: 26 * 10 + gutterWidth - scrollLeft}
}

expectStateUpdate presenter, -> editor.insertText('a')
expectStateUpdate presenter, -> editor.insertText('abc', autoscroll: false)
expectValues stateForOverlay(presenter, decoration), {
item: item
pixelPosition: {top: 1 * 10, left: windowWidth - itemWidth}
}

expectStateUpdate presenter, -> editor.insertText('b')
expectStateUpdate presenter, -> editor.insertText('d', autoscroll: false)
expectValues stateForOverlay(presenter, decoration), {
item: item
pixelPosition: {top: 1 * 10, left: windowWidth - itemWidth}
Expand All @@ -2528,18 +2528,17 @@ describe "TextEditorPresenter", ->
}

expectStateUpdate presenter, ->
editor.insertNewline()
presenter.setScrollTop(scrollTop) # I'm fighting the editor
editor.insertNewline(autoscroll: false)

expectValues stateForOverlay(presenter, decoration), {
item: item
pixelPosition: {top: 6 * 10 - scrollTop - itemHeight, left: gutterWidth}
}

it "does not slide horizontally when set to stable", ->
it "when avoidOverflow is false, does not move horizontally when overflowing the editor's scrollView horizontally", ->
scrollLeft = 20
marker = editor.markBufferPosition([0, 26], invalidate: 'never')
decoration = editor.decorateMarker(marker, {type: 'overlay', item, stable: true})
decoration = editor.decorateMarker(marker, {type: 'overlay', item, avoidOverflow: false})

presenter = buildPresenter({scrollLeft, windowWidth, windowHeight, contentFrameWidth, boundingClientRect, gutterWidth})
expectStateUpdate presenter, ->
Expand All @@ -2550,16 +2549,16 @@ describe "TextEditorPresenter", ->
pixelPosition: {top: 1 * 10, left: 26 * 10 + gutterWidth - scrollLeft}
}

expectStateUpdate presenter, -> editor.insertText('a')
expectStateUpdate presenter, -> editor.insertText('a', autoscroll: false)
expectValues stateForOverlay(presenter, decoration), {
item: item
pixelPosition: {top: 1 * 10, left: 26 * 10 + gutterWidth - scrollLeft}
pixelPosition: {top: 1 * 10, left: 27 * 10 + gutterWidth - scrollLeft}
}

it "does not flip vertically when set to stable", ->
it "when avoidOverflow is false, does not flip vertically when overflowing the editor's scrollView vertically", ->
scrollTop = 10
marker = editor.markBufferPosition([5, 0], invalidate: 'never')
decoration = editor.decorateMarker(marker, {type: 'overlay', item, stable: true})
decoration = editor.decorateMarker(marker, {type: 'overlay', item, avoidOverflow: false})

presenter = buildPresenter({scrollTop, windowWidth, windowHeight, contentFrameWidth, boundingClientRect, gutterWidth})
expectStateUpdate presenter, ->
Expand All @@ -2571,12 +2570,11 @@ describe "TextEditorPresenter", ->
}

expectStateUpdate presenter, ->
editor.insertNewline()
presenter.setScrollTop(scrollTop) # I'm fighting the editor
editor.insertNewline(autoscroll: false)

expectValues stateForOverlay(presenter, decoration), {
item: item
pixelPosition: {top: 6 * 10 - scrollTop, left: gutterWidth}
pixelPosition: {top: 7 * 10 - scrollTop, left: gutterWidth}
}

describe "when the overlay item has a margin", ->
Expand Down
4 changes: 2 additions & 2 deletions src/selection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class Selection extends Model
insertText: (text, options={}) ->
oldBufferRange = @getBufferRange()
wasReversed = @isReversed()
@clear()
@clear(options)

autoIndentFirstLine = false
precedingText = @editor.getTextInRange([[oldBufferRange.start.row, 0], oldBufferRange.start])
Expand Down Expand Up @@ -403,7 +403,7 @@ class Selection extends Model
else if options.autoDecreaseIndent and NonWhitespaceRegExp.test(text)
@editor.autoDecreaseIndentForBufferRow(newBufferRange.start.row)

@autoscroll() if @isLastSelection()
@autoscroll() if options.autoscroll ? @isLastSelection()

newBufferRange

Expand Down
5 changes: 2 additions & 3 deletions src/text-editor-presenter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ class TextEditorPresenter
for decoration in @model.getOverlayDecorations()
continue unless decoration.getMarker().isValid()

{item, position, class: klass, stable} = decoration.getProperties()
{item, position, class: klass, avoidOverflow} = decoration.getProperties()
if position is 'tail'
screenPosition = decoration.getMarker().getTailScreenPosition()
else
Expand All @@ -466,8 +466,7 @@ class TextEditorPresenter
if overlayDimensions = @overlayDimensions[decoration.id]
{itemWidth, itemHeight, contentMargin} = overlayDimensions

if not stable

if avoidOverflow isnt false
rightDiff = left + itemWidth + contentMargin - @windowWidth
left -= rightDiff if rightDiff > 0

Expand Down
4 changes: 2 additions & 2 deletions src/text-editor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,8 @@ class TextEditor extends Model
)

# Essential: For each selection, replace the selected text with a newline.
insertNewline: ->
@insertText('\n')
insertNewline: (options) ->
@insertText('\n', options)

# Essential: For each selection, if the selection is empty, delete the character
# following the cursor. Otherwise delete the selected text.
Expand Down

0 comments on commit f4c45c1

Please sign in to comment.