-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoldFunctions.py
More file actions
91 lines (71 loc) · 3.33 KB
/
foldFunctions.py
File metadata and controls
91 lines (71 loc) · 3.33 KB
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
import sublime, sublime_plugin
# Fold functions on command
class FoldCffunctionsCommand(sublime_plugin.TextCommand):
def run(self, edit):
contentRegions = findCffunctionContent(self.view)
contentRegions += findScriptFunctionContent(self.view)
# 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(view):
contentRegions = []
try:
cffunctionOpens = view.find_all('<cffunction.*?name=".*?".*?>')
cffunctionEnds = 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(view):
contentRegions = []
try:
scriptFuncOpens = view.find_all('function.*?\{')
for func in scriptFuncOpens:
startPosition = func.end()
closingBracket = findClosingBracket(view,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(view, 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 = view.find("\{|\}", startPosition)
bracket = 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