Add visibility to comments of Cursor

This commit is contained in:
Matt Colyer
2013-08-23 15:29:37 -07:00
parent ec37ec1e49
commit dd1a04aec0

View File

@@ -2,9 +2,11 @@
EventEmitter = require 'event-emitter'
_ = require 'underscore'
# Public: The `Cursor` class represents the little blinking line identifying where text can be inserted.
# Public: The `Cursor` class represents the little blinking line identifying
# where text can be inserted.
#
# Cursors have some metadata attached in the form of a {StringMarker}.
# Cursors belong to {EditSession}s and have some metadata attached in the form
# of a {StringMarker}.
module.exports =
class Cursor
_.extend @prototype, EventEmitter
@@ -15,8 +17,7 @@ class Cursor
visible: true
needsAutoscroll: null
### Internal ###
# Private: Instantiated by an {EditSession}
constructor: ({@editSession, @marker}) ->
@updateVisibility()
@marker.on 'changed', (e) =>
@@ -43,9 +44,11 @@ class Cursor
@trigger 'destroyed'
@needsAutoscroll = true
# Private:
destroy: ->
@marker.destroy()
# Private:
changePosition: (options, fn) ->
@goalColumn = null
@clearSelection()
@@ -53,66 +56,56 @@ class Cursor
unless fn()
@trigger 'autoscrolled' if @needsAutoscroll
### Public ###
# Moves a cursor to a given screen position.
# Public: 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:
# autoscroll: A {Boolean} which, if `true`, scrolls the {EditSession} to wherever the cursor moves to
#
setScreenPosition: (screenPosition, options={}) ->
@changePosition options, =>
@marker.setHeadScreenPosition(screenPosition, options)
# Gets the screen position of the cursor.
# Public: Gets the screen position of the cursor.
#
# Returns an {Array} of two numbers: the screen row, and the screen column.
getScreenPosition: ->
@marker.getHeadScreenPosition()
# Moves a cursor to a given buffer position.
# Public: 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:
# autoscroll: A {Boolean} which, if `true`, scrolls the {EditSession} to wherever the cursor moves to
#
setBufferPosition: (bufferPosition, options={}) ->
@changePosition options, =>
@marker.setHeadBufferPosition(bufferPosition, options)
# Gets the current buffer position.
# Public: Gets the current buffer position.
#
# Returns an {Array} of two numbers: the buffer row, and the buffer column.
getBufferPosition: ->
@marker.getHeadBufferPosition()
# If the marker range is empty, the cursor is marked as being visible.
# Public: If the marker range is empty, the cursor is marked as being visible.
updateVisibility: ->
@setVisible(@marker.getBufferRange().isEmpty())
# Sets the visibility of the cursor.
#
# visible - A {Boolean} indicating whether the cursor should be visible
# Public: Sets the {Boolean} visibility of the cursor.
setVisible: (visible) ->
if @visible != visible
@visible = visible
@needsAutoscroll ?= true if @visible and @isLastCursor()
@trigger 'visibility-changed', @visible
# Retrieves the visibility of the cursor.
#
# Returns a {Boolean}.
# Public: Returns the visibility of the cursor as a {Boolean}
isVisible: -> @visible
# Identifies what the cursor considers a "word" RegExp.
#
# Returns a {RegExp}.
# Public: Returns a {RegExp} of what the cursor considers a "word"
wordRegExp: ->
nonWordCharacters = config.get("editor.nonWordCharacters")
new RegExp("^[\t ]*$|[^\\s#{_.escapeRegExp(nonWordCharacters)}]+|[#{_.escapeRegExp(nonWordCharacters)}]+", "g")
# Identifies if this cursor is the last in the {EditSession}.
# Public: Identifies if this cursor is the last in the {EditSession}.
#
# "Last" is defined as the most recently added cursor.
#
@@ -120,7 +113,7 @@ class Cursor
isLastCursor: ->
this == @editSession.getCursor()
# Identifies if the cursor is surrounded by whitespace.
# Public: Identifies if the cursor is surrounded by whitespace.
#
# "Surrounded" here means that all characters before and after the cursor is whitespace.
#
@@ -130,50 +123,51 @@ class Cursor
range = [[row, Math.min(0, column - 1)], [row, Math.max(0, column + 1)]]
/^\s+$/.test @editSession.getTextInBufferRange(range)
# Public:
isInsideWord: ->
{row, column} = @getBufferPosition()
range = [[row, column], [row, Infinity]]
@editSession.getTextInBufferRange(range).search(@wordRegExp()) == 0
# Removes the setting for auto-scroll.
# Public: Removes the setting for auto-scroll.
clearAutoscroll: ->
@needsAutoscroll = null
# Deselects whatever the cursor is selecting.
# Public: Deselects whatever the cursor is selecting.
clearSelection: ->
@selection?.clear()
# Retrieves the cursor's screen row.
# Public: Retrieves the cursor's screen row.
#
# Returns a {Number}.
getScreenRow: ->
@getScreenPosition().row
# Retrieves the cursor's screen column.
# Public: Retrieves the cursor's screen column.
#
# Returns a {Number}.
getScreenColumn: ->
@getScreenPosition().column
# Retrieves the cursor's buffer row.
# Public: Retrieves the cursor's buffer row.
#
# Returns a {Number}.
getBufferRow: ->
@getBufferPosition().row
# Retrieves the cursor's buffer column.
# Public: Retrieves the cursor's buffer column.
#
# Returns a {Number}.
getBufferColumn: ->
@getBufferPosition().column
# Retrieves the cursor's buffer row text.
# Public: Retrieves the cursor's buffer row text.
#
# Returns a {String}.
getCurrentBufferLine: ->
@editSession.lineForBufferRow(@getBufferRow())
# Moves the cursor up one screen row.
# Public: Moves the cursor up one screen row.
moveUp: (rowCount = 1, {moveToEndOfSelection}={}) ->
range = @marker.getScreenRange()
if moveToEndOfSelection and not range.isEmpty()
@@ -185,7 +179,7 @@ class Cursor
@setScreenPosition({row: row - rowCount, column: column})
@goalColumn = column
# Moves the cursor down one screen row.
# Public: Moves the cursor down one screen row.
moveDown: (rowCount = 1, {moveToEndOfSelection}={}) ->
range = @marker.getScreenRange()
if moveToEndOfSelection and not range.isEmpty()
@@ -197,7 +191,7 @@ class Cursor
@setScreenPosition({row: row + rowCount, column: column})
@goalColumn = column
# Moves the cursor left one screen column.
# Public: Moves the cursor left one screen column.
#
# options -
# moveToEndOfSelection: true will move to the left of the selection if a selection
@@ -210,7 +204,7 @@ class Cursor
[row, column] = if column > 0 then [row, column - 1] else [row - 1, Infinity]
@setScreenPosition({row, column})
# Moves the cursor right one screen column.
# Public: Moves the cursor right one screen column.
#
# options -
# moveToEndOfSelection: true will move to the right of the selection if a selection
@@ -222,19 +216,19 @@ class Cursor
{ row, column } = @getScreenPosition()
@setScreenPosition([row, column + 1], skipAtomicTokens: true, wrapBeyondNewlines: true, wrapAtSoftNewlines: true)
# Moves the cursor to the top of the buffer.
# Public: Moves the cursor to the top of the buffer.
moveToTop: ->
@setBufferPosition([0,0])
# Moves the cursor to the bottom of the buffer.
# Public: Moves the cursor to the bottom of the buffer.
moveToBottom: ->
@setBufferPosition(@editSession.getEofBufferPosition())
# Moves the cursor to the beginning of the screen line.
# Public: Moves the cursor to the beginning of the screen line.
moveToBeginningOfLine: ->
@setScreenPosition([@getScreenRow(), 0])
# Moves the cursor to the beginning of the first character in the line.
# Public: Moves the cursor to the beginning of the first character in the line.
moveToFirstCharacterOfLine: ->
{row, column} = @getScreenPosition()
screenline = @editSession.lineForScreenRow(row)
@@ -245,7 +239,7 @@ class Cursor
goalColumn = 0 if goalColumn == column
@setScreenPosition([row, goalColumn])
# Moves the cursor to the beginning of the buffer line, skipping all whitespace.
# Public: Moves the cursor to the beginning of the buffer line, skipping all whitespace.
skipLeadingWhitespace: ->
position = @getBufferPosition()
scanRange = @getCurrentLineBufferRange()
@@ -255,35 +249,35 @@ class Cursor
@setBufferPosition(endOfLeadingWhitespace) if endOfLeadingWhitespace.isGreaterThan(position)
# Moves the cursor to the end of the buffer line.
# Public: Moves the cursor to the end of the buffer line.
moveToEndOfLine: ->
@setScreenPosition([@getScreenRow(), Infinity])
# Moves the cursor to the beginning of the word.
# Public: Moves the cursor to the beginning of the word.
moveToBeginningOfWord: ->
@setBufferPosition(@getBeginningOfCurrentWordBufferPosition())
# Moves the cursor to the end of the word.
# Public: Moves the cursor to the end of the word.
moveToEndOfWord: ->
if position = @getEndOfCurrentWordBufferPosition()
@setBufferPosition(position)
# Moves the cursor to the beginning of the next word.
# Public: Moves the cursor to the beginning of the next word.
moveToBeginningOfNextWord: ->
if position = @getBeginningOfNextWordBufferPosition()
@setBufferPosition(position)
# Moves the cursor to the previous word boundary.
# Public: Moves the cursor to the previous word boundary.
moveToPreviousWordBoundary: ->
if position = @getPreviousWordBoundaryBufferPosition()
@setBufferPosition(position)
# Moves the cursor to the next word boundary.
# Public: Moves the cursor to the next word boundary.
moveToNextWordBoundary: ->
if position = @getMoveNextWordBoundaryBufferPosition()
@setBufferPosition(position)
# Retrieves the buffer position of where the current word starts.
# Public: 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})
@@ -304,7 +298,7 @@ class Cursor
beginningOfWordPosition or currentBufferPosition
# Retrieves buffer position of previous word boiundry. It might be on the
# Public: Retrieves buffer position of previous word boiundry. It might be on the
# current word, or the previous word.
getPreviousWordBoundaryBufferPosition: (options = {}) ->
currentBufferPosition = @getBufferPosition()
@@ -326,7 +320,7 @@ class Cursor
beginningOfWordPosition or currentBufferPosition
# Retrieves buffer position of previous word boiundry. It might be on the
# Public: Retrieves buffer position of previous word boiundry. It might be on the
# current word, or the previous word.
getMoveNextWordBoundaryBufferPosition: (options = {}) ->
currentBufferPosition = @getBufferPosition()
@@ -347,7 +341,7 @@ class Cursor
endOfWordPosition or currentBufferPosition
# Retrieves the buffer position of where the current word ends.
# Public: 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})
@@ -367,7 +361,7 @@ class Cursor
endOfWordPosition ? currentBufferPosition
# Retrieves the buffer position of where the next word starts.
# Public: 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})
@@ -385,7 +379,7 @@ class Cursor
beginningOfNextWordPosition or currentBufferPosition
# Gets the word located under the cursor.
# Public: Gets the word located under the cursor.
#
# options - An object with properties based on {.getBeginningOfCurrentWordBufferPosition}.
#
@@ -395,7 +389,7 @@ class Cursor
endOptions = _.extend(_.clone(options), allowNext: false)
new Range(@getBeginningOfCurrentWordBufferPosition(startOptions), @getEndOfCurrentWordBufferPosition(endOptions))
# Retrieves the range for the current line.
# Public: Retrieves the range for the current line.
#
# options - A hash with the same keys as {EditSession.bufferRangeForBufferRow}
#
@@ -403,7 +397,7 @@ class Cursor
getCurrentLineBufferRange: (options) ->
@editSession.bufferRangeForBufferRow(@getBufferRow(), options)
# Retrieves the range for the current paragraph.
# Public: Retrieves the range for the current paragraph.
#
# A paragraph is defined as a block of text surrounded by empty lines.
#
@@ -411,19 +405,19 @@ class Cursor
getCurrentParagraphBufferRange: ->
@editSession.languageMode.rowRangeForParagraphAtBufferRow(@getBufferRow())
# Retrieves the characters that constitute a word preceeding the current cursor position.
# Public: Retrieves the characters that constitute a word preceeding the current cursor position.
#
# Returns a {String}.
getCurrentWordPrefix: ->
@editSession.getTextInBufferRange([@getBeginningOfCurrentWordBufferPosition(), @getBufferPosition()])
# Identifies if the cursor is at the start of a line.
# Public: Identifies if the cursor is at the start of a line.
#
# Returns a {Boolean}.
isAtBeginningOfLine: ->
@getBufferPosition().column == 0
# Retrieves the indentation level of the current line.
# Public: Retrieves the indentation level of the current line.
#
# Returns a {Number}.
getIndentLevel: ->
@@ -432,13 +426,13 @@ class Cursor
else
@getBufferColumn()
# Identifies if the cursor is at the end of a line.
# Public: Identifies if the cursor is at the end of a line.
#
# Returns a {Boolean}.
isAtEndOfLine: ->
@getBufferPosition().isEqual(@getCurrentLineBufferRange().end)
# Retrieves the grammar's token scopes for the line.
# Public: Retrieves the grammar's token scopes for the line.
#
# Returns an {Array} of {String}s.
getScopes: ->