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