From 4e4794f3fd0857818004c846992c46d982bae6c9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 16 Dec 2014 09:44:06 -0800 Subject: [PATCH] Take 'scope' option in Config::observe Deprecate using the scope as an optional first argument --- spec/config-spec.coffee | 31 ++++++++++++++++++++++++++++-- src/config.coffee | 33 +++++++++++++++++--------------- src/display-buffer.coffee | 2 +- src/text-editor-component.coffee | 6 +++--- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 501ddd4ee..dd8beda76 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -2,6 +2,7 @@ path = require 'path' temp = require 'temp' CSON = require 'season' fs = require 'fs-plus' +Grim = require 'grim' describe "Config", -> dotAtomPath = null @@ -469,7 +470,7 @@ describe "Config", -> beforeEach -> observeHandler = jasmine.createSpy("observeHandler") atom.config.set("foo.bar.baz", "value 1") - observeSubscription = atom.config.observe "foo.bar.baz", observeHandler + observeSubscription = atom.config.observe("foo.bar.baz", observeHandler) it "fires the given callback with the current value at the keypath", -> expect(observeHandler).toHaveBeenCalledWith("value 1") @@ -511,6 +512,31 @@ describe "Config", -> atom.config.set('foo.bar.baz', "value 10") expect(bazCatHandler).not.toHaveBeenCalled() + describe "observing scoped settings", -> + otherHandler = null + + beforeEach -> + observeSubscription.dispose() + otherHandler = jasmine.createSpy('otherHandler') + + it "allows settings to be observed in a specific scope", -> + atom.config.observe("foo.bar.baz", scope: [".some.scope"], observeHandler) + atom.config.observe("foo.bar.baz", scope: [".another.scope"], otherHandler) + + atom.config.set('foo.bar.baz', "value 2", scopeSelector: ".some") + expect(observeHandler).toHaveBeenCalledWith("value 2") + expect(otherHandler).not.toHaveBeenCalledWith("value 2") + + it "deprecates using a scope descriptor as the first argument", -> + spyOn(Grim, 'deprecate') + atom.config.observe([".some.scope"], "foo.bar.baz", observeHandler) + atom.config.observe([".another.scope"], "foo.bar.baz", otherHandler) + expect(Grim.deprecate).toHaveBeenCalled() + + atom.config.set('foo.bar.baz', "value 2", scopeSelector: ".some") + expect(observeHandler).toHaveBeenCalledWith("value 2") + expect(otherHandler).not.toHaveBeenCalledWith("value 2") + describe ".initializeConfigDirectory()", -> beforeEach -> if fs.existsSync(dotAtomPath) @@ -1192,7 +1218,8 @@ describe "Config", -> describe ".observe(scopeDescriptor, keyPath)", -> it 'calls the supplied callback when the value at the descriptor/keypath changes', -> - atom.config.observe [".source.coffee", ".string.quoted.double.coffee"], "foo.bar.baz", changeSpy = jasmine.createSpy() + changeSpy = jasmine.createSpy() + atom.config.observe("foo.bar.baz", scope: [".source.coffee", ".string.quoted.double.coffee"], changeSpy) expect(changeSpy).toHaveBeenCalledWith(undefined) changeSpy.reset() diff --git a/src/config.coffee b/src/config.coffee index 8ddaa3739..30aa5f396 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -348,21 +348,24 @@ class Config # # Returns a {Disposable} with the following keys on which you can call # `.dispose()` to unsubscribe. - observe: (scopeDescriptor, keyPath, options, callback) -> - args = Array::slice.call(arguments) - if args.length is 2 - # observe(keyPath, callback) - [keyPath, callback, scopeDescriptor, options] = args - else if args.length is 3 and (Array.isArray(scopeDescriptor) or scopeDescriptor instanceof ScopeDescriptor) - # observe(scopeDescriptor, keyPath, callback) - [scopeDescriptor, keyPath, callback, options] = args - else if args.length is 3 and _.isString(scopeDescriptor) and _.isObject(keyPath) - # observe(keyPath, options, callback) # Deprecated! - [keyPath, options, callback, scopeDescriptor] = args - - message = "" - message = "`callNow` was set to false. Use ::onDidChange instead. Note that ::onDidChange calls back with different arguments." if options.callNow == false - Grim.deprecate "Config::observe no longer supports options; see https://atom.io/docs/api/latest/Config. #{message}" + observe: -> + if arguments.length is 2 + [keyPath, callback] = arguments + else if arguments.length is 3 and (_.isArray(arguments[0]) or arguments[0] instanceof ScopeDescriptor) + Grim.deprecate """ + Passing a scope descriptor as the first argument to Config::observe is deprecated. + Pass a `scope` in an options hash as the third argument instead. + """ + [scopeDescriptor, keyPath, callback] = arguments + else if arguments.length is 3 and (_.isString(arguments[0]) and _.isObject(arguments[1])) + [keyPath, options, callback] = arguments + scopeDescriptor = options.scope + if options.callNow? + Grim.deprecate """ + Config::observe no longer takes a `callNow` option. Use ::onDidChange instead. + Note that ::onDidChange passes its callback different arguments. + See https://atom.io/docs/api/latest/Config + """ else console.error 'An unsupported form of Config::observe is being used. See https://atom.io/docs/api/latest/Config for details' return diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index d0d9dcc9a..c1472ac5c 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -84,7 +84,7 @@ class DisplayBuffer extends Model @configSettings.preferredLineLength = newValue @updateWrappedScreenLines() if @isSoftWrapped() and atom.config.get('editor.softWrapAtPreferredLineLength', scope: scopeDescriptor) - subscriptions.add atom.config.observe scopeDescriptor, 'editor.scrollPastEnd', (value) => + subscriptions.add atom.config.observe 'editor.scrollPastEnd', scope: scopeDescriptor, (value) => @configSettings.scrollPastEnd = value @updateWrappedScreenLines() if oldConfigSettings? and not _.isEqual(oldConfigSettings, @configSettings) diff --git a/src/text-editor-component.coffee b/src/text-editor-component.coffee index 09df150df..4d0afd153 100644 --- a/src/text-editor-component.coffee +++ b/src/text-editor-component.coffee @@ -462,9 +462,9 @@ TextEditorComponent = React.createClass scopeDescriptor = editor.getRootScopeDescriptor() - subscriptions.add atom.config.observe scopeDescriptor, 'editor.showIndentGuide', @setShowIndentGuide - subscriptions.add atom.config.observe scopeDescriptor, 'editor.showLineNumbers', @setShowLineNumbers - subscriptions.add atom.config.observe scopeDescriptor, 'editor.scrollSensitivity', @setScrollSensitivity + subscriptions.add atom.config.observe 'editor.showIndentGuide', scope: scopeDescriptor, @setShowIndentGuide + subscriptions.add atom.config.observe 'editor.showLineNumbers', scope: scopeDescriptor, @setShowLineNumbers + subscriptions.add atom.config.observe 'editor.scrollSensitivity', scope: scopeDescriptor, @setScrollSensitivity focused: -> if @isMounted()