 
   hasScopeAtBufferRow: (editor, row, scope) ->
     found = false
-    text = editor.lineTextForBufferRow(row)
+    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]
+        scopes = editor.scopesForBufferPosition([row, pos])
 
-    # scan the text line to see if there is a function somewhere
-    for pos in [0..text.length]
-      scopes = editor.scopesForBufferPosition([row, pos])
-      found = true for item in scopes when item.startsWith(scope)
-      if found then break
+        # see if we found the scope we're after...
+        found = true for item in scopes when item.startsWith(scope)
+        if found then break
     found

  fold: (action) ->
    if !action then action = 'fold'
    editor = atom.workspace.getActiveTextEditor()
    indentLevel = null
    for row in [0..editor.getLastBufferRow()]
      foldable = editor.isFoldableAtBufferRow(row)
      is_function = @hasScopeAtBufferRow(editor, row, 'meta.function')
      if foldable and is_function
        thisIndentLevel = editor.indentationForBufferRow(row)
        if indentLevel == null
          indentLevel = thisIndentLevel
        if thisIndentLevel == indentLevel
          if action == 'toggle'
            editor.toggleFoldAtBufferRow(row)
          else if action == 'unfold' and editor.isFoldedAtBufferRow(row)
            editor.unfoldBufferRow(row)
          else if !editor.isFoldedAtBufferRow(row)
            editor.foldBufferRow(row)

  toggle: ->
    @fold('toggle')

module.exports = AtomFoldFunctions =
  modalPanel: null
  subscriptions: null
  indentLevel: null

  activate: (state) ->
    # Events subscribed to in atom's system can be easily cleaned up with a
