blob: 46a228e8b650fe37fab6a97516575178a69b5c58 [file] [log] [blame]
Bram Moolenaar4a748032010-09-30 21:47:56 +02001" Vim indent file
2" Language: Falcon
3" Maintainer: Steven Oliver <oliver.steven@gmail.com>
4" Website: https://steveno@github.com/steveno/falconpl-vim.git
5" Credits: Thanks to the ruby.vim authors, I borrow a lot!
6" Previous Maintainer: Brent A. Fulgham <bfulgham@debian.org>
7" -----------------------------------------------------------
Bram Moolenaar4a748032010-09-30 21:47:56 +02008
9"======================================
10" SETUP
11"======================================
12
13" Only load this indent file when no other was loaded.
14if exists("b:did_indent")
15 finish
16endif
17let b:did_indent = 1
18
19setlocal nosmartindent
20
21" Setup indent function and when to use it
22setlocal indentexpr=FalconGetIndent()
23setlocal indentkeys=0{,0},0),0],!^F,o,O,e
24setlocal indentkeys+==~case,=~catch,=~default,=~elif,=~else,=~end,=~\"
25
26" Define the appropriate indent function but only once
27if exists("*FalconGetIndent")
28 finish
29endif
30
31let s:cpo_save = &cpo
32set cpo&vim
33
34"======================================
35" VARIABLES
36"======================================
37
38" Regex of syntax group names that are strings AND comments
39let s:syng_strcom = '\<falcon\%(String\|StringEscape\|Comment\)\>'
40
41" Regex of syntax group names that are strings
42let s:syng_string = '\<falcon\%(String\|StringEscape\)\>'
43
44" Keywords to indent on
45let s:falcon_indent_keywords = '^\s*\(case\|catch\|class\|enum\|default\|elif\|else' .
46 \ '\|for\|function\|if.*"[^"]*:.*"\|if \(\(:\)\@!.\)*$\|loop\|object\|select' .
47 \ '\|switch\|try\|while\|\w*\s*=\s*\w*([$\)'
48
49" Keywords to deindent on
50let s:falcon_deindent_keywords = '^\s*\(case\|catch\|default\|elif\|else\|end\)'
51
52"======================================
53" FUNCTIONS
54"======================================
55
56" Check if the character at lnum:col is inside a string
57function s:IsInStringOrComment(lnum, col)
58 return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
59endfunction
60
61"======================================
62" INDENT ROUTINE
63"======================================
64
65function FalconGetIndent()
66 " Get the line to be indented
67 let cline = getline(v:lnum)
68
69 " Don't reindent comments on first column
70 if cline =~ '^\/\/'
71 return 0
72 endif
73
74 " Find the previous non-blank line
75 let lnum = prevnonblank(v:lnum - 1)
76
77 " Use zero indent at the top of the file
78 if lnum == 0
79 return 0
80 endif
81
82 let prevline=getline(lnum)
83 let ind = indent(lnum)
84 let chg = 0
85
86 " If we are in a multi-line string or line-comment, don't do anything
87 if s:IsInStringOrComment(v:lnum, matchend(cline, '^\s*') + 1 )
88 return indent('.')
89 endif
90
91 " If the start of the line equals a double quote, then indent to the
92 " previous lines first double quote
93 if cline =~? '^\s*"'
94 let chg = chg + &sw
95 endif
96
97 " If previous line started with a double quote and this one
98 " doesn't, unindent
99 if prevline =~? '^\s*"' && cline =~? '^\s*'
100 let chg = chg - &sw
101 endif
102
103 " Indent if proper keyword
104 if prevline =~? s:falcon_indent_keywords
105 let chg = &sw
106 " If previous line opened a parenthesis, and did not close it, indent
107 elseif prevline =~ '^.*(\s*[^)]*\((.*)\)*[^)]*$'
108 " Make sure this isn't just a function split between two lines
109 if prevline =~ ',\s*$'
110 return indent(prevnonblank(v:lnum - 1)) + &sw
111 else
112 return match(prevline, '(.*\((.*)\|[^)]\)*.*$') + 1
113 endif
114 elseif prevline =~ '^[^(]*)\s*$'
115 " This line closes a parenthesis. Finds opening.
116 let curr_line = prevnonblank(lnum - 1)
117 while curr_line >= 0
118 let str = getline(curr_line)
119 if str !~ '^.*(\s*[^)]*\((.*)\)*[^)]*$'
120 let curr_line = prevnonblank(curr_line - 1)
121 else
122 break
123 endif
124 endwhile
125 if curr_line < 0
126 return -1
127 endif
128 let ind = indent(curr_line)
129 endif
130
131 " If previous line ends in a semi-colon reset indent to previous
132 " lines setting
133 if prevline =~? ';\s*$' && prevnonblank(prevline) =~? ',\s*$'
Bram Moolenaar34700a62013-03-07 13:20:54 +0100134 let chg = chg - (2 * &sw)
Bram Moolenaar4a748032010-09-30 21:47:56 +0200135 endif
136
137 " If previous line ended in a comma, indent again
138 if prevline =~? ',\s*$'
139 let chg = chg + &sw
140 endif
141
142 " If previous line ended in a =>, indent again
143 if prevline =~? '=>\s*$'
144 let chg = chg + &sw
145 endif
146
147 " Deindent on proper keywords
148 if cline =~? s:falcon_deindent_keywords
149 let chg = chg - &sw
150 endif
151
152 return ind + chg
153endfunction
154
Bram Moolenaarf1568ec2011-12-14 21:17:39 +0100155let &cpo = s:cpo_save
156unlet s:cpo_save
157
Bram Moolenaar4a748032010-09-30 21:47:56 +0200158" vim: set sw=4 sts=4 et tw=80 :