snippets icon indicating copy to clipboard operation
snippets copied to clipboard

Dynamic Snippets

Open rgardner opened this issue 11 years ago • 20 comments

I'd like to dynamically include the current date/time in a snippet for markdown. The snippet would look something like this.

'.source.gfm':
  'Header':
    'prefix': 'header'
    'body': 'My name - `Date.today.strftime('%D')`'

which would print:

My name - 4/2/14 Date.today.strftime('%D') is Ruby, the equivalent js a little longer (unless there's an easier way):

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month++;
var curr_year = d.getFullYear();
console.log(curr_month + "/" + curr_date + "/" + curr_year);

Thoughts? I originally posted this on the atom forum

rgardner avatar Apr 06 '14 15:04 rgardner

Related: atom/snippets#83

pathawks avatar Jan 13 '15 01:01 pathawks

#106 + an autocomplete-plus provider could address this, once I am done with #106 and https://github.com/atom-community/autocomplete-plus/pull/245.

joefitzgerald avatar Feb 05 '15 19:02 joefitzgerald

So this still hasn't been implemented? What needs doing? Someone needs to fix pull request #83?


@rgardner: I would use Moment.js:

require moment
moment().format("YYYY-MM-DD")

Please don't format dates jumbled up. You wouldn't write 50:43:08, so please don't write 05-21-98. It's annoying to anyone who either a) wants to sort the dates programmatically or b) is not American. I.e: most of the world.

zaz avatar May 21 '15 19:05 zaz

This is done and is not dependent on #83. It was implemented in https://github.com/atom/snippets/pull/110. autocomplete-plus uses this API to allow autocomplete providers to emit dynamic snippets for their suggestions. You can do so also, outside the context of an autocomplete-plus provider.

joefitzgerald avatar May 21 '15 19:05 joefitzgerald

I suppose me stating this is done isn't fully accurate. The ability for packages to integrate with snippets to add dynamic snippets exists. Autocomplete-plus allows you to do so based on the scopechain underneath the current cursor. In either case, it requires an additional package to drive the requirements you're describing above, but the path is paved for that to happen.

#83 would allow people to add their own snippets and drive dynamic behavior without any other packages. You could also look to add this to autocomplete-snippets.

joefitzgerald avatar May 21 '15 19:05 joefitzgerald

+1 Really wish to see this feature coming out as it is a bit annoying to add a timestamp to a file by hand

imcoddy avatar Nov 10 '15 05:11 imcoddy

+1 really wish to have this function to add a timestamp too

twz915 avatar Nov 23 '15 07:11 twz915

+1

Strae avatar May 12 '16 14:05 Strae

@Strae Rather than just adding a +1 style comment, Github now allows you to add an emoji reaction to an issue. Giving an issue a :+1: is more appropriate than leaving the equivalent comment.

pathawks avatar May 12 '16 14:05 pathawks

Alternative way to accomplish this that's mildly more flexible:

'.source.gfm':
  'Header':
    'prefix': 'header'
    'body': 'My name - $date'
    'variables':
      'date': '''
        require moment
        moment().format("YYYY-MM-DD")
      '''

If $date seems weird then we could put date in backticks instead

I say this way is more flexible because it allows you to reuse the variables more than once.

calebmeyer avatar May 12 '16 14:05 calebmeyer

@calebmeyer this is great, requiring modules also let us keep the snippet file clean

Im wondering if is possible declaring all the variables in the begin of the snippets file, then use them when needed inside elements

Strae avatar May 12 '16 15:05 Strae

@Strae yeah, that would be even better than what I had there, although we'd have to be clear about when things were evaluated. Ideally every variable is evaluated lazily when it's needed; that way you don't end up with yesterday's date if you leave atom open overnight.

Scoping rules would be pretty easy, too. Just search up the tree until you find a variables block that contains the variable you're looking for.

calebmeyer avatar May 12 '16 15:05 calebmeyer

You're right. In my case for example, I just want to add a random string to a console.log() invoke, so I can easily search the code for that string when I forget to remove the quick'n'dirty logs.. this implies that the code that generate the string must be evalutated everytime the snippet is used.

Strae avatar May 12 '16 15:05 Strae

I'm sorry, is there any workaround to insert current date and time as a part of snippet? Using Service API?

Vanav avatar May 16 '16 20:05 Vanav

@Vanav I've used the date package to insert the current date/time. But not as part of a larger snippet. HTH

chrishiestand avatar May 18 '16 08:05 chrishiestand

I'd love to see something like this again. The settings used to be eval()'ed, making this possible, but it slowed down starting Atom.

So it was removed in 2014: https://github.com/atom/atom/pull/4971

Redsandro avatar Jun 28 '16 11:06 Redsandro

+1

rhoglund avatar Feb 07 '17 11:02 rhoglund

Sorry it this has already been answered anywhere else, but I still haven't found it. Right now is there any way to insert current date / time as part of an atom snippet? If so, could anybody post an example snippet? Thanks in advance!

metalelf0 avatar Feb 21 '17 09:02 metalelf0

Yes. You can do this in an autocomplete-plus provider (see generateSnippet in https://github.com/joefitzgerald/go-plus/blob/master/lib/autocomplete/gocodeprovider.js) or directly by copying the logic from replaceTextWithMatch in https://github.com/atom/autocomplete-plus/blob/master/lib/autocomplete-manager.js.

joefitzgerald avatar Feb 21 '17 13:02 joefitzgerald

:+1:

akshitarora avatar Feb 24 '17 12:02 akshitarora