From 790ea8e6f2fbed4a2f0fa614799fe27c6f7b4a78 Mon Sep 17 00:00:00 2001 From: George Ogata Date: Wed, 15 Jul 2015 01:29:22 -0400 Subject: [PATCH] Support descriptions for enum values in config. The enum property may now specify options as a list of {value:, description:} objects. --- spec/config-spec.coffee | 18 ++++++++++++++++++ src/config.coffee | 29 ++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 5aff0eff2..907faf447 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -1572,6 +1572,14 @@ describe "Config", -> items: type: 'string' enum: ['one', 'two', 'three'] + str_options: + type: 'string' + default: 'one' + enum: [ + value: 'one', description: 'One' + 'two', + value: 'three', description: 'Three' + ] atom.config.setSchema('foo.bar', schema) @@ -1595,3 +1603,13 @@ describe "Config", -> expect(atom.config.set('foo.bar.arr', ['two', 'three'])).toBe true expect(atom.config.get('foo.bar.arr')).toEqual ['two', 'three'] + + it 'will honor the enum when specified as an array', -> + expect(atom.config.set('foo.bar.str_options', 'one')).toBe true + expect(atom.config.get('foo.bar.str_options')).toEqual 'one' + + expect(atom.config.set('foo.bar.str_options', 'two')).toBe true + expect(atom.config.get('foo.bar.str_options')).toEqual 'two' + + expect(atom.config.set('foo.bar.str_options', 'One')).toBe false + expect(atom.config.get('foo.bar.str_options')).toEqual 'two' diff --git a/src/config.coffee b/src/config.coffee index 176390869..0eae7f335 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -235,9 +235,13 @@ ScopeDescriptor = require './scope-descriptor' # # #### enum # -# All types support an `enum` key. The enum key lets you specify all values -# that the config setting can possibly be. `enum` _must_ be an array of values -# of your specified type. Schema: +# All types support an `enum` key, which lets you specify all the values the +# setting can take. `enum` may be an array of allowed values (of the specified +# type), or an array of objects with `value` and `description` properties, where +# the `value` is an allowed value, and the `description` is a descriptive string +# used in the settings view. +# +# In this example, the setting must be one of the 4 integers: # # ```coffee # config: @@ -247,6 +251,20 @@ ScopeDescriptor = require './scope-descriptor' # enum: [2, 4, 6, 8] # ``` # +# In this example, the setting must be either 'foo' or 'bar', which are +# presented using the provided descriptions in the settings pane: +# +# ```coffee +# config: +# someSetting: +# type: 'string' +# default: 'foo' +# enum: [ +# {value: 'foo', description: 'Foo mode. You want this.'} +# {value: 'bar', description: 'Bar mode. Nobody wants that!'} +# ] +# ``` +# # Usage: # # ```coffee @@ -1132,6 +1150,11 @@ Config.addSchemaEnforcers validateEnum: (keyPath, value, schema) -> possibleValues = schema.enum + + if Array.isArray(possibleValues) + possibleValues = possibleValues.map (value) -> + if value.hasOwnProperty('value') then value.value else value + return value unless possibleValues? and Array.isArray(possibleValues) and possibleValues.length for possibleValue in possibleValues