From 2cea51b50efdfa87abddc826b5bd60794d63e833 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 16 Dec 2014 09:44:36 -0800 Subject: [PATCH] Take 'scope' option in Config::onDidChange Deprecate passing the scope as an optional first argument --- spec/config-spec.coffee | 42 +++++++++++++++++++++++++++++++++---- src/config.coffee | 16 ++++++++++---- src/display-buffer.coffee | 6 +++--- src/text-editor.coffee | 4 ++-- src/tokenized-buffer.coffee | 2 +- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index dd8beda76..e44d4f0f1 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -721,7 +721,8 @@ describe "Config", -> runs -> updatedHandler.reset() it "does not fire a change event for paths that did not change", -> - atom.config.onDidChange 'foo.bar', noChangeSpy = jasmine.createSpy() + noChangeSpy = jasmine.createSpy() + atom.config.onDidChange('foo.bar', noChangeSpy) fs.writeFileSync(atom.config.configFilePath, "foo: { bar: ['baz', 'ok'], baz: 'another'}") waitsFor 'update event', -> updatedHandler.callCount > 0 @@ -732,7 +733,8 @@ describe "Config", -> describe 'when scoped settings are used', -> it "fires a change event for scoped settings that are removed", -> - atom.config.onDidChange ['.source.ruby'], 'foo.scoped', scopedSpy = jasmine.createSpy() + scopedSpy = jasmine.createSpy() + atom.config.onDidChange('foo.scoped', scope: ['.source.ruby'], scopedSpy) fs.writeFileSync atom.config.configFilePath, """ global: @@ -745,7 +747,8 @@ describe "Config", -> expect(atom.config.get('foo.scoped', scope: ['.source.ruby'])).toBe false it "does not fire a change event for paths that did not change", -> - atom.config.onDidChange ['.source.ruby'], 'foo.scoped', noChangeSpy = jasmine.createSpy() + noChangeSpy = jasmine.createSpy() + atom.config.onDidChange('foo.scoped', scope: ['.source.ruby'], noChangeSpy) fs.writeFileSync atom.config.configFilePath, """ global: @@ -1250,7 +1253,38 @@ describe "Config", -> describe ".onDidChange(scopeDescriptor, keyPath)", -> it 'calls the supplied callback when the value at the descriptor/keypath changes', -> keyPath = "foo.bar.baz" - atom.config.onDidChange [".source.coffee", ".string.quoted.double.coffee"], keyPath, changeSpy = jasmine.createSpy() + changeSpy = jasmine.createSpy('onDidChange callback') + atom.config.onDidChange keyPath, scope: [".source.coffee", ".string.quoted.double.coffee"], changeSpy + + atom.config.set("foo.bar.baz", 12) + expect(changeSpy).toHaveBeenCalledWith({oldValue: undefined, newValue: 12, keyPath}) + changeSpy.reset() + + disposable1 = atom.config.addScopedSettings("a", ".source .string.quoted.double", foo: bar: baz: 22) + expect(changeSpy).toHaveBeenCalledWith({oldValue: 12, newValue: 22, keyPath}) + changeSpy.reset() + + disposable2 = atom.config.addScopedSettings("b", ".source.coffee .string.quoted.double.coffee", foo: bar: baz: 42) + expect(changeSpy).toHaveBeenCalledWith({oldValue: 22, newValue: 42, keyPath}) + changeSpy.reset() + + disposable2.dispose() + expect(changeSpy).toHaveBeenCalledWith({oldValue: 42, newValue: 22, keyPath}) + changeSpy.reset() + + disposable1.dispose() + expect(changeSpy).toHaveBeenCalledWith({oldValue: 22, newValue: 12, keyPath}) + changeSpy.reset() + + atom.config.set("foo.bar.baz", undefined) + expect(changeSpy).toHaveBeenCalledWith({oldValue: 12, newValue: undefined, keyPath}) + changeSpy.reset() + + it 'deprecates using a scope descriptor as an optional first argument', -> + keyPath = "foo.bar.baz" + spyOn(Grim, 'deprecate') + atom.config.onDidChange [".source.coffee", ".string.quoted.double.coffee"], keyPath, changeSpy = jasmine.createSpy() + expect(Grim.deprecate).toHaveBeenCalled() atom.config.set("foo.bar.baz", 12) expect(changeSpy).toHaveBeenCalledWith({oldValue: undefined, newValue: 12, keyPath}) diff --git a/src/config.coffee b/src/config.coffee index 30aa5f396..6f6533c02 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -393,12 +393,20 @@ class Config # # Returns a {Disposable} with the following keys on which you can call # `.dispose()` to unsubscribe. - onDidChange: (scopeDescriptor, keyPath, callback) -> - args = Array::slice.call(arguments) + onDidChange: -> if arguments.length is 1 - [callback, scopeDescriptor, keyPath] = args + [callback] = arguments else if arguments.length is 2 - [keyPath, callback, scopeDescriptor] = args + [keyPath, callback] = arguments + else if _.isArray(arguments[0]) or arguments[0] instanceof ScopeDescriptor + Grim.deprecate """ + Passing a scope descriptor as the first argument to Config::onDidChange is deprecated. + Pass a `scope` in an options hash as the third argument instead. + """ + [scopeDescriptor, keyPath, callback] = arguments + else + [keyPath, options, callback] = arguments + scopeDescriptor = options.scope if scopeDescriptor? @onDidChangeScopedKeyPath(scopeDescriptor, keyPath, callback) diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index c1472ac5c..8b2940000 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -72,15 +72,15 @@ class DisplayBuffer extends Model softWrapAtPreferredLineLength: atom.config.get('editor.softWrapAtPreferredLineLength', scope: scopeDescriptor) preferredLineLength: atom.config.get('editor.preferredLineLength', scope: scopeDescriptor) - subscriptions.add atom.config.onDidChange scopeDescriptor, 'editor.softWrap', ({newValue}) => + subscriptions.add atom.config.onDidChange 'editor.softWrap', scope: scopeDescriptor, ({newValue}) => @configSettings.softWrap = newValue @updateWrappedScreenLines() - subscriptions.add atom.config.onDidChange scopeDescriptor, 'editor.softWrapAtPreferredLineLength', ({newValue}) => + subscriptions.add atom.config.onDidChange 'editor.softWrapAtPreferredLineLength', scope: scopeDescriptor, ({newValue}) => @configSettings.softWrapAtPreferredLineLength = newValue @updateWrappedScreenLines() if @isSoftWrapped() - subscriptions.add atom.config.onDidChange scopeDescriptor, 'editor.preferredLineLength', ({newValue}) => + subscriptions.add atom.config.onDidChange 'editor.preferredLineLength', scope: scopeDescriptor, ({newValue}) => @configSettings.preferredLineLength = newValue @updateWrappedScreenLines() if @isSoftWrapped() and atom.config.get('editor.softWrapAtPreferredLineLength', scope: scopeDescriptor) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index e2e71c783..1263936e0 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -168,8 +168,8 @@ class TextEditor extends Model scopeDescriptor = @getRootScopeDescriptor() - subscriptions.add atom.config.onDidChange scopeDescriptor, 'editor.showInvisibles', => @updateInvisibles() - subscriptions.add atom.config.onDidChange scopeDescriptor, 'editor.invisibles', => @updateInvisibles() + subscriptions.add atom.config.onDidChange 'editor.showInvisibles', scope: scopeDescriptor, => @updateInvisibles() + subscriptions.add atom.config.onDidChange 'editor.invisibles', scope: scopeDescriptor, => @updateInvisibles() getViewClass: -> require './text-editor-view' diff --git a/src/tokenized-buffer.coffee b/src/tokenized-buffer.coffee index ba35ec2dd..5557ea9ac 100644 --- a/src/tokenized-buffer.coffee +++ b/src/tokenized-buffer.coffee @@ -87,7 +87,7 @@ class TokenizedBuffer extends Model @configSettings = tabLength: atom.config.get('editor.tabLength', scope: @rootScopeDescriptor) @grammarTabLengthSubscription?.dispose() - @grammarTabLengthSubscription = atom.config.onDidChange @rootScopeDescriptor, 'editor.tabLength', ({newValue}) => + @grammarTabLengthSubscription = atom.config.onDidChange 'editor.tabLength', scope: @rootScopeDescriptor, ({newValue}) => @configSettings.tabLength = newValue @retokenizeLines() @subscribe @grammarTabLengthSubscription