So I’m completely new to Python and finally bothered to mess around with it and plugins. Perhaps I picked something not so beginner friendly to start with, but oh well. The plugin functions perfectly though I pretty much have no confidence in the code. It feels horrendous and is probably incredibly inefficient. Not being familiar with either Python or the Sublime API probably didn’t help much either.
Any who, I’m looking for any advice on how I can make it better, anything I did wrong, or any part of it that’s inefficient or the likes. Thanks to any one who has the know how and doesn’t mind helping me out.
[code]class selectTagContents(sublimeplugin.TextCommand):
def run(self, view, args):
current = view.sel()[0].a
closedPoints = ]
closedTags = ]
openedPoints = ]
openedTags = ]
for region in view.findAll('</[a-zA-Z0-9]+>'):
if region.a >= current:
closedPoints.append(region.a)
for region in view.findAll('(?<=</)[a-zA-Z0-9]+(?=>)'):
if region.a >= current:
closedTags.append(view.substr(region))
for region in view.findAll('<^/].+?^/]>'):
if region.b <= current:
openedPoints.append(region.b)
for region in view.findAll('(?<=<)[a-zA-Z0-9]+(?!.*?/>)'):
if region.b <= current:
openedTags.append(view.substr(region))
oLen = len(openedPoints)
cLen = len(closedPoints)
if cLen > 0 and oLen > 0:
openedTags.reverse()
openedPoints.reverse()
closed = ([closedTags[i], closedPoints*]) for i in range(0, cLen)]
opened = ([openedTags[i], openedPoints*]) for i in range(0, oLen)]
done = None
for cTag, cPoint in closed:
for oTag, oPoint in opened:
if cTag == oTag:
selection = sublime.Region(oPoint, cPoint)
view.sel().clear()
view.sel().add(selection)
done = True
break
if done is True:
break[/code]
Some specific questions I had if anyone can answer them.
1)* I REALLY REALLY hate Pythons data structures. They are driving me nuts. I searched everywhere and couldn’t find anything, is there anyway to get the index of the current item when looping through a list? That would make it so I could cut out the loop comprehensions converting my lists to a list of tuples but I couldn’t find how to do it anywhere.
The possible solutions I came across was .index(x) but that doesn’t work because it finds the nearest and that doesn’t work when tags repeat and I may be needing one further down the list. The other was converting them to a dictionary but that didn’t work because the only thing I could really use as a key was the tag and that doesn’t work because tags aren’t unique and there may need to be multiple entries for the same tag.
2) Is there anyway to break out of multiple loops? Such as in PHP you can just do break for the x amount of loops you want to break out of. I’d think there’d be a better way to stop the outer for loop at the end rather than having to set a check.
3) Lastly, and it’s more of a general question I guess, when I’m converting between data structures why does it mess with the types of the values in them? For instance in this plugin when I first pull the tag names and points, they are strings and integers. However, when I convert those lists into tuples it converts the strings to unicode strings (I think it does at least, that’s what the u in front of a string like u’my string’ means right?) and integers into long(?) integers (integers show up like 5476L). It doesn’t break anything I’m just wondering why it messes with them in the first place.
Thanks again for any help anyone can offer.*