misc: add vim syntax and indent files
[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,=with,=then,=special,=class,=interface,=universal
34 setlocal sw=8
35
36 " Indent after
37 let s:relative_indent = '\<\(do\|then\|else\|if\)\s*$\|^\s*\(\<\(redef\|private\)\>\s*\)\?\(\<abstract\>\s*\)\?\<\(class\|interface\|universal\|special\)\>'
38 " Unindent on them
39 let s:outdent = '^\s*\(else\|then\|with\|end\)\>'
40 " At 0
41 let s:no_indent = '^\s*\(class\|import\|special\)\>'
42
43 " Only define the function once.
44 if exists("*GetNITIndent")
45         finish
46 endif
47
48 function GetNITIndent()
49         " Find a non-blank line above the current line.
50         let lnum = prevnonblank(v:lnum - 1)
51
52         " At the start of the file use zero indent.
53         if lnum == 0
54                 return 0
55         endif
56
57         let cline = getline(v:lnum) " The line to indent
58         let prevline=getline(lnum) " The previous nonempty line
59         let ind = indent(lnum) " The previous nonempty indent level
60
61         " Add a 'shiftwidth' after lines that start with an indent word
62         if prevline =~ s:relative_indent
63                 let ind = ind + &sw
64         endif
65
66         " Subtract a 'shiftwidth', for lines that start with an outdent word
67         if cline =~ s:outdent
68                 let ind = ind - &sw
69         endif
70
71         " The following should always be at the start of a line, no indenting
72         if cline =~ s:no_indent
73                 let ind = 0
74         endif
75
76         " Subtract a shiftwidth for end statements
77         if prevline =~ '$\s*end\s*\(#|$\)'
78                 let ind = ind - &sw
79         endif
80
81         return ind
82 endfunction