Can replace multiple selections on same line with newlines.

Also storing both buffer position and screen position of anchor in selection at the same time, so we can always reference either one. Whenever one is updated, we automatically update the other.
This commit is contained in:
Nathan Sobo
2012-03-26 18:16:55 -07:00
parent 37df49f77e
commit e6e24158ae
3 changed files with 45 additions and 27 deletions

View File

@@ -828,22 +828,40 @@ describe "Editor", ->
expect(cursor2.getBufferPosition()).toEqual [8,0]
describe "when selections are on the same line", ->
it "replaces each selection range with the inserted characters", ->
editor.attachToDom()
beforeEach ->
editor.setSelectionBufferRange([[0,4], [0,13]])
editor.addSelectionForBufferRange([[0,22], [0,24]])
editor.insertText("x")
describe "when inserting characters other than newlines", ->
it "replaces each selection range with the inserted characters", ->
editor.insertText("x")
[cursor1, cursor2] = editor.compositeCursor.getCursors()
[selection1, selection2] = editor.compositeSelection.getSelections()
[cursor1, cursor2] = editor.compositeCursor.getCursors()
[selection1, selection2] = editor.compositeSelection.getSelections()
expect(cursor1.getScreenPosition()).toEqual [0, 5]
expect(cursor2.getScreenPosition()).toEqual [0, 14]
expect(selection1.isEmpty()).toBeTruthy()
expect(selection2.isEmpty()).toBeTruthy()
expect(cursor1.getScreenPosition()).toEqual [0, 5]
expect(cursor2.getScreenPosition()).toEqual [0, 15]
expect(selection1.isEmpty()).toBeTruthy()
expect(selection2.isEmpty()).toBeTruthy()
expect(editor.lineForBufferRow(0)).toBe "var x = functix () {"
describe "when inserting newlines", ->
it "replaces all selected ranges with newlines", ->
editor.insertText("\n")
[cursor1, cursor2] = editor.compositeCursor.getCursors()
[selection1, selection2] = editor.compositeSelection.getSelections()
expect(cursor1.getScreenPosition()).toEqual [1, 0]
expect(cursor2.getScreenPosition()).toEqual [2, 0]
expect(selection1.isEmpty()).toBeTruthy()
expect(selection2.isEmpty()).toBeTruthy()
expect(editor.lineForBufferRow(0)).toBe "var "
expect(editor.lineForBufferRow(1)).toBe " = functi"
expect(editor.lineForBufferRow(2)).toBe " () {"
expect(editor.lineForBufferRow(0)).toBe "var x = functx () {"
describe "backspace", ->
describe "when cursors are on the same line", ->

View File

@@ -17,7 +17,7 @@ describe "Selection", ->
it "places the anchor at the start of the range and the cursor at the end", ->
range = new Range({row: 2, column: 7}, {row: 3, column: 18})
selection.setBufferRange(range)
expect(selection.anchorPosition).toEqual range.start
expect(selection.anchorScreenPosition).toEqual range.start
expect(selection.cursor.getScreenPosition()).toEqual range.end
describe ".delete()", ->

View File

@@ -1,6 +1,6 @@
Cursor = require 'cursor'
AceOutdentAdaptor = require 'ace-outdent-adaptor'
Point = require 'point'
Range = require 'range'
{View, $$} = require 'space-pen'
@@ -21,13 +21,11 @@ class Selection extends View
else
@clearSelection()
handleBufferChange: (e) ->
return unless @anchorPosition
return unless @anchorScreenPosition
{ oldRange, newRange } = e
position = @getAnchorBufferPosition()
position = @anchorBufferPosition
return if position.isLessThan(oldRange.end)
newRow = newRange.end.row
@@ -40,9 +38,8 @@ class Selection extends View
@setAnchorBufferPosition([newRow, newColumn])
clearSelection: ->
@anchorPosition = null
@anchorScreenPosition = null
@updateAppearance()
updateAppearance: ->
@@ -79,8 +76,8 @@ class Selection extends View
@regions = []
getScreenRange: ->
if @anchorPosition
new Range(@anchorPosition, @cursor.getScreenPosition())
if @anchorScreenPosition
new Range(@anchorScreenPosition, @cursor.getScreenPosition())
else
new Range(@cursor.getScreenPosition(), @cursor.getScreenPosition())
@@ -152,15 +149,18 @@ class Selection extends View
@retainSelection = false
placeAnchor: ->
return if @anchorPosition
cursorPosition = @cursor.getScreenPosition()
@anchorPosition = cursorPosition
return if @anchorScreenPosition
@setAnchorScreenPosition(@cursor.getScreenPosition())
getAnchorBufferPosition: ->
@editor.bufferPositionForScreenPosition(@anchorPosition)
setAnchorScreenPosition: (screenPosition) ->
bufferPosition = Point.fromObject(screenPosition)
@anchorScreenPosition = screenPosition
@anchorBufferPosition = @editor.bufferPositionForScreenPosition(screenPosition)
setAnchorBufferPosition: (position) ->
@anchorPosition = @editor.screenPositionForBufferPosition(position)
setAnchorBufferPosition: (bufferPosition) ->
bufferPosition = Point.fromObject(bufferPosition)
@anchorBufferPosition = bufferPosition
@anchorScreenPosition = @editor.screenPositionForBufferPosition(bufferPosition)
selectWord: ->
row = @cursor.getScreenRow()