From 9c7c2ab8009927d7c62c96cd17d3c585ee57389d Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 28 Dec 2012 13:27:45 -0600 Subject: [PATCH] Store TM bundle start/end comment strings in scoped properties Previously, we had a custom method on the `TextMateBundle` class for retrieving these variables from the bundle. Now we're using Atom's `syntax.getProperty` mechanism. The idea is to map TextMate concepts to their Atom equivalent, rather than building everything directly around TextMate. --- src/app/language-mode.coffee | 4 ++-- src/app/package.coffee | 4 ++-- src/app/syntax.coffee | 3 ++- src/app/text-mate-bundle.coffee | 6 ------ src/app/text-mate-package.coffee | 25 ++++++++++++++++++++----- src/stdlib/underscore-extensions.coffee | 6 ++++++ 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 1305f9191..eb5db0e60 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -67,14 +67,14 @@ class LanguageMode toggleLineCommentsForBufferRows: (start, end) -> scopes = @editSession.scopesForBufferPosition([start, 0]) - return unless commentStartString = TextMateBundle.lineCommentStartStringForScope(scopes[0]) + return unless commentStartString = syntax.getProperty(scopes, "editor.commentStart") buffer = @editSession.buffer commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '($1)?') commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})") shouldUncomment = commentStartRegex.test(buffer.lineForRow(start)) - if commentEndString = TextMateBundle.lineCommentEndStringForScope(scopes[0]) + if commentEndString = syntax.getProperty(scopes, "editor.commentEnd") if shouldUncomment commentEndRegexString = _.escapeRegExp(commentEndString).replace(/^(\s+)/, '($1)?') commentEndRegex = new OnigRegExp("(#{commentEndRegexString})(\\s*)$") diff --git a/src/app/package.coffee b/src/app/package.coffee index 793fcb86a..88fcf7ef1 100644 --- a/src/app/package.coffee +++ b/src/app/package.coffee @@ -17,5 +17,5 @@ class Package @path = fs.directory(@path) unless fs.isDirectory(@path) load: -> - # WIP: Going to load scoped properties into `syntax` global here - @getScopedProperties() + for { selector, properties } in @getScopedProperties() + syntax.addProperties(selector, properties) diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index f54b3f3da..5a71da15e 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -42,7 +42,8 @@ class Syntax matchingProperties.concat([@globalProperties]) matchingPropertiesForElement: (element, candidates) -> - matchingScopedProperties = candidates.filter ({selector}) -> jQuery.find.matchesSelector(element, selector) + matchingScopedProperties = candidates.filter ({selector}) -> + jQuery.find.matchesSelector(element, selector) matchingScopedProperties.sort (a, b) -> if a.specificity == b.specificity b.index - a.index diff --git a/src/app/text-mate-bundle.coffee b/src/app/text-mate-bundle.coffee index 777f245df..4be12f689 100644 --- a/src/app/text-mate-bundle.coffee +++ b/src/app/text-mate-bundle.coffee @@ -58,12 +58,6 @@ class TextMateBundle values = @getPreferenceInScope(scope, preferenceName) (_.find values, ({name}) -> name is valueName)?['value'] - @lineCommentStartStringForScope: (scope) -> - @getPreferenceValueInScope(scope, 'shellVariables', 'TM_COMMENT_START') - - @lineCommentEndStringForScope: (scope) -> - @getPreferenceValueInScope(scope, 'shellVariables', 'TM_COMMENT_END') - @indentRegexForScope: (scope) -> if source = @getPreferenceInScope(scope, 'increaseIndentPattern') new OnigRegExp(source) diff --git a/src/app/text-mate-package.coffee b/src/app/text-mate-package.coffee index 07203d3b2..75e509f41 100644 --- a/src/app/text-mate-package.coffee +++ b/src/app/text-mate-package.coffee @@ -2,17 +2,18 @@ Package = require 'package' TextMateBundle = require 'text-mate-bundle' fs = require 'fs' plist = require 'plist' +_ = require 'underscore' module.exports = class TextMatePackage extends Package @testName: (packageName) -> /(\.|_|-)tmbundle$/.test(packageName) - @translateScopeSelector: (scopeSelector) -> + @cssSelectorForScopeSelector: (scopeSelector) -> scopeSelector.split(', ').map((commaFragment) -> commaFragment.split(' ').map((spaceFragment) -> spaceFragment.split('.').map((dotFragment) -> - '.' + dotFragment + '.' + dotFragment.replace(/\+/g, '\\+') ).join('') ).join(' ') ).join(', ') @@ -30,11 +31,25 @@ class TextMatePackage extends Package scopedProperties = [] if fs.exists(@preferencesPath) for preferencePath in fs.list(@preferencesPath) - plist.parseString fs.read(preferencePath), (e, data) -> + plist.parseString fs.read(preferencePath), (e, data) => if e console.warn "Failed to parse preference at path '#{preferencePath}'", e.stack else { scope, settings } = data[0] - selector = TextMatePackage.translateScopeSelector(scope) if scope? - scopedProperties.push({selector: selector, properties: settings}) + if properties = @translateProperties(settings) + selector = TextMatePackage.cssSelectorForScopeSelector(scope) if scope? + scopedProperties.push({selector, properties}) scopedProperties + + translateProperties: (textMateSettings) -> + if textMateSettings.shellVariables + shellVariables = {} + for {name, value} in textMateSettings.shellVariables + shellVariables[name] = value + textMateSettings.shellVariables = shellVariables + + editorProperties = _.compactObject( + commentStart: _.valueForKeyPath(textMateSettings, 'shellVariables.TM_COMMENT_START') + commentEnd: _.valueForKeyPath(textMateSettings, 'shellVariables.TM_COMMENT_END') + ) + { editor: editorProperties } if _.size(editorProperties) > 0 diff --git a/src/stdlib/underscore-extensions.coffee b/src/stdlib/underscore-extensions.coffee index 32c8b7909..4946bcdea 100644 --- a/src/stdlib/underscore-extensions.coffee +++ b/src/stdlib/underscore-extensions.coffee @@ -92,3 +92,9 @@ _.mixin object = object[key] return unless object? object + + compactObject: (object) -> + newObject = {} + for key, value of object + newObject[key] = value if value? + newObject