Customised snippets:expand


#1

Hi all.

Sorry to bother you all with a silly question.

I’d like to modify the original snippets:expand command, simply inserting the clipboard contentj ust after the expansion.

I tried this into my init.coffee file, which doesn’t work.

atom.commands.add 'atom-text-editor', 'kn:snippets:expand', ->
  return unless editor = atom.workspace.getActiveTextEditor()

  clipboardText = atom.clipboard.read()
  snippets:expand
  editor.insertText(clipboardText)

Any clue? Many thanks in advance.


#2

Atom’s commands aren’t valid CoffeeScript. snippets:expand means nothing to the editor unless you have it in the function atom.commands.dispatch editor.element, 'snippets:expand'.


#3

Thank you DamnedScholar, it works great!

:-))


#4

No problem. Keep in mind that the second argument of that method requires a DOM element (or something that resolves to a DOM element). If you want to do that trick in an Atom pane that may or may not be a TextEditor, you can pass the event into your callback function like this:

atom.commands.add 'atom-text-editor', 'kn:snippets:expand', (evt) ->
  return unless editor = atom.workspace.getActiveTextEditor()

  clipboardText = atom.clipboard.read()
  atom.commands.dispatch evt.target, 'snippets:expand'
  editor.insertText(clipboardText)

Here’s a custom command that I use that demonstrates the principle:

# Open file in system default program.
{spawn} = require 'child_process'

atom.contextMenu.add {
  ".tree-view": [{
    label: "Open file in default program"
    command: "custom:open-in-default"
  }]
}

atom.commands.add ".tree-view", "custom:open-in-default", (evt) ->
  spawn evt.target.attributes.getNamedItem("data-path").value, {shell: true}

This just sends the file that you’ve right-clicked to the system shell, which causes your OS to open it with the default program. You can open Word files or Photoshop files from Atom without thinking about it.


#5

Wow, a lot of information here!

Thank you again DamnedScholar. I’ll definitely try to understand what you wrote.

Now I’m actually facing this problem: with your help, I discovered that what I really need is not a new command kn:snippents:expand, but a modified version of snippets:expand, instead. The “editor.insertText(clipboardText)” addition should be put there.
Still struggling with this, though…