Use config.update w/ a key path everywhere we update the config

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-12-14 15:21:18 -08:00
parent 827d6b57fb
commit 8a675fd538
7 changed files with 41 additions and 43 deletions

View File

@@ -1,5 +1,13 @@
describe "Config", ->
describe "#observe(keyPath)", ->
describe ".update(keyPath, value)", ->
it "sets the value at the given key path and emits an update event", ->
updateHandler = jasmine.createSpy 'updateHandler'
config.on 'update', updateHandler
config.update("foo.bar.baz", "hello")
expect(config.foo.bar.baz).toBe "hello"
expect(updateHandler).toHaveBeenCalled()
describe ".observe(keyPath)", ->
observeHandler = null
beforeEach ->
@@ -12,23 +20,19 @@ describe "Config", ->
it "fires the callback every time the observed value changes", ->
observeHandler.reset() # clear the initial call
config.foo.bar.baz = "value 2"
config.update()
config.update('foo.bar.baz', "value 2")
expect(observeHandler).toHaveBeenCalledWith("value 2")
observeHandler.reset()
config.foo.bar.baz = "value 1"
config.update()
config.update('foo.bar.baz', "value 1")
expect(observeHandler).toHaveBeenCalledWith("value 1")
it "fires the callback when the full key path goes into and out of existence", ->
observeHandler.reset() # clear the initial call
delete config.foo.bar
config.update()
config.update("foo.bar", undefined)
expect(observeHandler).toHaveBeenCalledWith(undefined)
observeHandler.reset()
config.foo.bar = { baz: "i'm back" }
config.update()
config.update("foo.bar.baz", "i'm back")
expect(observeHandler).toHaveBeenCalledWith("i'm back")

View File

@@ -504,8 +504,7 @@ describe "Editor", ->
describe "font size", ->
it "sets the initial font size based on the value from config", ->
config.editor.fontSize = 20
config.update()
config.update("editor.fontSize", 20)
newEditor = editor.splitRight()
expect(editor.css('font-size')).toBe '20px'
expect(newEditor.css('font-size')).toBe '20px'
@@ -516,14 +515,12 @@ describe "Editor", ->
rootView.height(200)
rootView.width(200)
config.editor.fontSize = 10
config.update()
config.update("editor.fontSize", fontSize)
lineHeightBefore = editor.lineHeight
charWidthBefore = editor.charWidth
editor.setCursorScreenPosition [5, 6]
config.editor.fontSize = 30
config.update()
config.update("editor.fontSize", 30)
expect(editor.css('font-size')).toBe '30px'
expect(editor.lineHeight).toBeGreaterThan lineHeightBefore
expect(editor.charWidth).toBeGreaterThan charWidthBefore
@@ -533,12 +530,10 @@ describe "Editor", ->
it "updates the position and size of selection regions", ->
rootView.attachToDom()
config.editor.fontSize = 10
config.update()
config.update("editor.fontSize", 10)
editor.setSelectedBufferRange([[5, 2], [5, 7]])
config.editor.fontSize = 30
config.update()
config.update("editor.fontSize", 30)
selectionRegion = editor.find('.region')
expect(selectionRegion.position().top).toBe 5 * editor.lineHeight
expect(selectionRegion.position().left).toBe 2 * editor.charWidth
@@ -547,8 +542,7 @@ describe "Editor", ->
it "updates the gutter width and font size", ->
rootView.attachToDom()
config.editor.fontSize = 16 * 4
config.update()
config.update("editor.fontSize", 16 * 4)
expect(editor.gutter.css('font-size')).toBe "#{16 * 4}px"
expect(editor.gutter.width()).toBe(141)
@@ -557,8 +551,7 @@ describe "Editor", ->
originalLineCount = editor.renderedLines.find(".line").length
expect(originalLineCount).toBeGreaterThan 0
config.editor.fontSize = 10
config.update()
config.update("editor.fontSize", 10)
expect(editor.renderedLines.find(".line").length).toBeGreaterThan originalLineCount
describe "mouse events", ->
@@ -1562,11 +1555,10 @@ describe "Editor", ->
expect(config.editor.showInvisibles).toBeFalsy()
expect(editor.renderedLines.find('.line').text()).toBe " a line with tabs and spaces "
config.editor.showInvisibles = true
config.update()
config.update("editor.showInvisibles", true)
expect(editor.renderedLines.find('.line').text()).toBe "•a line with tabs▸ and spaces•¬"
config.editor.showInvisibles = false
config.update("editor.showInvisibles", false)
config.update()
expect(editor.renderedLines.find('.line').text()).toBe " a line with tabs and spaces "

