From 1a096f0901264629947afb89436019895e5e7ef5 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Wed, 9 Jun 2021 16:54:25 +0200 Subject: Fix script indentation inside html, closes #693 --- indent/html.vim | 78 +++++++++++++++++++++------------------------------------ 1 file changed, 29 insertions(+), 49 deletions(-) (limited to 'indent') diff --git a/indent/html.vim b/indent/html.vim index 96b32b62..176e69c5 100644 --- a/indent/html.vim +++ b/indent/html.vim @@ -1,12 +1,13 @@ -if polyglot#init#is_disabled(expand(':p'), 'html', 'indent/html.vim') +if polyglot#init#is_disabled(expand(':p'), 'html5', 'indent/html.vim') finish endif " Vim indent script for HTML +" Header: "{{{ " Maintainer: Bram Moolenaar " Original Author: Andy Wokula -" Last Change: 2021 Jan 26 -" Version: 1.0 "{{{ +" Last Change: 2017 Jun 13 +" Version: 1.0 " Description: HTML indent script with cached state for faster indenting on a " range of lines. " Supports template systems through hooks. @@ -59,9 +60,6 @@ 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 @@ -220,27 +218,25 @@ endfunc "}}} " Self-closing tags and tags that are sometimes {{{ " self-closing (e.g.,

) are not here (when encountering

we can find " the matching

, 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', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'fieldset', 'font', + \ 'colgroup', 'del', 'dfn', 'dir', 'div', 'dl', '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', 'q', 's', 'samp', 'select', 'small', 'span', 'strong', 'sub', + \ 'optgroup', 'p', '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, [ - \ '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']) + \ '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']) " Tags added for web components: call s:AddITags(s:indent_tags, [ @@ -288,7 +284,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/\=' . s:tagname . '\>\|\|', '\=s:CheckTag(submatch(0))', 'g') + call substitute(a:text, '<\zs/\=\w\+\(-\w\+\)*\>\|\|', '\=s:CheckTag(submatch(0))', 'g') let s:countonly = 0 endfunc "}}} @@ -300,7 +296,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/\=' . s:tagname . '\>\|\|', '\=s:CheckTag(submatch(0))', 'g') + 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.*\zs[^>]*')) endif @@ -538,7 +534,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 =~ '\s*$' + if !swendtag && text =~ '\s*$' call cursor(state.lnum, 99999) normal! F< let start_lnum = HtmlIndent_FindStartTag() @@ -590,7 +586,7 @@ func! s:Alien3() return eval(b:hi_js1indent) endif if b:hi_indent.scripttype == "javascript" - return eval(b:hi_js1indent) + GetJavascriptIndent() + return GetJavascriptIndent() else return -1 endif @@ -629,7 +625,7 @@ func! s:CSSIndent() return eval(b:hi_css1indent) endif - " If the current line starts with "}" align with its match. + " If the current line starts with "}" align with it's match. if curtext =~ '^\s*}' call cursor(v:lnum, 1) try @@ -667,7 +663,7 @@ func! s:CSSIndent() else let cur_hasfield = curtext =~ '^\s*[a-zA-Z0-9-]\+:' let prev_unfinished = s:CssUnfinished(prev_text) - if prev_unfinished + if !cur_hasfield && (prev_hasfield || prev_unfinished) " Continuation line has extra indent if the previous line was not a " continuation line. let extra = shiftwidth() @@ -720,13 +716,9 @@ func! s:CSSIndent() endfunc "}}} " Inside