 
   registerEvents: ->
     @disposables.push atom.workspace.onDidOpen (e) =>
-      return unless e.item
-      return unless !e.item.mini
-      return unless e.item.getGrammar?
-      return unless e.item.getGrammar().name in @grammars
+      return unless e.item and @isValidEditor(e.item)
       @startServer()
     @disposables.push atom.workspace.onDidChangeActivePaneItem =>
       @setCurrentProvider()
 
-  isGrammarInGrammars: (editor) ->
-    grammar = editor.getGrammar().name
-    if grammar in @grammars
-      return true
-    else
-      return false
+  isValidEditor: (editor) ->
+    return false if editor.mini
+    return false if !editor.getGrammar
+    return false if editor.getGrammar().name not in @grammars
+    return true
 
   setCurrentProvider: ->
-    editor = atom.workspace.getActiveTextEditor()
-    return unless editor
+    @activeTextEditor = atom.workspace.getActiveTextEditor()
+    return unless @activeTextEditor
     for provider, idx in @providers
       provider.isActive = false
-      if provider.editor.id is editor.id
+      if provider.editor.id is @activeTextEditor.id
         @currentProviderIdx = idx
         provider.isActive = true
         break
 
   registerEditors: ->
     @editorSubscription = atom.workspace.observeTextEditors (editor) =>
-      return unless !editor.mini
-      return unless editor.getGrammar().name in @grammars
+      return unless @isValidEditor(editor)
       @registerEditor(editor)
       @setCurrentProvider()
 

  client: null
  documentationView: null
  currentProviderIdx: 0

  # autocomplete
  autocompletePlus = null


  registerCommands: ->
    atom.commands.add 'atom-text-editor', 'tern:definition': (event) =>
        @findDefinition()
    atom.commands.add 'atom-text-editor', 'tern:startCompletion': (event) =>
      @providers[@currentProviderIdx].callPreBuildSuggestions(true)
    atom.commands.add 'atom-text-editor', 'tern:stop': (event) =>