View File

@@ -557,8 +557,7 @@ describe "RootView", ->
expect(editor.getFontSize()).toBe fontSizeBefore
it "does not allow the font size to be less than 1", ->
config.editor.fontSize = 1
config.update()
config.update("editor.fontSize", 1)
rootView.trigger 'window:decrease-font-size'
expect(editor.getFontSize()).toBe 1

View File

@@ -24,24 +24,21 @@ describe "SpacePen extensions", ->
expect(observeHandler).toHaveBeenCalledWith(undefined)
observeHandler.reset()
config.foo = { bar: "hello" }
config.update()
config.update("foo.bar", "hello")
expect(observeHandler).toHaveBeenCalledWith("hello")
observeHandler.reset()
view.unsubscribe()
config.foo.bar = "goodbye"
config.update()
config.update("foo.bar", "goodbye")
expect(observeHandler).not.toHaveBeenCalled()
it "unsubscribes when the view is removed", ->
observeHandler.reset()
parent.remove()
config.foo = { bar: "hello" }
config.update()
config.update("foo.bar", "hello")
expect(observeHandler).not.toHaveBeenCalled()
describe "View#subscribe(eventEmitter, eventName, callback)", ->

View File

@@ -21,7 +21,8 @@ class Config
@editor ?= {}
_.defaults(@editor, require('editor').configDefaults)
update: ->
update: (keyPathString, value) ->
@setValueAtKeyPath(keyPathString.split('.'), value) if keyPathString
@save()
@trigger 'update'
@@ -47,6 +48,15 @@ class Config
break unless value = value[key]
value
setValueAtKeyPath: (keyPath, value) ->
keyPath = new Array(keyPath...)
hash = this
while keyPath.length > 1
key = keyPath.shift()
hash[key] ?= {}
hash = hash[key]
hash[keyPath.shift()] = value
observe: (keyPathString, callback) ->
keyPath = keyPathString.split('.')
value = @valueAtKeyPath(keyPath)

View File

@@ -88,11 +88,9 @@ class RootView extends View
@command 'window:focus-next-pane', => @focusNextPane()
@command 'window:save-all', => @saveAll()
@command 'window:toggle-invisibles', =>
config.editor.showInvisibles = not config.editor.showInvisibles
config.update()
config.update("editor.showInvisibles", not config.editor.showInvisibles)
@command 'window:toggle-ignored-files', =>
config.core.hideGitIgnoredFiles = not config.core.hideGitIgnoredFiles
config.update()
config.update("core.hideGitIgnoredFiles", not config.core.hideGitIgnoredFiles)
afterAttach: (onDom) ->
@focus() if onDom

View File

@@ -865,12 +865,10 @@ describe "TreeView", ->
it "hides git-ignored files if the option is set, but otherwise shows them", ->
expect(treeView.find('.file:contains(tree-view.js)').length).toBe 1
config.core.hideGitIgnoredFiles = true
config.update()
config.update("core.hideGitIgnoredFiles", true)
expect(treeView.find('.file:contains(tree-view.js)').length).toBe 0
config.core.hideGitIgnoredFiles = false
config.update()
config.update("core.hideGitIgnoredFiles", false)
expect(treeView.find('.file:contains(tree-view.js)').length).toBe 1