]>
Commit | Line | Data |
---|---|---|
24c7594d BB |
1 | {CompositeDisposable} = require 'atom' |
2 | ||
3 | module.exports = class Base | |
4 | ||
5 | constructor: (config) -> | |
6 | @disposables = new CompositeDisposable | |
7 | ||
8 | @curPairs = [] | |
9 | @registerPairs config.pairs | |
10 | ||
11 | registerPairs: (pairs) -> | |
12 | pairs = (x for x in pairs when x.length > 0 and x.length %2 == 0) | |
13 | ||
14 | for pair in pairs | |
15 | if pair not in @curPairs | |
16 | @registerPair pair | |
17 | @curPairs.push(pair) | |
18 | ||
19 | registerPair: (pair) -> | |
20 | [left, right] = @splitPair(pair) | |
21 | ||
22 | if left != right | |
23 | @createPairBindings left, "#{left} ", " #{right}" | |
24 | @createPairBindings right, left, right | |
25 | ||
26 | createPairBindings: (key, left, right) -> | |
27 | name = "vim-surround:#{@getName key}" | |
28 | ||
29 | # First, we add a command to the system to actually perform the surround. | |
30 | # We attach the disposable to our object's list. | |
31 | @disposables.add atom.commands.add @context, name, @getRunner left, right | |
32 | ||
33 | # Next, we build up keybindings for our command. First, we build a | |
34 | # space-delineated version of our key that's passed in. This breaks up | |
35 | # double keys like `{%` into the seperate keystroke form: `{ %` | |
36 | keys = "" | |
37 | for i in [0..key.length-1] | |
38 | if i == 0 | |
39 | keys = key[i] | |
40 | else | |
41 | keys = "#{keys} #{key[i]}" | |
42 | ||
43 | # Making a one-command keymap data structure here. Basically: | |
44 | # "atom-text-editor.vim-mode.visual-mode": | |
45 | # "s (": | |
46 | # "vim-surround:surround-(" | |
47 | ||
48 | keymapArg = {} | |
49 | fullCommand = "#{@command} #{keys}" | |
50 | keymapArg[fullCommand] = name | |
51 | ||
52 | contextArg = {} | |
53 | contextArg[@context] = keymapArg | |
54 | ||
55 | # Capture the disposable heretom test! | |
56 | @disposables.add atom.keymaps.add name, contextArg | |
57 | ||
58 | splitPair: (pair) -> | |
59 | return [pair[..(pair.length/2)-1], pair[pair.length/2..]] |