Merge pull request #3847 from atom/bo-scoped-defaults

Add scoped defaults
This commit is contained in:
Ben Ogle
2014-10-16 11:04:15 -07:00
3 changed files with 125 additions and 18 deletions

View File

@@ -519,29 +519,58 @@ class Config
# * `keyPath` The {String} name of the key.
#
# Returns the new value.
restoreDefault: (keyPath) ->
@set(keyPath, _.valueForKeyPath(@defaultSettings, keyPath))
@get(keyPath)
restoreDefault: (scopeSelector, keyPath) ->
if arguments.length == 1
keyPath = scopeSelector
scopeSelector = null
if scopeSelector?
settings = @scopedSettingsStore.propertiesForSourceAndSelector('user-config', scopeSelector)
@scopedSettingsStore.removePropertiesForSourceAndSelector('user-config', scopeSelector)
_.setValueForKeyPath(settings, keyPath, undefined)
@addScopedSettings('user-config', scopeSelector, settings)
@getDefault(scopeSelector, keyPath)
else
@set(keyPath, _.valueForKeyPath(@defaultSettings, keyPath))
@get(keyPath)
# Extended: Get the global default value of the key path. _Please note_ that in most
# cases calling this is not necessary! {::get} returns the default value when
# a custom value is not specified.
#
# * `scopeSelector` (optional) {String}. eg. '.source.ruby'
# * `keyPath` The {String} name of the key.
#
# Returns the default value.
getDefault: (keyPath) ->
defaultValue = _.valueForKeyPath(@defaultSettings, keyPath)
getDefault: (scopeSelector, keyPath) ->
if arguments.length == 1
keyPath = scopeSelector
scopeSelector = null
if scopeSelector?
defaultValue = @scopedSettingsStore.getPropertyValue(scopeSelector, keyPath, excludeSources: ['user-config'])
defaultValue ?= _.valueForKeyPath(@defaultSettings, keyPath)
else
defaultValue = _.valueForKeyPath(@defaultSettings, keyPath)
_.deepClone(defaultValue)
# Extended: Is the value at `keyPath` its default value?
#
# * `scopeSelector` (optional) {String}. eg. '.source.ruby'
# * `keyPath` The {String} name of the key.
#
# Returns a {Boolean}, `true` if the current value is the default, `false`
# otherwise.
isDefault: (keyPath) ->
not _.valueForKeyPath(@settings, keyPath)?
isDefault: (scopeSelector, keyPath) ->
if arguments.length == 1
keyPath = scopeSelector
scopeSelector = null
if scopeSelector?
settings = @scopedSettingsStore.propertiesForSourceAndSelector('user-config', scopeSelector)
not _.valueForKeyPath(settings, keyPath)?
else
not _.valueForKeyPath(@settings, keyPath)?
# Extended: Retrieve the schema for a specific key path. The schema will tell
# you what type the keyPath expects, and other metadata about the config
@@ -560,9 +589,17 @@ class Config
schema
# Extended: Returns a new {Object} containing all of the global settings and
# defaults. This does not include scoped settings.
getSettings: ->
_.deepExtend(@settings, @defaultSettings)
# defaults. Returns the scoped settings when a `scopeSelector` is specified.
#
# * `scopeSelector` (optional) {String}. eg. '.source.ruby'
getSettings: (scopeSelector) ->
settings = _.deepExtend(@settings, @defaultSettings)
if scopeSelector?
scopedSettings = @scopedSettingsStore.propertiesForSelector(scopeSelector)
settings = _.deepExtend(scopedSettings, settings)
settings
# Extended: Get the {String} path to the config file being used.
getUserConfigPath: ->
@@ -810,10 +847,10 @@ class Config
@usersScopedSettings.add @scopedSettingsStore.addProperties('user-config', newScopedSettings)
@emitter.emit 'did-change'
addScopedSettings: (name, selector, value) ->
addScopedSettings: (source, selector, value) ->
settingsBySelector = {}
settingsBySelector[selector] = value
disposable = @scopedSettingsStore.addProperties(name, settingsBySelector)
disposable = @scopedSettingsStore.addProperties(source, settingsBySelector)
@emitter.emit 'did-change'
new Disposable =>
disposable.dispose()
@@ -831,11 +868,7 @@ class Config
@emitter.emit 'did-change'
getRawScopedValue: (scopeDescriptor, keyPath) ->
scopeChain = scopeDescriptor
.map (scope) ->
scope = ".#{scope}" unless scope[0] is '.'
scope
.join(' ')
scopeChain = @scopeChainForScopeDescriptor(scopeDescriptor)
@scopedSettingsStore.getPropertyValue(scopeChain, keyPath)
observeScopedKeyPath: (scopeDescriptor, keyPath, callback) ->
@@ -870,6 +903,13 @@ class Config
.join(' ')
@scopedSettingsStore.getProperties(scopeChain, keyPath)
scopeChainForScopeDescriptor: (scopeDescriptor) ->
scopeDescriptor
.map (scope) ->
scope = ".#{scope}" unless scope[0] is '.'
scope
.join(' ')
# Base schema enforcers. These will coerce raw input into the specified type,
# and will throw an error when the value cannot be coerced. Throwing the error
# will indicate that the value should not be set.