*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:
Nathan Sobo
2012-12-17 20:56:28 -07:00
parent b82fe25b99
commit fc9bf38a2a
10 changed files with 60 additions and 43 deletions

View File

@@ -15,8 +15,17 @@ describe "Config", ->
expect(config.save).toHaveBeenCalled()
expect(observeHandler).toHaveBeenCalledWith 42
describe ".setDefaults(keyPath, defaults)", ->
it "assigns any previously-unassigned keys to the object at the key path", ->
config.set("foo.bar.baz", a: 1)
config.setDefaults("foo.bar.baz", a: 2, b: 3, c: 4)
expect(config.get("foo.bar.baz")).toEqual(a: 1, b: 3, c: 4)
config.setDefaults("foo.quux", x: 0, y: 1)
expect(config.get("foo.quux")).toEqual(x: 0, y: 1)
describe ".update()", ->
it "updates observers if a value is mutated without the use of .set()", ->
it "updates observers if a value is mutated without the use of .set", ->
config.set("foo.bar.baz", ["a"])
observeHandler = jasmine.createSpy "observeHandler"
config.observe "foo.bar.baz", observeHandler

View File

@@ -1552,7 +1552,7 @@ describe "Editor", ->
editor.setText " a line with tabs\tand spaces "
editor.attachToDom()
expect(config.editor.showInvisibles).toBeFalsy()
expect(config.get("editor.showInvisibles")).toBeFalsy()
expect(editor.renderedLines.find('.line').text()).toBe " a line with tabs and spaces "
config.set("editor.showInvisibles", true)
@@ -1574,8 +1574,8 @@ describe "Editor", ->
rightEditor.setText(" \t ")
leftEditor = rightEditor.splitLeft()
config.editor.showInvisibles = true
config.editor.invisibles =
config.set "editor.showInvisibles", true
config.set "editor.invisibles",
eol: ";"
space: "_"
tab: "tab"
@@ -1939,7 +1939,7 @@ describe "Editor", ->
describe "when autosave is enabled", ->
it "autosaves the current buffer when the editor loses focus or switches edit sessions", ->
config.set("editor.autosave", true)
config.set "editor.autosave", true
rootView.attachToDom()
editor2 = editor.splitRight()
spyOn(editor2.activeEditSession, 'save')

View File

@@ -131,7 +131,7 @@ describe "Project", ->
describe "when config.core.hideGitIgnoredFiles is true", ->
it "ignores files that are present in .gitignore if the project is a git repo", ->
config.core.hideGitIgnoredFiles = true
config.set "core.hideGitIgnoredFiles", true
project.setPath(require.resolve('fixtures/git/working-dir'))
paths = null
waitsForPromise ->
@@ -153,7 +153,7 @@ describe "Project", ->
it "ignores ignored.txt file", ->
paths = null
config.get("core.ignoredNames").push("ignored.txt")
config.set("core.ignoredNames", config.get("core.ignoredNames"))
config.update()
waitsForPromise ->
project.getFilePaths().done (foundPaths) -> paths = foundPaths

View File

@@ -26,7 +26,7 @@ beforeEach ->
spyOn(config, 'load')
spyOn(config, 'save')
config.assignDefaults()
config.editor.fontSize = 16
config.set "editor.fontSize", 16
# make editor display updates synchronous
spyOn(Editor.prototype, 'requestDisplayUpdate').andCallFake -> @updateDisplay()

View File

@@ -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

View File

@@ -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")

View File

@@ -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] == '/'

View File

@@ -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)

View File

@@ -857,7 +857,7 @@ describe "TreeView", ->
beforeEach ->
ignoreFile = fs.join(require.resolve('fixtures/tree-view'), '.gitignore')
fs.write(ignoreFile, 'tree-view.js')
config.core.hideGitIgnoredFiles = false
config.set "core.hideGitIgnoredFiles", false
afterEach ->
fs.remove(ignoreFile) if fs.exists(ignoreFile)

View File

@@ -27,7 +27,7 @@ class DirectoryView extends View
@directory.path
isPathIgnored: (path) ->
config.core.hideGitIgnoredFiles and @project.repo?.isPathIgnored(path)
config.get("core.hideGitIgnoredFiles") and @project.repo?.isPathIgnored(path)
buildEntries: ->
@unwatchDescendantEntries()