Help with custom ex-mode command


#1

I’d like to register a new command with ex-mode, for example ‘ga’, that runs ‘git-plus:add’ on the given file. However, I don’t quite understand how to reference the ‘git-plus’ package given the example provided by ‘ex-mode’: The example is:

# in Atom's init.coffee
atom.packages.onDidActivatePackage (pack) ->
  if pack.name == 'ex-mode'
    Ex = pack.mainModule.provideEx()
    Ex.registerCommand 'z', -> console.log("Zzzzzz...")

I’ve tried:
# in Atom’s init.coffee
atom.packages.onDidActivatePackage (pack) ->
if pack.name == ‘ex-mode’
Ex = pack.mainModule.provideEx()
Ex.registerCommand ‘ga’, -> ‘git-plus:add’

With the intention that when I type the sequence ‘:ga’, it will run the add command from git-plus. What would this snippet need to look like in order for it to have the desired effect?


#2

This translates into JavaScript when Atom compiles your config files and packages like so:

Ex.registerCommand('ga', function() { 'git-plus:add' })

This is valid JavaScript, and it technically matches the specifications of the variables that ex-mode is looking for (a string and a function). This is why you don’t see an error message. However, the function you have written returns a string, whereas the function in the example returns a call to another function (which then gets run because your atom.packages.onDidActivatePackage instruction will follow your code until it runs out of things to run).

This is what you need instead:

Ex.registerCommand 'ga', -> atom.commands.dispatch(atom.workspace.getActiveTextEditor().element, 'git-plus:add')

The atom.commands API interacts with the command names used by keybindings and the Command Palette. The parentheses for dispatch() are unnecessary, but I included them to make it easier to read.


#3

Great! Thanks for the very helpful explanation. This is exactly what I needed and helped me get a better understanding of how this works.


#4

No problem. I’ve been through all of that territory in recent memory.