TextMateTheme extend Theme

This commit is contained in:
Corey Johnson
2012-12-27 11:12:40 -08:00
parent d77fc88ff4
commit d509507b0c
3 changed files with 56 additions and 33 deletions

View File

@@ -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

View File

@@ -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()

46
src/app/theme.coffee Normal file
View File

@@ -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)