-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsublime-css-auto-comments.py
203 lines (147 loc) · 5.28 KB
/
sublime-css-auto-comments.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
import sublime, sublime_plugin
import re
class CssautocommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
# get the nearest css property
cssProp = self.findCss()
if(cssProp != None):
# enter edit mode
self.writeOut(edit, cssProp)
def getCursor(self):
cursorReg = self.view.sel()[0]
return cursorReg
def findCss(self):
regex = self.view.find(".*?(\n*\s*)\{", self.getCursor().begin());
if(regex != None):
cssProp = self.view.substr(regex)
cssName = cssProp = cssProp[:-1].strip()
cssType = ''
cssAttrs = ''
if (cssProp[0] == '.'):
cssType = 'class'
cssName = cssProp[1:]
elif (cssProp[0] == '#'):
cssType = 'id'
cssName = cssProp[1:]
else:
cssType = 'element'
attrs = re.match('(.*)(\[(.*)=(.*)\])', cssName)
if attrs:
cssAttrs = attrs.group(2)[1:-1]
cssName = attrs.group(1)
relatedCss = self.findRelatedCss(cssProp)
relatedSass = self.findNestedCss(cssProp)
# Combine all lists
relatedCss = relatedCss + relatedSass
# Remove dupes whilst preserving order
relatedCss = [x for i,x in enumerate(relatedCss) if i==relatedCss.index(x)]
#Remove empty elements
relatedCss = list(filter(None, relatedCss))
return {
'name' : cssName,
'type' : cssType,
'attrs': cssAttrs,
'related' : relatedCss
}
return None
def findRelatedCss(self, cssProp):
relatedCss = self.view.find_all(str(cssProp) + "(\.|\:)+.*(\n*\s*)\{")
for i in range(0,len(relatedCss)):
relatedCss[i] = self.view.substr(relatedCss[i])[len(cssProp):-1].strip()
return relatedCss
def findNestedCss(self, cssProp):
relatedCss = self.view.find_all(cssProp + "(\.|\:)?.*(\n*\s*)\{")
nestedCss = []
recurseLimit = 100
for i in range(0, len(relatedCss)):
cssBlock = ""
linelen = 0
offset = 0
bracketStack = []
recurseAmount = 0
firstBracketFound = False
point = relatedCss[i].begin()
line = self.getLineContents(self.view.line(point).begin())
# Start with something on the bracket stack
bracketStack.append('{')
# Start building the CSS block to parse
cssBlock += line.replace(" ","")
#This is for keeping track of what line its on
linelen = len(line)
offset = offset + linelen + 1
# Keep going when the brackets are unmatched
while len(bracketStack) > 0 and recurseAmount < recurseLimit:
# Stack operations
for j in range (0,line.count('{')):
if firstBracketFound:
bracketStack.append('{')
if(line.count('{')):
firstBracketFound = True
for j in range (0,line.count('}')):
bracketStack.pop()
recurseAmount+=1
# weird way of getting the next line. Start where the cursor began
# and keep adding on the cumulative length of the next line and recurse
line = self.getLineContents(self.view.line(point).begin() + offset)
cssBlock += line.replace(" ","")
linelen = len(line) + 1
offset = offset + linelen
if recurseAmount == 99:
print('Recursion limit hit. Check your brackets are evenly matched for element ' + self.view.substr(relatedCss[i])[:-1])
bracketCount = 0
fullClass = ""
hookFound = True
hookedClass = True
# Go through the concatted css block to find stuff
# Could probably do this in the while loop up there ^^^
# Pretty funny logic. Don't try and follow it D:
for j in range (0, len(cssBlock)):
if cssBlock[j] == "{":
bracketCount+=1
if hookFound == True:
hookedClass = True
else:
hookedClass = False
hookFound = False
if cssBlock[j] == "}":
bracketCount-=1
if bracketCount == 1:
hookedClass = True
if cssBlock[j] == "&":
if hookedClass:
hookFound = True
tempString = cssBlock[j+1:]
if bracketCount == 1:
fullClass = tempString.partition('{')[0]
else:
fullClass = fullClass + tempString.partition('{')[0]
hookedClass = False
nestedCss.append(fullClass)
return nestedCss
def getLineContents(self, line):
return self.view.substr(self.view.line(line))
def writeOut(self, edit, cssAttr):
markup = ''
cssName = cssAttr['name']
cssType = cssAttr['type']
cssAttrs = cssAttr['attrs']
relatedCss = cssAttr['related']
if cssAttrs:
cssAttrs = ' ' + cssAttrs
if(cssType == 'class'):
markup = '<div class="' + cssName + cssAttrs + '">markup</div>'
elif(cssType == 'id'):
markup = '<div id="' + cssName + cssAttrs + '">markup</div>'
elif(cssType == 'element'):
markup = '<' + cssName + cssAttrs + '>Markup</' + cssName + '>'
# replace the current cursor with a comment block and auto fill in all the attributes
cursor = self.getCursor()
line = self.view.line(cursor)
self.view.insert(edit, cursor.begin(), '/**\n')
self.view.insert(edit, self.getCursor().begin(), ' * @name ' + cssName.title() + '\n')
self.view.insert(edit, self.getCursor().begin(), ' * @description Style for the ' + cssName + ' ' + cssType + '\n')
for i in range(0, len(relatedCss)):
self.view.insert(edit, self.getCursor().begin(), ' * @state ' + relatedCss[i] + ' - ' + relatedCss[i].replace("."," ").replace(":"," ")[1:] + ' state\n')
self.view.insert(edit, self.getCursor().begin(), ' * @markup\n')
self.view.insert(edit, self.getCursor().begin(), ' * ' + markup + '\n')
self.view.insert(edit, self.getCursor().begin(), ' */\n')