├── README.md ├── indent ├── html.vim ├── html2.vim ├── javascript.vim └── typescript.vim ├── test.js └── test_script.html /README.md: -------------------------------------------------------------------------------- 1 | vim-js-indent 2 | ============= 3 | 4 | Vim indenter for standalone and embedded JavaScript and TypeScript. 5 | 6 | Installation 7 | ------------ 8 | 9 | ### vim-plug 10 | 11 | 1. Add `Plug 'jason0x43/vim-js-indent'` to your `.vimrc` 12 | 1. Restart vim 13 | 1. Run `:PlugInstall` 14 | 15 | ### Pathogen 16 | 17 | Clone `https://github.com/jason0x43/vim-js-indent.git` into your bundles 18 | directory (`~/.vim/bundle`). 19 | 20 | Configuration 21 | ------------- 22 | 23 |
js_indent_flat_switch
js_indent_logging
js_indent_typescript
) are not here (when encountering
we can find 226 | " the matching, but not the other way around). 227 | " Old HTML tags: 228 | call s:AddITags(s:indent_tags, [ 229 | \ 'a', 'abbr', 'acronym', 'address', 'b', 'bdo', 'big', 230 | \ 'blockquote', 'body', 'button', 'caption', 'center', 'cite', 'code', 231 | \ 'colgroup', 'del', 'dfn', 'dir', 'div', 'dl', 'em', 'fieldset', 'font', 232 | \ 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'html', 233 | \ 'i', 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', 234 | \ 'map', 'menu', 'noframes', 'noscript', 'object', 'ol', 235 | \ 'optgroup', 'q', 's', 'samp', 'select', 'small', 'span', 'strong', 'sub', 236 | \ 'sup', 'table', 'textarea', 'title', 'tt', 'u', 'ul', 'var', 'th', 'td', 237 | \ 'tr', 'tbody', 'tfoot', 'thead']) 238 | 239 | " Tags added 2011 Sep 09 (especially HTML5 tags): 240 | call s:AddITags(s:indent_tags, [ 241 | \ 'area', 'article', 'aside', 'audio', 'bdi', 'canvas', 242 | \ 'command', 'datalist', 'details', 'embed', 'figure', 'footer', 243 | \ 'header', 'group', 'keygen', 'mark', 'math', 'meter', 'nav', 'output', 244 | \ 'progress', 'ruby', 'section', 'svg', 'texture', 'time', 'video', 245 | \ 'wbr', 'text']) 246 | 247 | " Tags added for web components: 248 | call s:AddITags(s:indent_tags, [ 249 | \ 'content', 'shadow', 'template']) 250 | "}}} 251 | 252 | " Add Block Tags: these contain alien content 253 | "{{{ 254 | call s:AddBlockTag('pre', 2) 255 | call s:AddBlockTag('script', 3) 256 | call s:AddBlockTag('style', 4) 257 | call s:AddBlockTag('') 258 | "}}} 259 | 260 | " Return non-zero when "tagname" is an opening tag, not being a block tag, for 261 | " which there should be a closing tag. Can be used by scripts that include 262 | " HTML indenting. 263 | func! HtmlIndent_IsOpenTag(tagname) 264 | "{{{ 265 | if get(s:indent_tags, a:tagname) == 1 266 | return 1 267 | endif 268 | return get(b:hi_tags, a:tagname) == 1 269 | endfunc "}}} 270 | 271 | " Get the value for "tagname", taking care of buffer-local tags. 272 | func! s:get_tag(tagname) 273 | "{{{ 274 | let i = get(s:indent_tags, a:tagname) 275 | if (i == 1 || i == -1) && get(b:hi_removed_tags, a:tagname) != 0 276 | return 0 277 | endif 278 | if i == 0 279 | let i = get(b:hi_tags, a:tagname) 280 | endif 281 | return i 282 | endfunc "}}} 283 | 284 | " Count the number of start and end tags in "text". 285 | func! s:CountITags(text) 286 | "{{{ 287 | " Store the result in s:curind and s:nextrel. 288 | let s:curind = 0 " relative indent steps for current line [unit &sw]: 289 | let s:nextrel = 0 " relative indent steps for next line [unit &sw]: 290 | let s:block = 0 " assume starting outside of a block 291 | let s:countonly = 1 " don't change state 292 | call substitute(a:text, '<\zs/\=\w\+\(-\w\+\)*\>\|', '\=s:CheckTag(submatch(0))', 'g') 293 | let s:countonly = 0 294 | endfunc "}}} 295 | 296 | " Count the number of start and end tags in text. 297 | func! s:CountTagsAndState(text) 298 | "{{{ 299 | " Store the result in s:curind and s:nextrel. Update b:hi_newstate.block. 300 | let s:curind = 0 " relative indent steps for current line [unit &sw]: 301 | let s:nextrel = 0 " relative indent steps for next line [unit &sw]: 302 | 303 | let s:block = b:hi_newstate.block 304 | let tmp = substitute(a:text, '<\zs/\=\w\+\(-\w\+\)*\>\|', '\=s:CheckTag(submatch(0))', 'g') 305 | if s:block == 3 306 | let b:hi_newstate.scripttype = s:GetScriptType(matchstr(tmp, '\C.*) 456 | " if comment opener found, 457 | " assume a:lnum within comment 458 | " else 459 | " assume usual html for a:lnum 460 | " if a:lnum-1 has a closing comment 461 | " look back to get indent of comment opener 462 | " FI 463 | 464 | " look back for a blocktag 465 | call cursor(a:lnum, 1) 466 | let [stopline, stopcol] = searchpos('\c<\zs\/\=\%(pre\>\|script\>\|style\>\)', "bW") 467 | if stopline > 0 468 | " fugly ... why isn't there searchstr() 469 | let tagline = tolower(getline(stopline)) 470 | let blocktag = matchstr(tagline, '\/\=\%(pre\>\|script\>\|style\>\)', stopcol - 1) 471 | if blocktag[0] != "/" 472 | " opening tag found, assume a:lnum within block 473 | let state.block = s:indent_tags[blocktag] 474 | if state.block == 3 475 | let state.scripttype = s:GetScriptType(matchstr(tagline, '\>[^>]*', stopcol)) 476 | endif 477 | let state.blocklnr = stopline 478 | " check preceding tags in the line: 479 | call s:CountITags(tagline[: stopcol-2]) 480 | let state.blocktagind = indent(stopline) + (s:curind + s:nextrel) * s:ShiftWidth() 481 | return state 482 | elseif stopline == state.lnum 483 | " handle special case: previous line (= state.lnum) contains a 484 | " closing blocktag which is preceded by line-noise; 485 | " blocktag == "/..." 486 | let swendtag = match(tagline, '^\s*') >= 0 487 | if !swendtag 488 | let [bline, bcol] = searchpos('<'.blocktag[1:].'\>', "bW") 489 | call s:CountITags(tolower(getline(bline)[: bcol-2])) 490 | let state.baseindent = indent(bline) + (s:curind + s:nextrel) * s:ShiftWidth() 491 | return state 492 | endif 493 | endif 494 | endif 495 | 496 | " else look back for comment 497 | call cursor(a:lnum, 1) 498 | let [comlnum, comcol, found] = searchpos('\(', 'bpW', stopline) 499 | if found == 2 500 | " comment opener found, assume a:lnum within comment 501 | let state.block = 5 502 | let state.blocklnr = comlnum 503 | " check preceding tags in the line: 504 | call s:CountITags(tolower(getline(comlnum)[: comcol-2])) 505 | let state.blocktagind = indent(comlnum) + (s:curind + s:nextrel) * s:ShiftWidth() 506 | return state 507 | endif 508 | 509 | " else within usual HTML 510 | let text = tolower(getline(state.lnum)) 511 | 512 | " Check a:lnum-1 for closing comment (we need indent from the opening line). 513 | " Not when other tags follow (might be --> inside a string). 514 | let comcol = stridx(text, '-->') 515 | if comcol >= 0 && match(text, '[<>]', comcol) <= 0 516 | call cursor(state.lnum, comcol + 1) 517 | let [comlnum, comcol] = searchpos('" 526 | return state 527 | endif 528 | 529 | " Check if the previous line starts with end tag. 530 | let swendtag = match(text, '^\s*') >= 0 531 | 532 | " If previous line ended in a closing tag, line up with the opening tag. 533 | if !swendtag && text =~ '\w\+\s*>\s*$' 534 | call cursor(state.lnum, 99999) 535 | normal! F< 536 | let start_lnum = HtmlIndent_FindStartTag() 537 | if start_lnum > 0 538 | let state.baseindent = indent(start_lnum) 539 | if col('.') > 2 540 | " check for tags before the matching opening tag. 541 | let text = getline(start_lnum) 542 | let swendtag = match(text, '^\s*') >= 0 543 | call s:CountITags(text[: col('.') - 2]) 544 | let state.baseindent += s:nextrel * s:ShiftWidth() 545 | if !swendtag 546 | let state.baseindent += s:curind * s:ShiftWidth() 547 | endif 548 | endif 549 | return state 550 | endif 551 | endif 552 | 553 | " Else: no comments. Skip backwards to find the tag we're inside. 554 | let [state.lnum, found] = HtmlIndent_FindTagStart(state.lnum) 555 | " Check if that line starts with end tag. 556 | let text = getline(state.lnum) 557 | let swendtag = match(text, '^\s*') >= 0 558 | call s:CountITags(tolower(text)) 559 | let state.baseindent = indent(state.lnum) + s:nextrel * s:ShiftWidth() 560 | if !swendtag 561 | let state.baseindent += s:curind * s:ShiftWidth() 562 | endif 563 | return state 564 | endfunc "}}} 565 | 566 | " Indent inside a
block: Keep indent as-is. 567 | func! s:Alien2() 568 | "{{{ 569 | return -1 570 | endfunc "}}} 571 | 572 | " Return the indent inside a 123 |