Allow scoped-properties to be added/removed by name

Also:
  - remove "global" properties
  - clear scoped properties between specs
This commit is contained in:
Corey Johnson & Nathan Sobo
2013-03-26 12:45:19 -06:00
committed by Nathan Sobo
parent 368e10a9f5
commit c8ec73d2f0
3 changed files with 29 additions and 15 deletions

View File

@@ -68,12 +68,11 @@ describe "the `syntax` global", ->
syntax.addProperties(".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42)
syntax.addProperties(".source .string.quoted.double", foo: bar: baz: 22)
syntax.addProperties(".source", foo: bar: baz: 11)
syntax.addProperties(foo: bar: baz: 1)
expect(syntax.getProperty([".source.coffee", ".string.quoted.double.coffee"], "foo.bar.baz")).toBe 42
expect(syntax.getProperty([".source.js", ".string.quoted.double.js"], "foo.bar.baz")).toBe 22
expect(syntax.getProperty([".source.js", ".variable.assignment.js"], "foo.bar.baz")).toBe 11
expect(syntax.getProperty([".text"], "foo.bar.baz")).toBe 1
expect(syntax.getProperty([".text"], "foo.bar.baz")).toBeUndefined()
it "favors the most recently added properties in the event of a specificity tie", ->
syntax.addProperties(".source.coffee .string.quoted.single", foo: bar: baz: 42)
@@ -81,3 +80,12 @@ describe "the `syntax` global", ->
expect(syntax.getProperty([".source.coffee", ".string.quoted.single"], "foo.bar.baz")).toBe 42
expect(syntax.getProperty([".source.coffee", ".string.quoted.single.double"], "foo.bar.baz")).toBe 22
describe ".removeProperties(name)", ->
it "allows properties to be removed by name", ->
syntax.addProperties("a", ".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42)
syntax.addProperties("b", ".source .string.quoted.double", foo: bar: baz: 22)
syntax.removeProperties("b")
expect(syntax.getProperty([".source.js", ".string.quoted.double.js"], "foo.bar.baz")).toBeUndefined()
expect(syntax.getProperty([".source.coffee", ".string.quoted.double.coffee"], "foo.bar.baz")).toBe 42

View File

@@ -42,6 +42,7 @@ beforeEach ->
spyOn(atom, 'getSavedWindowState').andReturn(null)
$native.setWindowState('')
syntax.clearGrammarOverrides()
syntax.clearProperties()
# used to reset keymap after each spec
bindingSetsToRestore = _.clone(keymap.bindingSets)

View File

@@ -22,7 +22,6 @@ class Syntax
@grammarsByFileType = {}
@grammarsByScopeName = {}
@grammarOverridesByPath = {}
@globalProperties = {}
@scopedPropertiesIndex = 0
@scopedProperties = []
@nullGrammar = new NullGrammar
@@ -101,18 +100,24 @@ class Syntax
@grammarsByScopeName[scopeName]
addProperties: (args...) ->
selector = args.shift() if args.length > 1
properties = args.shift()
name = args.shift() if args.length > 2
[selector, properties] = args
if selector
@scopedProperties.unshift(
selector: selector,
properties: properties,
specificity: Specificity(selector),
index: @scopedPropertiesIndex++
)
else
_.extend(@globalProperties, properties)
@scopedProperties.unshift(
name: name
selector: selector,
properties: properties,
specificity: Specificity(selector),
index: @scopedPropertiesIndex++
)
removeProperties: (name) ->
for properties in @scopedProperties.filter((properties) -> properties.name is name)
_.remove(@scopedProperties, properties)
clearProperties: ->
@scopedProperties = []
@scopedPropertiesIndex = 0
getProperty: (scope, keyPath) ->
for object in @propertiesForScope(scope, keyPath)
@@ -128,7 +133,7 @@ class Syntax
while element
matchingProperties.push(@matchingPropertiesForElement(element, candidates)...)
element = element.parentNode
matchingProperties.concat([@globalProperties])
matchingProperties
matchingPropertiesForElement: (element, candidates) ->
matchingScopedProperties = candidates.filter ({selector}) ->