forked from coronalabs/CoronaSDK-SublimeText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.py
208 lines (173 loc) · 7.06 KB
/
snippets.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#
# Sublime Text plugin to support Corona Editor
#
# Copyright (c) 2013 Corona Labs Inc. A mobile development software company. All rights reserved.
#
# MIT License - see https://raw.github.com/coronalabs/CoronaSDK-SublimeText/master/LICENSE
import sublime
import sublime_plugin
import os.path
import json
import threading
import re
import zipfile
import tempfile
from xml.dom import minidom
try:
from . import completions # P3
except:
import completions # P2
try:
from . import _corona_utils # P3
except:
import _corona_utils # P2
def XMLGetText(nodelist):
parts = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
parts.append(node.data)
return ''.join(parts)
def UnZIPToDir(zipFilePath, destDir):
zfile = zipfile.ZipFile(zipFilePath)
for name in zfile.namelist():
(dirName, fileName) = os.path.split(name)
if fileName == '':
# directory
newDir = os.path.join(destDir, dirName)
if not os.path.exists(newDir):
os.makedirs(newDir)
else:
# file
path = os.path.join(destDir, name)
# print("Unzip: " + path)
fd = open(path, 'wb')
fd.write(zfile.read(name))
fd.close()
zfile.close()
class CoronaSnippetFolderIndexer(threading.Thread):
_initialized = False
_snippets_dir = None
def __init__(self):
threading.Thread.__init__(self)
def run(self):
# Wait for _corona_utils initialization to complete
# print("Waiting for InitializedEvent...")
_corona_utils.InitializedEvent.wait()
self._snippets_dir = os.path.join(_corona_utils.PACKAGE_USER_DIR, "Snippets")
if not os.path.exists(self._snippets_dir):
os.makedirs(self._snippets_dir)
print(_corona_utils.PACKAGE_NAME + ": Extracting snippets ...")
try:
# In ST3 our ZIP file is not on the filesystem, it's in our package so we have to extract
# it and unzip from there (to work on Windows the temp file must be closed before it can
# be reopened to be unzipped)
zip_bytes = sublime.load_binary_resource(_corona_utils.ST_PACKAGE_PATH + "snippets.zip")
with tempfile.NamedTemporaryFile(suffix = ".zip", delete=False) as tempzip:
tempzip.write(zip_bytes)
tempzip.close()
UnZIPToDir(tempzip.name, self._snippets_dir)
os.remove(tempzip.name)
except:
# We're on ST2 and the ZIP file is just a file in our package directory
UnZIPToDir(os.path.join(_corona_utils.PACKAGE_DIR, "snippets.zip"), self._snippets_dir)
snippetMenuArray = []
snippetJSON = ""
if os.path.isdir(self._snippets_dir):
snippetMenuArray = self.addDirectory(self._snippets_dir)
snippetJSON = json.dumps(snippetMenuArray, indent=4, separators=(',', ': '))
if snippetJSON == "":
print(_corona_utils.PACKAGE_NAME + ": Failed to build Snippets menu")
return
# Put our menu into the Main.sublime-menu.template file
menus = ""
templatePath = _corona_utils.ST_PACKAGE_PATH + "Main.sublime-menu.template"
try:
if _corona_utils.SUBLIME_VERSION < 3000:
templatePath = os.path.join(_corona_utils.PACKAGE_DIR, "Main.sublime-menu.template")
with open(templatePath, "r") as fd:
menus = fd.read()
else:
menus = sublime.load_resource(templatePath)
except Exception as e:
menus = ""
print("snippets exception: " + str(e))
if menus == "":
print(_corona_utils.PACKAGE_NAME + ": Failed to load Snippets menu: " + templatePath)
return
menus = menus.replace("$corona_package_name", _corona_utils.PACKAGE_NAME)
menus = menus.replace("$corona_snippets", snippetJSON)
if not os.path.exists(_corona_utils.PACKAGE_DIR):
os.makedirs(_corona_utils.PACKAGE_DIR)
if _corona_utils.SUBLIME_VERSION < 3000:
with open(os.path.join(_corona_utils.PACKAGE_DIR, "Main.sublime-menu"), "w") as fd:
fd.write("// Generated file - do not edit - modify 'Main.sublime-menu.template' instead\n")
fd.write(menus)
else: # ST3/P3
with open(os.path.join(_corona_utils.PACKAGE_DIR, "Main.sublime-menu"), "w", encoding='utf-8') as fd:
fd.write("// Generated file - do not edit - modify 'Main.sublime-menu.template' instead\n")
fd.write(menus)
def addDirectory(self, path):
jsonArray = []
pathnames = os.listdir(path)
for pathname in pathnames:
if pathname.startswith(".") or pathname == "README.md":
continue
realpath = os.path.join(path, pathname)
# _corona_utils.debug("realpath: " + realpath)
if os.path.isdir(realpath):
jsonArray.append({"caption": pathname, "children": self.addDirectory(realpath)})
else:
# Parse XML snippet file to get the "Description"
if realpath.endswith(".sublime-snippet"):
# _corona_utils.debug("Parsing: " + realpath)
try:
xmldoc = minidom.parse(realpath)
except:
print(_corona_utils.PACKAGE_NAME + ": Invalid XML in " + realpath)
else:
desc = XMLGetText(xmldoc.getElementsByTagName('description')[0].childNodes)
if desc is None:
desc = path
else:
desc = pathname
jsonArray.append({"caption": desc, "command": "corona_snippet", "args": {"file": realpath}})
return jsonArray
# This is run automatically after the plugin has loaded in ST3
def plugin_loaded():
# Index the "snippets" directory in the background
indexer = CoronaSnippetFolderIndexer()
indexer.start()
# We need to run it manually in ST2
if _corona_utils.SUBLIME_VERSION < 3000:
plugin_loaded()
class CoronaSnippetCommand(sublime_plugin.TextCommand):
_comps = None
def run(self, edit, **args):
if 'name' in args:
trigger = args['name']
else:
trigger = args['file']
if self._comps is None:
completions.CoronaCompletions.initialize()
self._comps = completions.CoronaCompletions._completions
# print("CoronaSnippetCommand:")
# print(str(self._comps))
# print(str(len(self._comps['completions'])) + " completions available")
# print("trigger: " + trigger)
if trigger.endswith(".sublime-snippet"):
# The command wants names in the form: "Packages/User/Snippets/foo.sublime-snippet" so we
# need to remove everything in the path before "Packages" and convert backslashes to slashes
# (TODO: note that this wont work for a user called "Packages")
trigger = re.sub(r'.*Packages', "Packages", trigger, 1)
trigger = trigger.replace('\\', '/')
_corona_utils.debug("modified trigger: " + trigger)
self.view.run_command("insert_snippet", {"name": trigger})
else:
# Find a completion keyed by the contents of the snippet file
with open(trigger, "r") as fd:
lookup = fd.read().strip()
key = [key for key, item in enumerate(self._comps['completions']) if item['trigger'].lower().startswith(lookup.lower())]
if key is not None and len(key) != 0:
self.view.run_command("insert_snippet", {"contents": self._comps['completions'][key[0]]['contents']})
else:
self.view.run_command('insert', {'characters': lookup})