       if foldable
         hasFoldableLines = true
 
-      isFunction = @hasScopeAtBufferRow(editor, row,
-      'meta.function', 'meta.method',
-      'storage.type.arrow', 'entity.name.function.constructor')
+      isFunction = @hasScopeAtBufferRow(
+        editor,
+        row,
+        'meta.function',
+        'meta.method',
+        'storage.type.arrow',
+        'entity.name.function.constructor'
+      )
+      @debugMessage('fold functions: is foldable', (foldable and isFunction and not isCommented))
       if foldable and isFunction and not isCommented
         if @indentLevel == null
           @indentLevel = thisIndentLevel
         if action == 'toggle'
           editor.toggleFoldAtBufferRow(row)
+          toggle++
         else if action == 'unfold' and isFolded
           editor.unfoldBufferRow(row)
+          unfold++
         else if !editor.isFoldedAtBufferRow(row)
           editor.foldBufferRow(row)
+          fold++
+    @debugMessage('fold functions: done scanning ' + lines + ' lines (' + fold + ':' + unfold + ':' + toggle + ')')
 
   toggle: ->
     @fold('toggle')

      editor = atom.workspace.getActiveTextEditor()

    if not editor
      @debugMessage('no editor, skipping')
      return

    @debugMessage('fold functions:', action)
    @indentLevel = @indentLevel || null
    hasFoldableLines = false
    for row in [0..editor.getLastBufferRow()]
      foldable = editor.isFoldableAtBufferRow(row)
      isFolded = editor.isFoldedAtBufferRow(row)
      isCommented = editor.isBufferRowCommented(row)

      # check the indent level for this line and make sure it is the same as
      # previous lines where we found functions
      thisIndentLevel = editor.indentationForBufferRow(row)

  unfold: ->
    @fold('unfold')

  # get all the scopes in a buffer row
  getScopesForBufferRow: (editor, row) ->
    scopes = []
    text = editor.lineTextForBufferRow(row).trim()
    if text.length > 0
      # scan the text line to see if there is a function somewhere
      for pos in [0..text.length]
        positionScopes = editor.scopeDescriptorForBufferPosition([row, pos])
        for currentScope in positionScopes.scopes
          scopes.push(currentScope) if currentScope not in scopes
    scopes

  # Check the scopes for this buffer row to see if it matches what we want
  hasScopeAtBufferRow: (editor, row, scopes...) ->
    rowScopes = @getScopesForBufferRow(editor, row)
    for scope in scopes
      # incase there is an exact match, return quickly
      return true if scope in rowScopes

      # if not, we need to look at each piece of the scope to see if they match
      # using startsWith is problematic for things like 'meta.function-call'
      # would get folded instead of just 'meta.function'
      pieces = scope.split('.')
      for rowScope in rowScopes
        rowScopePieces = rowScope.split('.')
        match = true
        match = (false for piece, i in pieces when rowScopePieces[i] != piece)
        if match
          return true
    false
