diff --git a/spec/app/text-mate-grammar-spec.coffee b/spec/app/text-mate-grammar-spec.coffee index 9e5610d14..26040951c 100644 --- a/spec/app/text-mate-grammar-spec.coffee +++ b/spec/app/text-mate-grammar-spec.coffee @@ -1,4 +1,5 @@ TextMateGrammar = require 'text-mate-grammar' +TextMateBundle = require 'text-mate-bundle' plist = require 'plist' fs = require 'fs' _ = require 'underscore' @@ -8,17 +9,7 @@ describe "TextMateGrammar", -> beforeEach -> coffeePlist = fs.read(require.resolve 'CoffeeScriptBundle.tmbundle/Syntaxes/CoffeeScript.tmLanguage') - plist.parseString coffeePlist, (err, data) -> - grammar = new TextMateGrammar(data[0]) - - describe ".loadFromBundles()", -> - it "creates grammars for all plist files in all bundles' Syntaxes directories", -> - TextMateGrammar.loadFromBundles() - coffeeGrammar = TextMateGrammar.grammarForExtension("coffee") - rubyGrammar = TextMateGrammar.grammarForExtension("rb") - - expect(coffeeGrammar.name).toBe "CoffeeScript" - expect(rubyGrammar.name).toBe "Ruby" + grammar = TextMateBundle.grammarForFileName("hello.coffee") describe ".getLineTokens(line, currentRule)", -> describe "when the entire line matches a single pattern with no capture groups", -> @@ -118,4 +109,4 @@ describe "TextMateGrammar", -> expect(tokens[5]).toEqual value: '}', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"] expect(tokens[6]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","string.quoted.double.coffee","punctuation.definition.string.end.coffee"] expect(tokens[7]).toEqual value: '}', scopes: ["source.coffee","string.quoted.double.coffee","source.coffee.embedded.source","punctuation.section.embedded.coffee"] - expect(tokens[8]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.end.coffee"] \ No newline at end of file + expect(tokens[8]).toEqual value: '"', scopes: ["source.coffee","string.quoted.double.coffee","punctuation.definition.string.end.coffee"] diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 3e34ef28c..a6f8bcd09 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -7,13 +7,13 @@ Project = require 'project' Directory = require 'directory' File = require 'file' RootView = require 'root-view' -TextMateGrammar = require 'text-mate-grammar' +TextMateBundle = require 'text-mate-bundle' fs = require 'fs' require 'window' $native.showDevTools() requireStylesheet "jasmine.css" -TextMateGrammar.loadFromBundles() +TextMateBundle.loadAll() defaultTitle = document.title pathsWithSubscriptions = null diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 52ddcbd61..ea3efae22 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -1,6 +1,6 @@ AceAdaptor = require 'ace-adaptor' Range = require 'range' -TextMateGrammar = require 'text-mate-grammar' +TextMateBundle = require 'text-mate-bundle' _ = require 'underscore' module.exports = @@ -15,7 +15,8 @@ class LanguageMode constructor: (@editSession) -> @buffer = @editSession.buffer @aceMode = @requireAceMode() - @grammar = TextMateGrammar.grammarForExtension(@editSession.buffer.getExtension()) + + @grammar = TextMateBundle.grammarForFileName(@editSession.buffer.getBaseName()) @aceAdaptor = new AceAdaptor(@editSession) _.adviseBefore @editSession, 'insertText', (text) => diff --git a/src/app/text-mate-bundle.coffee b/src/app/text-mate-bundle.coffee new file mode 100644 index 000000000..fd012617c --- /dev/null +++ b/src/app/text-mate-bundle.coffee @@ -0,0 +1,37 @@ +_ = require 'underscore' +fs = require 'fs' +plist = require 'plist' + +TextMateGrammar = require 'text-mate-grammar' + + +module.exports = +class TextMateBundle + @grammarsByFileType: {} + @bundles: [] + + @loadAll: -> + for bundlePath in fs.list(require.resolve("bundles")) + @registerBundle(new TextMateBundle(bundlePath)) + + @registerBundle: (bundle)-> + @bundles.push(bundle) + + for grammar in bundle.grammars + for fileType in grammar.fileTypes + @grammarsByFileType[fileType] = grammar + + @grammarForFileName: (fileName) -> + extension = fs.extension(fileName)[1...] + @grammarsByFileType[extension] or @grammarsByFileType["txt"] + + grammars: null + + constructor: (bundlePath) -> + @grammars = [] + syntaxesPath = fs.join(bundlePath, "Syntaxes") + if fs.exists(syntaxesPath) + for syntaxPath in fs.list(syntaxesPath) + @grammars.push TextMateGrammar.loadFromPath(syntaxPath) + + diff --git a/src/app/text-mate-grammar.coffee b/src/app/text-mate-grammar.coffee index c1f65e997..aa4d70153 100644 --- a/src/app/text-mate-grammar.coffee +++ b/src/app/text-mate-grammar.coffee @@ -4,36 +4,18 @@ plist = require 'plist' module.exports = class TextMateGrammar - @grammarsByExtension: {} - - @loadFromBundles: -> - for bundlePath in fs.list(require.resolve("bundles")) - syntaxesPath = fs.join(bundlePath, "Syntaxes") - continue unless fs.exists(syntaxesPath) - for path in fs.list(syntaxesPath) - grammar = @loadGrammarFromPath(path) - @registerGrammar(grammar) - - @loadGrammarFromPath: (path) -> + @loadFromPath: (path) -> grammar = null plist.parseString fs.read(path), (e, data) -> throw new Error(e) if e grammar = new TextMateGrammar(data[0]) grammar - @registerGrammar: (grammar) -> - for extension in grammar.extensions - @grammarsByExtension[extension] = grammar - - @grammarForExtension: (extension) -> - @grammarsByExtension[extension] or @grammarsByExtension["txt"] - name: null repository: null initialRule: null - constructor: ({ @name, fileTypes, scopeName, patterns, repository }) -> - @extensions = fileTypes + constructor: ({ @name, @fileTypes, scopeName, patterns, repository }) -> @initialRule = new Rule(this, {scopeName, patterns}) @repository = {} for name, data of repository diff --git a/src/app/window.coffee b/src/app/window.coffee index d507ab656..b2395b6f3 100644 --- a/src/app/window.coffee +++ b/src/app/window.coffee @@ -2,7 +2,7 @@ # the DOM window. Native = require 'native' -TextMateGrammar = require 'text-mate-grammar' +TextMateBundle = require 'text-mate-bundle' fs = require 'fs' _ = require 'underscore' $ = require 'jquery' @@ -31,7 +31,7 @@ windowAdditions = false $(window).focus() atom.windowOpened this - TextMateGrammar.loadFromBundles() + TextMateBundle.loadAll() shutdown: -> @rootView.deactivate()