Skip to content

Commit

Permalink
Use strings instead of functions in the modes object
Browse files Browse the repository at this point in the history
Since we run things properly in the `startup()` function of bootstrap.js since
last commit, the `chrome://` URI to the string bundles is now available when the
`modes` object is constructed. This means that there's no need anymore to let
mode names, category names and command descriptions be functions. Instead they
can be the sane thing: Simply strings.

Note that this is a backwards incompatible change. Luckily it does not affect
the `.addCommand()` API method, so propably won't impact anyone.
  • Loading branch information
lydell committed Jan 26, 2016
1 parent bd00a85 commit 0ca8076
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 28 deletions.
24 changes: 14 additions & 10 deletions documentation/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ listeners is the current [vim object].

```js
vimfx.on('modeChange', vim => {
let mode = vimfx.modes[vim.mode].name()
let mode = vimfx.modes[vim.mode].name
vim.notify(`Entering mode: ${mode}`)
})
```
Expand Down Expand Up @@ -316,7 +316,7 @@ This is a very low-level part of the API. It allows to:
pref: 'extensions.my_extension.mode.normal.new_command',
category: 'misc',
order: 10000,
description: () => translate('mode.normal.new_command'),
description: translate('mode.normal.new_command'),
run: args => console.log('New command! args:', args)
}
```
Expand Down Expand Up @@ -364,8 +364,10 @@ categories.custom = {

A mode is an object with the following properties:

- name(): `Function`. Returns a human readable name of the mode used in the help
dialog and VimFx’s settings page in the Add-ons Manager.
- name: `String`. A human readable name of the mode used in the help dialog and
VimFx’s settings page in the Add-ons Manager. Config file users adding custom
modes could simply use a hard-coded string; extension authors are encouraged
to look up the name from a locale file.
- order: `Number`. The first of the default modes has the order `100` and then
they increase by `100` per mode. This allows to put new modes between two
already existing ones.
Expand Down Expand Up @@ -416,10 +418,10 @@ to the browser and web pages, and `false` otherwise.

A category is an object with the following properties:

- name(): `Function`. Returns a human readable name of the category used in the
help dialog and VimFx’s settings page in the Add-ons Manager. Config file
users adding custom categories could simply return a string; extension authors
are encouraged to look up the name from a locale file.
- name: `String`. A human readable name of the category used in the help dialog
and VimFx’s settings page in the Add-ons Manager. Config file users adding
custom categories could simply a use hard-coded string; extension authors are
encouraged to look up the name from a locale file.
- order: `Number`. The first of the default categories is the “uncategorized”
category. It has the order `100` and then they increase by `100` per category.
This allows to put new categories between two already existing ones.
Expand All @@ -430,8 +432,10 @@ A command is an object with the following properties:

- pref: `String`. The pref used to store the shortcuts for the command.
- run(args): `Function`. Called when the command is activated.
- description(): `Function`. Returns a description of the command (as a string),
shown in the help dialog and VimFx’s settings page in the Add-ons Manager.
- description: `String`. A description of the command, shown in the help dialog
and VimFx’s settings page in the Add-ons Manager. Config file users adding
custom commands could simply use a hard-coded string; extension authors are
encouraged to look up the name from a locale file.
- category: `String`. The category to add the command to. The value has to be
one of the keys of [`vimfx.get('categories')`][categories].
- order: `Number`. The first of the default commands has the order `100` and
Expand Down
2 changes: 1 addition & 1 deletion extension/lib/api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ createAPI = (vimfx) ->
pref = "#{defaults.BRANCH}custom.mode.#{mode}.#{name}"
prefs.root.default.set(pref, '')
vimfx.modes[mode].commands[name] = {
pref, category, order, run: fn, description: -> description
pref, category, order, run: fn, description
}

addOptionOverrides: (rules...) ->
Expand Down
6 changes: 1 addition & 5 deletions extension/lib/defaults.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ utils = require('./utils')

addCategory = (category, order) ->
uncategorized = (category == '')
categoryName =
if uncategorized
-> ''
else
translate.bind(null, "category.#{category}")
categoryName = if uncategorized then '' else translate("category.#{category}")
parsed_options.categories[category] = {
name: categoryName
order: if uncategorized then 0 else order
Expand Down
2 changes: 1 addition & 1 deletion extension/lib/help.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ createContent = (window, vimfx) ->
[specialKeys, rest] = splitSequence(sequence, vimfx.SPECIAL_KEYS)
$('key-sequence-special-keys', keySequence, specialKeys)
$('key-sequence-rest search-text', keySequence, rest)
$('description search-text', commandContainer, command.description())
$('description search-text', commandContainer, command.description)

return content

Expand Down
4 changes: 2 additions & 2 deletions extension/lib/modes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ utils = require('./utils')

# Helper to create modes in a DRY way.
mode = (modeName, obj, commands = null) ->
obj.name = translate.bind(null, "mode.#{modeName}")
obj.name = translate("mode.#{modeName}")
obj.order = defaults.mode_order[modeName]
obj.commands = {}
for commandName, fn of commands
Expand All @@ -41,7 +41,7 @@ mode = (modeName, obj, commands = null) ->
pref: defaults.BRANCH + pref
run: fn
category: defaults.categoryMap[pref]
description: translate.bind(null, pref)
description: translate(pref)
order: defaults.command_order[pref]
exports[modeName] = obj

Expand Down
2 changes: 1 addition & 1 deletion extension/lib/options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Observer extends BaseObserver
@appendSetting({
pref: command.pref
type: 'string'
title: command.description()
title: command.description
desc: @generateErrorMessage(command.pref)
class: 'is-shortcut'
})
Expand Down
6 changes: 3 additions & 3 deletions extension/lib/vimfx.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,14 @@ class VimFx extends utils.EventEmitter
for categoryName, commands of categories
category = @options.categories[categoryName]
categoriesSorted.push({
name: category.name()
name: category.name
_name: categoryName
order: category.order
commands: commands.sort(byOrder)
})
mode = @modes[modeName]
modesSorted.push({
name: mode.name()
name: mode.name
_name: modeName
order: mode.order
categories: categoriesSorted.sort(byOrder)
Expand All @@ -186,7 +186,7 @@ createKeyTrees = (groupedCommands, specialKeyStrings) ->
{command: overridingCommand} = getFirstLeaf(tree)
error =
id: 'overridden_by'
subject: overridingCommand.description()
subject: overridingCommand.description
context: originalSequence
pushError(error, command)

Expand Down
10 changes: 5 additions & 5 deletions extension/test/test-api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ exports['test customization'] = (assert, $vimfx, teardown) -> getAPI((vimfx) ->

# Add a slightly more complex command.
vimfx.get('categories')['new_category'] = {
name: -> 'New category'
name: 'New category'
order: -100
}
vimfx.addCommand({
Expand Down Expand Up @@ -125,14 +125,14 @@ exports['test customization'] = (assert, $vimfx, teardown) -> getAPI((vimfx) ->
(category) -> category._name == 'misc'
)
[..., {command: test_command}] = category_misc.commands
assert.equal(test_command.description(), 'Test command')
assert.equal(test_command.description, 'Test command')

# Test that the new complex command can show up in the help dialog.
mode_ignore = modes.find((mode) -> mode._name == 'ignore')
[category_new] = mode_ignore.categories
assert.equal(category_new.name, 'New category')
[test_command] = category_new.commands
assert.equal(test_command.command.description(), 'Test ignore mode command')
assert.equal(test_command.command.description, 'Test ignore mode command')
assert.deepEqual(test_command.enabledSequences, ['ö'])

# Remove the added commands.
Expand Down Expand Up @@ -162,7 +162,7 @@ exports['test customization'] = (assert, $vimfx, teardown) -> getAPI((vimfx) ->
(category) -> category._name == 'misc'
)
[..., {command: last_command}] = category_misc.commands
assert.notEqual(last_command.description(), 'Test command')
assert.notEqual(last_command.description, 'Test command')

# Test that the new complex command cannot show up in the help dialog.
mode_ignore = modes.find((mode) -> mode._name == 'ignore')
Expand Down Expand Up @@ -190,7 +190,7 @@ exports['test addCommand order'] = \
(category) -> category._name == 'misc'
)
[{command: first_command}] = category_misc.commands
assert.equal(first_command.description(), 'Test command')
assert.equal(first_command.description, 'Test command')
)

exports['test addOptionOverrides'] = \
Expand Down

0 comments on commit 0ca8076

Please sign in to comment.