mirror of
https://github.com/atom/atom.git
synced 2026-01-24 06:18:03 -05:00
Update cursor
This commit is contained in:
@@ -53,7 +53,7 @@ class Cursor
|
||||
|
||||
### Public ###
|
||||
|
||||
# Public: Moves a cursor to a given screen position.
|
||||
# Moves a cursor to a given screen position.
|
||||
#
|
||||
# screenPosition - An {Array} of two numbers: the screen row, and the screen column.
|
||||
# options - An object with the following keys:
|
||||
@@ -63,13 +63,13 @@ class Cursor
|
||||
@changePosition options, =>
|
||||
@editSession.setMarkerHeadScreenPosition(@marker, screenPosition, options)
|
||||
|
||||
# Public: Gets the screen position of the cursor.
|
||||
# Gets the screen position of the cursor.
|
||||
#
|
||||
# Returns an {Array} of two numbers: the screen row, and the screen column.
|
||||
getScreenPosition: ->
|
||||
@editSession.getMarkerHeadScreenPosition(@marker)
|
||||
|
||||
# Public: Moves a cursor to a given buffer position.
|
||||
# Moves a cursor to a given buffer position.
|
||||
#
|
||||
# bufferPosition - An {Array} of two numbers: the buffer row, and the buffer column.
|
||||
# options - An object with the following keys:
|
||||
@@ -79,17 +79,17 @@ class Cursor
|
||||
@changePosition options, =>
|
||||
@editSession.setMarkerHeadBufferPosition(@marker, bufferPosition, options)
|
||||
|
||||
# Public: Gets the current buffer position.
|
||||
# Gets the current buffer position.
|
||||
#
|
||||
# Returns an {Array} of two numbers: the buffer row, and the buffer column.
|
||||
getBufferPosition: ->
|
||||
@editSession.getMarkerHeadBufferPosition(@marker)
|
||||
|
||||
# Public: If the marker range is empty, the cursor is marked as being visible.
|
||||
# If the marker range is empty, the cursor is marked as being visible.
|
||||
updateVisibility: ->
|
||||
@setVisible(@editSession.isMarkerRangeEmpty(@marker))
|
||||
|
||||
# Public: Sets the visibility of the cursor.
|
||||
# Sets the visibility of the cursor.
|
||||
#
|
||||
# visible - A {Boolean} indicating whether the cursor should be visible
|
||||
setVisible: (visible) ->
|
||||
@@ -98,25 +98,27 @@ class Cursor
|
||||
@needsAutoscroll ?= true if @visible and @isLastCursor()
|
||||
@trigger 'visibility-changed', @visible
|
||||
|
||||
# Public: Retrieves the visibility of the cursor.
|
||||
# Retrieves the visibility of the cursor.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
isVisible: -> @visible
|
||||
|
||||
# Public: Identifies what the cursor considers a "word" RegExp.
|
||||
# Identifies what the cursor considers a "word" RegExp.
|
||||
#
|
||||
# Returns a {RegExp}.
|
||||
wordRegExp: ->
|
||||
nonWordCharacters = config.get("editor.nonWordCharacters")
|
||||
new RegExp("^[\t ]*$|[^\\s#{_.escapeRegExp(nonWordCharacters)}]+|[#{_.escapeRegExp(nonWordCharacters)}]+", "g")
|
||||
|
||||
# Public: Identifies if this cursor is the last in the {EditSession}.
|
||||
# Identifies if this cursor is the last in the {EditSession}.
|
||||
#
|
||||
# "Last" is defined as the most recently added cursor.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
isLastCursor: ->
|
||||
this == @editSession.getCursor()
|
||||
|
||||
# Public: Identifies if the cursor is surrounded by whitespace.
|
||||
# Identifies if the cursor is surrounded by whitespace.
|
||||
#
|
||||
# "Surrounded" here means that all characters before and after the cursor is whitespace.
|
||||
#
|
||||
@@ -126,84 +128,84 @@ class Cursor
|
||||
range = [[row, Math.min(0, column - 1)], [row, Math.max(0, column + 1)]]
|
||||
/^\s+$/.test @editSession.getTextInBufferRange(range)
|
||||
|
||||
# Public: Removes the setting for auto-scroll.
|
||||
# Removes the setting for auto-scroll.
|
||||
clearAutoscroll: ->
|
||||
@needsAutoscroll = null
|
||||
|
||||
# Public: Deselects whatever the cursor is selecting.
|
||||
# Deselects whatever the cursor is selecting.
|
||||
clearSelection: ->
|
||||
if @selection
|
||||
@selection.goalBufferRange = null
|
||||
@selection.clear() unless @selection.retainSelection
|
||||
|
||||
# Public: Retrieves the cursor's screen row.
|
||||
# Retrieves the cursor's screen row.
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getScreenRow: ->
|
||||
@getScreenPosition().row
|
||||
|
||||
# Public: Retrieves the cursor's screen column.
|
||||
# Retrieves the cursor's screen column.
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getScreenColumn: ->
|
||||
@getScreenPosition().column
|
||||
|
||||
# Public: Retrieves the cursor's buffer row.
|
||||
# Retrieves the cursor's buffer row.
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getBufferRow: ->
|
||||
@getBufferPosition().row
|
||||
|
||||
# Public: Retrieves the cursor's buffer column.
|
||||
# Retrieves the cursor's buffer column.
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getBufferColumn: ->
|
||||
@getBufferPosition().column
|
||||
|
||||
# Public: Retrieves the cursor's buffer row text.
|
||||
# Retrieves the cursor's buffer row text.
|
||||
#
|
||||
# Returns a {String}.
|
||||
getCurrentBufferLine: ->
|
||||
@editSession.lineForBufferRow(@getBufferRow())
|
||||
|
||||
# Public: Moves the cursor up one screen row.
|
||||
# Moves the cursor up one screen row.
|
||||
moveUp: (rowCount = 1) ->
|
||||
{ row, column } = @getScreenPosition()
|
||||
column = @goalColumn if @goalColumn?
|
||||
@setScreenPosition({row: row - rowCount, column: column})
|
||||
@goalColumn = column
|
||||
|
||||
# Public: Moves the cursor down one screen row.
|
||||
# Moves the cursor down one screen row.
|
||||
moveDown: (rowCount = 1) ->
|
||||
{ row, column } = @getScreenPosition()
|
||||
column = @goalColumn if @goalColumn?
|
||||
@setScreenPosition({row: row + rowCount, column: column})
|
||||
@goalColumn = column
|
||||
|
||||
# Public: Moves the cursor left one screen column.
|
||||
# Moves the cursor left one screen column.
|
||||
moveLeft: ->
|
||||
{ row, column } = @getScreenPosition()
|
||||
[row, column] = if column > 0 then [row, column - 1] else [row - 1, Infinity]
|
||||
@setScreenPosition({row, column})
|
||||
|
||||
# Public: Moves the cursor right one screen column.
|
||||
# Moves the cursor right one screen column.
|
||||
moveRight: ->
|
||||
{ row, column } = @getScreenPosition()
|
||||
@setScreenPosition([row, column + 1], skipAtomicTokens: true, wrapBeyondNewlines: true, wrapAtSoftNewlines: true)
|
||||
|
||||
# Public: Moves the cursor to the top of the buffer.
|
||||
# Moves the cursor to the top of the buffer.
|
||||
moveToTop: ->
|
||||
@setBufferPosition([0,0])
|
||||
|
||||
# Public: Moves the cursor to the bottom of the buffer.
|
||||
# Moves the cursor to the bottom of the buffer.
|
||||
moveToBottom: ->
|
||||
@setBufferPosition(@editSession.getEofBufferPosition())
|
||||
|
||||
# Public: Moves the cursor to the beginning of the buffer line.
|
||||
# Moves the cursor to the beginning of the buffer line.
|
||||
moveToBeginningOfLine: ->
|
||||
@setBufferPosition([@getBufferRow(), 0])
|
||||
|
||||
# Public: Moves the cursor to the beginning of the first character in the line.
|
||||
# Moves the cursor to the beginning of the first character in the line.
|
||||
moveToFirstCharacterOfLine: ->
|
||||
position = @getBufferPosition()
|
||||
scanRange = @getCurrentLineBufferRange()
|
||||
@@ -214,7 +216,7 @@ class Cursor
|
||||
newPosition = [position.row, 0] if newPosition.isEqual(position)
|
||||
@setBufferPosition(newPosition)
|
||||
|
||||
# Public: Moves the cursor to the beginning of the buffer line, skipping all whitespace.
|
||||
# Moves the cursor to the beginning of the buffer line, skipping all whitespace.
|
||||
skipLeadingWhitespace: ->
|
||||
position = @getBufferPosition()
|
||||
scanRange = @getCurrentLineBufferRange()
|
||||
@@ -224,25 +226,25 @@ class Cursor
|
||||
|
||||
@setBufferPosition(endOfLeadingWhitespace) if endOfLeadingWhitespace.isGreaterThan(position)
|
||||
|
||||
# Public: Moves the cursor to the end of the buffer line.
|
||||
# Moves the cursor to the end of the buffer line.
|
||||
moveToEndOfLine: ->
|
||||
@setBufferPosition([@getBufferRow(), Infinity])
|
||||
|
||||
# Public: Moves the cursor to the beginning of the word.
|
||||
# Moves the cursor to the beginning of the word.
|
||||
moveToBeginningOfWord: ->
|
||||
@setBufferPosition(@getBeginningOfCurrentWordBufferPosition())
|
||||
|
||||
# Public: Moves the cursor to the end of the word.
|
||||
# Moves the cursor to the end of the word.
|
||||
moveToEndOfWord: ->
|
||||
if position = @getEndOfCurrentWordBufferPosition()
|
||||
@setBufferPosition(position)
|
||||
|
||||
# Public: Moves the cursor to the beginning of the next word.
|
||||
# Moves the cursor to the beginning of the next word.
|
||||
moveToBeginningOfNextWord: ->
|
||||
if position = @getBeginningOfNextWordBufferPosition()
|
||||
@setBufferPosition(position)
|
||||
|
||||
# Public: Retrieves the buffer position of where the current word starts.
|
||||
# Retrieves the buffer position of where the current word starts.
|
||||
#
|
||||
# options - A hash with one option:
|
||||
# :wordRegex - A {RegExp} indicating what constitutes a "word" (default: {wordRegExp})
|
||||
@@ -263,7 +265,7 @@ class Cursor
|
||||
|
||||
beginningOfWordPosition or currentBufferPosition
|
||||
|
||||
# Public: Retrieves the buffer position of where the current word ends.
|
||||
# Retrieves the buffer position of where the current word ends.
|
||||
#
|
||||
# options - A hash with one option:
|
||||
# :wordRegex - A {RegExp} indicating what constitutes a "word" (default: {wordRegExp})
|
||||
@@ -283,7 +285,7 @@ class Cursor
|
||||
|
||||
endOfWordPosition ? currentBufferPosition
|
||||
|
||||
# Public: Retrieves the buffer position of where the next word starts.
|
||||
# Retrieves the buffer position of where the next word starts.
|
||||
#
|
||||
# options - A hash with one option:
|
||||
# :wordRegex - A {RegExp} indicating what constitutes a "word" (default: {wordRegExp})
|
||||
@@ -301,7 +303,7 @@ class Cursor
|
||||
|
||||
beginningOfNextWordPosition or currentBufferPosition
|
||||
|
||||
# Public: Gets the word located under the cursor.
|
||||
# Gets the word located under the cursor.
|
||||
#
|
||||
# options - An object with properties based on {.getBeginningOfCurrentWordBufferPosition}.
|
||||
#
|
||||
@@ -311,7 +313,7 @@ class Cursor
|
||||
endOptions = _.extend(_.clone(options), allowNext: false)
|
||||
new Range(@getBeginningOfCurrentWordBufferPosition(startOptions), @getEndOfCurrentWordBufferPosition(endOptions))
|
||||
|
||||
# Public: Retrieves the range for the current line.
|
||||
# Retrieves the range for the current line.
|
||||
#
|
||||
# options - A hash with the same keys as {EditSession.bufferRangeForBufferRow}
|
||||
#
|
||||
@@ -319,7 +321,7 @@ class Cursor
|
||||
getCurrentLineBufferRange: (options) ->
|
||||
@editSession.bufferRangeForBufferRow(@getBufferRow(), options)
|
||||
|
||||
# Public: Retrieves the range for the current paragraph.
|
||||
# Retrieves the range for the current paragraph.
|
||||
#
|
||||
# A paragraph is defined as a block of text surrounded by empty lines.
|
||||
#
|
||||
@@ -341,19 +343,19 @@ class Cursor
|
||||
|
||||
new Range([startRow, 0], [endRow, @editSession.lineLengthForBufferRow(endRow)])
|
||||
|
||||
# Public: Retrieves the characters that constitute a word preceeding the current cursor position.
|
||||
# Retrieves the characters that constitute a word preceeding the current cursor position.
|
||||
#
|
||||
# Returns a {String}.
|
||||
getCurrentWordPrefix: ->
|
||||
@editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(), @getBufferPosition()])
|
||||
|
||||
# Public: Identifies if the cursor is at the start of a line.
|
||||
# Identifies if the cursor is at the start of a line.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
isAtBeginningOfLine: ->
|
||||
@getBufferPosition().column == 0
|
||||
|
||||
# Public: Retrieves the indentation level of the current line.
|
||||
# Retrieves the indentation level of the current line.
|
||||
#
|
||||
# Returns a {Number}.
|
||||
getIndentLevel: ->
|
||||
@@ -362,13 +364,13 @@ class Cursor
|
||||
else
|
||||
@getBufferColumn()
|
||||
|
||||
# Public: Identifies if the cursor is at the end of a line.
|
||||
# Identifies if the cursor is at the end of a line.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
isAtEndOfLine: ->
|
||||
@getBufferPosition().isEqual(@getCurrentLineBufferRange().end)
|
||||
|
||||
# Public: Retrieves the grammar's token scopes for the line.
|
||||
# Retrieves the grammar's token scopes for the line.
|
||||
#
|
||||
# Returns an {Array} of {String}s.
|
||||
getScopes: ->
|
||||
|
||||
Reference in New Issue
Block a user