atom / snippets

Atom snippets package
MIT License
205 stars 101 forks source link

Wrap selection #317

Open Skydancer9 opened 2 years ago

Skydancer9 commented 2 years ago

Hi, Sorry for my beginner's question. Is it possible to wrap selected text in snippet code? Example: Select some text, for example number 10 below: <p>Some text<sup>10</sup></p>

AFTER (inserts a snippet and adds the selected number 10 in three places): <p>Some text<sup><a epub:type="noteref" role="doc-noteref" href="#fn10" id="ref10">10</a></sup></p>

savetheclocktower commented 2 years ago

No, but you can define a custom command in your init.js to do this.

Haven't tested this, but here's how your example could work:

atom.commands.add('atom-text-editor', {
  'custom:some-command': () => {
    let editor = atom.workspace.getActiveTextEditor();
    let selection = editor.getLastSelection();

    let output = `<a epub:type="noteref" role="doc-noteref" href="#fn${selection} id="ref${selection}">${selection}</a>`;

    selection.insertText(output);
  }
});

Feel free to name it whatever you want, of course. Then you could map it to a keystroke in your keymap.cson:

'atom-text-editor':
  'ctrl-shift-0': 'custom:some-command'

There's a similar programmatic interface to the snippets module, so you could define a snippet on-the-fly that incorporates the selection. But in your example, there's no reason to do so; it's simpler to do it with Atom's built-in APIs.

Skydancer9 commented 2 years ago

@savetheclocktower Thank you!

pbsds commented 2 years ago

Could it be possible to add support for the $TM_SELECTED_TEXT variable, or $VISUAL as in vim?