Read and write scoped settings from the user’s config

This commit is contained in:
Ben Ogle
2014-10-07 15:19:55 -07:00
parent b44a5dd1f0
commit a3bbbc19b5
4 changed files with 123 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
_ = require 'underscore-plus'
fs = require 'fs-plus'
EmitterMixin = require('emissary').Emitter
{Disposable, Emitter} = require 'event-kit'
{CompositeDisposable, Disposable, Emitter} = require 'event-kit'
CSON = require 'season'
path = require 'path'
async = require 'async'
@@ -312,6 +312,7 @@ class Config
@defaultSettings = {}
@settings = {}
@scopedSettingsStore = new ScopedPropertyStore
@usersScopedSettings = new CompositeDisposable
@configFileHasErrors = false
@configFilePath = fs.resolve(@configDirPath, 'config', ['json', 'cson'])
@configFilePath ?= path.join(@configDirPath, 'config.cson')
@@ -658,7 +659,7 @@ class Config
try
userConfig = CSON.readFileSync(@configFilePath)
@setAll(userConfig)
@resetUserSettings(userConfig)
@configFileHasErrors = false
catch error
@configFileHasErrors = true
@@ -678,18 +679,30 @@ class Config
@watchSubscription = null
save: ->
CSON.writeFileSync(@configFilePath, @settings)
allSettings = global: @settings
for settingsObject in @scopedSettingsStore.propertiesForSource('user-config')
for selector, properties of settingsObject
allSettings[selector] = properties
CSON.writeFileSync(@configFilePath, allSettings)
###
Section: Private methods managing global settings
###
setAll: (newSettings) ->
resetUserSettings: (newSettings) ->
unless isPlainObject(newSettings)
@settings = {}
@emitter.emit 'did-change'
return
if newSettings.global?
scopedSettings = newSettings
newSettings = newSettings.global
delete scopedSettings.global
@resetUserScopedSettings(scopedSettings)
unsetUnspecifiedValues = (keyPath, value) =>
if isPlainObject(value)
keys = if keyPath? then keyPath.split('.') else []
@@ -782,12 +795,13 @@ class Config
Section: Private Scoped Settings
###
addScopedSettings: (name, selector, value) ->
if arguments.length < 3
value = selector
selector = name
name = null
resetUserScopedSettings: (newScopedSettings) ->
@usersScopedSettings?.dispose()
@usersScopedSettings = new CompositeDisposable
@usersScopedSettings.add @scopedSettingsStore.addProperties('user-config', newScopedSettings)
@emitter.emit 'did-change'
addScopedSettings: (name, selector, value) ->
settingsBySelector = {}
settingsBySelector[selector] = value
disposable = @scopedSettingsStore.addProperties(name, settingsBySelector)
@@ -801,7 +815,11 @@ class Config
newValue = {}
_.setValueForKeyPath(newValue, keyPath, value)
value = newValue
@addScopedSettings(null, selector, value)
settingsBySelector = {}
settingsBySelector[selector] = value
@usersScopedSettings.add @scopedSettingsStore.addProperties('user-config', settingsBySelector)
@emitter.emit 'did-change'
getRawScopedValue: (scopeDescriptor, keyPath) ->
scopeChain = scopeDescriptor

View File

@@ -40,6 +40,7 @@ class Syntax extends GrammarRegistry
atom.config.scopedSettingsStore
addProperties: (args...) ->
args.unshift(null) if args.length == 2
deprecate 'Consider using atom.config.set() instead. A direct (but private) replacement is available at atom.config.addScopedSettings().'
atom.config.addScopedSettings(args...)