Merge pull request #7916 from oggy/descriptive-enums

Support descriptions for enum values in config.
This commit is contained in:
Ben Ogle 2016-01-26 14:45:00 -08:00
commit 8acb742c30
2 changed files with 44 additions and 3 deletions

View File

@ -1689,6 +1689,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)
@ -1712,3 +1720,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'

View File

@ -238,9 +238,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:
@ -250,6 +254,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
@ -1185,6 +1203,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