xref: /vim-8.2.3635/runtime/indent/php.vim (revision 19a09a18)
1" Vim indent file
2" Language:	PHP
3" Author:	Miles Lott <[email protected]>
4" URL:		http://milosch.dyndns.org/php.vim
5" Last Change:	2004 May 18
6" Version:	0.4
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: Fixes for closing php tag, switch statement closure, and php_indent_shortopentags
15"          option from Steffen Bruentjen <[email protected]>
16"
17" Options: php_noindent_switch=1 -- do not try to indent switch/case statements (version 0.1 behavior)
18"          php_indent_shortopentags=1 -- indent after short php open tags, too
19
20" Only load this indent file when no other was loaded.
21if exists("b:did_indent")
22	finish
23endif
24let b:did_indent = 1
25
26setlocal indentexpr=GetPhpIndent()
27"setlocal indentkeys+=0=,0),=EO
28setlocal indentkeys+=0=,0),=EO,=>
29
30" Only define the function once.
31if exists("*GetPhpIndent")
32	finish
33endif
34
35" Handle option(s)
36if exists("php_noindent_switch")
37	let b:php_noindent_switch=1
38endif
39
40function GetPhpIndent()
41	" Find a non-blank line above the current line.
42	let lnum = prevnonblank(v:lnum - 1)
43	" Hit the start of the file, use zero indent.
44	if lnum == 0
45		return 0
46	endif
47	let line = getline(lnum)    " last line
48	let cline = getline(v:lnum) " current line
49	let pline = getline(lnum - 1) " previous to last line
50	let ind = indent(lnum)
51
52	" Indent after php open tags
53	if line =~ '<?php'
54		let ind = ind + &sw
55		" indent after short open tags
56	endif
57	if exists('g:php_indent_shortopentags')
58		if line =~ '<?'
59			let ind = ind + &sw
60		endif
61	endif
62	if cline =~ '\M?>'
63		let ind = ind - &sw
64	endif
65
66	if exists("b:php_noindent_switch") " version 1 behavior, diy switch/case,etc
67		" Indent blocks enclosed by {} or ()
68		if line =~ '[{(]\s*\(#[^)}]*\)\=$'
69			let ind = ind + &sw
70		endif
71		if cline =~ '^\s*[)}]'
72			let ind = ind - &sw
73		endif
74		return ind
75	else " Try to indent switch/case statements as well
76		" Indent blocks enclosed by {} or () or case statements, with some anal requirements
77		if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$'
78			let ind = ind + &sw
79			" return if the current line is not another case statement of the previous line is a bracket open
80			if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$'
81				return ind
82			endif
83		endif
84		if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]'
85			let ind = ind - &sw
86			" if the last line is a break or return, or the current line is a close bracket,
87			" or if the previous line is a default statement, subtract another
88			if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:'
89				let ind = ind - &sw
90			endif
91		endif
92		" Search the matching bracket (with searchpair()) and set the indent of cline
93		" to the indent of the matching line.
94		if cline =~ '^\s*}'
95			call cursor(line('.'), 1)
96			let ind = indent(searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
97			return ind
98		endif
99
100		if line =~ 'default:'
101			let ind = ind + &sw
102		endif
103		return ind
104	endif
105endfunction
106" vim: set ts=4 sw=4:
107