The above version doesn’t work correctly in a couple of common scenarios, such as the following (| is the cursor):
<html><a href="#">|
<html><div><div>hello</div>|
Here is a version that does:
[code]import sublime, sublimeplugin, re
class CloseTagCommand(sublimeplugin.TextCommand):
def run(self, view, args):
leftOfCursor = view.substr(sublime.Region(0, view.sel()[0].begin()))
regex = re.compile(’<(/?\w+)^>]*>’)
tags = regex.findall(leftOfCursor)
opentags = ]
for tag in tags:
if tag[0] == ‘/’:
if opentags-1] == tag[1:]: opentags.pop()
else: opentags.append(tag)
if len(opentags) > 0:
tag = '</' + opentags-1] + '>'
view.insert(view.sel()[0].begin(), tag)[/code]