diff --git a/spec/app/config-spec.coffee b/spec/app/config-spec.coffee index 7bfe22e2f..f09fa913b 100644 --- a/spec/app/config-spec.coffee +++ b/spec/app/config-spec.coffee @@ -17,6 +17,17 @@ describe "Config", -> expect(config.save).toHaveBeenCalled() expect(observeHandler).toHaveBeenCalledWith 42 + describe ".getPositiveInt(keyPath, defaultValue)", -> + it "returns the proper current or default value", -> + config.set('editor.preferredLineLength', 0) + expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80 + config.set('editor.preferredLineLength', -1234) + expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80 + config.set('editor.preferredLineLength', 'abcd') + expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80 + config.set('editor.preferredLineLength', null) + expect(config.getPositiveInt('editor.preferredLineLength', 80)).toBe 80 + describe ".save()", -> beforeEach -> spyOn(fsUtils, 'write') diff --git a/src/app/config.coffee b/src/app/config.coffee index 067f3d0cb..8209307cf 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -100,6 +100,24 @@ class Config _.valueForKeyPath(@settings, keyPath) ? _.valueForKeyPath(@defaultSettings, keyPath) + # Public: Retrieves the setting for the given key as an integer number. + # + # keyPath - The {String} name of the key to retrieve + # Returns the value from Atom's default settings, the user's configuration file, + # or `NaN` if the key doesn't exist in either. + getInt: (keyPath, defaultValueWhenFalsy) -> + parseInt(@get(keyPath)) + + # Public: Retrieves the setting for the given key as a positive integer number. + # + # keyPath - The {String} name of the key to retrieve + # defaultValue - The integer {Number} to fall back to if the value isn't + # positive + # Returns the value from Atom's default settings, the user's configuration file, + # or `defaultValue` if the key value isn't greater than zero. + getPositiveInt: (keyPath, defaultValue) -> + Math.max(@getInt(keyPath), 0) or defaultValue + # Public: Sets the value for a configuration setting. # # This value is stored in Atom's internal configuration file. diff --git a/src/packages/autoflow/autoflow.coffee b/src/packages/autoflow/autoflow.coffee index 058b9c3b5..83bf56d02 100644 --- a/src/packages/autoflow/autoflow.coffee +++ b/src/packages/autoflow/autoflow.coffee @@ -8,7 +8,7 @@ module.exports = editor.getBuffer().change(range, @reflow(editor.getTextInRange(range))) reflow: (text) -> - wrapColumn = config.get('editor.preferredLineLength') ? 80 + wrapColumn = config.getPositiveInt('editor.preferredLineLength', 80) lines = [] currentLine = [] diff --git a/src/packages/wrap-guide/lib/wrap-guide-view.coffee b/src/packages/wrap-guide/lib/wrap-guide-view.coffee index dcce9c44e..dbfbc6333 100644 --- a/src/packages/wrap-guide/lib/wrap-guide-view.coffee +++ b/src/packages/wrap-guide/lib/wrap-guide-view.coffee @@ -19,7 +19,7 @@ class WrapGuideView extends View @subscribe $(window), 'resize', => @updateGuide() getDefaultColumn: -> - config.get('editor.preferredLineLength') ? 80 + config.getPositiveInt('editor.preferredLineLength', 80) getGuideColumn: (path) -> customColumns = config.get('wrapGuide.columns')