Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add support for seperate files

  • Loading branch information...
commit 86e608a042d1dcd8e73058344a101c5df9072d49 1 parent 8686c0c
Mark Fowler authored October 18, 2011
38  README.markdown
Source Rendered
@@ -18,7 +18,19 @@ following expansion.  In order the expansions go:
18 18
 Usage
19 19
 -----
20 20
 
21  
-To add new snippets create a `SuperSnippet.sublime-settings` file in your 
  21
+To use type the *exact* name of the snippet and hit tab.  If no match is found then Sublime Text 2 will
  22
+execute `insert_best_completion` as usual (meaning that the normal tab completion will occur.)
  23
+
  24
+Creating Snippets
  25
+-----------------
  26
+
  27
+To add new snippets you have two choices.
  28
+
  29
+** Using the Settings File **
  30
+
  31
+This methods is ideal for short snippets.
  32
+
  33
+Create a `SuperSnippet.sublime-settings` file in your 
22 34
 `Packages/User` directory.  The format
23 35
 of this file is a simple JSON object where the keys are the short text
24 36
 to expand and the values are templates for the expansion.
@@ -32,8 +44,26 @@ In other words, on Mac OS X create `~/Library/Application Support/Sublime Text 2
32 44
 }
33 45
 ```
34 46
 
35  
-To use type the *exact* name of the snippet and hit tab.  If no match is found then Sublime Text 2 will
36  
-execute `insert_best_completion` as usual (meaning that the normal tab completion will occur.)
  47
+** Using an Individual File for Snippets **
  48
+
  49
+This is more suited to longer snippets.
  50
+
  51
+Create a `whatever.sublime-supersnippet` file in your 
  52
+`Packages/User` directory for each snippet you want to expand, using
  53
+whatever snippet you want to expand instead of *whatever*. 
  54
+The file should contain the bare snippet template for expansion
  55
+
  56
+In other words, on Mac OS X create `~/Library/Application Support/Sublime Text 2/Packages/User/thanks.sublime-supersnippet` that looks something like this:
  57
+
  58
+	Dear ${0:Granny},
  59
+
  60
+	Thank you for the birthday gift.
  61
+
  62
+	All My Love
  63
+
  64
+	${! os.environ['LOGNAME'] !}
  65
+
  66
+Please note, for security and sanity that this will only work for snippets that match the regular expression `^[A-Za-z_-]$`
37 67
 
38 68
 Installation
39 69
 ------------
@@ -43,8 +73,6 @@ Assuming you've got git installed (you don't?  shame on you!)
43 73
 	cd ~/Library/Application Support/Sublime Text 2/Packages/User
44 74
 	git co git://github.com/2shortplanks/SuperSnippet.git
45 75
 
46  
-Then create the `SuperSnippet.sublime-settings` file as described above.
47  
-
48 76
 To update
49 77
 
50 78
 	cd ~/Library/Application Support/Sublime Text 2/Packages/User
25  SuperSnippetCommand.py
... ...
@@ -1,7 +1,10 @@
1  
-import sublime, sublime_plugin, os
  1
+import sublime, sublime_plugin, os, re
  2
+
  3
+a_to_z = re.compile("^[A-Za-z_-]*$")
2 4
 
3 5
 class SuperSnippetCommand(sublime_plugin.TextCommand):
4 6
 
  7
+
5 8
 	# the delimiters
6 9
 	def start_cmd(self):    return "${`"
7 10
 	def end_cmd(self):      return "`}"
@@ -52,8 +55,24 @@ def insert_super_snippet(self, edit):
52 55
 		# is this in our settings file?  This should be in your User dir
53 56
 		# and look like { "shorttext": "expanded texmplate", ... }
54 57
 		settings = sublime.load_settings("SuperSnippet.sublime-settings")
55  
-		if (not settings.has(word)): return
56  
-		template = settings.get(word)
  58
+		if (settings.has(word)):
  59
+			template = settings.get(word)
  60
+		else:
  61
+			# look for a file that is named after the snippet in the
  62
+			# User directory.  we will only do this if the snippet has an
  63
+			# ascii A-Za-z short form
  64
+			if (a_to_z.match(word)):
  65
+			    path = os.path.join(sublime.packages_path(),"User","%s.sublime-supersnippet" % word)
  66
+			    if not os.path.isfile(path):
  67
+			        return
  68
+			    try:
  69
+			    	template = open(path,"r").read()
  70
+			    except IOError as e:
  71
+			    	sublime.error_message("Problem reading file %s: %s" % (path, e))
  72
+			    	return
  73
+			else:
  74
+				return
  75
+
57 76
 
58 77
 		# remove the current word
59 78
 		self.view.erase(edit,self.view.word(self.view.sel()[0]))

0 notes on commit 86e608a

Please sign in to comment.
Something went wrong with that request. Please try again.