snippets icon indicating copy to clipboard operation
snippets copied to clipboard

Ability to import multiple snippets.cson files

Open izuzak opened this issue 9 years ago • 32 comments

From @morseldesign on March 29, 2015 19:51

Feature

Ability to do an @import or require() syntax for breaking up snippets into multiple files. For example @import "/snippets/documentation-snippets.cson" or require('snippets/javascript-snippets.cson').

Why

After awhile, snippet files can become pretty overwhelming to manage. Having multiple files would not only help with organization, but make it a little easier to manage changes to certain snippets.

Example

Given a file called snippets/javascript-snippets.cson with contents like:

###
  JavaScript Snippets
###
'.source.js':
  'Console log':
    'prefix': 'log'
    'body': "console.log($1);"

We could import it lnto ~/.atom/snippets.cson like:

# Your snippets
#
# Atom snippets allow you to enter a simple prefix in the editor and hit tab to
# expand the prefix into a larger code block with templated values.
#
# You can create a new snippet in this file by typing "snip" and then hitting
# tab.
#
# An example CoffeeScript snippet to expand log to console.log:
#
# '.source.coffee':
#   'Console log':
#     'prefix': 'log'
#     'body': "console.log $1'"
#

# Syntax #1
@import "snippets/javascript-snippets.cson";

# Syntax #2
require('snippets/javascript-snippets.cson');

Copied from original issue: atom/atom#6155

izuzak avatar Mar 30 '15 15:03 izuzak

I say we cannot implement the suggestions; CSON is nothing more than JSON. Why not simply support what was previously (or still is?) documented regarding a snippets folder, e.g. ~/atom/snippets, and simply load every all CSON files in that directory? Simple and requires no unnecessary "ceremony".

Should be pretty trivial to implement as well, so might be candidate for good-first label.

thomasjo avatar Mar 30 '15 19:03 thomasjo

