-
Notifications
You must be signed in to change notification settings - Fork 8
/
makerAutoComplete.py
executable file
·342 lines (287 loc) · 10.3 KB
/
makerAutoComplete.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os
import makerCSSTools
import makerController
class AutoComplete(makerController.SuperController):
def __init__(self, model, view, editor):
self.editor = editor
self.view = view
self.model = model
self.createAbstractNameForViewObjects()
self.bindActions()
def bindActions(self):
self.editor.Bind(self.view.wx.stc.EVT_STC_CHARADDED, self.autoComplete)
def createAbstractNameForViewObjects(self):
pass
def autoComplete(self, event):
# support functions
def showAutoCompList(function):
self.editor.SetAnchor(self.editor.GetCurrentPos())
items = function()
if items == []:
return
items.sort()
self.editor.AutoCompSetSeparator(124) # 124 == |
self.editor.AutoCompShow(0, "|".join(items))
def showAutoCompListForTags(function):
items = function()
items.sort()
self.editor.AutoCompSetSeparator(124) # 124 == |
self.editor.AutoCompShow(0, "|".join(items))
def makeListOfPossibleLinks():
listOfLinks = [u"http://www.", u"mailto:", u"callto:"]
for item in self.model.core.getLocalFilesFromDistTable():
listOfLinks.append(item + self.quotes + " ")
# can either be ' or " and a space is added
# so that after inserting the user can continue to
# type in the proper position
return listOfLinks
def getArgument(leftOfArg, rightOfArg, searchRange):
"""For completing quotes we find out what the argument is
e.g. arg="
"""
startingPos = self.editor.GetCurrentPos()
self.editor.SearchAnchor()
result = self.editor.FindText(
startingPos - searchRange,
startingPos,
leftOfArg + ".*" + rightOfArg,
self.view.wx.stc.STC_FIND_REGEXP,
)
if result == -1:
self.editor.SetCurrentPos(startingPos)
self.editor.SetSelection(startingPos, startingPos)
return False
self.editor.SetSelection(result, startingPos)
argument = self.editor.GetSelectedText()[1:-1] # trim
self.editor.SetCurrentPos(startingPos)
return argument
def getCSSIds():
name = os.path.join(
self.model.core.getPathParts(), self.model.core.getCurrentFileName()
)
css = cssTool.listUsedStyleSheetsForFilename(name)
if not css:
return None
listOfIds = []
for sheet in css:
for id in cssTool.getIDsFromStyleSheet(
os.path.join(self.model.core.getPathParts(), sheet)
):
listOfIds.append(id)
return listOfIds
def getCSSClasses():
name = os.path.join(
self.model.core.getPathParts(), self.model.core.getCurrentFileName()
)
css = cssTool.listUsedStyleSheetsForFilename(name)
if not css:
return None
listOfClasses = []
for sheet in css:
for id in cssTool.getClassesFromStyleSheet(
os.path.join(self.model.core.getPathParts(), sheet)
):
listOfClasses.append(id)
return listOfClasses
def getListOfDynamics():
list = self.model.core.getFilesByExtension(".dynamic")
finalList = []
for item in list:
finalList.append(item + " />")
return finalList
def getImageList():
final = []
q = self.quotes
for image in self.model.core.getImageFiles():
final.append(image + self.quotes)
return final
def autoCompleteHTML():
"""
is autocompleting XHTML tags
<tag> becomes </tag>
with the cursor in between the tags like this
<tag>|</tag>
"""
currentPosition = self.editor.GetCurrentPos()
if (
self.editor.GetTextRange(currentPosition - 2, currentPosition - 1)
== "/"
):
# slash found, this is a complete tag
# eg. <br />
return
elif (
self.editor.GetTextRange(currentPosition - 2, currentPosition - 1)
== "-"
):
# dash found, this is a comment
# eg. <!-- -->
return
self.editor.SearchAnchor()
openB = self.editor.SearchPrev(self.view.wx.stc.STC_FIND_REGEXP, "<")
if openB == -1:
# there is no matching open brace
# so > is a bigger than sign
self.editor.GotoPos(currentPosition)
return
else:
# openB is our matching < brace
self.editor.GotoPos(openB)
self.editor.SearchAnchor()
space = self.editor.SearchNext(self.view.wx.stc.STC_FIND_REGEXP, " ")
if space == -1 or space > currentPosition:
# there is no space or:
# the space is from somewhere else in the text
complete = self.editor.GetTextRange(openB + 1, currentPosition - 1)
else:
if space < currentPosition:
complete = self.editor.GetTextRange(openB + 1, space)
else:
# for all other cases
complete = ""
# # < a href="#"> fix for leading spaces
# ^- trouble
if len(complete) == 0:
self.editor.GotoPos(currentPosition)
# check for slashes
elif "/" in complete:
self.editor.GotoPos(currentPosition)
else:
self.editor.GotoPos(currentPosition)
self.editor.AddText("</" + complete + ">")
# set the cursor in between the tags
self.editor.GotoPos(currentPosition)
def autoCompleteHTMLArgument():
currentPosition = self.editor.GetCurrentPos()
argument = getArgument(" ", self.quotes, 8)
if argument == "src=":
showAutoCompList(getImageList)
elif argument == "href=":
showAutoCompList(makeListOfPossibleLinks)
elif argument == "class=":
if getCSSClasses():
showAutoCompList(getCSSClasses)
elif argument == "id=":
if getCSSIds():
showAutoCompList(getCSSIds)
cssArgument = getArgument(":", "(", 10)
if cssArgument != False and cssArgument.count("url") != 0:
showAutoCompList(getImageList)
def autoCompleteDynamic():
currentPosition = self.editor.GetCurrentPos()
argument = getArgument("_", "c", 10)
if argument == "dynamic":
showAutoCompList(getListOfDynamics)
def autoCompleteTag():
showAutoCompListForTags(getTagList)
def getTagList():
return [
"maker_dynamic",
"body",
"head",
"html",
"span",
"div id=",
"style",
"meta",
"link",
"DOCTYPE",
"title",
"em",
"pre",
"code",
"h2",
"h3",
"h1",
"h6",
"h4",
"ins",
"strong",
"bdo",
"dfn",
"var",
"samp",
"cite",
"blockquote",
"acronym",
"abbr",
"br />",
"address",
"h5",
"q",
"p",
"del",
"kbd",
"a name=",
"a href=",
"base",
"map",
"object",
"param",
"img src=",
"area",
"dl",
"ol",
"dd",
"li",
"ul",
"dt",
"colgroup",
"tr",
"tbody",
"caption",
"tfoot",
"th",
"table",
"td",
"col",
"thead",
"fieldset",
"form",
"textarea",
"button",
"label",
"optgroup",
"input",
"legend",
"select",
"option",
"noscript",
"script",
"b",
"sub",
"i",
"big",
"tt",
"hr />",
"sup",
"small",
]
# end support functions
cssTool = makerCSSTools.CSSTools()
key = event.GetKey()
if key == 62:
autoCompleteHTML()
elif key == self.view.wx.WXK_RETURN:
indentPrev = self.editor.GetLineIndentation(
self.editor.GetCurrentLine() - 1
)
self.editor.SetLineIndentation(self.editor.GetCurrentLine(), indentPrev)
self.editor.GotoPos(
self.editor.GetLineIndentPosition(self.editor.GetCurrentLine())
)
elif key == 60:
autoCompleteTag()
elif key == 34 or key == 39: # checking for quotes
self.quotes = '"'
if key == 39:
self.quotes = "'"
autoCompleteHTMLArgument()
# css autocomplete
elif key == 40: # check for (
self.quotes = ""
autoCompleteHTMLArgument()
elif key == 58:
autoCompleteDynamic()
else:
pass