Support descriptions for enum values in config.

The enum property may now specify options as a list of {value:,
description:} objects.
This commit is contained in:
George Ogata
2015-07-15 01:29:22 -04:00
parent 10b8de6fc4
commit 790ea8e6f2
2 changed files with 44 additions and 3 deletions

View File

@@ -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'

View File

@@ -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