import sublime, sublime_plugin import re def match(rex, str): m = rex.match(str) if m: return m.group(0) else: return None # This responds to on_query_completions, but conceptually it's expanding # expressions, rather than completing words. # # It expands these simple expressions: # tag.class # tag#id class HtmlCompletions(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations): # Only trigger within HTML if not view.match_selector(locations[0], "text.html - source - meta.tag, punctuation.definition.tag.begin"): return [] # Get the contents of each line, from the beginning of the line to # each point lines = [view.substr(sublime.Region(view.line(l).a, l)) for l in locations] # Reverse the contents of each line, to simulate having the regex # match backwards lines = [l[::-1] for l in lines] # Check the first location looks like an expression rex = re.compile("([\w-]+)([.#])(\w+)") expr = match(rex, lines[0]) if not expr: return [] # Ensure that all other lines have identical expressions for i in xrange(1, len(lines)): ex = match(rex, lines[i]) if ex != expr: return [] # Return the completions arg, op, tag = rex.match(expr).groups() arg = arg[::-1] tag = tag[::-1] expr = expr[::-1] if op == '.': snippet = "<{0} class=\"{1}\">$1$0".format(tag, arg) else: snippet = "<{0} id=\"{1}\">$1$0".format(tag, arg) return [(expr, snippet)] # Provide completions that match just after typing an opening angle bracket class TagCompletions(sublime_plugin.EventListener): def on_query_completions(self, view, prefix, locations): # Only trigger within HTML if not view.match_selector(locations[0], "text.html - source"): return [] pt = locations[0] - len(prefix) - 1 ch = view.substr(sublime.Region(pt, pt + 1)) if ch != '<': return [] return ([ ("a\tTag", "a href=\"$1\">$2"), ("abbr\tTag", "abbr>$1"), ("acronym\tTag", "acronym>$1"), ("address\tTag", "address>$1"), ("applet\tTag", "applet>$1"), ("area\tTag", "area>$1"), ("b\tTag", "b>$1"), ("base\tTag", "base>$1"), ("big\tTag", "big>$1"), ("blockquote\tTag", "blockquote>$1"), ("body\tTag", "body>$1"), ("button\tTag", "button>$1"), ("center\tTag", "center>$1"), ("caption\tTag", "caption>$1"), ("cdata\tTag", "cdata>$1"), ("cite\tTag", "cite>$1"), ("col\tTag", "col>$1"), ("colgroup\tTag", "colgroup>$1"), ("code\tTag", "code>$1"), ("div\tTag", "div>$1"), ("dd\tTag", "dd>$1"), ("del\tTag", "del>$1"), ("dfn\tTag", "dfn>$1"), ("dl\tTag", "dl>$1"), ("dt\tTag", "dt>$1"), ("em\tTag", "em>$1"), ("fieldset\tTag", "fieldset>$1"), ("font\tTag", "font>$1"), ("form\tTag", "form>$1"), ("frame\tTag", "frame>$1"), ("frameset\tTag", "frameset>$1"), ("head\tTag", "head>$1"), ("h1\tTag", "h1>$1"), ("h2\tTag", "h2>$1"), ("h3\tTag", "h3>$1"), ("h4\tTag", "h4>$1"), ("h5\tTag", "h5>$1"), ("h6\tTag", "h6>$1"), ("i\tTag", "i>$1"), ("iframe\tTag", "iframe src=\"$1\">"), ("ins\tTag", "ins>$1"), ("kbd\tTag", "kbd>$1"), ("li\tTag", "li>$1"), ("label\tTag", "label>$1"), ("legend\tTag", "legend>$1"), ("link\tTag", "link rel=\"stylesheet\" type=\"text/css\" href=\"$1\">"), ("map\tTag", "map>$1"), ("noframes\tTag", "noframes>$1"), ("object\tTag", "object>$1"), ("ol\tTag", "ol>$1"), ("optgroup\tTag", "optgroup>$1"), ("option\tTag", "option>$0"), ("p\tTag", "p>$1

