+{CompositeDisposable} = require 'atom'
 formatter = {}
 
 formatter.space = (scope) ->

  return formatter.stringify newObject, scope, sorted

formatter.doEntireFile = (editor) ->
  if not editor.getLastSelection().isEmpty()
    return false
  grammars = atom.config.get('pretty-json.grammars') ? []
  return editor.getGrammar().scopeName in grammars

PrettyJSON =
  config:
    notifyOnParseError:
      type: 'boolean'
      default: true
    grammars:
      type: 'array'
      default: ['source.json', 'text.plain.null-grammar']

        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
