diff options
Diffstat (limited to 'indent/blade.vim')
-rw-r--r-- | indent/blade.vim | 94 |
1 files changed, 55 insertions, 39 deletions
diff --git a/indent/blade.vim b/indent/blade.vim index 97f7f293..95cc00a4 100644 --- a/indent/blade.vim +++ b/indent/blade.vim @@ -4,22 +4,22 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'blade') == -1 " Language: Blade (Laravel) " Maintainer: Jason Walton <jwalton512@gmail.com> -if exists("b:did_indent") +if exists('b:did_indent') finish endif runtime! indent/html.vim let s:htmlindent = &indentexpr -unlet! b:did_indent +unlet! b:did_indent runtime! indent/php.vim let s:phpindent = &indentexpr -unlet! b:did_indent let b:did_indent = 1 " Doesn't include 'foreach' and 'forelse' because these already get matched by 'for'. -let s:directives_start = 'if\|else\|unless\|for\|while\|empty\|push\|section\|can\|hasSection\|verbatim' +let s:directives_start = 'if\|else\|unless\|for\|while\|empty\|push\|section\|can\|hasSection\|verbatim\|php\|' . + \ 'component\|slot\|prepend' let s:directives_end = 'else\|end\|empty\|show\|stop\|append\|overwrite' if exists('g:blade_custom_directives_pairs') @@ -29,55 +29,71 @@ endif setlocal autoindent setlocal indentexpr=GetBladeIndent() -exe "setlocal indentkeys=o,O,<>>,!^F,0=}},0=!!},=@" . substitute(s:directives_end, '\\|', ',=@', 'g') +exe 'setlocal indentkeys=o,O,<>>,!^F,0=}},0=!!},=@' . substitute(s:directives_end, '\\|', ',=@', 'g') " Only define the function once. -if exists("*GetBladeIndent") +if exists('*GetBladeIndent') finish endif +function! s:IsStartingDelimiter(lnum) + let line = getline(a:lnum) + return line =~# '\%(\w\|@\)\@<!@\%(' . s:directives_start . '\)\%(.*@end\|.*@stop\)\@!' + \ || line =~# '{{\%(.*}}\)\@!' + \ || line =~# '{!!\%(.*!!}\)\@!' + \ || line =~# '<?\%(.*?>\)\@!' +endfunction + function! GetBladeIndent() - let lnum = prevnonblank(v:lnum-1) + let lnum = prevnonblank(v:lnum - 1) if lnum == 0 return 0 endif - let line = substitute(substitute(getline(lnum), '\s\+$', '', ''), '^\s\+', '', '') - let cline = substitute(substitute(getline(v:lnum), '\s\+$', '', ''), '^\s\+', '', '') + let line = getline(lnum) + let cline = getline(v:lnum) let indent = indent(lnum) - if cline =~# '@\%(' . s:directives_end . '\)' || - \ cline =~# '\%(<?.*\)\@<!?>\|\%({{.*\)\@<!}}\|\%({!!.*\)\@<!!!}' - let indent = indent - &sw - elseif line =~# '<?\%(.*?>\)\@!\|@php\%(\s*(\)\@!' - let indent = indent + &sw - else - if exists("*GetBladeIndentCustom") - let hindent = GetBladeIndentCustom() - " Don't use PHP indentation if line is a comment - elseif line !~# '^\s*\%(#\|//\)\|\*/\s*$' && ( - \ searchpair('@include\%(If\)\?\s*(', '', ')', 'bWr') || - \ searchpair('{!!', '', '!!}', 'bWr') || - \ searchpair('{{', '', '}}', 'bWr') || - \ searchpair('<?', '', '?>', 'bWr') || - \ searchpair('@php\%(\s*(\)\@!', '', '@endphp', 'bWr') ) - execute 'let hindent = ' . s:phpindent - else - execute 'let hindent = ' . s:htmlindent - endif - if hindent > -1 - let indent = hindent - endif - endif - let increase = indent + &sw - if line =~# '@\%(section\)\%(.*@end\)\@!' && line !~# '@\%(section\)\s*([^,]*)' - return indent - elseif line =~# '@\%(' . s:directives_start . '\)\%(.*@end\|.*@stop\)\@!' || - \ line =~# '{{\%(.*}}\)\@!' || line =~# '{!!\%(.*!!}\)\@!' - return increase - else + " 1. Check for special directives + " @section is a single-line directive if it has a second argument. + " @php is a single-line directive if it is followed by parentheses. + if (line =~# '@section\%(.*@end\)\@!' && line !~# '@section\s*([^,]*)') + \ || line =~# '@php\s*(' return indent endif + + " 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 endfunction endif |