From 8437f3ff7fd110b829aea401d37f8918521d37de Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Mon, 25 Mar 2013 17:31:01 -0600 Subject: [PATCH] Only load TextMate packages as needed in specs Also: when a TextMate package is deactivated, remove its grammars --- spec/app/display-buffer-spec.coffee | 1 + spec/app/edit-session-spec.coffee | 6 ++++-- spec/app/editor-spec.coffee | 2 ++ spec/app/grammar-view-spec.coffee | 2 ++ spec/app/language-mode-spec.coffee | 5 ++++- spec/app/syntax-spec.coffee | 16 ++++++++++++++++ spec/app/text-mate-grammar-spec.coffee | 11 +++++++++++ spec/app/tokenized-buffer-spec.coffee | 6 ++++++ spec/spec-helper.coffee | 11 +---------- src/app/syntax.coffee | 13 ++++++++++--- src/app/text-mate-package.coffee | 3 ++- src/packages/snippets/spec/snippets-spec.coffee | 7 ++++--- .../spell-check/spec/spell-check-spec.coffee | 2 ++ .../status-bar/spec/status-bar-spec.coffee | 5 +++++ 14 files changed, 70 insertions(+), 20 deletions(-) diff --git a/spec/app/display-buffer-spec.coffee b/spec/app/display-buffer-spec.coffee index a46f2f63d..8227a94e2 100644 --- a/spec/app/display-buffer-spec.coffee +++ b/spec/app/display-buffer-spec.coffee @@ -6,6 +6,7 @@ describe "DisplayBuffer", -> [editSession, displayBuffer, buffer, changeHandler, tabLength] = [] beforeEach -> tabLength = 2 + atom.activatePackage('javascript.tmbundle', sync: true) editSession = project.buildEditSession('sample.js', { tabLength }) { buffer, displayBuffer } = editSession changeHandler = jasmine.createSpy 'changeHandler' diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 7afe8d888..c6870c7f7 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -9,6 +9,7 @@ describe "EditSession", -> buffer.setText(buffer.getText().replace(/[ ]{2}/g, "\t")) beforeEach -> + atom.activatePackage('javascript.tmbundle', sync: true) editSession = project.buildEditSession('sample.js', autoIndent: false) buffer = editSession.buffer lineLengths = buffer.getLines().map (line) -> line.length @@ -2048,13 +2049,14 @@ describe "EditSession", -> describe "when the 'grammars-loaded' event is triggered on the syntax global", -> it "reloads the edit session's grammar and re-tokenizes the buffer if it changes", -> editSession.destroy() - grammarToReturn = syntax.grammarByPath('a.txt') + jsGrammar = syntax.selectGrammar('a.js') + grammarToReturn = syntax.nullGrammar spyOn(syntax, 'selectGrammar').andCallFake -> grammarToReturn editSession = project.buildEditSession('sample.js', autoIndent: false) expect(editSession.lineForScreenRow(0).tokens.length).toBe 1 - grammarToReturn = syntax.grammarByPath('a.js') + grammarToReturn = jsGrammar syntax.trigger 'grammars-loaded' expect(editSession.lineForScreenRow(0).tokens.length).toBeGreaterThan 1 diff --git a/spec/app/editor-spec.coffee b/spec/app/editor-spec.coffee index a642e3559..ee150eca5 100644 --- a/spec/app/editor-spec.coffee +++ b/spec/app/editor-spec.coffee @@ -12,6 +12,8 @@ describe "Editor", -> [buffer, editor, editSession, cachedLineHeight, cachedCharWidth] = [] beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) editSession = project.buildEditSession('sample.js') buffer = editSession.buffer editor = new Editor(editSession) diff --git a/spec/app/grammar-view-spec.coffee b/spec/app/grammar-view-spec.coffee index 45157aab0..9928f85ee 100644 --- a/spec/app/grammar-view-spec.coffee +++ b/spec/app/grammar-view-spec.coffee @@ -6,6 +6,8 @@ describe "GrammarView", -> [editor, textGrammar, jsGrammar] = [] beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) window.rootView = new RootView rootView.open('sample.js') editor = rootView.getActiveView() diff --git a/spec/app/language-mode-spec.coffee b/spec/app/language-mode-spec.coffee index 715296e6b..4244d3c72 100644 --- a/spec/app/language-mode-spec.coffee +++ b/spec/app/language-mode-spec.coffee @@ -10,6 +10,7 @@ describe "LanguageMode", -> describe "common behavior", -> beforeEach -> + atom.activatePackage('javascript.tmbundle', sync: true) editSession = project.buildEditSession('sample.js', autoIndent: false) { buffer, languageMode } = editSession @@ -21,6 +22,7 @@ describe "LanguageMode", -> describe "javascript", -> beforeEach -> + atom.activatePackage('javascript.tmbundle', sync: true) editSession = project.buildEditSession('sample.js', autoIndent: false) { buffer, languageMode } = editSession @@ -60,9 +62,9 @@ describe "LanguageMode", -> expect(languageMode.suggestedIndentForBufferRow(2)).toBe 2 expect(languageMode.suggestedIndentForBufferRow(9)).toBe 1 - describe "coffeescript", -> beforeEach -> + atom.activatePackage('coffee-script-tmbundle', sync: true) editSession = project.buildEditSession('coffee.coffee', autoIndent: false) { buffer, languageMode } = editSession @@ -98,6 +100,7 @@ describe "LanguageMode", -> describe "css", -> beforeEach -> + atom.activatePackage('css.tmbundle', sync: true) editSession = project.buildEditSession('css.css', autoIndent: false) { buffer, languageMode } = editSession diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index 789e44159..01da46baa 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -1,6 +1,12 @@ fs = require 'fs-utils' describe "the `syntax` global", -> + beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) + atom.activatePackage('coffee-script-tmbundle', sync: true) + atom.activatePackage('ruby.tmbundle', sync: true) + describe "serialization", -> it "remembers grammar overrides by path", -> path = '/foo/bar/file.js' @@ -12,6 +18,8 @@ describe "the `syntax` global", -> describe ".selectGrammar(filePath)", -> it "can use the filePath to load the correct grammar based on the grammar's filetype", -> + atom.activatePackage('git.tmbundle', sync: true) + expect(syntax.selectGrammar("file.js").name).toBe "JavaScript" # based on extension (.js) expect(syntax.selectGrammar("/tmp/.git/config").name).toBe "Git Config" # based on end of the path (.git/config) expect(syntax.selectGrammar("Rakefile").name).toBe "Ruby" # based on the file's basename (Rakefile) @@ -23,6 +31,8 @@ describe "the `syntax` global", -> expect(syntax.selectGrammar(filePath).name).toBe "Ruby" it "uses the number of newlines in the first line regex to determine the number of lines to test against", -> + atom.activatePackage('property-list.tmbundle', sync: true) + fileContent = "first-line\n" expect(syntax.selectGrammar("dummy.coffee", fileContent).name).toBe "CoffeeScript" @@ -47,6 +57,12 @@ describe "the `syntax` global", -> syntax.clearGrammarOverrideForPath(path) expect(syntax.selectGrammar(path).name).not.toBe 'Ruby' + describe ".removeGrammar(grammar)", -> + it "removes the grammar, so it won't be returned by selectGrammar", -> + grammar = syntax.selectGrammar('foo.js') + syntax.removeGrammar(grammar) + expect(syntax.selectGrammar('foo.js').name).not.toBe grammar.name + describe ".getProperty(scopeDescriptor)", -> it "returns the property with the most specific scope selector", -> syntax.addProperties(".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42) diff --git a/spec/app/text-mate-grammar-spec.coffee b/spec/app/text-mate-grammar-spec.coffee index f524e69e5..b52f6451a 100644 --- a/spec/app/text-mate-grammar-spec.coffee +++ b/spec/app/text-mate-grammar-spec.coffee @@ -8,6 +8,10 @@ describe "TextMateGrammar", -> grammar = null beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) + atom.activatePackage('coffee-script-tmbundle', sync: true) + atom.activatePackage('ruby.tmbundle', sync: true) grammar = syntax.selectGrammar("hello.coffee") describe ".tokenizeLine(line, ruleStack)", -> @@ -192,6 +196,9 @@ describe "TextMateGrammar", -> describe "when the pattern includes rules from another grammar", -> it "parses tokens inside the begin/end patterns based on the included grammar's rules", -> + atom.activatePackage('html.tmbundle', sync: true) + atom.activatePackage('ruby-on-rails-tmbundle', sync: true) + grammar = syntax.grammarsByFileType["html.erb"] {tokens} = grammar.tokenizeLine("
<%= User.find(2).full_name %>
") @@ -248,6 +255,9 @@ describe "TextMateGrammar", -> {tokens, ruleStack} = grammar.tokenizeLine(" // second line comment with a single leading space", ruleStack) describe "when inside a C block", -> + beforeEach -> + atom.activatePackage('c.tmbundle', sync: true) + it "correctly parses a method. (regression)", -> grammar = syntax.selectGrammar("hello.c") {tokens, ruleStack} = grammar.tokenizeLine("if(1){m()}") @@ -271,6 +281,7 @@ describe "TextMateGrammar", -> describe "when a grammar has a pattern that has back references in the match value", -> it "does not special handle the back references and instead allows oniguruma to resolve them", -> + atom.activatePackage('sass.tmbundle', sync: true) grammar = syntax.selectGrammar("style.scss") {tokens} = grammar.tokenizeLine("@mixin x() { -moz-selector: whatever; }") expect(tokens[9]).toEqual value: "-moz-selector", scopes: ["source.css.scss", "meta.property-list.scss", "meta.property-name.scss"] diff --git a/spec/app/tokenized-buffer-spec.coffee b/spec/app/tokenized-buffer-spec.coffee index 378534b37..4f7ad41ce 100644 --- a/spec/app/tokenized-buffer-spec.coffee +++ b/spec/app/tokenized-buffer-spec.coffee @@ -8,6 +8,7 @@ describe "TokenizedBuffer", -> [editSession, tokenizedBuffer, buffer, changeHandler] = [] beforeEach -> + atom.activatePackage('javascript.tmbundle', sync: true) # enable async tokenization TokenizedBuffer.prototype.chunkSize = 5 jasmine.unspy(TokenizedBuffer.prototype, 'tokenizeInBackground') @@ -298,6 +299,7 @@ describe "TokenizedBuffer", -> describe "when the buffer contains hard-tabs", -> beforeEach -> + atom.activatePackage('coffee-script-tmbundle', sync: true) tabLength = 2 editSession = project.buildEditSession('sample-with-tabs.coffee', { tabLength }) buffer = editSession.buffer @@ -328,6 +330,7 @@ describe "TokenizedBuffer", -> describe "when a Git commit message file is tokenized", -> beforeEach -> + atom.activatePackage('git.tmbundle', sync: true) editSession = project.buildEditSession('COMMIT_EDITMSG', autoIndent: false) buffer = editSession.buffer tokenizedBuffer = editSession.displayBuffer.tokenizedBuffer @@ -355,6 +358,7 @@ describe "TokenizedBuffer", -> describe "when a C++ source file is tokenized", -> beforeEach -> + atom.activatePackage('c.tmbundle', sync: true) editSession = project.buildEditSession('includes.cc', autoIndent: false) buffer = editSession.buffer tokenizedBuffer = editSession.displayBuffer.tokenizedBuffer @@ -403,6 +407,8 @@ describe "TokenizedBuffer", -> describe "when an Objective-C source file is tokenized", -> beforeEach -> + atom.activatePackage('c.tmbundle', sync: true) + atom.activatePackage('objective-c.tmbundle', sync: true) editSession = project.buildEditSession('function.mm', autoIndent: false) buffer = editSession.buffer tokenizedBuffer = editSession.displayBuffer.tokenizedBuffer diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index d66fea65e..2bef64dc2 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -17,6 +17,7 @@ RootView = require 'root-view' Git = require 'git' requireStylesheet "jasmine" fixturePackagesPath = fs.resolveOnLoadPath('fixtures/packages') +config.packageDirPaths.unshift(fixturePackagesPath) keymap.loadBundledKeymaps() [bindingSetsToRestore, bindingSetsByFirstKeystrokeToRestore] = [] @@ -92,16 +93,6 @@ afterEach -> syntax.off() waits(0) # yield to ui thread to make screen update more frequently -# Specs rely on TextMate bundles (but not atom packages) -window.loadTextMatePackages = -> - TextMatePackage = require 'text-mate-package' - config.packageDirPaths.unshift(fixturePackagesPath) - window.textMatePackages = [] - for path in atom.getPackagePaths() when TextMatePackage.testName(path) - window.textMatePackages.push atom.activatePackage(path, sync: true) - -window.loadTextMatePackages() - ensureNoPathSubscriptions = -> watchedPaths = $native.getWatchedPaths() $native.unwatchAllPaths() diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index f37949370..fe15047aa 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -32,9 +32,14 @@ class Syntax addGrammar: (grammar) -> @grammars.push(grammar) - for fileType in grammar.fileTypes - @grammarsByFileType[fileType] = grammar - @grammarsByScopeName[grammar.scopeName] = grammar + @grammarsByFileType[fileType] = grammar for fileType in grammar.fileTypes + @grammarsByScopeName[grammar.scopeName] = grammar + + removeGrammar: (grammar) -> + if _.include(@grammars, grammar) + _.remove(@grammars, grammar) + delete @grammarsByFileType[fileType] for fileType in grammar.fileTypes + delete @grammarsByScopeName[grammar.scopeName] setGrammarOverrideForPath: (path, scopeName) -> @grammarOverridesByPath[path] = scopeName @@ -46,7 +51,9 @@ class Syntax @grammarOverridesByPath = {} selectGrammar: (filePath, fileContents) -> + return @grammarsByFileType["txt"] ? @nullGrammar unless filePath + @grammarOverrideForPath(filePath) ? @grammarByFirstLineRegex(filePath, fileContents) ? @grammarByPath(filePath) ? diff --git a/src/app/text-mate-package.coffee b/src/app/text-mate-package.coffee index 62114e52a..f9a2dfaea 100644 --- a/src/app/text-mate-package.coffee +++ b/src/app/text-mate-package.coffee @@ -36,7 +36,8 @@ class TextMatePackage extends Package syntax.addProperties(selector, properties) deactivate: -> - # we should remove grammars and unregister properties, snippets, etc + syntax.removeGrammar(grammar) for grammar in @grammars + # we should also unregister properties, snippets, etc legalGrammarExtensions: ['plist', 'tmLanguage', 'tmlanguage'] diff --git a/src/packages/snippets/spec/snippets-spec.coffee b/src/packages/snippets/spec/snippets-spec.coffee index fad90f109..5e2f2c74e 100644 --- a/src/packages/snippets/spec/snippets-spec.coffee +++ b/src/packages/snippets/spec/snippets-spec.coffee @@ -9,13 +9,11 @@ Package = require 'package' describe "Snippets extension", -> [buffer, editor, editSession] = [] beforeEach -> + atom.activatePackage('javascript.tmbundle', sync: true) window.rootView = new RootView rootView.open('sample.js') packageWithSnippets = atom.loadPackage("package-with-snippets") - spyOn(atom, "getLoadedPackages").andCallFake -> - textMatePackages = window.textMatePackages.filter (pack) -> /package-with-a-cson-grammar|test|textmate-package|javascript/.test(pack.name) - textMatePackages.concat([packageWithSnippets]) spyOn(require("snippets/lib/snippets"), 'loadAll') atom.activatePackage("snippets") @@ -238,6 +236,9 @@ describe "Snippets extension", -> describe "snippet loading", -> beforeEach -> + atom.loadPackage('textmate-package.tmbundle', sync: true) + atom.loadPackage('package-with-snippets') + jasmine.unspy(window, "setTimeout") jasmine.unspy(snippets, 'loadAll') spyOn(snippets, 'loadAtomSnippets').andCallFake (path, done) -> done() diff --git a/src/packages/spell-check/spec/spell-check-spec.coffee b/src/packages/spell-check/spec/spell-check-spec.coffee index d6eb88995..5ac927d96 100644 --- a/src/packages/spell-check/spec/spell-check-spec.coffee +++ b/src/packages/spell-check/spec/spell-check-spec.coffee @@ -4,6 +4,8 @@ describe "Spell check", -> [editor] = [] beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) window.rootView = new RootView rootView.open('sample.js') config.set('spell-check.grammars', []) diff --git a/src/packages/status-bar/spec/status-bar-spec.coffee b/src/packages/status-bar/spec/status-bar-spec.coffee index dd067dbf2..ec4329b6b 100644 --- a/src/packages/status-bar/spec/status-bar-spec.coffee +++ b/src/packages/status-bar/spec/status-bar-spec.coffee @@ -180,6 +180,11 @@ describe "StatusBar", -> expect(statusBar.gitStatusIcon).toHaveText('+1') describe "grammar label", -> + beforeEach -> + atom.activatePackage('text.tmbundle', sync: true) + atom.activatePackage('javascript.tmbundle', sync: true) + syntax.trigger 'grammars-loaded' + it "displays the name of the current grammar", -> expect(statusBar.find('.grammar-name').text()).toBe 'JavaScript'