diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index f1911e557..1883bb362 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -1618,20 +1618,35 @@ describe "Workspace", -> expect(pane.getPendingItem()).toBeFalsy() describe "grammar activation", -> - beforeEach -> - waitsForPromise -> - atom.packages.activatePackage('language-javascript') - it "notifies the workspace of which grammar is used", -> editor = null + atom.packages.triggerDeferredActivationHooks() - grammarUsed = jasmine.createSpy() - atom.workspace.handleGrammarUsed = grammarUsed + javascriptGrammarUsed = jasmine.createSpy('js grammar used') + rubyGrammarUsed = jasmine.createSpy('ruby grammar used') + cGrammarUsed = jasmine.createSpy('c grammar used') + + atom.packages.onDidTriggerActivationHook('language-javascript:grammar-used', javascriptGrammarUsed) + atom.packages.onDidTriggerActivationHook('language-ruby:grammar-used', rubyGrammarUsed) + atom.packages.onDidTriggerActivationHook('language-c:grammar-used', cGrammarUsed) + + waitsForPromise -> atom.packages.activatePackage('language-ruby') + waitsForPromise -> atom.packages.activatePackage('language-javascript') + waitsForPromise -> atom.packages.activatePackage('language-c') + waitsForPromise -> atom.workspace.open('sample-with-comments.js') - waitsForPromise -> atom.workspace.open('sample-with-comments.js').then (o) -> editor = o - waitsFor -> grammarUsed.callCount is 1 runs -> - expect(grammarUsed.argsForCall[0][0].name).toBe 'JavaScript' + # Hooks are triggered when opening new editors + expect(javascriptGrammarUsed).toHaveBeenCalled() + + # Hooks are triggered when changing existing editors grammars + atom.workspace.getActiveTextEditor().setGrammar(atom.grammars.grammarForScopeName('source.c')) + expect(cGrammarUsed).toHaveBeenCalled() + + # Hooks are triggered when editors are added in other ways. + atom.workspace.getActivePane().splitRight(copyActiveItem: true) + atom.workspace.getActiveTextEditor().setGrammar(atom.grammars.grammarForScopeName('source.ruby')) + expect(rubyGrammarUsed).toHaveBeenCalled() describe ".checkoutHeadRevision()", -> editor = null diff --git a/src/workspace.coffee b/src/workspace.coffee index 8d0ff38fd..089b73aeb 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -88,6 +88,7 @@ class Workspace extends Model subscribeToEvents: -> @subscribeToActiveItem() @subscribeToFontSize() + @subscribeToAddedItems() consumeServices: ({serviceHub}) -> @directorySearchers = [] @@ -160,6 +161,13 @@ class Workspace extends Model @activeItemSubscriptions.add(titleSubscription) if titleSubscription? @activeItemSubscriptions.add(modifiedSubscription) if modifiedSubscription? + subscribeToAddedItems: -> + @onDidAddPaneItem ({item, pane, index}) => + if item instanceof TextEditor + grammarSubscription = item.observeGrammar(@handleGrammarUsed.bind(this)) + item.onDidDestroy -> grammarSubscription.dispose() + @emitter.emit 'did-add-text-editor', {textEditor: item, pane, index} + # Updates the application's title and proxy icon based on whichever file is # open. updateWindowTitle: => @@ -383,8 +391,7 @@ class Workspace extends Model # # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidAddTextEditor: (callback) -> - @onDidAddPaneItem ({item, pane, index}) -> - callback({textEditor: item, pane, index}) if item instanceof TextEditor + @emitter.on 'did-add-text-editor', callback ### Section: Opening @@ -551,10 +558,7 @@ class Workspace extends Model @project.bufferForPath(filePath, options).then (buffer) => editor = @buildTextEditor(Object.assign({buffer, largeFileMode}, options)) disposable = atom.textEditors.add(editor) - grammarSubscription = editor.observeGrammar(@handleGrammarUsed.bind(this)) - editor.onDidDestroy -> - grammarSubscription.dispose() - disposable.dispose() + editor.onDidDestroy -> disposable.dispose() editor handleGrammarUsed: (grammar) ->