PageRenderTime 217ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Packages/HTML/html_completions.py

https://gitlab.com/Blueprint-Marketing/sublime-config
Python | 263 lines | 228 code | 19 blank | 16 comment | 15 complexity | 38ba0149ec8945ce7ebb93e2d7efbf40 MD5 | raw file
  1. import sublime, sublime_plugin
  2. import re
  3. def match(rex, str):
  4. m = rex.match(str)
  5. if m:
  6. return m.group(0)
  7. else:
  8. return None
  9. # This responds to on_query_completions, but conceptually it's expanding
  10. # expressions, rather than completing words.
  11. #
  12. # It expands these simple expressions:
  13. # tag.class
  14. # tag#id
  15. class HtmlCompletions(sublime_plugin.EventListener):
  16. def on_query_completions(self, view, prefix, locations):
  17. # Only trigger within HTML
  18. if not view.match_selector(locations[0],
  19. "text.html - source - meta.tag, punctuation.definition.tag.begin"):
  20. return []
  21. # Get the contents of each line, from the beginning of the line to
  22. # each point
  23. lines = [view.substr(sublime.Region(view.line(l).a, l))
  24. for l in locations]
  25. # Reverse the contents of each line, to simulate having the regex
  26. # match backwards
  27. lines = [l[::-1] for l in lines]
  28. # Check the first location looks like an expression
  29. rex = re.compile("([\w-]+)([.#])(\w+)")
  30. expr = match(rex, lines[0])
  31. if not expr:
  32. return []
  33. # Ensure that all other lines have identical expressions
  34. for i in xrange(1, len(lines)):
  35. ex = match(rex, lines[i])
  36. if ex != expr:
  37. return []
  38. # Return the completions
  39. arg, op, tag = rex.match(expr).groups()
  40. arg = arg[::-1]
  41. tag = tag[::-1]
  42. expr = expr[::-1]
  43. if op == '.':
  44. snippet = "<{0} class=\"{1}\">$1</{0}>$0".format(tag, arg)
  45. else:
  46. snippet = "<{0} id=\"{1}\">$1</{0}>$0".format(tag, arg)
  47. return [(expr, snippet)]
  48. # Provide completions that match just after typing an opening angle bracket
  49. class TagCompletions(sublime_plugin.EventListener):
  50. def on_query_completions(self, view, prefix, locations):
  51. # Only trigger within HTML
  52. if not view.match_selector(locations[0],
  53. "text.html - source"):
  54. return []
  55. pt = locations[0] - len(prefix) - 1
  56. ch = view.substr(sublime.Region(pt, pt + 1))
  57. if ch != '<':
  58. return []
  59. return ([
  60. ("a\tTag", "a href=\"$1\">$2</a>"),
  61. ("abbr\tTag", "abbr>$1</abbr>"),
  62. ("acronym\tTag", "acronym>$1</acronym>"),
  63. ("address\tTag", "address>$1</address>"),
  64. ("applet\tTag", "applet>$1</applet>"),
  65. ("area\tTag", "area>$1</area>"),
  66. ("b\tTag", "b>$1</b>"),
  67. ("base\tTag", "base>$1</base>"),
  68. ("big\tTag", "big>$1</big>"),
  69. ("blockquote\tTag", "blockquote>$1</blockquote>"),
  70. ("body\tTag", "body>$1</body>"),
  71. ("button\tTag", "button>$1</button>"),
  72. ("center\tTag", "center>$1</center>"),
  73. ("caption\tTag", "caption>$1</caption>"),
  74. ("cdata\tTag", "cdata>$1</cdata>"),
  75. ("cite\tTag", "cite>$1</cite>"),
  76. ("col\tTag", "col>$1</col>"),
  77. ("colgroup\tTag", "colgroup>$1</colgroup>"),
  78. ("code\tTag", "code>$1</code>"),
  79. ("div\tTag", "div>$1</div>"),
  80. ("dd\tTag", "dd>$1</dd>"),
  81. ("del\tTag", "del>$1</del>"),
  82. ("dfn\tTag", "dfn>$1</dfn>"),
  83. ("dl\tTag", "dl>$1</dl>"),
  84. ("dt\tTag", "dt>$1</dt>"),
  85. ("em\tTag", "em>$1</em>"),
  86. ("fieldset\tTag", "fieldset>$1</fieldset>"),
  87. ("font\tTag", "font>$1</font>"),
  88. ("form\tTag", "form>$1</form>"),
  89. ("frame\tTag", "frame>$1</frame>"),
  90. ("frameset\tTag", "frameset>$1</frameset>"),
  91. ("head\tTag", "head>$1</head>"),
  92. ("h1\tTag", "h1>$1</h1>"),
  93. ("h2\tTag", "h2>$1</h2>"),
  94. ("h3\tTag", "h3>$1</h3>"),
  95. ("h4\tTag", "h4>$1</h4>"),
  96. ("h5\tTag", "h5>$1</h5>"),
  97. ("h6\tTag", "h6>$1</h6>"),
  98. ("i\tTag", "i>$1</i>"),
  99. ("iframe\tTag", "iframe src=\"$1\"></iframe>"),
  100. ("ins\tTag", "ins>$1</ins>"),
  101. ("kbd\tTag", "kbd>$1</kbd>"),
  102. ("li\tTag", "li>$1</li>"),
  103. ("label\tTag", "label>$1</label>"),
  104. ("legend\tTag", "legend>$1</legend>"),
  105. ("link\tTag", "link rel=\"stylesheet\" type=\"text/css\" href=\"$1\">"),
  106. ("map\tTag", "map>$1</map>"),
  107. ("noframes\tTag", "noframes>$1</noframes>"),
  108. ("object\tTag", "object>$1</object>"),
  109. ("ol\tTag", "ol>$1</ol>"),
  110. ("optgroup\tTag", "optgroup>$1</optgroup>"),
  111. ("option\tTag", "option>$0</option>"),
  112. ("p\tTag", "p>$1</p>"),
  113. ("pre\tTag", "pre>$1</pre>"),
  114. ("span\tTag", "span>$1</span>"),
  115. ("samp\tTag", "samp>$1</samp>"),
  116. ("script\tTag", "script type=\"${1:text/javascript}\">$0</script>"),
  117. ("style\tTag", "style type=\"${1:text/css}\">$0</style>"),
  118. ("select\tTag", "select>$1</select>"),
  119. ("small\tTag", "small>$1</small>"),
  120. ("strong\tTag", "strong>$1</strong>"),
  121. ("sub\tTag", "sub>$1</sub>"),
  122. ("sup\tTag", "sup>$1</sup>"),
  123. ("table\tTag", "table>$1</table>"),
  124. ("tbody\tTag", "tbody>$1</tbody>"),
  125. ("td\tTag", "td>$1</td>"),
  126. ("textarea\tTag", "textarea>$1</textarea>"),
  127. ("tfoot\tTag", "tfoot>$1</tfoot>"),
  128. ("th\tTag", "th>$1</th>"),
  129. ("thead\tTag", "thead>$1</thead>"),
  130. ("title\tTag", "title>$1</title>"),
  131. ("tr\tTag", "tr>$1</tr>"),
  132. ("tt\tTag", "tt>$1</tt>"),
  133. ("u\tTag", "u>$1</u>"),
  134. ("ul\tTag", "ul>$1</ul>"),
  135. ("var\tTag", "var>$1</var>"),
  136. ("br\tTag", "br>"),
  137. ("embed\tTag", "embed>"),
  138. ("hr\tTag", "hr>"),
  139. ("img\tTag", "img src=\"$1\">"),
  140. ("input\tTag", "input>"),
  141. ("meta\tTag", "meta>"),
  142. ("param\tTag", "param name=\"$1\" value=\"$2\">"),
  143. ("article\tTag", "article>$1</article>"),
  144. ("aside\tTag", "aside>$1</aside>"),
  145. ("audio\tTag", "audio>$1</audio>"),
  146. ("canvas\tTag", "canvas>$1</canvas>"),
  147. ("footer\tTag", "footer>$1</footer>"),
  148. ("header\tTag", "header>$1</header>"),
  149. ("nav\tTag", "nav>$1</nav>"),
  150. ("section\tTag", "section>$1</section>"),
  151. ("video\tTag", "video>$1</video>"),
  152. ("A\tTag", "A HREF=\"$1\">$2</A>"),
  153. ("ABBR\tTag", "ABBR>$1</ABBR>"),
  154. ("ACRONYM\tTag", "ACRONYM>$1</ACRONYM>"),
  155. ("ADDRESS\tTag", "ADDRESS>$1</ADDRESS>"),
  156. ("APPLET\tTag", "APPLET>$1</APPLET>"),
  157. ("AREA\tTag", "AREA>$1</AREA>"),
  158. ("B\tTag", "B>$1</B>"),
  159. ("BASE\tTag", "BASE>$1</BASE>"),
  160. ("BIG\tTag", "BIG>$1</BIG>"),
  161. ("BLOCKQUOTE\tTag", "BLOCKQUOTE>$1</BLOCKQUOTE>"),
  162. ("BODY\tTag", "BODY>$1</BODY>"),
  163. ("BUTTON\tTag", "BUTTON>$1</BUTTON>"),
  164. ("CENTER\tTag", "CENTER>$1</CENTER>"),
  165. ("CAPTION\tTag", "CAPTION>$1</CAPTION>"),
  166. ("CDATA\tTag", "CDATA>$1</CDATA>"),
  167. ("CITE\tTag", "CITE>$1</CITE>"),
  168. ("COL\tTag", "COL>$1</COL>"),
  169. ("COLGROUP\tTag", "COLGROUP>$1</COLGROUP>"),
  170. ("CODE\tTag", "CODE>$1</CODE>"),
  171. ("DIV\tTag", "DIV>$1</DIV>"),
  172. ("DD\tTag", "DD>$1</DD>"),
  173. ("DEL\tTag", "DEL>$1</DEL>"),
  174. ("DFN\tTag", "DFN>$1</DFN>"),
  175. ("DL\tTag", "DL>$1</DL>"),
  176. ("DT\tTag", "DT>$1</DT>"),
  177. ("EM\tTag", "EM>$1</EM>"),
  178. ("FIELDSET\tTag", "FIELDSET>$1</FIELDSET>"),
  179. ("FONT\tTag", "FONT>$1</FONT>"),
  180. ("FORM\tTag", "FORM>$1</FORM>"),
  181. ("FRAME\tTag", "FRAME>$1</FRAME>"),
  182. ("FRAMESET\tTag", "FRAMESET>$1</FRAMESET>"),
  183. ("HEAD\tTag", "HEAD>$1</HEAD>"),
  184. ("H1\tTag", "H1>$1</H1>"),
  185. ("H2\tTag", "H2>$1</H2>"),
  186. ("H3\tTag", "H3>$1</H3>"),
  187. ("H4\tTag", "H4>$1</H4>"),
  188. ("H5\tTag", "H5>$1</H5>"),
  189. ("H6\tTag", "H6>$1</H6>"),
  190. ("I\tTag", "I>$1</I>"),
  191. ("IFRAME\tTag", "IFRAME src=\"$1\"></IFRAME>"),
  192. ("INS\tTag", "INS>$1</INS>"),
  193. ("KBD\tTag", "KBD>$1</KBD>"),
  194. ("LI\tTag", "LI>$1</LI>"),
  195. ("LABEL\tTag", "LABEL>$1</LABEL>"),
  196. ("LEGEND\tTag", "LEGEND>$1</LEGEND>"),
  197. ("LINK\tTag", "LINK>$1</LINK>"),
  198. ("MAP\tTag", "MAP>$1</MAP>"),
  199. ("NOFRAMES\tTag", "NOFRAMES>$1</NOFRAMES>"),
  200. ("OBJECT\tTag", "OBJECT>$1</OBJECT>"),
  201. ("OL\tTag", "OL>$1</OL>"),
  202. ("OPTGROUP\tTag", "OPTGROUP>$1</OPTGROUP>"),
  203. ("OPTION\tTag", "OPTION>$1</OPTION>"),
  204. ("P\tTag", "P>$1</P>"),
  205. ("PRE\tTag", "PRE>$1</PRE>"),
  206. ("SPAN\tTag", "SPAN>$1</SPAN>"),
  207. ("SAMP\tTag", "SAMP>$1</SAMP>"),
  208. ("SCRIPT\tTag", "SCRIPT TYPE=\"${1:text/javascript}\">$0</SCRIPT>"),
  209. ("STYLE\tTag", "STYLE TYPE=\"${1:text/css}\">$0</STYLE>"),
  210. ("SELECT\tTag", "SELECT>$1</SELECT>"),
  211. ("SMALL\tTag", "SMALL>$1</SMALL>"),
  212. ("STRONG\tTag", "STRONG>$1</STRONG>"),
  213. ("SUB\tTag", "SUB>$1</SUB>"),
  214. ("SUP\tTag", "SUP>$1</SUP>"),
  215. ("TABLE\tTag", "TABLE>$1</TABLE>"),
  216. ("TBODY\tTag", "TBODY>$1</TBODY>"),
  217. ("TD\tTag", "TD>$1</TD>"),
  218. ("TEXTAREA\tTag", "TEXTAREA>$1</TEXTAREA>"),
  219. ("TFOOT\tTag", "TFOOT>$1</TFOOT>"),
  220. ("TH\tTag", "TH>$1</TH>"),
  221. ("THEAD\tTag", "THEAD>$1</THEAD>"),
  222. ("TITLE\tTag", "TITLE>$1</TITLE>"),
  223. ("TR\tTag", "TR>$1</TR>"),
  224. ("TT\tTag", "TT>$1</TT>"),
  225. ("U\tTag", "U>$1</U>"),
  226. ("UL\tTag", "UL>$1</UL>"),
  227. ("VAR\tTag", "VAR>$1</VAR>"),
  228. ("BR\tTag", "BR>"),
  229. ("EMBED\tTag", "EMBED>"),
  230. ("HR\tTag", "HR>"),
  231. ("IMG\tTag", "IMG SRC=\"$1\">"),
  232. ("INPUT\tTag", "INPUT>"),
  233. ("META\tTag", "META>"),
  234. ("PARAM\tTag", "PARAM NAME=\"$1\" VALUE=\"$2\">)"),
  235. ("ARTICLE\tTag", "ARTICLE>$1</ARTICLE>"),
  236. ("ASIDE\tTag", "ASIDE>$1</ASIDE>"),
  237. ("AUDIO\tTag", "AUDIO>$1</AUDIO>"),
  238. ("CANVAS\tTag", "CANVAS>$1</CANVAS>"),
  239. ("FOOTER\tTag", "FOOTER>$1</FOOTER>"),
  240. ("HEADER\tTag", "HEADER>$1</HEADER>"),
  241. ("NAV\tTag", "NAV>$1</NAV>"),
  242. ("SECTION\tTag", "SECTION>$1</SECTION>"),
  243. ("VIDEO\tTag", "VIDEO>$1</VIDEO>")
  244. ], sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS)