Merge cursors after buffer changes, but not after movement

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-24 15:22:37 -07:00
parent 3d88477e9c
commit 205bc47b3e
5 changed files with 52 additions and 5 deletions

View File

@@ -769,6 +769,24 @@ describe "Editor", ->
expect(cursor2.position()).toEqual(top: 6 * editor.lineHeight, left: 0)
expect(cursor2.getBufferPosition()).toEqual [6, 0]
it "consolidates cursors when they overlap", ->
editor.setCursorScreenPosition([0, 0])
editor.addCursorAtScreenPosition([0, 1])
editor.addCursorAtScreenPosition([1, 1])
[cursor1, cursor2, cursor3] = editor.compositeCursor.getCursors()
expect(editor.compositeCursor.getCursors().length).toBe 3
editor.backspace()
expect(editor.compositeCursor.getCursors().length).toBe 2
expect(cursor1.getBufferPosition()).toEqual [0,0]
expect(cursor3.getBufferPosition()).toEqual [1,0]
expect(cursor2.parent().length).toBe 0
editor.insertText "x"
expect(editor.lineForBufferRow(0)).toBe "xar quicksort = function () {"
expect(editor.lineForBufferRow(1)).toBe "x var sort = function(items) {"
describe "inserting text", ->
describe "when cursors are on the same line", ->
describe "when inserting newlines", ->

View File

@@ -1,4 +1,5 @@
Cursor = require 'cursor'
_ = require 'underscore'
module.exports =
class CompositeCursor
@@ -20,15 +21,15 @@ class CompositeCursor
cursor = @addCursor()
cursor.setScreenPosition(screenPosition)
removeCursor: (cursor) ->
_.remove(@cursors, cursor)
setScreenPosition: (screenPosition) ->
cursor.setScreenPosition(screenPosition) for cursor in @cursors
getScreenPosition: ->
@cursors[0].getScreenPosition()
handleBufferChange: (e) ->
cursor.handleBufferChange(e) for cursor in @cursors
moveLeft: ->
cursor.moveLeft() for cursor in @cursors
@@ -40,3 +41,16 @@ class CompositeCursor
moveDown: ->
cursor.moveDown() for cursor in @cursors
handleBufferChange: (e) ->
cursor.handleBufferChange(e) for cursor in @cursors
@mergeCursors()
mergeCursors: ->
positions = []
for cursor in new Array(@cursors...)
position = cursor.getBufferPosition().toString()
if position in positions
cursor.remove()
else
positions.push(position)

View File

@@ -1,19 +1,27 @@
Selection = require 'selection'
_ = require 'underscore'
module.exports =
class CompositeSeleciton
constructor: (@editor) ->
@selections = []
getSelections: -> @selections
getSelections: -> new Array(@selections...)
addSelectionForCursor: (cursor) ->
selection = new Selection({@editor, cursor})
@selections.push(selection)
@editor.lines.append(selection)
removeSelectionForCursor: (cursor) ->
_.remove(@selections, @selectionForCursor(cursor))
selectionForCursor: (cursor) ->
_.find @selections, (selection) -> selection.cursor == cursor
insertText: (text) ->
selection.insertText(text) for selection in @selections
backspace: ->
selection.backspace() for selection in @selections
for selection in @getSelections()
selection.backspace()

View File

@@ -30,6 +30,11 @@ class Cursor extends View
@setBufferPosition([newRow, newColumn])
remove: ->
@editor.compositeCursor.removeCursor(this)
@editor.compositeSelection.removeSelectionForCursor(this)
super()
setScreenPosition: (position, options={}) ->
position = Point.fromObject(position)
clip = options.clip ? true

View File

@@ -74,3 +74,5 @@ class Point
inspect: ->
"(#{@row}, #{@column})"
toString: ->
"#{@row},#{@column}"