Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/operators/general-operators.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,20 @@ class Yank extends Operator
#
# Returns nothing.
execute: (count) ->
oldTop = @editor.getScrollTop()
oldLeft = @editor.getScrollLeft()
oldLastCursorPosition = @editor.getCursorBufferPosition()

originalPositions = @editor.getCursorBufferPositions()
if _.contains(@motion.select(count), true)
text = @editor.getSelectedText()
startPositions = _.pluck(@editor.getSelectedBufferRanges(), "start")
newPositions = for originalPosition, i in originalPositions
if startPositions[i] and (@vimState.mode is 'visual' or not @motion.isLinewise?())
Point.min(startPositions[i], originalPositions[i])
if startPositions[i]
position = Point.min(startPositions[i], originalPositions[i])
if @vimState.mode isnt 'visual' and @motion.isLinewise?()
position = new Point(position.row, originalPositions[i].column)
position
else
originalPosition
else
Expand All @@ -194,6 +201,11 @@ class Yank extends Operator
@setTextRegister(@register, text)

@editor.setSelectedBufferRanges(newPositions.map (p) -> new Range(p, p))

if oldLastCursorPosition.isEqual(@editor.getCursorBufferPosition())
@editor.setScrollLeft(oldLeft)
@editor.setScrollTop(oldTop)

@vimState.activateNormalMode()

#
Expand Down
60 changes: 55 additions & 5 deletions spec/operators-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ describe "Operators", ->
keydown('u')
expect(editor.getText()).toBe "abc\n012345\n\nxyz"


describe "with vim-mode.wrapLeftRightMotion", ->
beforeEach ->
editor.setText("abc\n012345\n\nxyz")
Expand Down Expand Up @@ -160,7 +159,6 @@ describe "Operators", ->
expect(editor.getCursorScreenPosition()).toEqual [0, 1]
expect(vimState.getRegister('"').text).toBe '0123\n\nx'


describe "on an empty line", ->
beforeEach ->
editor.setText("abc\n012345\n\nxyz")
Expand All @@ -178,7 +176,6 @@ describe "Operators", ->
expect(editor.getText()).toBe "abc\n012345\nxyz"
expect(editor.getCursorScreenPosition()).toEqual [2, 0]


describe "the X keybinding", ->
describe "on a line with content", ->
beforeEach ->
Expand Down Expand Up @@ -207,7 +204,6 @@ describe "Operators", ->
expect(editor.getCursorScreenPosition()).toEqual [0, 2]
expect(vimState.getRegister('"').text).toBe '\n'


describe "on an empty line", ->
beforeEach ->
editor.setText("012345\n\nabcdef")
Expand Down Expand Up @@ -745,8 +741,9 @@ describe "Operators", ->

describe "the y keybinding", ->
beforeEach ->
editor.getBuffer().setText("012 345\nabc\n")
editor.getBuffer().setText("012 345\nabc\ndefg\n")
editor.setCursorScreenPosition([0, 4])
vimState.setRegister('"', text: '345')

describe "when selected lines in visual linewise mode", ->
beforeEach ->
Expand Down Expand Up @@ -857,6 +854,18 @@ describe "Operators", ->
it "leaves the cursor at the starting position", ->
expect(editor.getCursorScreenPosition()).toEqual [0, 4]

describe "with an up motion", ->
beforeEach ->
editor.setCursorScreenPosition([2, 2])
keydown 'y'
keydown 'k'

it "saves both full lines to the default register", ->
expect(vimState.getRegister('"').text).toBe "abc\ndefg\n"

it "puts the cursor on the first line and the original column", ->
expect(editor.getCursorScreenPosition()).toEqual [1, 2]

describe "when followed by a G", ->
beforeEach ->
originalText = "12345\nabcde\nABCDE"
Expand Down Expand Up @@ -913,6 +922,47 @@ describe "Operators", ->
expect(vimState.getRegister('"').text).toBe '123'
expect(editor.getCursorBufferPositions()).toEqual [[0, 0], [1, 2]]

describe "in a long file", ->
beforeEach ->
editor.setHeight(400)
editor.setLineHeightInPixels(10)
editor.setDefaultCharWidth(10)
text = ""
for i in [1..200]
text += "#{i}\n"
editor.setText(text)

describe "yanking many lines forward", ->
it "does not scroll the window", ->
editor.setCursorBufferPosition [40, 1]
previousScrollTop = editor.getScrollTop()

# yank many lines
keydown('y')
keydown('1')
keydown('6')
keydown('0')
keydown('G', shift: true)

expect(editor.getScrollTop()).toEqual(previousScrollTop)
expect(editor.getCursorBufferPosition()).toEqual [40, 1]
expect(vimState.getRegister('"').text.split('\n').length).toBe 121

describe "yanking many lines backwards", ->
it "scrolls the window", ->
editor.setCursorBufferPosition [140, 1]
previousScrollTop = editor.getScrollTop()

# yank many lines
keydown('y')
keydown('6')
keydown('0')
keydown('G', shift: true)

expect(editor.getScrollTop()).toNotEqual previousScrollTop
expect(editor.getCursorBufferPosition()).toEqual [59, 1]
expect(vimState.getRegister('"').text.split('\n').length).toBe 83

describe "the yy keybinding", ->
describe "on a single line file", ->
beforeEach ->
Expand Down