Can I just have multiple snippets.cson files within ~/atom/snippets as opposed to a single ~/atom/snippets.cson? If so (didn't realized that was the case), I would agree with @thomasjo!

morseldesign avatar Mar 31 '15 16:03 morseldesign

Not currently available. It was incorrectly documented that way in the flight guide. I think it's something we should implement though. On Mar 31, 2015 6:43 PM, "Morsel" [email protected] wrote:

Can I just have multiple snippets.cson files within ~/atom/snippets as opposed to a single ~/atom/snippets.cson? If so (didn't realized that was the case), I would agree with @thomasjo https://github.com/thomasjo!

— Reply to this email directly or view it on GitHub https://github.com/atom/snippets/issues/124#issuecomment-88165948.

thomasjo avatar Mar 31 '15 16:03 thomasjo

Ah. Well in that case, I do prefer your version. Managing imports would be a pain. Auto importing all files from ~/atom/snippets makes a lot more sense.

morseldesign avatar Mar 31 '15 16:03 morseldesign

+1 for auto importing but just having anything at all available to manage snippets in partials would be so much better

thibmaek avatar May 12 '15 22:05 thibmaek

+1 this is a real must as my .cson file is becoming unmanageable. A simple ./snippets folder would be an ideal solution...

castlefields avatar May 14 '15 13:05 castlefields

+1 it's getting difficult to manage all my snippets, especially for different languages.

NelsonScott avatar May 14 '15 16:05 NelsonScott

The solution, for now, I am using is my own package. Within the package there is a ./snippets folder and within this I am currently creating multiple .cson files. Not ideal as any additional snippets added while working only show after reloading Atom. Therefore I'm using the default snippet.cson while working and then, at then end of the day, pasting any new snippets into my package - ready for start up next day.

Hopefully this solution means that if a ./snippets folder is ever added, as discussed above, I can just copy my .cson files into this...

castlefields avatar May 14 '15 17:05 castlefields

@castlefields Would be great if you published that package to APM so we can use that method for now. :wink:

thibmaek avatar May 14 '15 17:05 thibmaek

@castlefields I created a similar solution to yours because I really got tired of the long snippets file. Published to apm: https://github.com/thibmaek/modular-snippets

Install with apm install modular-snippets

thibmaek avatar Jun 13 '15 10:06 thibmaek

:+1:

pmcalabrese avatar Jun 14 '15 10:06 pmcalabrese

It would be awesome to have project-specific snippet files as well. That way teams/repositories could have version-tracked snippets.cson files. I usually keep my snippets.cson files in a gist but it would be super awesome to have them versioned in my repositories with specific snippets in each. This isn't entirely in the same vein as the proposed idea above but having distributed snippet files is the general idea here. :smile:

adammagana avatar Jun 29 '15 20:06 adammagana

:+1: to project-specific snippets, as @adammagana proposed. It's particularly annoying when working across projects (sometimes across organizations) with different conventions/linting rules.

wmadden avatar Jul 12 '15 09:07 wmadden

:+1: Auto-importing would be awesome and so would project specific snippets

ghost avatar Aug 14 '15 16:08 ghost

+1

rossmcf avatar Nov 03 '15 16:11 rossmcf

+1

rpedroni avatar Dec 07 '15 21:12 rpedroni

+10 project-specific snippets, really a must have +1 multiple file snippets, would be much better to manage, but not as essential

garralab avatar Jan 28 '16 16:01 garralab

+9001 for project-specific snippets sometimes you have snippets that would only work for a given project, so it doesn't make sense to have them in memory when you are not working on that project. it would be awesome if the package would look for a snippets.[cson|json] in the root of the project and load that last.

HaykoKoryun avatar Feb 02 '16 07:02 HaykoKoryun

My modular-keymaps package does exactly this, but for keymaps. It automatically reloads them on any change too. I'd imagine the code would be very similar for snippets. I also had the idea of adding the option to synchronise your snippets/ folder as Gists. Local snippets is another interesting idea…

danielbayley avatar Jun 11 '16 16:06 danielbayley

~~I think project-specific snippets could be accomplished with a simple linking~~

$ ln -s snippets.cson $HOME/.atom/snippets/$(basename $PWD).cson

Edit: didn't take into consideration that snippets shouldn't intersect

ewnd9 avatar Jun 12 '16 07:06 ewnd9

Well it turned out the code wasn't too similar after all due to the lack of an equivalent API for snippets, but I managed to work around it and now I have a proper modular-snippets package working locally.

Project specific snippets are implemented too; they are live reloaded for each project (and exist only while that project is open), and take precedence over any global snippets in your new ATOM_HOME/snippets/ directory (~~this is relevant only in case of matching scope selectors). Maybe this should be the other way around?~~

Local project snippets are loaded from the first file found in your project root in the order: snippets.cson/.snippets.cson, a snippets: object inside package.json, or, finally a snippets: object inside project.cson (this ties in with project-manager/project-plus and another package I am working on project-config).

@thibmaek I sent you a PM on the Atom #Slack asking if you'd possibly like to remove your modular-snippets package to make way for this, unless you really don't in which case I can publish as modular-snippets-plus instead. But please do let me know, as this is now ready to go! 😃

Edit: I devised a way to add to existing scope selectors rather than override them, and even prefix each snippet name key with it's file name, so any clashes should be minimal, but in the rare case they do then the local snippet will take precedence.

danielbayley avatar Jun 16 '16 00:06 danielbayley

@danielbayley awesome!

HaykoKoryun avatar Jun 16 '16 09:06 HaykoKoryun

@danielbayley awesome indeed! Thanks!

rpedroni avatar Jun 16 '16 13:06 rpedroni

any package that we can install it to get multi snippets files loaded from '.atom/snippets' folder?

phpgit avatar Jun 23 '16 13:06 phpgit

@phpgit Yep, I'll be releasing my modular-snippets package very shortly… just ironing out a couple of last-minute bugs. I'll announce it here when it's up.

danielbayley avatar Jun 23 '16 13:06 danielbayley

Well I wanted to get this out a little sooner but life kept getting in the way 😄 Anyway, finally published here. 🎉 apm install modular-snippets

danielbayley avatar Jun 30 '16 23:06 danielbayley

modular snippets project seems to be dead

kaytrance avatar Jan 30 '17 08:01 kaytrance

@kaytrance yup, too bad though. There's an issue filed in there and a PR submitted too I think but @danielbayley seems to have left this project.

I'll see if I can whip something up.

thibmaek avatar Jan 31 '17 22:01 thibmaek

@kaytrance @thibmaek Hi, sorry no it's not dead… actually, I have a complete re-write of it running locally. It's just dependent a few other Atom packages I haven't published yet!

danielbayley avatar Jul 06 '17 13:07 danielbayley

I have error :-1: ` Installing “[email protected]” failed.Hide output…

[email protected] preinstall C:\Users\Bogdan\AppData\Local\Temp\apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi\node_modules.staging\modular-snippets-627558c1 mkdir -p "${ATOM_HOME:-$HOME/.atom}"/snippets

���⠪����᪠� �訡�� � ����� 䠩��, ����� ����� ��� ��⪥ ⮬�. �訡�� �� �६� ��ࠡ�⪨: ${ATOM_HOME:-$HOME/.atom}/snippets. npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Bogdan\AppData\Local\Temp\apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi\package.json' npm WARN apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi No description npm WARN apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi No repository field. npm WARN apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi No README data npm WARN apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi No license field. npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\Users\Bogdan\AppData\Local\atom\app-1.22.1\resources\app\apm\bin\node.exe" "C:\Users\Bogdan\AppData\Local\atom\app-1.22.1\resources\app\apm\node_modules\npm\bin\npm-cli.js" "--globalconfig" "C:\Users\Bogdan\.atom\.apm\.apmrc" "--userconfig" "C:\Users\Bogdan\.atom\.apmrc" "install" "C:\Users\Bogdan\AppData\Local\Temp\d-1171029-3708-pod1ee.qdbcfvfgvi\package.tgz" "--runtime=electron" "--target=1.6.15" "--arch=x64" "--global-style" "--msvs_version=2015" npm ERR! node v6.9.5 npm ERR! npm v3.10.10 npm ERR! code ELIFECYCLE

npm ERR! [email protected] preinstall: 'mkdir -p "${ATOM_HOME:-$HOME/.atom}"/snippets' npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] preinstall script 'mkdir -p "${ATOM_HOME:-$HOME/.atom}"/snippets'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the modular-snippets package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! mkdir -p "${ATOM_HOME:-$HOME/.atom}"/snippets npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs modular-snippets npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls modular-snippets npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request: npm ERR! C:\Users\Bogdan\AppData\Local\Temp\apm-install-dir-1171029-3708-129hiuv.3hl9za1yvi\npm-debug.log`

Bogdan808 avatar Nov 29 '17 10:11 Bogdan808