xref: /vim-8.2.3635/runtime/indent/php.vim (revision 6bb68366)
1" Vim indent file
2" Language:	PHP
3" Author:	Miles Lott <[email protected]>
4" URL:		http://milosch.dyndns.org/php.vim
5" Last Change:	2005 Mar 21
6" Version:	0.6
7" Notes:  Close all switches with default:\nbreak; and it will look better.
8"         Also, open and close brackets should be alone on a line.
9"         This is my preference, and the only way this will look nice.
10"         Try an older version if you care less about the formatting of
11"         switch/case.  It is nearly perfect for anyone regardless of your
12"         stance on brackets.
13"
14" Changes: 0.6 - fix indention for closing bracket (patch from [email protected])
15"          0.5 - fix duplicate indent on open tag, and empty bracketed statements.
16"          0.4 - Fixes for closing php tag, switch statement closure, and php_indent_shortopentags
17"          option from Steffen Bruentjen <[email protected]>
18"
19" Options: php_noindent_switch=1 -- do not try to indent switch/case statements (version 0.1 behavior)
20"          php_indent_shortopentags=1 -- indent after short php open tags, too
21
22" Only load this indent file when no other was loaded.
23if exists("b:did_indent")
24	finish
25endif
26let b:did_indent = 1
27
28setlocal indentexpr=GetPhpIndent()
29"setlocal indentkeys+=0=,0),=EO
30setlocal indentkeys+=0=,0),=EO,=>
31
32" Only define the function once.
33if exists("*GetPhpIndent")
34	finish
35endif
36
37" Handle option(s)
38if exists("php_noindent_switch")
39	let b:php_noindent_switch=1
40endif
41
42function GetPhpIndent()
43	" Find a non-blank line above the current line.
44	let lnum = prevnonblank(v:lnum - 1)
45	" Hit the start of the file, use zero indent.
46	if lnum == 0
47		return 0
48	endif
49	let line = getline(lnum)    " last line
50	let cline = getline(v:lnum) " current line
51	let pline = getline(lnum - 1) " previous to last line
52	let ind = indent(lnum)
53
54	" Indent after php open tag
55	if line =~ '<?php'
56		let ind = ind + &sw
57	elseif exists('g:php_indent_shortopentags')
58		" indent after short open tag
59		if line =~ '<?'
60			let ind = ind + &sw
61		endif
62	endif
63	" indent after php closing tag
64	if cline =~ '\M?>'
65		let ind = ind - &sw
66	endif
67
68	if exists("b:php_noindent_switch") " version 1 behavior, diy switch/case,etc
69		" Indent blocks enclosed by {} or ()
70		if line =~ '[{(]\s*\(#[^)}]*\)\=$'
71			let ind = ind + &sw
72		endif
73		if cline =~ '^\s*[)}]'
74			let ind = ind - &sw
75		endif
76		return ind
77	else
78		" Search the matching bracket (with searchpair()) and set the indent of
79		" to the indent of the matching line.
80		if cline =~ '^\s*}'
81			call cursor(line('.'), 1)
82			let ind = indent(searchpair('{', '', '}','bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
83			return ind
84		endif
85		" Try to indent switch/case statements as well
86		" Indent blocks enclosed by {} or () or case statements, with some anal requirements
87		if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$'
88			let ind = ind + &sw
89			" return if the current line is not another case statement of the previous line is a bracket open
90			if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$'
91				return ind
92			endif
93		endif
94		if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]'
95			let ind = ind - &sw
96			" if the last line is a break or return, or the current line is a close bracket,
97			" or if the previous line is a default statement, subtract another
98			if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:'
99				let ind = ind - &sw
100			endif
101		endif
102		" Search the matching bracket (with searchpair()) and set the indent of cline
103		" to the indent of the matching line.
104		if cline =~ '^\s*}'
105			call cursor(line('. '), 1)
106			let ind = indent(searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
107			return ind
108		endif
109
110		if line =~ 'default:'
111			let ind = ind + &sw
112		endif
113		return ind
114	endif
115endfunction
116" vim: set ts=4 sw=4:
117