diff --git a/spec/app/text-mate-theme-spec.coffee b/spec/app/text-mate-theme-spec.coffee index 48d28b47d..b879ec7d0 100644 --- a/spec/app/text-mate-theme-spec.coffee +++ b/spec/app/text-mate-theme-spec.coffee @@ -1,19 +1,20 @@ fs = require 'fs' plist = require 'plist' TextMateTheme = require 'text-mate-theme' +Theme = require 'theme' describe "TextMateTheme", -> [theme, themePath] = [] beforeEach -> themePath = require.resolve(fs.join('fixtures', 'test.tmTheme')) - theme = TextMateTheme.load(themePath) + theme = Theme.load(themePath) - describe "@activate(name)", -> + describe "@load(name)", -> it "applies the theme's stylesheet to the current window", -> spyOn window, 'applyStylesheet' - TextMateTheme.activate(themePath) - expect(window.applyStylesheet).toHaveBeenCalledWith(theme.name, theme.getStylesheet()) + Theme.load(themePath) + expect(window.applyStylesheet).toHaveBeenCalledWith(themePath, theme.getStylesheet()) describe ".getRulesets()", -> rulesets = null diff --git a/src/app/text-mate-theme.coffee b/src/app/text-mate-theme.coffee index d0c28fb85..b8b60b33b 100644 --- a/src/app/text-mate-theme.coffee +++ b/src/app/text-mate-theme.coffee @@ -1,41 +1,17 @@ _ = require 'underscore' fs = require 'fs' -plist = require 'plist' + +Theme = require 'Theme' module.exports = -class TextMateTheme - @load: (name) -> - regex = new RegExp("#{_.escapeRegExp(name)}\.(tmTheme|plist)$", "i") - - if fs.exists(name) - path = name - else - path = _.find fs.list(config.themeDirPath), (path) -> regex.test(path) - - return null unless path - - plistString = fs.read(path) - theme = null - plist.parseString plistString, (err, data) -> - throw new Error("Error loading theme at '#{path}': #{err}") if err - theme = new TextMateTheme(data[0]) - theme - - @activate: (name) -> - if theme = @load(name) - theme.activate() - else - throw new Error("No theme with name '#{name}'") - - constructor: ({@name, settings}) -> +class TextMateTheme extends Theme + constructor: (@path, {settings}) -> + super @rulesets = [] globalSettings = settings[0] @buildGlobalSettingsRulesets(settings[0]) @buildScopeSelectorRulesets(settings[1..]) - activate: -> - applyStylesheet(@name, @getStylesheet()) - getStylesheet: -> lines = [] for {selector, properties} in @getRulesets() diff --git a/src/app/theme.coffee b/src/app/theme.coffee new file mode 100644 index 000000000..1849b518c --- /dev/null +++ b/src/app/theme.coffee @@ -0,0 +1,46 @@ +fs = require("fs") +plist = require 'plist' +_ = require 'underscore' + +module.exports = +class Theme + @load: (name) -> + if fs.exists(name) + path = name + else + regex = new RegExp("#{_.escapeRegExp(name)}(\.[^.]*)?$", "i") + path = _.find fs.list(config.themeDirPath), (path) -> regex.test(path) + + return null unless path + + if @isTextMateTheme(path) + theme = @loadTextMateTheme(path) + else + throw new Error("I only know how to load textmate themes!") + + if theme + theme.activate() + else + throw new Error("Cannot activate theme named '#{name}'") + + theme + + @loadTextMateTheme: (path) -> + TextMateTheme = require("text-mate-theme") + plistString = fs.read(path) + theme = null + plist.parseString plistString, (err, data) -> + throw new Error("Error loading theme at '#{path}': #{err}") if err + theme = new TextMateTheme(path, data[0]) + theme + + @isTextMateTheme: (path) -> + /\.(tmTheme|plist)$/.test(path) + + constructor: (@path) -> + + activate: -> + applyStylesheet(@path, @getStylesheet()) + + getStylesheet: -> + fs.read(@path) \ No newline at end of file