annotate runtime/indent/sqlanywhere.vim @ 1266:54588e2e8041

updated for version 7.1
author vimboss
date Sat, 12 May 2007 14:38:41 +0000
parents 8cd729851562
children b9740fb41986
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
1 " Vim indent file
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
2 " Language: SQL
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
3 " Maintainer: David Fishburn <fishburn at ianywhere dot com>
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
4 " Last Change: Wed Sep 14 2005 10:21:15 PM
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
5 " Version: 1.4
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
6 " Download: http://vim.sourceforge.net/script.php?script_id=495
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
7
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
8 " Notes:
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
9 " Indenting keywords are based on Oracle and Sybase Adaptive Server
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
10 " Anywhere (ASA). Test indenting was done with ASA stored procedures and
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
11 " fuctions and Oracle packages which contain stored procedures and
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
12 " functions.
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
13 " This has not been tested against Microsoft SQL Server or
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
14 " Sybase Adaptive Server Enterprise (ASE) which use the Transact-SQL
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
15 " syntax. That syntax does not have end tags for IF's, which makes
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
16 " indenting more difficult.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
17 "
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
18 " Known Issues:
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
19 " The Oracle MERGE statement does not have an end tag associated with
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
20 " it, this can leave the indent hanging to the right one too many.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
21
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
22 " Only load this indent file when no other was loaded.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
23 if exists("b:did_indent")
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
24 finish
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
25 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
26 let b:did_indent = 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
27 let b:current_indent = "sqlanywhere"
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
28
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
29 setlocal indentkeys-=0{
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
30 setlocal indentkeys-=0}
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
31 setlocal indentkeys-=:
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
32 setlocal indentkeys-=0#
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
33 setlocal indentkeys-=e
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
34
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
35 " This indicates formatting should take place when one of these
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
36 " expressions is used. These expressions would normally be something
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
37 " you would type at the BEGINNING of a line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
38 " SQL is generally case insensitive, so this files assumes that
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
39 " These keywords are something that would trigger an indent LEFT, not
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
40 " an indent right, since the SQLBlockStart is used for those keywords
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
41 setlocal indentkeys+==~end,=~else,=~elseif,=~elsif,0=~when,0=)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
42
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
43 " GetSQLIndent is executed whenever one of the expressions
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
44 " in the indentkeys is typed
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
45 setlocal indentexpr=GetSQLIndent()
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
46
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
47 " Only define the functions once.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
48 if exists("*GetSQLIndent")
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
49 finish
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
50 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
51
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
52 " List of all the statements that start a new block.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
53 " These are typically words that start a line.
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
54 " IS is excluded, since it is difficult to determine when the
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
55 " ending block is (especially for procedures/functions).
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
56 let s:SQLBlockStart = '^\s*\%('.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
57 \ 'if\|else\|elseif\|elsif\|'.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
58 \ 'while\|loop\|do\|'.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
59 \ 'begin\|'.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
60 \ 'case\|when\|merge\|exception'.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
61 \ '\)\>'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
62 let s:SQLBlockEnd = '^\s*\(end\)\>'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
63
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
64 " The indent level is also based on unmatched paranethesis
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
65 " If a line has an extra "(" increase the indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
66 " If a line has an extra ")" decrease the indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
67 function s:CountUnbalancedParan( line, paran_to_check )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
68 let l = a:line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
69 let lp = substitute(l, '[^(]', '', 'g')
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
70 let l = a:line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
71 let rp = substitute(l, '[^)]', '', 'g')
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
72
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
73 if a:paran_to_check =~ ')'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
74 " echom 'CountUnbalancedParan ) returning: ' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
75 " \ (strlen(rp) - strlen(lp))
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
76 return (strlen(rp) - strlen(lp))
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
77 elseif a:paran_to_check =~ '('
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
78 " echom 'CountUnbalancedParan ( returning: ' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
79 " \ (strlen(lp) - strlen(rp))
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
80 return (strlen(lp) - strlen(rp))
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
81 else
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
82 " echom 'CountUnbalancedParan unknown paran to check: ' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
83 " \ a:paran_to_check
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
84 return 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
85 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
86 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
87
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
88 " Unindent commands based on previous indent level
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
89 function s:CheckToIgnoreRightParan( prev_lnum, num_levels )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
90 let lnum = a:prev_lnum
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
91 let line = getline(lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
92 let ends = 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
93 let num_right_paran = a:num_levels
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
94 let ignore_paran = 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
95 let vircol = 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
96
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
97 while num_right_paran > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
98 silent! exec 'norm! '.lnum."G\<bar>".vircol."\<bar>"
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
99 let right_paran = search( ')', 'W' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
100 if right_paran != lnum
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
101 " This should not happen since there should be at least
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
102 " num_right_paran matches for this line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
103 break
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
104 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
105 let vircol = virtcol(".")
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
106
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
107 " if getline(".") =~ '^)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
108 let matching_paran = searchpair('(', '', ')', 'bW',
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
109 \ 'IsColComment(line("."), col("."))')
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
110
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
111 if matching_paran < 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
112 " No match found
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
113 " echom 'CTIRP - no match found, ignoring'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
114 break
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
115 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
116
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
117 if matching_paran == lnum
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
118 " This was not an unmatched parantenses, start the search again
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
119 " again after this column
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
120 " echom 'CTIRP - same line match, ignoring'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
121 continue
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
122 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
123
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
124 " echom 'CTIRP - match: ' . line(".") . ' ' . getline(".")
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
125
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
126 if getline(matching_paran) =~? '\(if\|while\)\>'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
127 " echom 'CTIRP - if/while ignored: ' . line(".") . ' ' . getline(".")
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
128 let ignore_paran = ignore_paran + 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
129 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
130
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
131 " One match found, decrease and check for further matches
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
132 let num_right_paran = num_right_paran - 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
133
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
134 endwhile
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
135
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
136 " Fallback - just move back one
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
137 " return a:prev_indent - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
138 return ignore_paran
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
139 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
140
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
141 " Based on the keyword provided, loop through previous non empty
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
142 " non comment lines to find the statement that initated the keyword.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
143 " Return its indent level
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
144 " CASE ..
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
145 " WHEN ...
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
146 " Should return indent level of CASE
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
147 " EXCEPTION ..
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
148 " WHEN ...
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
149 " something;
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
150 " WHEN ...
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
151 " Should return indent level of exception.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
152 function s:GetStmtStarterIndent( keyword, curr_lnum )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
153 let lnum = a:curr_lnum
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
154
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
155 " Default - reduce indent by 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
156 let ind = indent(a:curr_lnum) - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
157
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
158 if a:keyword =~? 'end'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
159 exec 'normal! ^'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
160 let stmts = '^\s*\%('.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
161 \ '\<begin\>\|' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
162 \ '\%(\%(\<end\s\+\)\@<!\<loop\>\)\|' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
163 \ '\%(\%(\<end\s\+\)\@<!\<case\>\)\|' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
164 \ '\%(\%(\<end\s\+\)\@<!\<for\>\)\|' .
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
165 \ '\%(\%(\<end\s\+\)\@<!\<if\>\)'.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
166 \ '\)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
167 let matching_lnum = searchpair(stmts, '', '\<end\>\zs', 'bW',
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
168 \ 'IsColComment(line("."), col(".")) == 1')
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
169 exec 'normal! $'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
170 if matching_lnum > 0 && matching_lnum < a:curr_lnum
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
171 let ind = indent(matching_lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
172 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
173 elseif a:keyword =~? 'when'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
174 exec 'normal! ^'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
175 let matching_lnum = searchpair(
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
176 \ '\%(\<end\s\+\)\@<!\<case\>\|\<exception\>\|\<merge\>',
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
177 \ '',
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
178 \ '\%(\%(\<when\s\+others\>\)\|\%(\<end\s\+case\>\)\)',
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
179 \ 'bW',
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
180 \ 'IsColComment(line("."), col(".")) == 1')
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
181 exec 'normal! $'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
182 if matching_lnum > 0 && matching_lnum < a:curr_lnum
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
183 let ind = indent(matching_lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
184 else
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
185 let ind = indent(a:curr_lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
186 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
187 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
188
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
189 return ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
190 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
191
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
192
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
193 " Check if the line is a comment
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
194 function IsLineComment(lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
195 let rc = synIDattr(
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
196 \ synID(a:lnum,
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
197 \ match(getline(a:lnum), '\S')+1, 0)
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
198 \ , "name")
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
199 \ =~? "comment"
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
200
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
201 return rc
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
202 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
203
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
204
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
205 " Check if the column is a comment
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
206 function IsColComment(lnum, cnum)
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
207 let rc = synIDattr(synID(a:lnum, a:cnum, 0), "name")
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
208 \ =~? "comment"
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
209
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
210 return rc
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
211 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
212
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
213
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
214 " Check if the column is a comment
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
215 function ModuloIndent(ind)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
216 let ind = a:ind
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
217
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
218 if ind > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
219 let modulo = ind % &shiftwidth
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
220
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
221 if modulo > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
222 let ind = ind - modulo
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
223 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
224 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
225
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
226 return ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
227 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
228
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
229
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
230 " Find correct indent of a new line based upon the previous line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
231 function GetSQLIndent()
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
232 let lnum = v:lnum
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
233 let ind = indent(lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
234
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
235 " If the current line is a comment, leave the indent as is
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
236 " Comment out this additional check since it affects the
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
237 " indenting of =, and will not reindent comments as it should
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
238 " if IsLineComment(lnum) == 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
239 " return ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
240 " endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
241
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
242 " while 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
243 " Get previous non-blank line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
244 let prevlnum = prevnonblank(lnum - 1)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
245 if prevlnum <= 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
246 return ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
247 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
248
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
249 if IsLineComment(prevlnum) == 1
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
250 if getline(v:lnum) =~ '^\s*\*'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
251 let ind = ModuloIndent(indent(prevlnum))
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
252 return ind + 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
253 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
254 " If the previous line is a comment, then return -1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
255 " to tell Vim to use the formatoptions setting to determine
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
256 " the indent to use
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
257 " But only if the next line is blank. This would be true if
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
258 " the user is typing, but it would not be true if the user
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
259 " is reindenting the file
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
260 if getline(v:lnum) =~ '^\s*$'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
261 return -1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
262 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
263 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
264
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
265 " let prevline = getline(prevlnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
266 " if prevline !~ '^\s*$'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
267 " " echom 'previous non blank - break: ' . prevline
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
268 " break
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
269 " endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
270 " endwhile
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
271
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
272 " echom 'PREVIOUS INDENT: ' . indent(prevlnum) . ' LINE: ' . getline(prevlnum)
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
273
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
274 " This is the line you just hit return on, it is not the current line
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
275 " which is new and empty
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
276 " Based on this line, we can determine how much to indent the new
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
277 " line
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
278
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
279 " Get default indent (from prev. line)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
280 let ind = indent(prevlnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
281 let prevline = getline(prevlnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
282
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
283 " Now check what's on the previous line to determine if the indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
284 " should be changed, for example IF, BEGIN, should increase the indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
285 " where END IF, END, should decrease the indent.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
286 if prevline =~? s:SQLBlockStart
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
287 " Move indent in
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
288 let ind = ind + &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
289 " echom 'prevl - SQLBlockStart - indent ' . ind . ' line: ' . prevline
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
290 elseif prevline =~ '[()]'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
291 if prevline =~ '('
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
292 let num_unmatched_left = s:CountUnbalancedParan( prevline, '(' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
293 else
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
294 let num_unmatched_left = 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
295 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
296 if prevline =~ ')'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
297 let num_unmatched_right = s:CountUnbalancedParan( prevline, ')' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
298 else
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
299 let num_unmatched_right = 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
300 " let num_unmatched_right = s:CountUnbalancedParan( prevline, ')' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
301 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
302 if num_unmatched_left > 0
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
303 " There is a open left paranethesis
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
304 " increase indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
305 let ind = ind + ( &sw * num_unmatched_left )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
306 elseif num_unmatched_right > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
307 " if it is an unbalanced paranethesis only unindent if
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
308 " it was part of a command (ie create table(..) )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
309 " instead of part of an if (ie if (....) then) which should
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
310 " maintain the indent level
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
311 let ignore = s:CheckToIgnoreRightParan( prevlnum, num_unmatched_right )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
312 " echom 'prevl - ) unbalanced - CTIRP - ignore: ' . ignore
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
313
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
314 if prevline =~ '^\s*)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
315 let ignore = ignore + 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
316 " echom 'prevl - begins ) unbalanced ignore: ' . ignore
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
317 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
318
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
319 if (num_unmatched_right - ignore) > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
320 let ind = ind - ( &sw * (num_unmatched_right - ignore) )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
321 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
322
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
323 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
324 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
325
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
326
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
327 " echom 'CURRENT INDENT: ' . ind . ' LINE: ' . getline(v:lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
328
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
329 " This is a new blank line since we just typed a carriage return
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
330 " Check current line; search for simplistic matching start-of-block
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
331 let line = getline(v:lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
332
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
333 if line =~? '^\s*els'
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
334 " Any line when you type else will automatically back up one
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
335 " ident level (ie else, elseif, elsif)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
336 let ind = ind - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
337 " echom 'curr - else - indent ' . ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
338 elseif line =~? '^\s*end\>'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
339 let ind = s:GetStmtStarterIndent('end', v:lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
340 " General case for end
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
341 " let ind = ind - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
342 " echom 'curr - end - indent ' . ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
343 elseif line =~? '^\s*when\>'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
344 let ind = s:GetStmtStarterIndent('when', v:lnum)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
345 " If the WHEN clause is used with a MERGE or EXCEPTION
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
346 " clause, do not change the indent level, since these
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
347 " statements do not have a corresponding END statement.
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
348 " if stmt_starter =~? 'case'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
349 " let ind = ind - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
350 " endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
351 " elseif line =~ '^\s*)\s*;\?\s*$'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
352 " elseif line =~ '^\s*)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
353 elseif line =~ '^\s*)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
354 let num_unmatched_right = s:CountUnbalancedParan( line, ')' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
355 let ignore = s:CheckToIgnoreRightParan( v:lnum, num_unmatched_right )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
356 " If the line ends in a ), then reduce the indent
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
357 " This catches items like:
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
358 " CREATE TABLE T1(
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
359 " c1 int,
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
360 " c2 int
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
361 " );
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
362 " But we do not want to unindent a line like:
856
8cd729851562 updated for version 7.0g
vimboss
parents: 718
diff changeset
363 " IF ( c1 = 1
718
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
364 " AND c2 = 3 ) THEN
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
365 " let num_unmatched_right = s:CountUnbalancedParan( line, ')' )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
366 " if num_unmatched_right > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
367 " elseif strpart( line, strlen(line)-1, 1 ) =~ ')'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
368 " let ind = ind - &sw
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
369 if line =~ '^\s*)'
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
370 " let ignore = ignore + 1
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
371 " echom 'curr - begins ) unbalanced ignore: ' . ignore
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
372 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
373
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
374 if (num_unmatched_right - ignore) > 0
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
375 let ind = ind - ( &sw * (num_unmatched_right - ignore) )
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
376 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
377 " endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
378 endif
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
379
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
380 " echom 'final - indent ' . ind
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
381 return ModuloIndent(ind)
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
382 endfunction
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
383
7b21554be7a1 updated for version 7.0219
vimboss
parents:
diff changeset
384 " vim:sw=4:ff=unix: