diff options
author | Adam Stankiewicz <sheerun@sher.pl> | 2020-10-08 00:40:28 +0200 |
---|---|---|
committer | Adam Stankiewicz <sheerun@sher.pl> | 2020-10-08 00:40:28 +0200 |
commit | bb3de8fa5e509d5f5faf173fd1a92635a0023274 (patch) | |
tree | a6401fbc683b8213f008d0564fa40e7f81e1c6f2 /indent | |
parent | b3763a917a6a3ca85ae51086fc8d29b28f66fc88 (diff) | |
download | vim-polyglot-bb3de8fa5e509d5f5faf173fd1a92635a0023274.tar.gz vim-polyglot-bb3de8fa5e509d5f5faf173fd1a92635a0023274.zip |
Fix markdown and other highlighting, #578
Diffstat (limited to 'indent')
-rw-r--r-- | indent/html.vim | 76 |
1 files changed, 48 insertions, 28 deletions
diff --git a/indent/html.vim b/indent/html.vim index 3b9c7eb4..1e56c85e 100644 --- a/indent/html.vim +++ b/indent/html.vim @@ -1,11 +1,10 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'html5') == -1 +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'html') == -1 " Vim indent script for HTML -" Header: "{{{ " Maintainer: Bram Moolenaar " Original Author: Andy Wokula <anwoku@yahoo.de> -" Last Change: 2017 Jun 13 -" Version: 1.0 +" Last Change: 2020 Jul 06 +" Version: 1.0 "{{{ " Description: HTML indent script with cached state for faster indenting on a " range of lines. " Supports template systems through hooks. @@ -58,6 +57,9 @@ let s:cpo_save = &cpo set cpo-=C "}}} +" Pattern to match the name of a tag, including custom elements. +let s:tagname = '\w\+\(-\w\+\)*' + " Check and process settings from b:html_indent and g:html_indent... variables. " Prefer using buffer-local settings over global settings, so that there can " be defaults for all HTML files and exceptions for specific types of HTML @@ -216,25 +218,27 @@ endfunc "}}} " Self-closing tags and tags that are sometimes {{{ " self-closing (e.g., <p>) are not here (when encountering </p> we can find " the matching <p>, but not the other way around). +" Known self-closing tags: " 'p', 'img', 'source', 'area', 'keygen', 'track', +" 'wbr'. " Old HTML tags: call s:AddITags(s:indent_tags, [ \ 'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', \ 'blockquote', 'body', 'button', 'caption', 'center', 'cite', 'code', - \ 'colgroup', 'del', 'dfn', 'dir', 'div', 'dl', 'em', 'fieldset', 'font', + \ 'colgroup', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', \ 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'html', \ 'i', 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', \ 'map', 'menu', 'noframes', 'noscript', 'object', 'ol', - \ 'optgroup', 'p', 'q', 's', 'samp', 'select', 'small', 'span', 'strong', 'sub', + \ 'optgroup', 'q', 's', 'samp', 'select', 'small', 'span', 'strong', 'sub', \ 'sup', 'table', 'textarea', 'title', 'tt', 'u', 'ul', 'var', 'th', 'td', \ 'tr', 'tbody', 'tfoot', 'thead']) " New HTML5 elements: call s:AddITags(s:indent_tags, [ - \ 'area', 'article', 'aside', 'audio', 'bdi', 'canvas', - \ 'command', 'data', 'datalist', 'details', 'dislog', 'embed', 'figcaption', - \ 'figure', 'footer', 'header', 'keygen', 'main', 'mark', 'meter', 'nav', 'output', - \ 'picture', 'progress', 'rp', 'rt', 'ruby', 'section', 'source', 'summary', 'svg', - \ 'time', 'track', 'video', 'wbr']) + \ 'article', 'aside', 'audio', 'bdi', 'canvas', 'command', 'data', + \ 'datalist', 'details', 'dialog', 'embed', 'figcaption', 'figure', + \ 'footer', 'header', 'hgroup', 'main', 'mark', 'meter', 'nav', 'output', + \ 'picture', 'progress', 'rp', 'rt', 'ruby', 'section', 'summary', + \ 'svg', 'time', 'video']) " Tags added for web components: call s:AddITags(s:indent_tags, [ @@ -282,7 +286,7 @@ func! s:CountITags(text) 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\+\)*\>\|<!--\[\|\[endif\]-->\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') + call substitute(a:text, '<\zs/\=' . s:tagname . '\>\|<!--\[\|\[endif\]-->\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') let s:countonly = 0 endfunc "}}} @@ -294,7 +298,7 @@ func! s:CountTagsAndState(text) 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\+\)*\>\|<!--\[\|\[endif\]-->\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') + let tmp = substitute(a:text, '<\zs/\=' . s:tagname . '\>\|<!--\[\|\[endif\]-->\|<!--\|-->', '\=s:CheckTag(submatch(0))', 'g') if s:block == 3 let b:hi_newstate.scripttype = s:GetScriptType(matchstr(tmp, '\C.*<SCRIPT\>\zs[^>]*')) endif @@ -532,7 +536,7 @@ func! s:FreshState(lnum) let swendtag = match(text, '^\s*</') >= 0 " If previous line ended in a closing tag, line up with the opening tag. - if !swendtag && text =~ '</\w\+\s*>\s*$' + if !swendtag && text =~ '</' . s:tagname . '\s*>\s*$' call cursor(state.lnum, 99999) normal! F< let start_lnum = HtmlIndent_FindStartTag() @@ -584,7 +588,7 @@ func! s:Alien3() return eval(b:hi_js1indent) endif if b:hi_indent.scripttype == "javascript" - return GetJavascriptIndent() + return eval(b:hi_js1indent) + GetJavascriptIndent() else return -1 endif @@ -623,7 +627,7 @@ func! s:CSSIndent() return eval(b:hi_css1indent) endif - " If the current line starts with "}" align with it's match. + " If the current line starts with "}" align with its match. if curtext =~ '^\s*}' call cursor(v:lnum, 1) try @@ -661,7 +665,7 @@ func! s:CSSIndent() else let cur_hasfield = curtext =~ '^\s*[a-zA-Z0-9-]\+:' let prev_unfinished = s:CssUnfinished(prev_text) - if !cur_hasfield && (prev_hasfield || prev_unfinished) + if prev_unfinished " Continuation line has extra indent if the previous line was not a " continuation line. let extra = shiftwidth() @@ -714,9 +718,13 @@ func! s:CSSIndent() endfunc "}}} " Inside <style>: Whether a line is unfinished. +" tag: +" tag: blah +" tag: blah && +" tag: blah || func! s:CssUnfinished(text) "{{{ - return a:text =~ '\s\(||\|&&\|:\)\s*$' + return a:text =~ '\(||\|&&\|:\|\k\)\s*$' endfunc "}}} " Search back for the first unfinished line above "lnum". @@ -843,11 +851,12 @@ func! HtmlIndent_FindTagStart(lnum) " - a flag indicating whether we found the end of a tag. " This method is global so that HTML-like indenters can use it. " To avoid matching " > " or " < " inside a string require that the opening - " "<" is followed by a word character + " "<" is followed by a word character and the closing ">" comes after a + " non-white character. let idx = match(getline(a:lnum), '\S>\s*$') if idx > 0 call cursor(a:lnum, idx) - let lnum = searchpair('<\w', '' , '>', 'bW', '', max([a:lnum - b:html_indent_line_limit, 0])) + let lnum = searchpair('<\w', '' , '\S>', 'bW', '', max([a:lnum - b:html_indent_line_limit, 0])) if lnum > 0 return [lnum, 1] endif @@ -861,7 +870,7 @@ func! HtmlIndent_FindStartTag() " The cursor must be on or before a closing tag. " If found, positions the cursor at the match and returns the line number. " Otherwise returns 0. - let tagname = matchstr(getline('.')[col('.') - 1:], '</\zs\w\+\(-\w\+\)*\ze') + let tagname = matchstr(getline('.')[col('.') - 1:], '</\zs' . s:tagname . '\ze') let start_lnum = searchpair('<' . tagname . '\>', '', '</' . tagname . '\>', 'bW') if start_lnum > 0 return start_lnum @@ -877,7 +886,7 @@ func! HtmlIndent_FindTagEnd() " a self-closing tag, to the matching ">". " Limited to look up to b:html_indent_line_limit lines away. let text = getline('.') - let tagname = matchstr(text, '\w\+\(-\w\+\)*\|!--', col('.')) + let tagname = matchstr(text, s:tagname . '\|!--', col('.')) if tagname == '!--' call search('--\zs>') elseif s:get_tag('/' . tagname) != 0 @@ -894,12 +903,19 @@ func! s:InsideTag(foundHtmlString) "{{{ if a:foundHtmlString " Inside an attribute string. - " Align with the previous line or use an external function. + " Align with the opening quote or use an external function. let lnum = v:lnum - 1 if lnum > 1 if exists('b:html_indent_tag_string_func') return b:html_indent_tag_string_func(lnum) endif + " If there is a double quote in the previous line, indent with the + " character after it. + if getline(lnum) =~ '"' + call cursor(lnum, 0) + normal f" + return virtcol('.') + endif return indent(lnum) endif endif @@ -923,17 +939,21 @@ func! s:InsideTag(foundHtmlString) let idx = match(text, '\s\zs[_a-zA-Z0-9-]\+="') endif if idx == -1 - let idx = match(text, '<\w\+\(-\w\+\)*\s\zs\w') + " try <tag attr + let idx = match(text, '<' . s:tagname . '\s\+\zs\w') endif if idx == -1 - let idx = match(text, '<\w\+\(-\w\+\)*') + " after just "<tag" indent one level more + let idx = match(text, '<' . s:tagname . '$') if idx >= 0 - let idx = idx + shiftwidth() + call cursor(lnum, idx) + return virtcol('.') + shiftwidth() endif endif if idx > 0 - " Found the attribute. TODO: assumes spaces, no Tabs. - return idx + " Found the attribute to align with. + call cursor(lnum, idx) + return virtcol('.') endif endwhile return -1 |