snippets icon indicating copy to clipboard operation
snippets copied to clipboard

Variables

Open Grimeh opened this issue 10 years ago • 15 comments

Feature Request: Variables, whether sourced from the editor, or other packages. One particular use-case are documentation generators; Javadoc snippets would certainly benefit from variables like user name, date, project version, etc., to give an idea.

Grimeh avatar Apr 28 '14 23:04 Grimeh

Would a PR be accepted that added these vars?

  • files
    • ${dirname} => /root/proj/lib
    • ${filename} => /root/proj/lib/main.coffee
    • ${basename} => main.coffee
    • ${extname} => .coffee
    • ${sep} => /
    • ${deliminator} => :
    • ${package} => /root/proj
    • ${dirnamerel} => lib
    • ${filenamerel} => lib/main.coffee
  • date & times (Note the colon)
    • ${:any-moment-format} current time in any format provided by the moment npm package
      • ${:MMMM Do YYYY, h:mm:ss a} => November 22nd 2014, 2:24:26 am
      • ${:dddd} => Saturday
      • ${:MMM Do YY} => Nov 22nd 14
  • positioning -- these variables always show an empty string
    • ${top} => insert entire snippet at top of file
    • ${bottom} => insert entire snippet at bottom of file
    • ${12} => insert such that top line of snippet is on line 12
    • ${cursor-3} => insert such that top line of snippet is 3 lines above cursor
  • Package.json (note the exclamation mark)
    • ${!path:in:project:json} Shows value from project.json file
      • ${!name} => moment
      • ${!version} => 2.8.4
      • ${!repository:url} => https://github.com/moment/moment.git

mark-hahn avatar Nov 22 '14 11:11 mark-hahn

See https://github.com/atom/snippets/pull/83

Edit: @Brandon-Grimshaw, you wouldn't want to help test this, would you? (grin)

mark-hahn avatar Nov 23 '14 23:11 mark-hahn

Another idea for variables I desperately need: ${clipboard}

Let's say I have a need to generate for my Rails project something as follows:

  # GET
  # /resource
  def index
  end

  # POST
  # /resource
  def create
  end

  # PATCH
  # /resource/:id
  def update
  end

  # DELETE
  # /resource/:id
  def destroy
  end

Let's say, I typed users, copied that text into clipboard, and if I had an access to the clipboard with that very text, I would get ready-to-use code blocks like follows:

  # DELETE
  # /users/:id
  def destroy
  end

and for copied teams:

  # DELETE
  # /teams/:id
  def destroy
  end

With the following snippet:

  'DELETE':
    'prefix': 'delete'
    'body': """
    # DELETE
    # /${clipboard}/:id
    def destroy
    end
    """

eleidan avatar Jun 18 '15 13:06 eleidan

Just to add another idea (I admit, one that I desperately need and did not find) would be the selected text [1]. I would love to mark something, press a hotkey and encapsulate the selected text. In Gedit it is possible with something like that: \emph{${1:$GEDIT_SELECTED_TEXT}}$0 for LaTeX.

May this be done with this plugin?

[1] https://wiki.gnome.org/Apps/Gedit/Plugins/Snippets

73 avatar Jul 08 '15 05:07 73

:+1: for the ${clipboard} variable.

dwelle avatar Dec 04 '15 20:12 dwelle

+1 for clipboard as well

some basic features that sublime has taken care of over the years but are essential to proper macro building

raptor235 avatar Dec 30 '15 03:12 raptor235

+1 clipboard xD

Chukatuk avatar Apr 27 '16 11:04 Chukatuk

Clipboard!!!

sixertoy avatar May 03 '16 20:05 sixertoy

+1 for clipboard as well

pgiani avatar Dec 15 '16 16:12 pgiani

+1 for clipboard

dublx avatar Feb 27 '17 12:02 dublx

The lack of variables like this is one of the biggest missing features that prevent me from switching from TM2 to Atom.

As a maker of snippets that use this type of variables, I would advise the addition of some transformation capabilities. For instance a snippet I use in TM2 looks like this

module Processes
  module ${TM_DIRECTORY/(?:\A|.+\/|_)([A-Za-z0-9]+)(?:\.[a-z]+)*/(?2::\u$1)/g}
    class ${TM_FILENAME/(?:\A|_)([A-Za-z0-9]+)(?:\.[a-z]+)*/(?2::\u$1)/g}
      $0
    end
  end
end

Sure, that's not pretty at all, but with a file named client_accounts/list_all.rb it would make

module Processes
  module ClientAccounts
    class ListAll

    end
  end
end    

You can't imagine the time I save with this kind of nuggets but the ability to transform the folder and filename values is key.

Cheers 😊

simonc avatar Sep 16 '17 20:09 simonc

What happened to this?

tjpeden avatar Jun 19 '18 21:06 tjpeden

My guess is that this never got prioritized because there are ways of achieving the same effect via commands.

For instance, if you're trying to replicate TextMate's $TM_SELECTED_TEXT variable, you could do something like this in your init file:

// WRAP IN HTML TAG
atom.commands.add('atom-text-editor', {
  'custom:wrap-in-html-tag': () => {
    let editor = atom.workspace.getActiveTextEditor();
    let selection = editor.getLastSelection();
    let snippet = atom.packages.activePackages.snippets.mainModule;
    snippet.insert('<${1:div}>' + selection.getText() + '</${1:div}>');
  }
});

So this feels like one of those features where anyone who knows Atom well enough to write the PR also knows alternative strategies for these use cases. But that's not an argument against the feature — just a description of how we got here.

I'd happily accept a PR. Looks like VS Code has implemented snippet variables also, so I'd be cool with a PR that added all (or a meaningful subset of) the variables on their list. Might as well standardize.

If anyone wants to take a stab at this, I can probably help them through some of the tricky parts, like changing the parser grammar.

savetheclocktower avatar Jun 19 '18 21:06 savetheclocktower

// WRAP IN HTML TAG atom.commands.add('atom-text-editor', { 'custom:wrap-in-html-tag': () => { let editor = atom.workspace.getActiveTextEditor(); let selection = editor.getLastSelection(); let snippet = atom.packages.activePackages.snippets.mainModule; snippet.insert('<${1:div}>' + selection.getText() + '</${1:div}>'); } });

I tried pasting it in init file and error appear "Reserved word let" What should be issue?

wasimxe avatar Nov 01 '18 18:11 wasimxe

I tried pasting it in init file and error appear "Reserved word let" What should be issue?

I don’t know; I pasted that code snippet into my init.js just now and it worked fine. Try the forums or the Slack channel for tech support.

savetheclocktower avatar Nov 01 '18 19:11 savetheclocktower