Browse files

Tweaks and fix "insert as tag"

  • Loading branch information...
1 parent 3f453a6 commit a310eef94cc3fed9e4a6b0a15db2ef457b7e2c05 @titoBouzout committed Feb 27, 2012
Showing with 36 additions and 30 deletions.
  1. +6 −0 Insert As Tag/Commands.sublime-commands
  2. +0 −5 Insert As Tag/default-tag.sublime-snippet
  3. +13 −7 Main.sublime-menu
  4. +17 −18 tag_insert_as_tag.py
View
6 Insert As Tag/Commands.sublime-commands
@@ -0,0 +1,6 @@
+[
+ {
+ "caption": "Tag: Insert as Tag",
+ "command": "tag_insert_as_tag"
+ }
+]
View
5 Insert As Tag/default-tag.sublime-snippet
@@ -1,5 +0,0 @@
-<snippet>
- <content><![CDATA[<${1:p}>${2:$SELECTION}</${1/([^ ]+).*/$1/}>]]></content>
- <scope>text.html, text.xml</scope>
- <description>Default Tag</description>
-</snippet>
View
20 Main.sublime-menu
@@ -13,14 +13,20 @@
[
{"caption":"-"},
{
- "command": "tag_indent",
- "id":"tag-indent-selection",
- "caption":"Indent Tags on Selection"
+ "command": "tag_insert_as_tag",
+ "id":"tag-insert-as-tag",
+ "caption":"Insert as Tag"
},
- {
- "command": "tag_indent_document",
- "id":"tag-indent-document",
- "caption":"Indent Tags on Document"
+ {"caption":"-"},
+ {
+ "command": "tag_indent",
+ "id":"tag-indent-selection",
+ "caption":"Indent Tags on Selection"
+ },
+ {
+ "command": "tag_indent_document",
+ "id":"tag-indent-document",
+ "caption":"Indent Tags on Document"
},
{"caption":"-"},
{
View
35 tag_insert_as_tag.py
@@ -2,28 +2,27 @@
import sublime_plugin
import re
-
class TagInsertAsTagCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
- self_closing = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]
- self_closing_empty = ["br", "hr", "wbr"]
-
+ self_closing_tags = re.compile("^(area|base|br|col|frame|hr|img|input|link|meta|param|command|embed|source)", re.I)
+ new_selections = []
for region in view.sel():
if view.score_selector(region.a, 'text.html, text.xml') > 0:
- word_reg = view.word(region)
- word = view.substr(word_reg)
- if re.search("\s\Z", word) or word == "":
- view.run_command("insert_snippet", {"name": "Packages/Tag/Insert As Tag/default-tag.sublime-snippet"})
+ source = view.substr(region)
+ if not source.strip():
+ pass
+ elif re.match("^\s", source):
+ view.replace(edit, region, '<p>'+source+'</p>')
+ new_selections.append(sublime.Region(region.end()+3, region.end()+3))
+ elif self_closing_tags.match(source):
+ view.replace(edit, region, '<'+source+'/>')
+ new_selections.append(sublime.Region(region.end()+3, region.end()+3))
else:
- view.sel().clear()
- if word in self_closing:
- if word in self_closing_empty:
- tags = "<%s>" % (word)
- else:
- tags = "<%s >" % (word)
- else:
- tags = "<%s></%s>" % (word, word)
- view.replace(edit, word_reg, tags)
- view.sel().add(sublime.Region(word_reg.b + 2))
+ view.replace(edit, region, '<'+source+'></'+source.split(' ')[0]+'>')
+ new_selections.append(sublime.Region(region.end()+2, region.end()+2))
+
+ view.sel().clear()
+ for sel in new_selections:
+ view.sel().add(sel)

0 comments on commit a310eef

Please sign in to comment.