2013-02-16 01:13:47 +04:00
|
|
|
## Configuration API
|
2012-12-17 06:48:21 +04:00
|
|
|
|
2013-02-16 01:13:47 +04:00
|
|
|
### Reading Config Settings
|
2012-12-17 06:48:21 +04:00
|
|
|
|
2013-02-13 08:51:09 +04:00
|
|
|
If you are writing a package that you want to make configurable, you'll need to
|
2014-01-22 07:56:30 +04:00
|
|
|
read config settings via the `atom.config` global. You can read the current
|
|
|
|
value of a namespaced config key with `atom.config.get`:
|
2012-12-17 06:48:21 +04:00
|
|
|
|
|
|
|
```coffeescript
|
2012-12-18 00:10:18 +04:00
|
|
|
# read a value with `config.get`
|
2014-01-22 08:00:54 +04:00
|
|
|
@showInvisibles() if atom.config.get "editor.showInvisibles"
|
2012-12-18 00:10:18 +04:00
|
|
|
```
|
2012-12-17 06:48:21 +04:00
|
|
|
|
2014-06-25 19:09:52 +04:00
|
|
|
Or you can use the `::subscribe` with `atom.config.observe` to track changes
|
|
|
|
from any view object.
|
2012-12-17 06:48:21 +04:00
|
|
|
|
|
|
|
```coffeescript
|
|
|
|
class MyView extends View
|
|
|
|
initialize: ->
|
2014-06-25 19:09:52 +04:00
|
|
|
@subscribe atom.config.observe 'editor.fontSize', (newValue, {previous}) =>
|
2013-01-24 23:36:25 +04:00
|
|
|
@adjustFontSize()
|
2012-12-17 06:48:21 +04:00
|
|
|
```
|
|
|
|
|
2014-06-25 19:09:52 +04:00
|
|
|
The `atom.config.observe` method will call the given callback immediately with
|
|
|
|
the current value for the specified key path, and it will also call it in the
|
|
|
|
future whenever the value of that key path changes.
|
2012-12-18 00:10:18 +04:00
|
|
|
|
2014-06-25 19:09:52 +04:00
|
|
|
Subscriptions made with `::subscribe` are automatically canceled when the
|
2012-12-18 00:10:18 +04:00
|
|
|
view is removed. You can cancel config subscriptions manually via the
|
2014-06-25 19:09:52 +04:00
|
|
|
`off` method on the subscription object that `atom.config.observe` returns.
|
2012-12-17 06:48:21 +04:00
|
|
|
|
|
|
|
```coffeescript
|
2014-06-25 19:09:52 +04:00
|
|
|
fontSizeSubscription = atom.config.observe 'editor.fontSize', (newValue, {previous}) =>
|
|
|
|
@adjustFontSize()
|
2014-01-22 07:56:30 +04:00
|
|
|
|
2014-06-25 19:09:52 +04:00
|
|
|
# ... later on
|
2014-01-22 07:56:30 +04:00
|
|
|
|
2014-06-25 19:09:52 +04:00
|
|
|
fontSizeSubscription.off() # Stop observing
|
2012-12-17 06:48:21 +04:00
|
|
|
```
|
|
|
|
|
2013-02-16 01:13:47 +04:00
|
|
|
### Writing Config Settings
|
2013-02-13 08:51:09 +04:00
|
|
|
|
2014-01-22 07:56:30 +04:00
|
|
|
The `atom.config` database is populated on startup from `~/.atom/config.cson`,
|
|
|
|
but you can programmatically write to it with `atom.config.set`:
|
2013-02-13 08:51:09 +04:00
|
|
|
|
|
|
|
```coffeescript
|
|
|
|
# basic key update
|
2014-01-22 07:56:30 +04:00
|
|
|
atom.config.set("core.showInvisibles", true)
|
|
|
|
```
|
2013-05-16 21:51:23 +04:00
|
|
|
|
2014-01-22 07:56:30 +04:00
|
|
|
You should never mutate the value of a config key, because that would circumvent
|
|
|
|
the notification of observers. You can however use methods like `pushAtKeyPath`,
|
|
|
|
`unshiftAtKeyPath`, and `removeAtKeyPath` to manipulate mutable config values.
|
|
|
|
|
|
|
|
```coffeescript
|
|
|
|
atom.config.pushAtKeyPath("core.disabledPackages", "wrap-guide")
|
|
|
|
atom.config.removeAtKeyPath("core.disabledPackages", "terminal")
|
2013-02-13 08:51:09 +04:00
|
|
|
```
|
|
|
|
|
|
|
|
You can also use `setDefaults`, which will assign default values for keys that
|
2014-01-22 07:56:30 +04:00
|
|
|
are always overridden by values assigned with `set`. Defaults are not written
|
|
|
|
out to the the `config.json` file to prevent it from becoming cluttered.
|
2013-02-13 08:51:09 +04:00
|
|
|
|
|
|
|
```coffeescript
|
2014-01-22 08:00:54 +04:00
|
|
|
atom.config.setDefaults("editor", fontSize: 18, showInvisibles: true)
|
2013-02-13 08:51:09 +04:00
|
|
|
```
|