mirror of
https://github.com/atom/atom.git
synced 2026-02-09 06:05:11 -05:00
*Must* use get and set to access config values.
The `config` object no longer stores config properties directly. Instead it stores them on an internal `settings` object, which makes it easier to serialize settings without getting them mixed up with non-setting state on the `config` object.
This commit is contained in:
@@ -13,8 +13,10 @@ require.paths.unshift userExtensionsDirPath
|
||||
module.exports =
|
||||
class Config
|
||||
configDirPath: configDirPath
|
||||
settings: null
|
||||
|
||||
load: ->
|
||||
@settings = {}
|
||||
@loadUserConfig()
|
||||
@assignDefaults()
|
||||
@registerNewExtensions()
|
||||
@@ -24,13 +26,12 @@ class Config
|
||||
loadUserConfig: ->
|
||||
if fs.exists(configJsonPath)
|
||||
userConfig = JSON.parse(fs.read(configJsonPath))
|
||||
_.extend(this, userConfig)
|
||||
_.extend(@settings, userConfig)
|
||||
|
||||
assignDefaults: ->
|
||||
@core ?= {}
|
||||
_.defaults(@core, require('root-view').configDefaults)
|
||||
@editor ?= {}
|
||||
_.defaults(@editor, require('editor').configDefaults)
|
||||
@settings ?= {}
|
||||
@setDefaults "core", require('root-view').configDefaults
|
||||
@setDefaults "editor", require('editor').configDefaults
|
||||
|
||||
registerNewExtensions: ->
|
||||
shouldUpdate = false
|
||||
@@ -51,33 +52,45 @@ class Config
|
||||
_.unique(availableExtensions)
|
||||
|
||||
requireExtensions: ->
|
||||
for extensionName in config.core.extensions
|
||||
for extensionName in config.get "core.extensions"
|
||||
requireExtension(extensionName) unless extensionName[0] == '!'
|
||||
|
||||
|
||||
get: (keyPath) ->
|
||||
keyPath = keyPath.split(".") if typeof keyPath is 'string'
|
||||
value = this
|
||||
for key in keyPath
|
||||
keys = @keysForKeyPath(keyPath)
|
||||
value = @settings
|
||||
for key in keys
|
||||
break unless value = value[key]
|
||||
value
|
||||
|
||||
set: (keyPath, value) ->
|
||||
if typeof keyPath is 'string'
|
||||
keyPath = keyPath.split(".")
|
||||
else
|
||||
keyPath = new Array(keyPath...)
|
||||
|
||||
hash = this
|
||||
while keyPath.length > 1
|
||||
key = keyPath.shift()
|
||||
keys = @keysForKeyPath(keyPath)
|
||||
hash = @settings
|
||||
while keys.length > 1
|
||||
key = keys.shift()
|
||||
hash[key] ?= {}
|
||||
hash = hash[key]
|
||||
hash[keyPath.shift()] = value
|
||||
hash[keys.shift()] = value
|
||||
|
||||
@update()
|
||||
value
|
||||
|
||||
setDefaults: (keyPath, defaults) ->
|
||||
keys = @keysForKeyPath(keyPath)
|
||||
hash = @settings
|
||||
for key in keys
|
||||
hash[key] ?= {}
|
||||
hash = hash[key]
|
||||
|
||||
_.defaults hash, defaults
|
||||
@update()
|
||||
|
||||
keysForKeyPath: (keyPath) ->
|
||||
if typeof keyPath is 'string'
|
||||
keyPath.split(".")
|
||||
else
|
||||
new Array(keyPath...)
|
||||
|
||||
observe: (keyPath, callback) ->
|
||||
value = @get(keyPath)
|
||||
previousValue = _.clone(value)
|
||||
@@ -97,11 +110,7 @@ class Config
|
||||
@trigger 'update'
|
||||
|
||||
save: ->
|
||||
keysToWrite = _.clone(this)
|
||||
delete keysToWrite.eventHandlersByEventName
|
||||
delete keysToWrite.eventHandlersByNamespace
|
||||
delete keysToWrite.configDirPath
|
||||
fs.write(configJsonPath, JSON.stringify(keysToWrite, undefined, 2) + "\n")
|
||||
fs.write(configJsonPath, JSON.stringify(@settings, undefined, 2) + "\n")
|
||||
|
||||
requireUserInitScript: ->
|
||||
try
|
||||
|
||||
@@ -324,7 +324,7 @@ class Editor extends View
|
||||
@hiddenInput.on 'focusout', =>
|
||||
@isFocused = false
|
||||
@removeClass 'focused'
|
||||
@autosave() if config.editor.autosave
|
||||
@autosave() if config.get "editor.autosave"
|
||||
|
||||
@overlayer.on 'mousedown', (e) =>
|
||||
@overlayer.hide()
|
||||
@@ -450,7 +450,7 @@ class Editor extends View
|
||||
throw new Error("Edit session not found") unless @editSessions[index]
|
||||
|
||||
if @activeEditSession
|
||||
@autosave() if config.editor.autosave
|
||||
@autosave() if config.get "editor.autosave"
|
||||
@saveScrollPositionForActiveEditSession()
|
||||
@activeEditSession.off(".editor")
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ class Project
|
||||
@ignoreRepositoryPath(path)
|
||||
|
||||
ignoreRepositoryPath: (path) ->
|
||||
config.core.hideGitIgnoredFiles and @repo.isPathIgnored(fs.join(@getPath(), path))
|
||||
config.get("core.hideGitIgnoredFiles") and @repo.isPathIgnored(fs.join(@getPath(), path))
|
||||
|
||||
resolve: (filePath) ->
|
||||
filePath = fs.join(@getPath(), filePath) unless filePath[0] == '/'
|
||||
|
||||
@@ -42,7 +42,7 @@ class RootView extends View
|
||||
|
||||
config.load()
|
||||
|
||||
TextMateTheme.activate(config.core.theme ? 'IR_Black')
|
||||
TextMateTheme.activate(config.get("core.theme") ? 'IR_Black')
|
||||
|
||||
@handleEvents()
|
||||
|
||||
@@ -79,18 +79,17 @@ class RootView extends View
|
||||
@setTitle("untitled")
|
||||
|
||||
@command 'window:increase-font-size', =>
|
||||
config.editor.fontSize += 1
|
||||
config.update()
|
||||
config.set("editor.fontSize", config.get("editor.fontSize") + 1)
|
||||
|
||||
@command 'window:decrease-font-size', =>
|
||||
if config.editor.fontSize > 1
|
||||
config.editor.fontSize -= 1
|
||||
config.update()
|
||||
fontSize = config.get "editor.fontSize"
|
||||
config.set("editor.fontSize", fontSize - 1) if fontSize > 1
|
||||
|
||||
|
||||
@command 'window:focus-next-pane', => @focusNextPane()
|
||||
@command 'window:save-all', => @saveAll()
|
||||
@command 'window:toggle-invisibles', =>
|
||||
config.set("editor.showInvisibles", not config.editor.showInvisibles)
|
||||
config.set("editor.showInvisibles", !config.get("editor.showInvisibles"))
|
||||
@command 'window:toggle-ignored-files', =>
|
||||
config.set("core.hideGitIgnoredFiles", not config.core.hideGitIgnoredFiles)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user