 
   prettify: (editor, sorted) ->
     if formatter.doEntireFile editor
+      pos = editor.getCursorScreenPosition()
       editor.setText formatter.pretty(editor.getText(), editor.getRootScopeDescriptor(), sorted)
     else
+      pos = editor.getLastSelection().getScreenRange().start
       @replaceText editor, (text) -> formatter.pretty text, ['source.json'], sorted
+    editor.setCursorScreenPosition pos
 
   minify: (editor) ->
     if formatter.doEntireFile editor
+      pos = [0, 0]
       editor.setText formatter.minify(editor.getText())
     else
+      pos = editor.getLastSelection().getScreenRange().start
       @replaceText editor, (text) -> formatter.minify text
+    editor.setCursorScreenPosition pos
 
   jsonify: (editor, sorted) ->
     if formatter.doEntireFile editor
+      pos = editor.getCursorScreenPosition()
       editor.setText formatter.jsonify(editor.getText(), editor.getRootScopeDescriptor(), sorted)
     else
+      pos = editor.getLastSelection().getScreenRange().start
       @replaceText editor, (text) -> formatter.jsonify text, ['source.json'], sorted
+    editor.setCursorScreenPosition pos
 
   activate: ->
     atom.commands.add 'atom-workspace',

  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']

{CompositeDisposable} = require 'atom'
formatter = {}

formatter.space = (scope) ->
