From c8ec73d2f03147df51edb8eba67a71b48ee77c7e Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 26 Mar 2013 12:45:19 -0600 Subject: [PATCH] Allow scoped-properties to be added/removed by name Also: - remove "global" properties - clear scoped properties between specs --- spec/app/syntax-spec.coffee | 12 ++++++++++-- spec/spec-helper.coffee | 1 + src/app/syntax.coffee | 31 ++++++++++++++++++------------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/spec/app/syntax-spec.coffee b/spec/app/syntax-spec.coffee index 01da46baa..ad12bee71 100644 --- a/spec/app/syntax-spec.coffee +++ b/spec/app/syntax-spec.coffee @@ -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 diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 2bef64dc2..65a1b85dd 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -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) diff --git a/src/app/syntax.coffee b/src/app/syntax.coffee index fe15047aa..7ab711a14 100644 --- a/src/app/syntax.coffee +++ b/src/app/syntax.coffee @@ -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}) ->