   return formatter.stringify newObject, scope, sorted
 
 formatter.doEntireFile = (editor) ->
-  if not editor.getLastSelection().isEmpty()
+  grammars = atom.config.get('pretty-json.grammars') ? []
+  if editor.getGrammar().scopeName not in grammars
     return false
-  grammars = atom.config.get('pretty-json.grammars') ? []
-  return editor.getGrammar().scopeName in grammars
+  return editor.getLastSelection().isEmpty()
 
 PrettyJSON =
   config:
     notifyOnParseError:
       type: 'boolean'
       default: true
+    prettifyOnSaveJSON:
+      type: 'boolean'
+      default: false
+      title: 'Prettify On Save JSON'
     grammars:
       type: 'array'
       default: ['source.json', 'text.plain.null-grammar']

formatter = {}

formatter.space = (scope) ->

        editor = atom.workspace.getActiveTextEditor()
        @jsonify editor, true

    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.config.observe 'pretty-json.prettifyOnSaveJSON', (value) =>
      if (@saveSubscriptions)
        @saveSubscriptions.dispose()
      @saveSubscriptions = new CompositeDisposable()
      if (value)
        @subscribeToSaveEvents()

  subscribeToSaveEvents: ->
    @saveSubscriptions.add atom.workspace.observeTextEditors (editor) =>
      return if not editor or not editor.getBuffer()
      bufferSubscriptions = new CompositeDisposable()
      bufferSubscriptions.add editor.getBuffer().onWillSave (filePath) =>
        if formatter.doEntireFile editor
          @prettify editor, false
      bufferSubscriptions.add editor.getBuffer().onDidDestroy ->
        bufferSubscriptions.dispose()
      this.saveSubscriptions.add(bufferSubscriptions)

  deactivate: ->
    @subscriptions?.dispose()
    @subscriptions = null

module.exports = PrettyJSON
