forked from SublimeText/PackageDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippet_dev.py
73 lines (55 loc) · 2.72 KB
/
snippet_dev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sublime
import sublime_plugin
from sublime_lib.view import has_file_ext
from sublime_lib.path import root_at_packages
from xml.etree import ElementTree as ET
import os
PLUGIN_NAME = os.getcwdu().replace(sublime.packages_path(), '')[1:]
RAW_SNIPPETS_SYNTAX = "Packages/%s/Syntax Definitions/Sublime Snippet (Raw).tmLanguage" % PLUGIN_NAME
TPL = """<snippet>
<content><![CDATA[$1]]></content>
<tabTrigger>${2:tab_trigger}</tabTrigger>
<scope>${3:source.name}</scope>
</snippet>"""
class NewRawSnippetCommand(sublime_plugin.WindowCommand):
def run(self):
v = self.window.new_file()
v.settings().set('default_dir', root_at_packages('User'))
v.settings().set('syntax', RAW_SNIPPETS_SYNTAX)
v.set_scratch(True)
class GenerateSnippetFromRawSnippetCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return self.view.match_selector(0, 'source.sublimesnippetraw')
def run(self, edit):
# XXX: sublime_lib: new whole_content(view) function?
content = self.view.substr(sublime.Region(0, self.view.size()))
self.view.replace(edit, sublime.Region(0, self.view.size()), '')
self.view.run_command('insert_snippet', {'contents': TPL})
self.view.settings().set('syntax', 'Packages/XML/XML.tmLanguage')
# Insert existing contents into CDATA section. We rely on the fact
# that Sublime will place the first selection in the first field of
# the newly inserted snippet.
self.view.insert(edit, self.view.sel()[0].begin(), content)
class NewRawSnippetFromSnippetCommand(sublime_plugin.TextCommand):
def is_enabled(self):
return has_file_ext(self.view, 'sublime-snippet')
def run(self, edit):
snippet = self.view.substr(sublime.Region(0, self.view.size()))
contents = ET.fromstring(snippet).findtext(".//content")
v = self.view.window().new_file()
v.insert(edit, 0, contents)
v.settings().set('syntax', RAW_SNIPPETS_SYNTAX)
class CopyAndInsertRawSnippetCommand(sublime_plugin.TextCommand):
"""Inserts the raw snippet contents into the first selection of
the previous view in the stack.
Allows a workflow where you're creating snippets for a .sublime-completions
file, for example, and you don't want to store them as .sublime-snippet
files.
"""
def is_enabled(self):
return self.view.match_selector(0, 'source.sublimesnippetraw')
def run(self, edit):
snip = self.view.substr(sublime.Region(0, self.view.size()))
self.view.window().run_command('close')
target = sublime.active_window().active_view()
target.replace(edit, target.sel()[0], snip)