Philip H | d3ff129 | 2024-04-21 15:44:10 +0200 | [diff] [blame] | 1 | " Vim indent file (experimental). |
| 2 | " Language: Astro |
| 3 | " Author: Wuelner Martínez <wuelner.martinez@outlook.com> |
| 4 | " Maintainer: Wuelner Martínez <wuelner.martinez@outlook.com> |
| 5 | " URL: https://github.com/wuelnerdotexe/vim-astro |
| 6 | " Last Change: 2022 Aug 07 |
| 7 | " Based On: Evan Lecklider's vim-svelte |
| 8 | " Changes: See https://github.com/evanleck/vim-svelte |
| 9 | " Credits: See vim-svelte on github |
| 10 | |
| 11 | " Only load this indent file when no other was loaded yet. |
| 12 | if exists('b:did_indent') |
| 13 | finish |
| 14 | endif |
| 15 | |
| 16 | let b:html_indent_script1 = 'inc' |
| 17 | let b:html_indent_style1 = 'inc' |
| 18 | |
| 19 | " Embedded HTML indent. |
| 20 | runtime! indent/html.vim |
| 21 | let s:html_indent = &l:indentexpr |
| 22 | unlet b:did_indent |
| 23 | |
| 24 | let b:did_indent = 1 |
| 25 | |
| 26 | setlocal indentexpr=GetAstroIndent() |
| 27 | setlocal indentkeys=<>>,/,0{,{,},0},0),0],0\,<<>,,!^F,*<Return>,o,O,e,; |
| 28 | |
| 29 | let b:undo_indent = 'setl inde< indk<' |
| 30 | |
| 31 | " Only define the function once. |
| 32 | if exists('*GetAstroIndent') |
| 33 | finish |
| 34 | endif |
| 35 | |
| 36 | let s:cpoptions_save = &cpoptions |
| 37 | setlocal cpoptions&vim |
| 38 | |
| 39 | function! GetAstroIndent() |
| 40 | let l:current_line_number = v:lnum |
| 41 | |
| 42 | if l:current_line_number == 0 |
| 43 | return 0 |
| 44 | endif |
| 45 | |
| 46 | let l:current_line = getline(l:current_line_number) |
| 47 | |
| 48 | if l:current_line =~ '^\s*</\?\(script\|style\)' |
| 49 | return 0 |
| 50 | endif |
| 51 | |
| 52 | let l:previous_line_number = prevnonblank(l:current_line_number - 1) |
| 53 | let l:previous_line = getline(l:previous_line_number) |
| 54 | let l:previous_line_indent = indent(l:previous_line_number) |
| 55 | |
| 56 | if l:previous_line =~ '^\s*</\?\(script\|style\)' |
| 57 | return l:previous_line_indent + shiftwidth() |
| 58 | endif |
| 59 | |
| 60 | execute 'let l:indent = ' . s:html_indent |
| 61 | |
| 62 | if searchpair('<style>', '', '</style>', 'bW') && |
| 63 | \ l:previous_line =~ ';$' && l:current_line !~ '}' |
| 64 | return l:previous_line_indent |
| 65 | endif |
| 66 | |
| 67 | if synID(l:previous_line_number, match( |
| 68 | \ l:previous_line, '\S' |
| 69 | \ ) + 1, 0) == hlID('htmlTag') && synID(l:current_line_number, match( |
| 70 | \ l:current_line, '\S' |
| 71 | \ ) + 1, 0) != hlID('htmlEndTag') |
| 72 | let l:indents_match = l:indent == l:previous_line_indent |
| 73 | let l:previous_closes = l:previous_line =~ '/>$' |
| 74 | |
| 75 | if l:indents_match && |
| 76 | \ !l:previous_closes && l:previous_line =~ '<\(\u\|\l\+:\l\+\)' |
| 77 | return l:previous_line_indent + shiftwidth() |
| 78 | elseif !l:indents_match && l:previous_closes |
| 79 | return l:previous_line_indent |
| 80 | endif |
| 81 | endif |
| 82 | |
| 83 | return l:indent |
| 84 | endfunction |
| 85 | |
| 86 | let &cpoptions = s:cpoptions_save |
| 87 | unlet s:cpoptions_save |
| 88 | " vim: ts=8 |