From 98ef3e424396a2870b80b076c348aecddb311dca Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Wed, 1 Aug 2012 16:27:32 -0600 Subject: [PATCH] Start on TextMateTheme object --- spec/app/text-mate-theme-spec.coffee | 49 +++++++++++++++++++++ src/app/text-mate-theme.coffee | 64 ++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 spec/app/text-mate-theme-spec.coffee create mode 100644 src/app/text-mate-theme.coffee diff --git a/spec/app/text-mate-theme-spec.coffee b/spec/app/text-mate-theme-spec.coffee new file mode 100644 index 000000000..88517ae13 --- /dev/null +++ b/spec/app/text-mate-theme-spec.coffee @@ -0,0 +1,49 @@ +fs = require 'fs' +plist = require 'plist' +TextMateTheme = require 'text-mate-theme' + +describe "TextMateTheme", -> + theme = null + beforeEach -> + twilightPlist = fs.read(require.resolve('Twilight.tmTheme')) + plist.parseString twilightPlist, (err, data) -> + theme = new TextMateTheme(data[0]) + + describe ".getRulesets()", -> + rulesets = null + + beforeEach -> + rulesets = theme.getRulesets() + + it "returns rulesets representing the theme's global style settings", -> + expect(rulesets[0]).toEqual + selector: '.editor' + properties: + 'background-color': '#141414' + 'color': '#F8F8F8' + + expect(rulesets[1]).toEqual + selector: '.editor.focused .cursor' + properties: + 'border-color': '#A7A7A7' + + expect(rulesets[2]).toEqual + selector: '.editor.focused .selection' + properties: + 'background-color': "rgba(221, 240, 255, 51)" + + it "returns an array of objects representing the theme's scope selectors", -> + expect(rulesets[11]).toEqual + comment: "Invalid – Deprecated" + selector: "invalid--deprecated" + properties: + 'color': "#D2A8A1" + 'font-style': 'italic' + 'text-decoration': 'underline' + + expect(rulesets[12]).toEqual + comment: "Invalid – Illegal" + selector: "invalid--illegal" + properties: + 'color': "#F8F8F8" + 'background-color': 'rgba(86, 45, 86, 191)' diff --git a/src/app/text-mate-theme.coffee b/src/app/text-mate-theme.coffee new file mode 100644 index 000000000..509c53ff4 --- /dev/null +++ b/src/app/text-mate-theme.coffee @@ -0,0 +1,64 @@ +_ = require 'underscore' + +module.exports = +class TextMateTheme + constructor: ({@name, settings}) -> + @rulesets = [] + globalSettings = settings[0] + @buildGlobalSettingsRulesets(settings[0]) + @buildScopeSelectorRulesets(settings[1..]) + + getRulesets: -> @rulesets + + buildGlobalSettingsRulesets: ({settings}) -> + { background, foreground, caret, selection } = settings + + @rulesets.push + selector: '.editor' + properties: + 'background-color': @translateColor(background) + 'color': @translateColor(foreground) + + @rulesets.push + selector: '.editor.focused .cursor' + properties: + 'border-color': @translateColor(caret) + + @rulesets.push + selector: '.editor.focused .selection' + properties: + 'background-color': @translateColor(selection) + + buildScopeSelectorRulesets: (scopeSelectorSettings) -> + for { name, scope, settings } in scopeSelectorSettings + continue unless scope + @rulesets.push + comment: name + selector: @translateScopeSelector(scope) + properties: @translateScopeSelectorSettings(settings) + + translateScopeSelector: (textmateScopeSelector) -> + textmateScopeSelector.replace('.', '--') + + translateScopeSelectorSettings: ({ foreground, background, fontStyle }) -> + properties = {} + + if fontStyle + fontStyles = fontStyle.split(/\s+/) + properties['font-weight'] = 'bold' if _.contains(fontStyles, 'bold') + properties['font-style'] = 'italic' if _.contains(fontStyles, 'italic') + properties['text-decoration'] = 'underline' if _.contains(fontStyles, 'underline') + + properties['color'] = @translateColor(foreground) if foreground + properties['background-color'] = @translateColor(background) if background + properties + + translateColor: (textmateColor) -> + if textmateColor.length <= 7 + textmateColor + else + r = parseInt(textmateColor[1..2], 16) + g = parseInt(textmateColor[3..4], 16) + b = parseInt(textmateColor[5..6], 16) + a = parseInt(textmateColor[7..8], 16) + "rgba(#{r}, #{g}, #{b}, #{a})"