diff --git a/docs/internals/configuration.md b/docs/internals/configuration.md index ab9dbedf7..162904607 100644 --- a/docs/internals/configuration.md +++ b/docs/internals/configuration.md @@ -48,6 +48,8 @@ the following way: ```coffeescript # basic key update config.set("core.autosave", true) + +config.pushAtKeyPath("core.disabledPackages", "wrap-guide") ``` You can also use `setDefaults`, which will assign default values for keys that diff --git a/spec/app/config-spec.coffee b/spec/app/config-spec.coffee index 41933f2db..2ca2045b8 100644 --- a/spec/app/config-spec.coffee +++ b/spec/app/config-spec.coffee @@ -41,6 +41,28 @@ describe "Config", -> config.set('foo.changes', 1) expect(config.settings.foo).toEqual {} + describe ".pushAtKeyPath(keyPath, value)", -> + it "pushes the given value to the array at the key path and updates observers", -> + config.set("foo.bar.baz", ["a"]) + observeHandler = jasmine.createSpy "observeHandler" + config.observe "foo.bar.baz", observeHandler + observeHandler.reset() + + expect(config.pushAtKeyPath("foo.bar.baz", "b")).toBe 2 + expect(config.get("foo.bar.baz")).toEqual ["a", "b"] + expect(observeHandler).toHaveBeenCalledWith config.get("foo.bar.baz") + + describe ".removeAtKeyPath(keyPath, value)", -> + it "removes the given value from the array at the key path and updates observers", -> + config.set("foo.bar.baz", ["a", "b", "c"]) + observeHandler = jasmine.createSpy "observeHandler" + config.observe "foo.bar.baz", observeHandler + observeHandler.reset() + + expect(config.removeAtKeyPath("foo.bar.baz", "b")).toEqual ["a", "c"] + expect(config.get("foo.bar.baz")).toEqual ["a", "c"] + expect(observeHandler).toHaveBeenCalledWith config.get("foo.bar.baz") + describe ".getPositiveInt(keyPath, defaultValue)", -> it "returns the proper current or default value", -> config.set('editor.preferredLineLength', 0) diff --git a/spec/app/project-spec.coffee b/spec/app/project-spec.coffee index 6180ceb23..239c56d40 100644 --- a/spec/app/project-spec.coffee +++ b/spec/app/project-spec.coffee @@ -180,9 +180,7 @@ describe "Project", -> it "ignores ignored.txt file", -> paths = null - ignoredNames = config.get("core.ignoredNames") - ignoredNames.push("ignored.txt") - config.set("core.ignoredNames", ignoredNames) + config.pushAtKeyPath("core.ignoredNames", "ignored.txt") waitsForPromise -> project.getFilePaths().done (foundPaths) -> paths = foundPaths diff --git a/src/app/config.coffee b/src/app/config.coffee index 5fbb5e08e..0b0de9cb2 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -144,6 +144,30 @@ class Config @update() value + # Push the value to the array at the key path. + # + # keyPath - The {String} key path. + # value - The value to push to the array. + # + # Returns the new array length of the setting. + pushAtKeyPath: (keyPath, value) -> + arrayValue = @get(keyPath) ? [] + result = arrayValue.push(value) + @set(keyPath, arrayValue) + result + + # Remove the value from the array at the key path. + # + # keyPath - The {String} key path. + # value - The value to remove from the array. + # + # Returns the new array value of the setting. + removeAtKeyPath: (keyPath, value) -> + arrayValue = @get(keyPath) ? [] + result = _.remove(arrayValue, value) + @set(keyPath, arrayValue) + result + # Establishes an event listener for a given key. # # `callback` is fired immediately and whenever the value of the key is changed diff --git a/src/app/package-config-panel.coffee b/src/app/package-config-panel.coffee index 137ca6851..febfc8978 100644 --- a/src/app/package-config-panel.coffee +++ b/src/app/package-config-panel.coffee @@ -29,12 +29,10 @@ class PackageConfigPanel extends ConfigPanel @on 'change', '#packages input[type=checkbox]', (e) -> checkbox = $(e.target) name = checkbox.closest('tr').attr('name') - disabledPackages = config.get('core.disabledPackages') if checkbox.attr('checked') - _.remove(disabledPackages, name) + config.removeAtKeyPath('core.disabledPackages', name) else - disabledPackages.push(name) - config.set('core.disabledPackages', disabledPackages) + config.pushAtKeyPath('core.disabledPackages', name) @observeConfig 'core.disabledPackages', (disabledPackages) => @packageTableBody.find("input[type='checkbox']").attr('checked', true) diff --git a/src/stdlib/underscore-extensions.coffee b/src/stdlib/underscore-extensions.coffee index 8251cbcd1..af28237d9 100644 --- a/src/stdlib/underscore-extensions.coffee +++ b/src/stdlib/underscore-extensions.coffee @@ -4,6 +4,7 @@ _.mixin remove: (array, element) -> index = array.indexOf(element) array.splice(index, 1) if index >= 0 + array spliceWithArray: (originalArray, start, length, insertedArray, chunkSize=100000) -> if insertedArray.length < chunkSize