diff --git a/spec/space-pen-extensions-spec.coffee b/spec/space-pen-extensions-spec.coffee index 3102752ca..7c6192d6c 100644 --- a/spec/space-pen-extensions-spec.coffee +++ b/spec/space-pen-extensions-spec.coffee @@ -1,4 +1,4 @@ -{View, $$} = require 'atom' +{View, $, $$} = require 'atom' describe "SpacePen extensions", -> class TestView extends View @@ -51,3 +51,17 @@ describe "SpacePen extensions", -> it "subscribes to the given event emitter and unsubscribes when unsubscribe is called", -> emitter.trigger "foo" expect(eventHandler).toHaveBeenCalled() + + describe "tooltips", -> + describe "replaceModifiers", -> + replaceModifiers = $.fn.setTooltip.replaceModifiers + + it "replaces single keystroke", -> + expect(replaceModifiers('cmd-O')).toEqual '⌘⇧O' + expect(replaceModifiers('cmd-shift-up')).toEqual '⌘⇧↑' + expect(replaceModifiers('cmd-option-down')).toEqual '⌘⌥↓' + expect(replaceModifiers('cmd-option-left')).toEqual '⌘⌥←' + expect(replaceModifiers('cmd-option-right')).toEqual '⌘⌥→' + + it "replaces multiple keystroke", -> + expect(replaceModifiers('cmd-o ctrl-2')).toEqual '⌘O ⌃2' diff --git a/src/space-pen-extensions.coffee b/src/space-pen-extensions.coffee index c766c1288..6d9472568 100644 --- a/src/space-pen-extensions.coffee +++ b/src/space-pen-extensions.coffee @@ -22,9 +22,39 @@ tooltipDefaults = container: 'body' html: true +modifiers = + cmd: '⌘' + option: '⌥' + ctrl: '⌃' + shift: '⇧' + left: '←' + right: '→' + up: '↑' + down: '↓' + +replaceKey = (key) -> + if modifiers[key] + modifiers[key] + else if key.length == 1 and key == key.toUpperCase() and key.toUpperCase() != key.toLowerCase() + [modifiers.shift, key.toUpperCase()] + else if key.length == 1 + key.toUpperCase() + else + key + +replaceModifiersInSingleKeystroke = (keystroke) -> + keys = keystroke.split('-') + keys = _.flatten(replaceKey(key) for key in keys) + keys.join('') + +replaceModifiers = (keystroke) -> + keystrokes = keystroke.split(' ') + keystrokes = (replaceModifiersInSingleKeystroke(stroke) for stroke in keystrokes) + keystrokes.join(' ') + getKeystroke = (bindings) -> if bindings?.length - "#{bindings[0].keystroke}" + "#{replaceModifiers(bindings[0].keystroke)}" else '' @@ -38,4 +68,7 @@ jQuery.fn.setTooltip = (title, {command, commandElement}={}) -> this.tooltip(jQuery.extend(tooltipDefaults, {title: "#{title} #{getKeystroke(bindings)}"})) +jQuery.fn.setTooltip.getKeystroke = getKeystroke +jQuery.fn.setTooltip.replaceModifiers = replaceModifiers + module.exports = spacePen