-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathcoldfusion-plugin.py
290 lines (237 loc) · 12.8 KB
/
coldfusion-plugin.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
import sublime, sublime_plugin, json, os
# for sorting COMPLETIONS SCOPES in on_query_completions
from operator import itemgetter
# try imports due to ST2/ST3 compatability
try:
import ColdFusion.dictionaries as dic
from urllib.request import urlopen
except ImportError:
import dictionaries as dic
from urllib import urlopen
# *****************************************************************************************
# COMPLETIONS
# *****************************************************************************************
class ColdFusionAutoComplete(sublime_plugin.EventListener):
def on_cfml_all(self, view, prefix, pos):
# adds opening markup bracket if it isn't found
lt = '<' if view.substr(pos) != '<' else ''
return [(v + '\tTag (cmfl)', lt + v) for v in sorted(dic.lang.TAGS.keys())]
def on_cfml_tag_attributes(self, view, prefix, pos):
tag = dic.get_tag_name(view, pos)
return sorted(dic.lang.TAGS.get(tag, []))
def on_cfscript_all(self, view, prefix, pos):
if view.substr(pos) == ".": # completions for dot variables
var = view.substr(view.word(pos)).lower()
return [(v,v) for v in dic.lang.VARIABLES[var]] if var in dic.lang.VARIABLES.keys() else []
functions = [(v.split('(').pop(0) + '\tfn. (cfscript)', v) for v in dic.lang.FUNCTIONS.keys()]
# add language variables
functions.extend([(v + '\tvar (cfscript)',v) for v in dic.lang.VARIABLES.keys()])
# add the current view's cfscript methods
functions.extend(dic.get_function_completions_from_file(view.file_name(), "this"))
# lets try and grab cfscript methods from the inherited cfc
try:
extends_value = view.find_by_selector("meta.component-operator.extends.value.cfscript").pop(0)
except IndexError:
extends_value = None
if extends_value:
extends_path = view.substr(extends_value).replace('.','/')
# get base path of project
this_file = view.file_name()
dir_len = this_file.rfind('/') #(for OSX)
if not dir_len > 0:
dir_len = this_file.rfind('\\') #(for Windows)
this_dir = this_file[:(dir_len + 1)] # adds ending '/'
cfc_file = this_dir + extends_path + ".cfc"
# check the cfc_file exists else look for it in project folders
if not os.path.isfile(cfc_file):
# check for the cfc in root folders
for folder in sublime.active_window().folders():
if os.path.isfile(folder + "/" + extends_path + ".cfc"):
cfc_file = folder + "/" + extends_path + ".cfc"
break
try:
functions.extend(dic.get_function_completions_from_file(cfc_file, view.substr(extends_value).split(".").pop(0)))
except (UnboundLocalError, IOError):
pass
# TODO: add tag operators
return functions
def on_cfscript_variables(self, view, prefix, pos):
return []
def on_cfscript_function_arguments(self, view, prefix, pos):
return []
def on_cfscript_tagoperator_attributes(self, view, prefix, pos):
return []
# ****************************************************************
def on_query_completions(self, view, prefix, locations):
pos = view.sel()[0].b - 1
COMPLETIONS = (
(view.score_selector(pos, dic.CFML_TAG_SCOPE), self.on_cfml_all),
(view.score_selector(pos, dic.CFML_TAG_ATTRIBUTE_SCOPE),self.on_cfml_tag_attributes),
(view.score_selector(pos, dic.CFSCRIPT_FUNCTION_SCOPE),self.on_cfscript_all),
(view.score_selector(pos, dic.CFSCRIPT_FUNCTION_ARGS_SCOPE),self.on_cfscript_function_arguments),
(view.score_selector(pos, dic.CFSCRIPT_VARIABLES_SCOPE),self.on_cfscript_variables),
(view.score_selector(pos, dic.CFSCRIPT_TAGOPERATOR_ATTRIBUTE_SCOPE),self.on_cfscript_tagoperator_attributes)
)
for score, completions in sorted(COMPLETIONS,key=itemgetter(0),reverse=True):
if score: return completions(view, prefix, pos)
def on_modified(self, view):
if not view.settings().get("auto_complete"):
return None
pos = view.sel()[0].b - 1
# triggers auto complete using just the space bar or the dot character
if view.score_selector(pos, dic.AC_ON_DOT_SCOPE) and view.substr(pos) == "." or \
view.score_selector(pos, dic.AC_ON_SPACE_SCOPE) and view.substr(pos) == " ":
t = view.settings().get("auto_complete_delay")
sublime.set_timeout(lambda:
view.run_command("auto_complete", {
'disable_auto_insert': True,
'next_completion_if_showing': False,
'api_completions_only': True}), t)
# *****************************************************************************************
# KEYBINDING TEXT COMMANDS
# *****************************************************************************************
class CloseCftagCommand(sublime_plugin.TextCommand):
def run(self, edit):
s = sublime.load_settings('ColdFusion.sublime-settings')
# current carat position
pos = self.view.sel()[0].end()
self.view.run_command("hide_auto_complete")
# insert the ">" char
for region in self.view.sel():
self.view.insert(edit, region.b, ">")
# return if disabled in ColdFusion.sublime-settings file
if not s.get("auto_close_cfml"):
return None
# prevents triggering inside strings and other scopes that are not block tags
# this should be taken care of in keybindings, but it's not working for cfcomponent
# TODO ST3: use scope_selector or clean this out it may work through keybindings now
# cursor position
# pos = self.view.sel()[0].b
# if self.view.match_selector(pos, "string") \
# or self.view.match_selector(pos, "source.cfscript.embedded.cfml") \
# or not self.view.match_selector(pos, "meta.tag.block.cf"):
# return
if self.view.match_selector(region.b - 1,"meta.tag.block.cf") \
and not self.view.substr(region.b - 2) == "/": # don't close an already closed tag
for temp in self.view.sel():
tag_name = dic.get_tag_name(self.view, temp.b)
indent = s.get("auto_indent_on_close")
if indent and tag_name != "cfoutput":
(row, col) = self.view.rowcol(temp.begin())
indent_region = self.view.find('^\s+', self.view.text_point(row, 0))
if self.view.rowcol(indent_region.begin())[0] == row:
indent_space = self.view.substr(indent_region)
else:
indent_space = ''
self.view.insert(edit, temp.b, "\n\t" + indent_space + "\n" + indent_space + "</" + tag_name + ">")
# we're using view.insert() so this returns the cursor to the previous line
self.view.run_command("move", {"by": "lines", "forward": False})
else:
self.view.run_command("insert_snippet", {"contents": "$0</" + tag_name + ">"})
return None
# *****************************************************************************************
# DEFAULT.SUBLIME-COMMANDS
# *****************************************************************************************
class ShowCflibCommand(sublime_plugin.WindowCommand):
cflib_category_url = r"http://www.cflib.org/api/api.cfc?method=getlibraries&returnformat=json"
cflib_udfs_url = r"http://www.cflib.org/api/api.cfc?method=getudfs&returnformat=json&libraryid="
cflib_udf_url = r"http://www.cflib.org/api/api.cfc?method=getudf&returnFormat=json&udfid="
libIndex = -1
def run(self):
global libs, udfs
if not 'libs' in globals():
libs = self.fetch_json(self.cflib_category_url)['DATA']
udfs = {}
sublime.set_timeout(lambda:self.window.show_quick_panel([[v] for k, v in (libs)], self.on_select_library), 10)
def on_select_library(self, index):
self.libIndex = index
if index == -1:
return
if not index in udfs:
udfs[index] = self.fetch_json(self.cflib_udfs_url + str(libs[index][0]))['DATA']
sublime.set_timeout(lambda:self.window.show_quick_panel([[v.strip(), c.strip()] for k, v, c in udfs[index]], self.on_select_udf), 10)
def on_select_udf(self, index):
if index == -1:
self.run()
else:
code = str(self.fetch_json(self.cflib_udf_url + str(udfs[self.libIndex][index][0]))['CODE'])
self.window.active_view().run_command("insert_udf", {"code":code})
def fetch_json(self,url):
req = urlopen(url)
return json.loads(req.read().decode('UTF-8'))
class InsertUdfCommand(sublime_plugin.TextCommand):
def run(self, edit, code):
for region in self.view.sel():
self.view.replace(edit, region, code.replace("\r",""))
# *****************************************************************************************
# FUNCTION AUTO-FOLD COMMANDS
# *****************************************************************************************
class FoldCffunctionsCommand(sublime_plugin.TextCommand):
def run(self, edit):
contentRegions = self.findCffunctionContent()
contentRegions += self.findScriptFunctionContent()
# if the regions are already folded, unfold them
if self.view.fold(contentRegions) == False:
self.view.unfold(contentRegions)
# Find the CFFunction regions to fold
def findCffunctionContent(self):
contentRegions = []
try:
cffunctionOpens = self.view.find_all('<cffunction.*?name=".*?".*?>')
cffunctionEnds = self.view.find_all('</cffunction>')
functionCnt = len(cffunctionOpens)
i = 0
n = 0
# Loop through opening & ending regions...create a new region containing the function content
while i < functionCnt:
nextElem = i + 1
if cffunctionOpens[i].end() > cffunctionEnds[n].end():
# function ending doesn't match up with function start (have an orphaned function close, skip the end tag)
i -= 1
elif (nextElem < functionCnt and cffunctionOpens[nextElem].end() > cffunctionEnds[n].end()) or nextElem == functionCnt:
# Create new region from end of function start tag to the end of the function close tag
contentRegions.append(sublime.Region(cffunctionOpens[i].end(),cffunctionEnds[n].end()))
else:
# function start doesn't match up with function end (missing closing function tag for this one, skip the open tag)
n -= 1
i += 1
n += 1
except:
print ("Fold Functions Plugin: Unexpected error when trying to find CFFUNCTIONS.")
return contentRegions
# Find the script function regions to fold
def findScriptFunctionContent(self):
contentRegions = []
try:
scriptFuncOpens = self.view.find_all('function.*?\{')
for func in scriptFuncOpens:
startPosition = func.end()
closingBracket = self.findClosingBracket(startPosition)
if closingBracket != None:
contentRegions.append(sublime.Region(startPosition,closingBracket))
except:
print ("Fold Functions Plugin: Unexpected error when trying to find CFSCRIPT functions.")
return contentRegions
# Find the closing bracket for a script function
def findClosingBracket(self, startPosition):
openBlocks = 1
try:
# Loop through & track all opening/closing brackets (that come after our initial function opener) until
# we find a closing bracket to match our origiinal opening bracket or we run out of brackets to check
while openBlocks > 0:
bracketRegion = self.view.find("\{|\}", startPosition)
bracket = self.view.substr(bracketRegion)
if(bracket != None):
if bracket == "{":
openBlocks += 1
else:
openBlocks -= 1
startPosition = bracketRegion.end()
else:
# Ran out of brackets to check, get us out the loop
openBlocks = -1
if openBlocks == 0:
return bracketRegion.end()
except:
print ("Fold Functions Plugin: Unexpected error when trying to find a closing bracket for CFSCRIPT function.")
return None