From 14d234d1827c3fbd01401fe0db7bb3ebbe904721 Mon Sep 17 00:00:00 2001 From: Gertjan Reynaert Date: Tue, 17 Nov 2015 16:39:21 +0100 Subject: [PATCH 1/3] Add option to register aliasses --- lib/ex.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ex.coffee b/lib/ex.coffee index 684b2f6..71fc153 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -71,6 +71,9 @@ class Ex @registerCommand: (name, func) => @singleton()[name] = func + @registerAlias: (alias, name) => + @singleton()[alias] = @singleton()[name] + quit: -> atom.workspace.getActivePane().destroyActiveItem() From 701f27130fa5fa92c5e664d6f06919dcf67ad46b Mon Sep 17 00:00:00 2001 From: jazzpi Date: Mon, 28 Dec 2015 12:45:19 +0100 Subject: [PATCH 2/3] Make Ex.registerAlias accessible from the outside --- README.md | 2 ++ lib/ex-mode.coffee | 1 + 2 files changed, 3 insertions(+) diff --git a/README.md b/README.md index abf3f64..bb7b6e0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ atom.packages.onDidActivatePackage (pack) -> if pack.name == 'ex-mode' Ex = pack.mainModule.provideEx() Ex.registerCommand 'z', -> console.log("Zzzzzz...") + # Register an alias - Now :W acts like :w + Ex.registerAlias 'W', 'w' ``` See `lib/ex.coffee` for some examples commands. Contributions are very welcome! diff --git a/lib/ex-mode.coffee b/lib/ex-mode.coffee index 1a4979d..e3a15b2 100644 --- a/lib/ex-mode.coffee +++ b/lib/ex-mode.coffee @@ -30,6 +30,7 @@ module.exports = ExMode = provideEx: -> registerCommand: Ex.registerCommand.bind(Ex) + registerAlias: Ex.registerAlias.bind(Ex) consumeVim: (vim) -> @vim = vim From 31875cff793b85c1bb63eb17d0e8d3a559f5cc0c Mon Sep 17 00:00:00 2001 From: jazzpi Date: Mon, 28 Dec 2015 13:01:56 +0100 Subject: [PATCH 3/3] Add specs for aliases --- lib/ex.coffee | 2 +- spec/ex-commands-spec.coffee | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/ex.coffee b/lib/ex.coffee index f998b2f..ca0ca0e 100644 --- a/lib/ex.coffee +++ b/lib/ex.coffee @@ -107,7 +107,7 @@ class Ex @singleton()[name] = func @registerAlias: (alias, name) => - @singleton()[alias] = @singleton()[name] + @singleton()[alias] = (args) => @singleton()[name](args) quit: -> atom.workspace.getActivePane().destroyActiveItem() diff --git a/spec/ex-commands-spec.coffee b/spec/ex-commands-spec.coffee index 89420f9..35bfd22 100644 --- a/spec/ex-commands-spec.coffee +++ b/spec/ex-commands-spec.coffee @@ -4,7 +4,8 @@ os = require 'os' uuid = require 'node-uuid' helpers = require './spec-helper' -Ex = require('../lib/ex').singleton() +ExClass = require('../lib/ex') +Ex = ExClass.singleton() describe "the commands", -> [editor, editorElement, vimState, exState, dir, dir2] = [] @@ -732,3 +733,21 @@ describe "the commands", -> atom.commands.dispatch(editorElement, 'ex-mode:open') submitNormalModeInputText(':set nonumber') expect(atom.config.get('editor.showLineNumbers')).toBe(false) + + describe "aliases", -> + it "calls the aliased function without arguments", -> + ExClass.registerAlias('W', 'w') + spyOn(Ex, 'write') + keydown(':') + submitNormalModeInputText('W') + expect(Ex.write).toHaveBeenCalled() + + it "calls the aliased function with arguments", -> + ExClass.registerAlias('W', 'write') + spyOn(Ex, 'W').andCallThrough() + spyOn(Ex, 'write') + keydown(':') + submitNormalModeInputText('W') + WArgs = Ex.W.calls[0].args[0] + writeArgs = Ex.write.calls[0].args[0] + expect(WArgs).toBe writeArgs