Update selection

This commit is contained in:
Garen Torikian
2013-05-01 16:43:34 -07:00
parent 75b40f603a
commit ddb09e98e8

View File

@@ -37,47 +37,49 @@ class Selection
clearAutoscroll: ->
@needsAutoscroll = null
### Public ###
# Public: Identifies if the selection is highlighting anything.
# Identifies if the selection is highlighting anything.
#
# Returns a {Boolean}.
isEmpty: ->
@getBufferRange().isEmpty()
# Public: Identifies if the selection is reversed, that is, it is highlighting "up."
# Identifies if the ending position of a marker is greater than the starting position.
#
# This can happen when, for example, you highlight text "up" in a {Buffer}.
#
# Returns a {Boolean}.
isReversed: ->
@editSession.isMarkerReversed(@marker)
# Public: Identifies if the selection is a single line.
# Identifies if the selection is a single line.
#
# Returns a {Boolean}.
isSingleScreenLine: ->
@getScreenRange().isSingleLine()
# Public: Retrieves the screen range for the selection.
# Retrieves the screen range for the selection.
#
# Returns a {Range}.
getScreenRange: ->
@editSession.getMarkerScreenRange(@marker)
# Public: Modifies the screen range for the selection.
# Modifies the screen range for the selection.
#
# screenRange - The new {Range} to use
# options - A hash of options matching those found in {.setBufferRange}
setScreenRange: (screenRange, options) ->
@setBufferRange(@editSession.bufferRangeForScreenRange(screenRange), options)
# Public: Retrieves the buffer range for the selection.
# Retrieves the buffer range for the selection.
#
# Returns a {Range}.
getBufferRange: ->
@editSession.getMarkerBufferRange(@marker)
# Public: Modifies the buffer range for the selection.
# Modifies the buffer range for the selection.
#
# screenRange - The new {Range} to select
# options - A hash of options with the following keys:
@@ -92,7 +94,7 @@ class Selection
@cursor.needsAutoscroll = false if options.autoscroll?
@editSession.setMarkerBufferRange(@marker, bufferRange, options)
# Public: Retrieves the starting and ending buffer rows the selection is highlighting.
# Retrieves the starting and ending buffer rows the selection is highlighting.
#
# Returns an {Array} of two {Number}s: the starting row, and the ending row.
getBufferRowRange: ->
@@ -102,22 +104,17 @@ class Selection
end = Math.max(start, end - 1) if range.end.column == 0
[start, end]
# Internal:
screenRangeChanged: ->
screenRange = @getScreenRange()
@trigger 'screen-range-changed', screenRange
# Public: Retrieves the text in the selection.
# Retrieves the text in the selection.
#
# Returns a {String}.
getText: ->
@editSession.buffer.getTextInRange(@getBufferRange())
# Public: Clears the selection, moving the marker to move to the head.
# Clears the selection, moving the marker to move to the head.
clear: ->
@editSession.clearMarkerTail(@marker)
# Public: Modifies the selection to mark the current word.
# Modifies the selection to mark the current word.
#
# Returns a {Range}.
selectWord: ->
@@ -131,7 +128,7 @@ class Selection
expandOverWord: ->
@setBufferRange(@getBufferRange().union(@cursor.getCurrentWordBufferRange()))
# Public: Selects an entire line in the {Buffer}.
# Selects an entire line in the {Buffer}.
#
# row - The line {Number} to select (default: the row of the cursor)
selectLine: (row=@cursor.getBufferPosition().row) ->
@@ -145,7 +142,7 @@ class Selection
range = @getBufferRange().union(@cursor.getCurrentLineBufferRange(includeNewline: true))
@setBufferRange(range)
# Public: Selects the text from the current cursor position to a given screen position.
# Selects the text from the current cursor position to a given screen position.
#
# position - An instance of {Point}, with a given `row` and `column`.
selectToScreenPosition: (position) ->
@@ -163,61 +160,61 @@ class Selection
else if @wordwise
@expandOverWord()
# Public: Selects the text from the current cursor position to a given buffer position.
# Selects the text from the current cursor position to a given buffer position.
#
# position - An instance of {Point}, with a given `row` and `column`.
selectToBufferPosition: (position) ->
@modifySelection => @cursor.setBufferPosition(position)
# Public: Selects the text one position right of the cursor.
# Selects the text one position right of the cursor.
selectRight: ->
@modifySelection => @cursor.moveRight()
# Public: Selects the text one position left of the cursor.
# Selects the text one position left of the cursor.
selectLeft: ->
@modifySelection => @cursor.moveLeft()
# Public: Selects all the text one position above the cursor.
# Selects all the text one position above the cursor.
selectUp: ->
@modifySelection => @cursor.moveUp()
# Public: Selects all the text one position below the cursor.
# Selects all the text one position below the cursor.
selectDown: ->
@modifySelection => @cursor.moveDown()
# Public: Selects all the text from the current cursor position to the top of the buffer.
# Selects all the text from the current cursor position to the top of the buffer.
selectToTop: ->
@modifySelection => @cursor.moveToTop()
# Public: Selects all the text from the current cursor position to the bottom of the buffer.
# Selects all the text from the current cursor position to the bottom of the buffer.
selectToBottom: ->
@modifySelection => @cursor.moveToBottom()
# Public: Selects all the text in the buffer.
# Selects all the text in the buffer.
selectAll: ->
@setBufferRange(@editSession.buffer.getRange(), autoscroll: false)
# Public: Selects all the text from the current cursor position to the beginning of the line.
# Selects all the text from the current cursor position to the beginning of the line.
selectToBeginningOfLine: ->
@modifySelection => @cursor.moveToBeginningOfLine()
# Public: Selects all the text from the current cursor position to the end of the line.
# Selects all the text from the current cursor position to the end of the line.
selectToEndOfLine: ->
@modifySelection => @cursor.moveToEndOfLine()
# Public: Selects all the text from the current cursor position to the beginning of the word.
# Selects all the text from the current cursor position to the beginning of the word.
selectToBeginningOfWord: ->
@modifySelection => @cursor.moveToBeginningOfWord()
# Public: Selects all the text from the current cursor position to the end of the word.
# Selects all the text from the current cursor position to the end of the word.
selectToEndOfWord: ->
@modifySelection => @cursor.moveToEndOfWord()
# Public: Selects all the text from the current cursor position to the beginning of the next word.
# Selects all the text from the current cursor position to the beginning of the next word.
selectToBeginningOfNextWord: ->
@modifySelection => @cursor.moveToBeginningOfNextWord()
# Public: Moves the selection down one row.
# Moves the selection down one row.
addSelectionBelow: ->
range = (@goalBufferRange ? @getBufferRange()).copy()
nextRow = range.end.row + 1
@@ -235,7 +232,7 @@ class Selection
@editSession.addSelectionForBufferRange(range, goalBufferRange: range, suppressMerge: true)
break
# Public: Moves the selection up one row.
# Moves the selection up one row.
addSelectionAbove: ->
range = (@goalBufferRange ? @getBufferRange()).copy()
previousRow = range.end.row - 1
@@ -253,7 +250,7 @@ class Selection
@editSession.addSelectionForBufferRange(range, goalBufferRange: range, suppressMerge: true)
break
# Public: Replaces text at the current selection.
# Replaces text at the current selection.
#
# text - A {String} representing the text to add
# options - A hash containing the following options:
@@ -281,9 +278,9 @@ class Selection
newBufferRange
# Public: Indents the selection.
# Indents the selection.
#
# options - A hash with one key, `autoIndent`. If `true`, the indentation is
# options - A hash with one key, `autoIndent`. If `true`, the indentation is
# performed appropriately. Otherwise, {EditSession#getTabText} is used
indent: ({ autoIndent }={})->
{ row, column } = @cursor.getBufferPosition()
@@ -300,7 +297,7 @@ class Selection
else
@indentSelectedRows()
# Public: If the selection spans multiple rows, indents all of them.
# If the selection spans multiple rows, indents all of them.
indentSelectedRows: ->
[start, end] = @getBufferRowRange()
for row in [start..end]
@@ -346,7 +343,7 @@ class Selection
desiredIndentString = @editSession.buildIndentString(desiredIndentLevel)
line.replace(/^[\t ]*/, desiredIndentString)
# Public: Performs a backspace, removing the character found behind the selection.
# Performs a backspace, removing the character found behind the selection.
backspace: ->
if @isEmpty() and not @editSession.isFoldedAtScreenRow(@cursor.getScreenRow())
if @cursor.isAtBeginningOfLine() and @editSession.isFoldedAtScreenRow(@cursor.getScreenRow() - 1)
@@ -356,12 +353,12 @@ class Selection
@deleteSelectedText()
# Public: Performs a backspace to the beginning of the current word, removing characters found there.
# Performs a backspace to the beginning of the current word, removing characters found there.
backspaceToBeginningOfWord: ->
@selectToBeginningOfWord() if @isEmpty()
@deleteSelectedText()
# Public: Performs a backspace to the beginning of the current line, removing characters found there.
# Performs a backspace to the beginning of the current line, removing characters found there.
backspaceToBeginningOfLine: ->
if @isEmpty() and @cursor.isAtBeginningOfLine()
@selectLeft()
@@ -369,7 +366,7 @@ class Selection
@selectToBeginningOfLine()
@deleteSelectedText()
# Public: Performs a delete, removing the character found ahead of the cursor position.
# Performs a delete, removing the character found ahead of the cursor position.
delete: ->
if @isEmpty()
if @cursor.isAtEndOfLine() and fold = @editSession.largestFoldStartingAtScreenRow(@cursor.getScreenRow() + 1)
@@ -378,12 +375,12 @@ class Selection
@selectRight()
@deleteSelectedText()
# Public: Performs a delete to the end of the current word, removing characters found there.
# Performs a delete to the end of the current word, removing characters found there.
deleteToEndOfWord: ->
@selectToEndOfWord() if @isEmpty()
@deleteSelectedText()
# Public: Deletes the selected text.
# Deletes the selected text.
deleteSelectedText: ->
bufferRange = @getBufferRange()
if fold = @editSession.largestFoldContainingBufferRow(bufferRange.end.row)
@@ -393,7 +390,7 @@ class Selection
@editSession.buffer.delete(bufferRange) unless bufferRange.isEmpty()
@cursor?.setBufferPosition(bufferRange.start)
# Public: Deletes the line.
# Deletes the line.
deleteLine: ->
if @isEmpty()
start = @cursor.getScreenRow()
@@ -410,7 +407,7 @@ class Selection
end--
@editSession.buffer.deleteRows(start, end)
# Public: Joins the current line with the one below it.
# Joins the current line with the one below it.
#
# If there selection spans more than one line, all the lines are joined together.
joinLine: ->
@@ -450,27 +447,27 @@ class Selection
[start, end] = @getBufferRowRange()
@editSession.autoIndentBufferRows(start, end)
# Public: Wraps the selected lines in comments.
# Wraps the selected lines in comments.
#
# Returns an {Array} of the commented {Ranges}.
toggleLineComments: ->
@editSession.toggleLineCommentsForBufferRows(@getBufferRowRange()...)
# Public: Performs a cut operation on the selection, until the end of the line.
# Performs a cut operation on the selection, until the end of the line.
#
# maintainPasteboard - A {Boolean} indicating TODO
cutToEndOfLine: (maintainPasteboard) ->
@selectToEndOfLine() if @isEmpty()
@cut(maintainPasteboard)
# Public: Performs a cut operation on the selection.
# Performs a cut operation on the selection.
#
# maintainPasteboard - A {Boolean} indicating TODO
cut: (maintainPasteboard=false) ->
@copy(maintainPasteboard)
@delete()
# Public: Performs a copy operation on the selection.
# Performs a copy operation on the selection.
#
# maintainPasteboard - A {Boolean} indicating TODO
copy: (maintainPasteboard=false) ->
@@ -484,7 +481,7 @@ class Selection
pasteboard.write(text, metadata)
# Public: Folds the selection.
# Folds the selection.
fold: ->
range = @getBufferRange()
@editSession.createFold(range.start.row, range.end.row)
@@ -505,7 +502,7 @@ class Selection
placeTail: ->
@editSession.placeMarkerTail(@marker)
# Public: Identifies if a selection intersects with a given buffer range.
# Identifies if a selection intersects with a given buffer range.
#
# bufferRange - A {Range} to check against
#
@@ -513,7 +510,7 @@ class Selection
intersectsBufferRange: (bufferRange) ->
@getBufferRange().intersectsWith(bufferRange)
# Public: Identifies if a selection intersects with another selection.
# Identifies if a selection intersects with another selection.
#
# otherSelection - A `Selection` to check against
#
@@ -521,7 +518,7 @@ class Selection
intersectsWith: (otherSelection) ->
@getBufferRange().intersectsWith(otherSelection.getBufferRange())
# Public: Merges two selections together.
# Merges two selections together.
#
# otherSelection - A `Selection` to merge with
# options - A hash of options matching those found in {.setBufferRange}
@@ -533,4 +530,10 @@ class Selection
@goalBufferRange = otherSelection.goalBufferRange
otherSelection.destroy()
### Internal ###
screenRangeChanged: ->
screenRange = @getScreenRange()
@trigger 'screen-range-changed', screenRange
_.extend Selection.prototype, EventEmitter