Home Download Buy Blog Forum Support

CloseTagCommand — closes closest unclosed tag

CloseTagCommand — closes closest unclosed tag

Postby Grepsy on Fri Oct 15, 2010 2:17 pm

I made a little plugin to close the last open XML or HTML node/tag.

Code: Select all
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+?)>(?![\s\S]+?</\\1>)')
    tags = regex.findall(leftOfCursor)

    if len(tags) > 0:
      tag = '</' + tags[len(tags) - 1] + '>'
      view.insert(view.sel()[0].begin(), tag)


I have it bound to CTRL+. (option+command+. in TextMate)

Code: Select all
<binding key="ctrl+period" command="closeTag"/>
Grepsy
 
Posts: 5
Joined: Fri Oct 15, 2010 2:09 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby FalconNL on Thu Jan 06, 2011 10:37 am

The above version doesn't work correctly in a couple of common scenarios, such as the following (| is the cursor):

Code: Select all
<html><a href="#">|


Code: Select all
<html><div><div>hello</div>|


Here is a version that does:

Code: Select all
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)
FalconNL
 
Posts: 67
Joined: Thu May 29, 2008 8:17 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby thedz on Wed Feb 02, 2011 10:48 pm

Here's an updated version that works with the Sublime Text 2 alpha:
Code: Select all
import sublime, sublime_plugin, re

class CloseTagCommand(sublime_plugin.TextCommand):
    def run(self, edit ):
        leftOfCursor = self.view.substr(sublime.Region(0, self.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] + '>'
            self.view.insert(edit, self.view.sel()[0].begin(), tag)
thedz
 
Posts: 2
Joined: Wed Feb 02, 2011 10:47 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby n00ge on Wed Feb 02, 2011 11:23 pm

Awesome. Thanks for the update. I'll have to grab that later.
http://twitter.com/n00ge
Sublime Text 3 dev builds | OS X 10.8.2 & Ubuntu 13.04
n00ge
 
Posts: 139
Joined: Mon Oct 05, 2009 1:23 am

Re: CloseTagCommand — closes closest unclosed tag

Postby vostok4 on Sun Feb 20, 2011 2:12 am

This is a really dumb question but I just spent 10 minutes on the forums looking for someone else who asked it...

I saved the contents into Data\Packages\closeTag.py, and I added the following keymap (using ST2 dev portable):

{ "keys": ["ctrl+period"], "command": "closeTag" }

When I run ctrl.period I get this output in the console:
unknown command closeTag

What exactly am I missing? I do see this: Reloading plugin C:\Users\Matt\Desktop\st2\Data\Packages\closeTag.py so I can tell the plugin is getting loaded..
vostok4
 
Posts: 25
Joined: Fri Oct 22, 2010 6:09 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby jps on Sun Feb 20, 2011 3:51 am

The above plugin is for Sublime Text 1, and will require changes to work with Sublime Text 2
jps
Site Admin
 
Posts: 3062
Joined: Wed Mar 19, 2008 12:33 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby vostok4 on Sun Feb 20, 2011 8:06 am

OK figured it out (@jps the 3rd post is actually for ST2).

I didn't know how command names are mapped from their classes to the actual command name, but it turns out that CloseTagCommand is avaiable as "close_tag".

After that it works great!

**EDIT**

Also, to prevent it from closing tags such as <input /> or <img /> use this RE:
regex = re.compile('<(\w+) *[^/]*?>')
vostok4
 
Posts: 25
Joined: Fri Oct 22, 2010 6:09 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby danimaltron on Mon Feb 28, 2011 8:42 pm

Sorry for my ignorance. I just started trying SublimeText so far, and like it a lot. But I really want tag closing.

This is what I did:

1) Went to Tools > New Plugin, and pasted in this:
Code: Select all
import sublime, sublime_plugin, re

class CloseTagCommand(sublime_plugin.TextCommand):
    def run(self, edit ):
        leftOfCursor = self.view.substr(sublime.Region(0, self.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] + '>'
            self.view.insert(edit, self.view.sel()[0].begin(), tag)


I then saved it to the default plugin directory: C:\Users\~\AppData\Roaming\Sublime Text\Packages

2) I then went to Preference > User Key Bindings, and pasted in the command I found here like this:
Code: Select all
<!--
Place your key bindings in here, this will ensure they don't get overwritten
when installing new versions of Sublime Text
-->
<bindings>
   <binding key="ctrl+period" command="closeTag"/>
</bindings>


Hitting Ctrl + . does not do anything though. And nothing shows up in the console when I do.

How can I make this work? Thanks
danimaltron
 
Posts: 2
Joined: Mon Feb 28, 2011 8:36 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby adzenith on Mon Feb 28, 2011 9:25 pm

It looks like you're using a Sublime Text 2 plugin but a Sublime Text 1 keybinding. Which version are you running?
A Sublime Text 2 keybinding looks like this:
Code: Select all
   { "keys": ["ctrl+period"], "command": "close_tag" },
adzenith
 
Posts: 1213
Joined: Mon Oct 19, 2009 9:12 pm

Re: CloseTagCommand — closes closest unclosed tag

Postby danimaltron on Fri Mar 04, 2011 10:30 pm

You're right, I was using 1.4

Switched to 2 and it works great! Thanks
danimaltron
 
Posts: 2
Joined: Mon Feb 28, 2011 8:36 pm

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 9 guests