Skip to content
This repository was archived by the owner on Apr 6, 2018. It is now read-only.
Closed
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
42 changes: 41 additions & 1 deletion lib/motions/general-motions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Motion
constructor: (@editor, @vimState) ->

select: (count, options) ->
if @isBlockwise()
@moveSelectionBlockwise(count, options)
return false

value = for selection in @editor.getSelections()
if @isLinewise()
@moveSelectionLinewise(selection, count, options)
Expand All @@ -27,14 +31,44 @@ class Motion
not selection.isEmpty()

@editor.mergeCursors()
@editor.mergeIntersectingSelections()
unless @isBlockwise()
@editor.mergeIntersectingSelections()
value

execute: (count) ->
for cursor in @editor.getCursors()
@moveCursor(cursor, count)
@editor.mergeCursors()

moveSelectionBlockwise: (count, options) ->
cursor = @editor.getLastCursor()
lastCol = cursor.getBufferColumn()
for sel in @editor.getSelections()
range = sel.getBufferRange()
lastCol = Math.max(Math.max(range.start.column, range.end.column), lastCol)

@moveCursor(cursor, count, options)

@vimState.blockwiseEnd = cursor.getBufferPosition()

if @operatesLinewise # maintain col
@vimState.blockwiseEnd.column = lastCol

startRow = Math.min(@vimState.blockwiseStart.row, @vimState.blockwiseEnd.row)
startCol = Math.min(@vimState.blockwiseStart.column - 1, @vimState.blockwiseEnd.column)

endRow = Math.max(@vimState.blockwiseStart.row, @vimState.blockwiseEnd.row)
endCol = Math.max(@vimState.blockwiseStart.column, @vimState.blockwiseEnd.column)

bufferRanges = []
for row in [startRow..endRow]
range = [[row, startCol], [row, endCol]]
if @editor.getTextInBufferRange(range).length > 0
bufferRanges.push(range)

if bufferRanges.length > 0
@editor.setSelectedBufferRanges bufferRanges

moveSelectionLinewise: (selection, count, options) ->
selection.modifySelection =>
[oldStartRow, oldEndRow] = selection.getBufferRowRange()
Expand Down Expand Up @@ -63,6 +97,7 @@ class Motion
moveSelectionInclusively: (selection, count, options) ->
selection.modifySelection =>
range = selection.getBufferRange()
console.log range
[oldStart, oldEnd] = [range.start, range.end]

wasEmpty = selection.isEmpty()
Expand Down Expand Up @@ -101,6 +136,11 @@ class Motion

isRecordable: -> false

isBlockwise: ->
if @vimState?.mode is 'visual'
return @vimState?.submode is 'blockwise'
false

isLinewise: ->
if @vimState?.mode is 'visual'
@vimState?.submode is 'linewise'
Expand Down
10 changes: 9 additions & 1 deletion lib/vim-state.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class VimState
mode: null
submode: null
destroyed: false
blockwiseStart: null
blockwiseEnd: null

constructor: (@editorElement, @statusBarManager, @globalVimState) ->
@emitter = new Emitter
Expand All @@ -30,7 +32,7 @@ class VimState

@subscriptions.add @editor.onDidChangeSelectionRange =>
if _.all(@editor.getSelections(), (selection) -> selection.isEmpty())
@activateCommandMode() if @mode is 'visual'
@activateCommandMode() if @mode is 'visual' and @submode isnt 'blockwise'
else
@activateVisualMode('characterwise') if @mode is 'command'

Expand Down Expand Up @@ -411,6 +413,8 @@ class VimState

deactivateVisualMode: ->
return unless @mode is 'visual'
@visualStart = null
@visualEnd = null
for selection in @editor.getSelections()
selection.cursor.moveLeft() unless selection.isEmpty()

Expand Down Expand Up @@ -438,6 +442,10 @@ class VimState
else if @editor.getSelectedText() is ''
@editor.selectRight()

if @submode is 'blockwise'
@blockwiseStart = @editor.getCursorBufferPosition()
@blockwiseEnd = @blockwiseStart

@updateStatusBar()

# Private: Used to re-enable visual mode
Expand Down