vim: move the function-once test before stuffs
[nit.git] / misc / vim / indent / nit.vim
1 " Vim indent file
2 " Language:     Nit
3 " Maintainer:   Jean Privat <jean@pryen.org>
4
5 " This file is part of NIT ( http://www.nitlanguage.org ).
6 "
7 " Copyright 2009 Jean Privat <jean@pryen.org>
8 "
9 " Licensed under the Apache License, Version 2.0 (the "License");
10 " you may not use this file except in compliance with the License.
11 " You may obtain a copy of the License at
12 "
13 "     http://www.apache.org/licenses/LICENSE-2.0
14 "
15 " Unless required by applicable law or agreed to in writing, software
16 " distributed under the License is distributed on an "AS IS" BASIS,
17 " WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 " See the License for the specific language governing permissions and
19 " limitations under the License.
20
21 " Only load this indent file when no other was loaded.
22 if exists("b:did_indent")
23         finish
24 endif
25 let b:did_indent = 1
26
27 setlocal indentexpr=GetNITIndent()
28 setlocal nolisp
29 setlocal nosmartindent
30 setlocal nocindent
31 setlocal autoindent
32 setlocal comments=:#
33 setlocal indentkeys+==end,=else,=do,=var,0!,=then,=loop,=special,=class,=interface,=universal
34 setlocal sw=8
35
36 " Only define the function once.
37 if exists("*GetNITIndent")
38         finish
39 endif
40
41 " Indent after
42 let s:relative_indent = '\<\(do\|loop\|then\|else\|if\)\s*\(#\|$\)\|^\s*\(\<\(redef\|private\)\>\s*\)\?\(\<abstract\>\s*\)\?\<\(class\|interface\|universal\|special\)\>'
43 " Unindent on them
44 let s:outdent = '^\s*\(else\|then\|end\)\>'
45 " At 0
46 let s:no_indent = '^\s*\(class\|import\|special\)\>'
47
48 let s:syng_strcom = '\<nit\%(String\|StringDelimiter\|Escape\|Comment\|Documentation\)\>'
49
50 " Check if the character at lnum:col is inside a string, comment, or is ascii.
51 function s:IsInStringOrComment(lnum, col)
52           return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_strcom
53 endfunction
54
55 function s:Match(lnum, regex)
56         let col = match(getline(a:lnum), a:regex) + 1
57         return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
58 endfunction
59
60 function GetNITIndent()
61         " Find a non-blank line above the current line.
62         let plnum = prevnonblank(v:lnum - 1)
63
64         " At the start of the file use zero indent.
65         if plnum == 0
66                 return 0
67         endif
68
69         let cline = getline(v:lnum) " The line to indent
70         let prevline=getline(plnum) " The previous nonempty line
71         let ind = indent(plnum) " The previous nonempty indent level
72
73         " Add a 'shiftwidth' after lines that start with an indent word
74         if s:Match(plnum, s:relative_indent)
75         "if prevline =~ s:relative_indent
76                 let ind = ind + &sw
77         endif
78
79         " Subtract a 'shiftwidth', for lines that start with an outdent word
80         "if cline =~ s:outdent
81         if s:Match(v:lnum, s:outdent)
82                 let ind = ind - &sw
83         endif
84
85         " Unindent line after a closure declaration
86         if prevline =~ '^\s*\(break\)\=\s*!'
87                 let col = match(prevline, "!")
88                 let ctx = synIDattr(synID(plnum, col, 0), 'name')
89                 echo "prev is " ctx
90                 if ctx !~ "NITStmtBlock"
91                         " closure declaration
92                         let ind = ind - &sw
93                 end
94         endif
95
96         " Indent line of a closure declaration
97         " Unindent line of a closure definition
98         if cline =~ '^\s*\(break\)\=\s*!'
99                 let col = match(cline, "!")
100                 let ctx = synIDattr(synID(v:lnum, col, 0), 'name')
101                 if ctx =~ "NITStmtBlock"
102                         " closure definition
103                         let ind = ind - &sw
104                 else
105                         " closure declaration
106                         let ind = ind + &sw
107                 end
108         endif
109
110         " The following should always be at the start of a line, no indenting
111         "if cline =~ s:no_indent
112         if s:Match(v:lnum, s:no_indent)
113                 let ind = 0
114         endif
115
116         return ind
117 endfunction