I'm new to Vim and trying to get HTML to indent properly with web components
has been, well, impossible.
This patch makes indent/html.vim aware of new HTML5 elements and support for
custom elements (hyphenated tags):
* <template> http://www.w3.org/TR/html5/scripting-1.html
* <content>, <shadow> http://www.w3.org/TR/shadow-dom/
* <custom-element-tags> http://www.w3.org/TR/custom-elements/
so that the descendants of these elements indent properly.
This goes beyond just adding tags to g:html_indent_inctags because with custom
elements, the set of possible tag names is virtually infinite.
For more information, see http://webcomponents.org/.
Please keep in mind I don't have a solid grasp of vimscript so feel free to
explain why this patch is awful :-)
Thanks,
Michael
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
*** html.vim.old Mon Feb 23 01:17:03 2015
--- html.vim.new Mon Feb 23 01:17:37 2015
***************
*** 245,250 ****
--- 245,254 ----
\ 'header', 'group', 'keygen', 'mark', 'math', 'meter', 'nav', 'output',
\ 'progress', 'ruby', 'section', 'svg', 'texture', 'time', 'video',
\ 'wbr', 'text'])
+
+ " Tags added for web components:
+ call s:AddITags(s:indent_tags, [
+ \ 'content', 'shadow', 'template'])
"}}}
" Add Block Tags: these contain alien content
***************
*** 287,293 ****
let s:nextrel = 0 " relative indent steps for next line [unit &sw]:
let s:block = 0 " assume starting outside of a block
let s:countonly = 1 " don't change state
! call substitute(a:text, '<\zs/\=\w\+\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g')
let s:countonly = 0
endfunc "}}}
--- 291,297 ----
let s:nextrel = 0 " relative indent steps for next line [unit &sw]:
let s:block = 0 " assume starting outside of a block
let s:countonly = 1 " don't change state
! call substitute(a:text, '<\zs/\=\w\+\(-\w\+\)*\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g')
let s:countonly = 0
endfunc "}}}
***************
*** 299,305 ****
let s:nextrel = 0 " relative indent steps for next line [unit &sw]:
let s:block = b:hi_newstate.block
! let tmp = substitute(a:text, '<\zs/\=\w\+\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g')
if s:block == 3
let b:hi_newstate.scripttype = s:GetScriptType(matchstr(tmp, '\C.*<SCRIPT\>\zs[^>]*'))
endif
--- 303,309 ----
let s:nextrel = 0 " relative indent steps for next line [unit &sw]:
let s:block = b:hi_newstate.block
! let tmp = substitute(a:text, '<\zs/\=\w\+\(-\w\+\)*\>\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g')
if s:block == 3
let b:hi_newstate.scripttype = s:GetScriptType(matchstr(tmp, '\C.*<SCRIPT\>\zs[^>]*'))
endif
***************
*** 311,316 ****
--- 315,323 ----
"{{{
" Returns an empty string or "SCRIPT".
" a:itag can be "tag" or "/tag" or "<!--" or "-->"
+ if (s:CheckCustomTag(a:itag))
+ return ""
+ endif
let ind = s:get_tag(a:itag)
if ind == -1
" closing tag
***************
*** 363,368 ****
--- 370,405 ----
" we get here if starting and closing a block-tag on the same line
endif
return ""
+ endfunc "}}}
+
+ " Used by s:CheckTag().
+ func! s:CheckCustomTag(ctag)
+ "{{{
+ " Returns 1 if ctag is the tag for a custom element, 0 otherwise.
+ " c:itag can be "tag" or "/tag" or "<!--" or "-->"
+ let pattern = "\\%\\(\\w\\+-\\)\\+\\w\\+"
+ if match(a:ctag, pattern) == -1
+ return 0
+ endif
+ if matchstr(a:ctag, "\\/\\ze.\\+") == "/"
+ " closing tag
+ if s:block != 0
+ " ignore ctag within a block
+ return 1
+ endif
+ if s:nextrel == 0
+ let s:curind -= 1
+ else
+ let s:nextrel -= 1
+ endif
+ else
+ " opening tag
+ if s:block != 0
+ return 1
+ endif
+ let s:nextrel += 1
+ endif
+ return 1
endfunc "}}}
" Return the <script> type: either "javascript" or ""