 
     console.log 'total', inspect(total)
 
-  reloadPackages: ->
-    packages = settings.get('devReloadPackages') ? []
-    packages.push('vim-mode-plus')
-
-    # Deactivate
-    for packName in packages
-      pack = atom.packages.getLoadedPackage(packName)
-
-      if pack?
-        console.log "- deactivating #{packName}"
-        atom.packages.deactivatePackage(packName)
-        atom.packages.unloadPackage(packName)
-
-        packPath = pack.path
-        Object.keys(require.cache)
-          .filter (p) ->
-            p.indexOf(packPath + path.sep) is 0
-          .forEach (p) ->
-            delete require.cache[p]
-
-    # Activate
-    for packName in packages
+  reload: (reloadDependencies) ->
+    packages = ['vim-mode-plus']
+    if reloadDependencies
+      packages.push(settings.get('devReloadPackages')...)
+
+    deactivate = (packName) ->
+      console.log "- deactivating #{packName}"
+      packPath = atom.packages.getLoadedPackage(packName).path
+      atom.packages.deactivatePackage(packName)
+      atom.packages.unloadPackage(packName)
+      invalidateRequireCacheForPackage(packPath)
+
+    activate = (packName) ->
+      console.log "+ activating #{packName}"
       atom.packages.loadPackage(packName)
-      console.log "+ activating #{packName}"
       atom.packages.activatePackage(packName)
 
-  toggleReloadPackagesOnSave: ->
-    return unless editor = atom.workspace.getActiveTextEditor()
-    buffer = editor.getBuffer()
-    fileName = path.basename(editor.getPath())
-
-    if subscription = @reloadSubscriptionByBuffer.get(buffer)
-      subscription.dispose()
-      @reloadSubscriptionByBuffer.delete(buffer)
-      console.log "disposed reloadPackagesOnSave for #{fileName}"
-    else
-      @reloadSubscriptionByBuffer.set buffer, buffer.onDidSave =>
-        console.clear()
-        @reloadPackages()
-      console.log "activated reloadPackagesOnSave for #{fileName}"
-
-  toggleDevEnvironment: ->
-    return unless editor = atom.workspace.getActiveTextEditor()
-    buffer = editor.getBuffer()
-    fileName = path.basename(editor.getPath())
-
-    if @devEnvironmentByBuffer.has(buffer)
-      @devEnvironmentByBuffer.get(buffer).dispose()
-      @devEnvironmentByBuffer.delete(buffer)
-      console.log "disposed dev env #{fileName}"
-    else
-      @devEnvironmentByBuffer.set(buffer, new DevEnvironment(editor))
-      console.log "activated dev env #{fileName}"
+    loadedPackages = packages.filter (packName) -> atom.packages.getLoadedPackages(packName)
+    console.log "reload", loadedPackages
+    loadedPackages.map(deactivate)
+    console.time('activate')
+    loadedPackages.map(activate)
+    console.timeEnd('activate')
 
   addCommand: (name, fn) ->
     atom.commands.add('atom-text-editor', "#{packageScope}:#{name}", fn)


packageScope = 'vim-mode-plus'
getEditorState = null

class Developer
  init: (service) ->

      'open-in-vim': => @openInVim()
      'generate-introspection-report': => @generateIntrospectionReport()
      'generate-command-summary-table': => @generateCommandSummaryTable()
      'clear-debug-output': => @clearDebugOutput()
      'reload': => @reload()
      'reload-with-dependencies': => @reload(true)
      'report-total-marker-count': => @getAllMarkerCount()
      'report-total-and-per-editor-marker-count': => @getAllMarkerCount(true)

