snippets
snippets copied to clipboard
Variables
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.
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
- ${:any-moment-format} current time in any format provided by the
- 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
- ${!path:in:project:json} Shows value from project.json file
See https://github.com/atom/snippets/pull/83
Edit: @Brandon-Grimshaw, you wouldn't want to help test this, would you? (grin)
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
"""
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
:+1: for the ${clipboard}
variable.
+1 for clipboard as well
some basic features that sublime has taken care of over the years but are essential to proper macro building
+1 clipboard xD
Clipboard!!!
+1 for clipboard as well
+1 for clipboard
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 😊
What happened to this?
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.
// 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?
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.