"), ("pre\tTag", "pre>$1"), ("span\tTag", "span>$1"), ("samp\tTag", "samp>$1"), ("script\tTag", "script type=\"${1:text/javascript}\">$0"), ("style\tTag", "style type=\"${1:text/css}\">$0"), ("select\tTag", "select>$1"), ("small\tTag", "small>$1"), ("strong\tTag", "strong>$1"), ("sub\tTag", "sub>$1"), ("sup\tTag", "sup>$1"), ("table\tTag", "table>$1"), ("tbody\tTag", "tbody>$1"), ("td\tTag", "td>$1"), ("textarea\tTag", "textarea>$1"), ("tfoot\tTag", "tfoot>$1"), ("th\tTag", "th>$1"), ("thead\tTag", "thead>$1"), ("title\tTag", "title>$1"), ("tr\tTag", "tr>$1"), ("tt\tTag", "tt>$1"), ("u\tTag", "u>$1"), ("ul\tTag", "ul>$1"), ("var\tTag", "var>$1"), ("br\tTag", "br>"), ("embed\tTag", "embed>"), ("hr\tTag", "hr>"), ("img\tTag", "img src=\"$1\">"), ("input\tTag", "input>"), ("meta\tTag", "meta>"), ("param\tTag", "param name=\"$1\" value=\"$2\">"), ("article\tTag", "article>$1"), ("aside\tTag", "aside>$1"), ("audio\tTag", "audio>$1"), ("canvas\tTag", "canvas>$1"), ("footer\tTag", "footer>$1"), ("header\tTag", "header>$1"), ("nav\tTag", "nav>$1"), ("section\tTag", "section>$1"), ("video\tTag", "video>$1"), ("A\tTag", "A HREF=\"$1\">$2"), ("ABBR\tTag", "ABBR>$1"), ("ACRONYM\tTag", "ACRONYM>$1"), ("ADDRESS\tTag", "ADDRESS>$1"), ("APPLET\tTag", "APPLET>$1"), ("AREA\tTag", "AREA>$1"), ("B\tTag", "B>$1"), ("BASE\tTag", "BASE>$1"), ("BIG\tTag", "BIG>$1"), ("BLOCKQUOTE\tTag", "BLOCKQUOTE>$1"), ("BODY\tTag", "BODY>$1"), ("BUTTON\tTag", "BUTTON>$1"), ("CENTER\tTag", "CENTER>$1"), ("CAPTION\tTag", "CAPTION>$1"), ("CDATA\tTag", "CDATA>$1"), ("CITE\tTag", "CITE>$1"), ("COL\tTag", "COL>$1"), ("COLGROUP\tTag", "COLGROUP>$1"), ("CODE\tTag", "CODE>$1"), ("DIV\tTag", "DIV>$1"), ("DD\tTag", "DD>$1"), ("DEL\tTag", "DEL>$1"), ("DFN\tTag", "DFN>$1"), ("DL\tTag", "DL>$1"), ("DT\tTag", "DT>$1"), ("EM\tTag", "EM>$1"), ("FIELDSET\tTag", "FIELDSET>$1"), ("FONT\tTag", "FONT>$1"), ("FORM\tTag", "FORM>$1"), ("FRAME\tTag", "FRAME>$1"), ("FRAMESET\tTag", "FRAMESET>$1"), ("HEAD\tTag", "HEAD>$1"), ("H1\tTag", "H1>$1"), ("H2\tTag", "H2>$1"), ("H3\tTag", "H3>$1"), ("H4\tTag", "H4>$1"), ("H5\tTag", "H5>$1"), ("H6\tTag", "H6>$1"), ("I\tTag", "I>$1"), ("IFRAME\tTag", "IFRAME src=\"$1\">"), ("INS\tTag", "INS>$1"), ("KBD\tTag", "KBD>$1"), ("LI\tTag", "LI>$1"), ("LABEL\tTag", "LABEL>$1"), ("LEGEND\tTag", "LEGEND>$1"), ("LINK\tTag", "LINK>$1"), ("MAP\tTag", "MAP>$1"), ("NOFRAMES\tTag", "NOFRAMES>$1"), ("OBJECT\tTag", "OBJECT>$1"), ("OL\tTag", "OL>$1"), ("OPTGROUP\tTag", "OPTGROUP>$1"), ("OPTION\tTag", "OPTION>$1"), ("P\tTag", "P>$1

"), ("PRE\tTag", "PRE>$1"), ("SPAN\tTag", "SPAN>$1"), ("SAMP\tTag", "SAMP>$1"), ("SCRIPT\tTag", "SCRIPT TYPE=\"${1:text/javascript}\">$0"), ("STYLE\tTag", "STYLE TYPE=\"${1:text/css}\">$0"), ("SELECT\tTag", "SELECT>$1"), ("SMALL\tTag", "SMALL>$1"), ("STRONG\tTag", "STRONG>$1"), ("SUB\tTag", "SUB>$1"), ("SUP\tTag", "SUP>$1"), ("TABLE\tTag", "TABLE>$1"), ("TBODY\tTag", "TBODY>$1"), ("TD\tTag", "TD>$1"), ("TEXTAREA\tTag", "TEXTAREA>$1"), ("TFOOT\tTag", "TFOOT>$1"), ("TH\tTag", "TH>$1"), ("THEAD\tTag", "THEAD>$1"), ("TITLE\tTag", "TITLE>$1"), ("TR\tTag", "TR>$1"), ("TT\tTag", "TT>$1"), ("U\tTag", "U>$1"), ("UL\tTag", "UL>$1"), ("VAR\tTag", "VAR>$1"), ("BR\tTag", "BR>"), ("EMBED\tTag", "EMBED>"), ("HR\tTag", "HR>"), ("IMG\tTag", "IMG SRC=\"$1\">"), ("INPUT\tTag", "INPUT>"), ("META\tTag", "META>"), ("PARAM\tTag", "PARAM NAME=\"$1\" VALUE=\"$2\">)"), ("ARTICLE\tTag", "ARTICLE>$1"), ("ASIDE\tTag", "ASIDE>$1"), ("AUDIO\tTag", "AUDIO>$1"), ("CANVAS\tTag", "CANVAS>$1"), ("FOOTER\tTag", "FOOTER>$1"), ("HEADER\tTag", "HEADER>$1"), ("NAV\tTag", "NAV>$1"), ("SECTION\tTag", "SECTION>$1"), ("VIDEO\tTag", "VIDEO>$1") ], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)