Store foldEndPattern in syntax global's scoped properties

There's a slight wrinkle in this commit… TextMate grammars sometimes store
the `foldStopMarker` directly in the grammar, rather than storing it
in a separate scoped preferences file like the other settings. So we
have to scan through grammars looking for those that have the fold end
marker and make a scoped property for that grammar's scope.
This commit is contained in:
Nathan Sobo
2012-12-28 14:46:39 -06:00
parent 5a075d515e
commit bb4f3c4efa
4 changed files with 16 additions and 24 deletions

View File

@@ -2,14 +2,6 @@ fs = require('fs')
TextMateBundle = require 'text-mate-bundle'
describe "TextMateBundle", ->
describe ".getPreferenceInScope(scope, preferenceName)", ->
it "returns the preference by the given name in the given scope or undefined if there isn't one", ->
expect(TextMateBundle.getPreferenceInScope('source.coffee', 'decreaseIndentPattern')).toBe '^\\s*(\\}|\\]|else|catch|finally)$'
expect(TextMateBundle.getPreferenceInScope('source.coffee', 'shellVariables')).toBeDefined()
it "returns the preference by the given name in the given scope for a scope registered via a comma-separated list of scopes", ->
expect(TextMateBundle.getPreferenceInScope('source.objc++', 'shellVariables')).toBeDefined()
describe ".getPreferencesByScopeSelector()", ->
it "logs warning, but does not raise errors if a preference can't be parsed", ->
bundlePath = fs.join(require.resolve('fixtures'), "test.tmbundle")

View File

@@ -119,7 +119,7 @@ class LanguageMode
continue if @editSession.isBufferRowBlank(row)
indentation = @editSession.indentationForBufferRow(row)
if indentation <= startIndentLevel
includeRowInFold = indentation == startIndentLevel and TextMateBundle.foldEndRegexForScope(@grammar, scopes[0]).search(@editSession.lineForBufferRow(row))
includeRowInFold = indentation == startIndentLevel and @foldEndRegexForScopes(scopes).search(@editSession.lineForBufferRow(row))
foldEndRow = row if includeRowInFold
break
@@ -196,3 +196,7 @@ class LanguageMode
decreaseIndentRegexForScopes: (scopes) ->
if decreaseIndentPattern = syntax.getProperty(scopes, 'editor.decreaseIndentPattern')
new OnigRegExp(decreaseIndentPattern)
foldEndRegexForScopes: (scopes) ->
if foldEndPattern = syntax.getProperty(scopes, 'editor.foldEndPattern')
new OnigRegExp(foldEndPattern)

View File

@@ -27,6 +27,8 @@ class TextMateBundle
@grammarsByFileType[fileType] = grammar
@grammarsByScopeName[grammar.scopeName] = grammar
bundle
@grammarForFilePath: (filePath) ->
return @grammarsByFileType["txt"] unless filePath
@@ -51,20 +53,6 @@ class TextMateBundle
@grammarForScopeName: (scopeName) ->
@grammarsByScopeName[scopeName]
@getPreferenceInScope: (scopeSelector, preferenceName) ->
@preferencesByScopeSelector[scopeSelector]?[preferenceName]
@getPreferenceValueInScope: (scope, preferenceName, valueName) ->
values = @getPreferenceInScope(scope, preferenceName)
(_.find values, ({name}) -> name is valueName)?['value']
@foldEndRegexForScope: (grammar, scope) ->
marker = @getPreferenceInScope(scope, 'foldingStopMarker')
if marker
new OnigRegExp(marker)
else
new OnigRegExp(grammar.foldingStopMarker)
grammars: null
constructor: (@path) ->

View File

@@ -19,7 +19,8 @@ class TextMatePackage extends Package
).join(', ')
load: ->
TextMateBundle.load(@name)
@bundle = TextMateBundle.load(@name)
@grammars = @bundle.grammars
super
constructor: ->
@@ -29,6 +30,12 @@ class TextMatePackage extends Package
getScopedProperties: ->
scopedProperties = []
for grammar in @grammars when grammar.foldingStopMarker
scopedProperties.push
selector: TextMatePackage.cssSelectorForScopeSelector(grammar.scopeName)
properties: { editor: { foldEndPattern: grammar.foldingStopMarker } }
if fs.exists(@preferencesPath)
for preferencePath in fs.list(@preferencesPath)
plist.parseString fs.read(preferencePath), (e, data) =>
@@ -53,5 +60,6 @@ class TextMatePackage extends Package
commentEnd: _.valueForKeyPath(textMateSettings, 'shellVariables.TM_COMMENT_END')
increaseIndentPattern: textMateSettings.increaseIndentPattern
decreaseIndentPattern: textMateSettings.decreaseIndentPattern
foldEndPattern: textMateSettings.foldingStopMarker
)
{ editor: editorProperties } if _.size(editorProperties) > 0