Add support for scoped defaults in config schemas

This commit is contained in:
Ben Ogle 2014-10-20 14:52:12 -07:00
parent 4d1e488ef4
commit 7ffe5d1385
2 changed files with 42 additions and 0 deletions

View File

@ -1032,6 +1032,21 @@ describe "Config", ->
expect(atom.config.set('foo.bar.arr', ['two', 'three'])).toBe true
expect(atom.config.get('foo.bar.arr')).toEqual ['two', 'three']
describe "when scoped settings are used", ->
beforeEach ->
schema =
type: 'string'
default: 'ok'
scopes:
'.source.js':
default: 'omg'
atom.config.setSchema('foo.bar.str', schema)
it 'it respects the scoped defaults', ->
expect(atom.config.get('foo.bar.str')).toBe 'ok'
expect(atom.config.get(['.source.js'], 'foo.bar.str')).toBe 'omg'
expect(atom.config.get(['.source.coffee'], 'foo.bar.str')).toBe 'ok'
describe "scoped settings", ->
describe ".get(scopeDescriptor, keyPath)", ->
it "returns the property with the most specific scope selector", ->

View File

@ -679,6 +679,7 @@ class Config
_.extend rootSchema, schema
@setDefaults(keyPath, @extractDefaultsFromSchema(schema))
@setScopedDefaultsFromSchema(keyPath, schema)
load: ->
@initializeConfigDirectory()
@ -833,6 +834,32 @@ class Config
catch e
console.warn("'#{keyPath}' could not set the default. Attempted default: #{JSON.stringify(defaults)}; Schema: #{JSON.stringify(@getSchema(keyPath))}")
# `schema` will look something like this
#
# ```coffee
# type: 'string'
# default: 'ok'
# scopes:
# '.source.js':
# default: 'omg'
# ```
setScopedDefaultsFromSchema: (keyPath, schema) ->
if schema.scopes? and isPlainObject(schema.scopes)
scopedDefaults = {}
for scope, scopeSchema of schema.scopes
continue unless scopeSchema.hasOwnProperty('default')
scopedDefaults[scope] = {}
_.setValueForKeyPath(scopedDefaults[scope], keyPath, scopeSchema.default)
@scopedSettingsStore.addProperties('schema-default', scopedDefaults)
if schema.type is 'object' and schema.properties? and isPlainObject(schema.properties)
keys = if keyPath? then keyPath.split('.') else []
for key, childValue of schema.properties
continue unless schema.properties.hasOwnProperty(key)
@setScopedDefaultsFromSchema(keys.concat([key]).join('.'), childValue)
return
extractDefaultsFromSchema: (schema) ->
if schema.default?
schema.default