vim-polyglot/indent/blade.vim

100 lines
3.3 KiB
VimL
Raw Normal View History

if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'blade') == -1
2016-01-22 03:08:00 -05:00
" Vim indent file
" Language: Blade (Laravel)
" Maintainer: Jason Walton <jwalton512@gmail.com>
2015-05-11 09:05:13 -04:00
2017-03-23 06:28:19 -04:00
if exists('b:did_indent')
2016-01-22 03:08:00 -05:00
finish
2015-05-11 09:05:13 -04:00
endif
2016-06-26 12:03:28 -04:00
2015-05-11 09:05:13 -04:00
runtime! indent/html.vim
2016-06-26 12:03:28 -04:00
let s:htmlindent = &indentexpr
2017-03-23 06:28:19 -04:00
unlet! b:did_indent
2016-06-26 12:03:28 -04:00
runtime! indent/php.vim
let s:phpindent = &indentexpr
2016-01-22 03:08:00 -05:00
let b:did_indent = 1
2015-05-11 09:05:13 -04:00
2016-07-19 04:09:54 -04:00
" Doesn't include 'foreach' and 'forelse' because these already get matched by 'for'.
2017-03-23 06:28:19 -04:00
let s:directives_start = 'if\|else\|unless\|for\|while\|empty\|push\|section\|can\|hasSection\|verbatim\|php\|' .
2017-11-19 15:26:59 -05:00
\ 'component\|slot\|prepend\|auth\|guest'
2016-07-19 04:09:54 -04:00
let s:directives_end = 'else\|end\|empty\|show\|stop\|append\|overwrite'
if exists('g:blade_custom_directives_pairs')
let s:directives_start .= '\|' . join(keys(g:blade_custom_directives_pairs), '\|')
let s:directives_end .= '\|' . join(values(g:blade_custom_directives_pairs), '\|')
endif
2016-01-22 03:08:00 -05:00
setlocal autoindent
setlocal indentexpr=GetBladeIndent()
2017-03-23 06:28:19 -04:00
exe 'setlocal indentkeys=o,O,<>>,!^F,0=}},0=!!},=@' . substitute(s:directives_end, '\\|', ',=@', 'g')
2015-05-11 09:05:13 -04:00
2016-01-22 03:08:00 -05:00
" Only define the function once.
2017-03-23 06:28:19 -04:00
if exists('*GetBladeIndent')
2016-01-22 03:08:00 -05:00
finish
endif
2015-05-11 09:05:13 -04:00
2017-03-23 06:28:19 -04:00
function! s:IsStartingDelimiter(lnum)
let line = getline(a:lnum)
return line =~# '\%(\w\|@\)\@<!@\%(' . s:directives_start . '\)\%(.*@end\|.*@stop\)\@!'
\ || line =~# '{{\%(.*}}\)\@!'
\ || line =~# '{!!\%(.*!!}\)\@!'
\ || line =~# '<?\%(.*?>\)\@!'
endfunction
2016-01-22 03:08:00 -05:00
function! GetBladeIndent()
2017-03-23 06:28:19 -04:00
let lnum = prevnonblank(v:lnum - 1)
2016-01-22 03:08:00 -05:00
if lnum == 0
return 0
endif
2017-03-23 06:28:19 -04:00
let line = getline(lnum)
let cline = getline(v:lnum)
2016-01-22 03:08:00 -05:00
let indent = indent(lnum)
2017-03-23 06:28:19 -04:00
" 1. Check for special directives
2019-03-04 03:15:44 -05:00
" @section and @slot are single-line if they have a second argument.
2017-03-23 06:28:19 -04:00
" @php is a single-line directive if it is followed by parentheses.
2019-03-04 03:15:44 -05:00
if (line =~# '@\%(section\|slot\)\%(.*@end\)\@!' && line !~# '@\%(section\|slot\)\s*([^,]*)')
2017-03-23 06:28:19 -04:00
\ || line =~# '@php\s*('
2016-01-22 03:08:00 -05:00
return indent
endif
2017-03-23 06:28:19 -04:00
" 2. When the current line is an ending delimiter: decrease indentation
" if the previous line wasn't a starting delimiter.
if cline =~# '^\s*@\%(' . s:directives_end . '\)'
\ || cline =~# '\%(<?.*\)\@<!?>'
\ || cline =~# '\%({{.*\)\@<!}}'
\ || cline =~# '\%({!!.*\)\@<!!!}'
return s:IsStartingDelimiter(lnum) ? indent : indent - &sw
endif
" 3. Increase indentation if the line contains a starting delimiter.
if s:IsStartingDelimiter(lnum)
return indent + &sw
endif
" 4. External indent scripts (PHP and HTML)
execute 'let indent = ' . s:htmlindent
if exists('*GetBladeIndentCustom')
let indent = GetBladeIndentCustom()
elseif line !~# '^\s*\%(#\|//\)\|\*/\s*$' && (
\ searchpair('@include\%(If\)\?\s*(', '', ')', 'bWr') ||
\ searchpair('{!!', '', '!!}', 'bWr') ||
\ searchpair('{{', '', '}}', 'bWr') ||
\ searchpair('<?', '', '?>', 'bWr') ||
\ searchpair('@php\s*(\@!', '', '@endphp', 'bWr') )
" Only use PHP's indent if the region spans multiple lines
if !s:IsStartingDelimiter(v:lnum)
execute 'let indent = ' . s:phpindent
endif
endif
return indent
2016-01-22 03:08:00 -05:00
endfunction
endif