3 " Maintainer: Jean Privat <jean@pryen.org>
5 " This file is part of NIT ( http://www.nitlanguage.org ).
7 " Copyright 2009 Jean Privat <jean@pryen.org>
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
13 " http://www.apache.org/licenses/LICENSE-2.0
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.
21 " Only load this indent file when no other was loaded.
22 if exists("b:did_indent")
27 setlocal indentexpr=GetNITIndent()
29 setlocal nosmartindent
33 setlocal indentkeys+==end,=else,=do,=var,0!,=then,=loop,=special,=class,=interface,=universal
37 let s:relative_indent = '\<\(do\|loop\|then\|else\|if\)\s*\(#\|$\)\|^\s*\(\<\(redef\|private\)\>\s*\)\?\(\<abstract\>\s*\)\?\<\(class\|interface\|universal\|special\)\>'
39 let s:outdent = '^\s*\(else\|then\|end\)\>'
41 let s:no_indent = '^\s*\(class\|import\|special\)\>'
43 let s:syng_strcom = '\<nit\%(String\|StringDelimiter\|Escape\|Comment\|Documentation\)\>'
45 " Check if the character at lnum:col is inside a string, comment, or is ascii.
46 function s:IsInStringOrComment(lnum, col)
47 return synIDattr(synID(a:lnum, a:col, 0), 'name') =~ s:syng_strcom
50 function s:Match(lnum, regex)
51 let col = match(getline(a:lnum), a:regex) + 1
52 return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
55 " Only define the function once.
56 if exists("*GetNITIndent")
60 function GetNITIndent()
61 " Find a non-blank line above the current line.
62 let plnum = prevnonblank(v:lnum - 1)
64 " At the start of the file use zero indent.
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
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
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)
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')
90 if ctx !~ "NITStmtBlock"
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"
105 " closure declaration
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)