   scopesForBufferPosition: (bufferPosition) ->
     @tokenizedBuffer.scopesForPosition(bufferPosition)
 
-  # Public: Retrieves the grammar's token for a buffer position.
+  # Retrieves the grammar's token for a buffer position.
   #
   # bufferPosition - A {Point} in the {Buffer}.
   #


    lineFragments

  ### Public ###

  # Public: Given a line, finds the point where it would wrap.
  #
  # line - The {String} to check
  # softWrapColumn - The {Number} where you want soft wrapping to occur
  #
  # Returns a {Number} representing the `line` position where the wrap would take place.
  # Returns `null` if a wrap wouldn't occur.
  findWrapColumn: (line, softWrapColumn) ->
    return unless line.length > softWrapColumn

    if /\s/.test(line[softWrapColumn])
      # search forward for the start of a word past the boundary
      for column in [softWrapColumn..line.length]
        return column if /\S/.test(line[column])
      return line.length
    else
      # search backward for the start of the word on the boundary
      for column in [softWrapColumn..0]
        return column + 1 if /\s/.test(line[column])
      return softWrapColumn

  # Public: Given a range in screen coordinates, this expands it to the start and end of a line
  #
  # screenRange - The {Range} to expand
  #
  # Returns a new {Range}.
  expandScreenRangeToLineEnds: (screenRange) ->
    screenRange = Range.fromObject(screenRange)
    { start, end } = screenRange
    new Range([start.row, 0], [end.row, @lineMap.lineForScreenRow(end.row).text.length])

  # Public: Given a range in buffer coordinates, this expands it to the start and end of a line
  #
  # screenRange - The {Range} to expand
  #
  # Returns a new {Range}.
  expandBufferRangeToLineEnds: (bufferRange) ->
    bufferRange = Range.fromObject(bufferRange)
    { start, end } = bufferRange
    new Range([start.row, 0], [end.row, Infinity])

  # Public: Calculates a {Range} representing the start of the {Buffer} until the end.
  #
  # Returns a {Range}.
  rangeForAllLines: ->
    new Range([0, 0], @clipScreenPosition([Infinity, Infinity]))

  # Public: Retrieves a {DisplayBufferMarker} based on its id.
  #
  # id - A {Number} representing a marker id
  #
  # Returns the {DisplayBufferMarker} (if it exists).
  getMarker: (id) ->
    @markers[id] ? new DisplayBufferMarker({id, displayBuffer: this})

  # Public: Retrieves the active markers in the buffer.
  #
  # Returns an {Array} of existing {DisplayBufferMarker}s.
  getMarkers: ->
    _.values(@markers)

  # Public: Constructs a new marker at the given screen range.
  #
  # range - The marker {Range} (representing the distance between the head and tail)
  # options - Options to pass to the {BufferMarker} constructor
  #
  # Returns a {Number} representing the new marker's ID.
  markScreenRange: (args...) ->
    bufferRange = @bufferRangeForScreenRange(args.shift())
    @markBufferRange(bufferRange, args...)

  # Public: Constructs a new marker at the given buffer range.
  #
  # range - The marker {Range} (representing the distance between the head and tail)
  # options - Options to pass to the {BufferMarker} constructor
  #
  # Returns a {Number} representing the new marker's ID.
  markBufferRange: (args...) ->
    @buffer.markRange(args...)

  # Public: Constructs a new marker at the given screen position.
  #
  # range - The marker {Range} (representing the distance between the head and tail)
  # options - Options to pass to the {BufferMarker} constructor
  #
  # Returns a {Number} representing the new marker's ID.
  markScreenPosition: (screenPosition, options) ->
    @markBufferPosition(@bufferPositionForScreenPosition(screenPosition), options)

  # Public: Constructs a new marker at the given buffer position.
  #
  # range - The marker {Range} (representing the distance between the head and tail)
  # options - Options to pass to the {BufferMarker} constructor
  #
  # Returns a {Number} representing the new marker's ID.
  markBufferPosition: (bufferPosition, options) ->
    @buffer.markPosition(bufferPosition, options)

  # Public: Removes the marker with the given id.
  #
  # id - The {Number} of the ID to remove
  destroyMarker: (id) ->
    @buffer.destroyMarker(id)
    delete @markers[id]

  # Public: Gets the screen range of the display marker.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Range}.
  getMarkerScreenRange: (id) ->
    @getMarker(id).getScreenRange()

  # Public: Modifies the screen range of the display marker.
  #
  # id - The {Number} of the ID to change
  # screenRange - The new {Range} to use
  # options - A hash of options matching those found in {BufferMarker.setRange}
  setMarkerScreenRange: (id, screenRange, options) ->
    @getMarker(id).setScreenRange(screenRange, options)

  # Public: Gets the buffer range of the display marker.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Range}.
  getMarkerBufferRange: (id) ->
    @getMarker(id).getBufferRange()

  # Public: Modifies the buffer range of the display marker.
  #
  # id - The {Number} of the ID to change
  # screenRange - The new {Range} to use
  # options - A hash of options matching those found in {BufferMarker.setRange}
  setMarkerBufferRange: (id, bufferRange, options) ->
    @getMarker(id).setBufferRange(bufferRange, options)

  # Public: Retrieves the screen position of the marker's head.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerScreenPosition: (id) ->
    @getMarkerHeadScreenPosition(id)

  # Public: Retrieves the buffer position of the marker's head.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerBufferPosition: (id) ->
    @getMarkerHeadBufferPosition(id)

  # Public: Retrieves the screen position of the marker's head.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerHeadScreenPosition: (id) ->
    @getMarker(id).getHeadScreenPosition()

  # Public: Sets the screen position of the marker's head.
  #
  # id - The {Number} of the ID to change
  # screenRange - The new {Point} to use
  # options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
  setMarkerHeadScreenPosition: (id, screenPosition, options) ->
    @getMarker(id).setHeadScreenPosition(screenPosition, options)

  # Public: Retrieves the buffer position of the marker's head.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerHeadBufferPosition: (id) ->
    @getMarker(id).getHeadBufferPosition()

  # Public: Sets the buffer position of the marker's head.
  #
  # id - The {Number} of the ID to check
  # screenRange - The new {Point} to use
  # options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
  setMarkerHeadBufferPosition: (id, bufferPosition) ->
    @getMarker(id).setHeadBufferPosition(bufferPosition)

  # Public: Retrieves the screen position of the marker's tail.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerTailScreenPosition: (id) ->
    @getMarker(id).getTailScreenPosition()

  # Public: Sets the screen position of the marker's tail.
  #
  # id - The {Number} of the ID to change
  # screenRange - The new {Point} to use
  # options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
  setMarkerTailScreenPosition: (id, screenPosition, options) ->
    @getMarker(id).setTailScreenPosition(screenPosition, options)

  # Public: Retrieves the buffer position of the marker's tail.
  #
  # id - The {Number} of the ID to check
  #
  # Returns a {Point}.
  getMarkerTailBufferPosition: (id) ->
    @getMarker(id).getTailBufferPosition()

  # Public: Sets the buffer position of the marker's tail.
  #
  # id - The {Number} of the ID to check
  # screenRange - The new {Point} to use
  # options - A hash of options matching those found in {DisplayBuffer.bufferPositionForScreenPosition}
  setMarkerTailBufferPosition: (id, bufferPosition) ->
    @getMarker(id).setTailBufferPosition(bufferPosition)

  # Public: Sets the marker's tail to the same position as the marker's head.
  #
  # This only works if there isn't already a tail position.
  #
  # id - A {Number} representing the marker to change
  #
  # Returns a {Point} representing the new tail position.
  placeMarkerTail: (id) ->
    @getMarker(id).placeTail()

  # Public: Removes the tail from the marker.
  #
  # id - A {Number} representing the marker to change
  clearMarkerTail: (id) ->
    @getMarker(id).clearTail()

  # Public: 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}.
  #
  # id - A {Number} representing the marker to check
  #
  # Returns a {Boolean}.
  isMarkerReversed: (id) ->
    @buffer.isMarkerReversed(id)

  # Public: Identifies if the marker's head position is equal to its tail.
  #
  # id - A {Number} representing the marker to check
  #
  # Returns a {Boolean}.
  isMarkerRangeEmpty: (id) ->
    @buffer.isMarkerRangeEmpty(id)

  # Public: Sets a callback to be fired whenever a marker is changed.
  #
  # id - A {Number} representing the marker to watch
  # callback - A {Function} to execute
  observeMarker: (id, callback) ->
    @getMarker(id).observe(callback)

  ### Internal ###

  pauseMarkerObservers: ->
    marker.pauseEvents() for marker in @getMarkers()



  ### Public ###

  # Sets the visibility of the tokenized buffer.
  #
  # visible - A {Boolean} indicating of the tokenized buffer is shown
  setVisible: (visible) -> @tokenizedBuffer.setVisible(visible)

  # Defines the limit at which the buffer begins to soft wrap text.
  #
  # softWrapColumn - A {Number} defining the soft wrap limit.
  setSoftWrapColumn: (@softWrapColumn) ->
