 Range = require 'range'
 Point = require 'point'
 
-# Public: Represents a fold that's hiding text from the screen.
+# Public: Represents a fold that collapses multiple buffer lines into a single
+# line on the screen.
 #
-# Folds are the primary reason that screen ranges and buffer ranges vary. Their
-# creation is managed by the {DisplayBuffer}.
+# Their creation is managed by the {DisplayBuffer}.
 module.exports =
 class Fold
   displayBuffer: null
   marker: null
 
+  # Internal
   constructor: (@displayBuffer, @marker) ->
     @displayBuffer.foldsByMarkerId[@marker.id] = this
     @updateDisplayBuffer()

      @updateDisplayBuffer() unless newRange.isEqual(oldRange)
    @marker.on 'destroyed', => @destroyed()

  updateDisplayBuffer: ->
    unless @isInsideLargerFold()
      @displayBuffer.updateScreenLines(@getStartRow(), @getEndRow(), 0, updateMarkers: true)

  isInsideLargerFold: ->
    @displayBuffer.findMarker(class: 'fold', containsBufferRange: @getBufferRange())?

  destroy: ->
    @marker.destroy()

  getBufferRange: ->
    @marker.getBufferRange()

  getStartRow: ->
    @getBufferRange().start.row

  getEndRow: ->
    @getBufferRange().end.row

  inspect: ->
    "Fold(#{@getStartRow()}, #{@getEndRow()})"



  ## Internal ##

  updateDisplayBuffer: ->
    unless @isInsideLargerFold()
      @displayBuffer.updateScreenLines(@getStartRow(), @getEndRow(), 0, updateMarkers: true)

  destroyed: ->
    delete @displayBuffer.foldsByMarkerId[@marker.id]
    @updateDisplayBuffer()
