Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Scala (http://scala-lang.org/) |
| 3 | " Original Author: Stefan Matthias Aust |
| 4 | " Modifications By: Derek Wyatt |
| 5 | " URL: https://github.com/derekwyatt/vim-scala |
| 6 | " Last Change: 2016 Aug 26 |
| 7 | |
| 8 | if exists("b:did_indent") |
| 9 | finish |
| 10 | endif |
| 11 | let b:did_indent = 1 |
| 12 | |
| 13 | setlocal autoindent |
| 14 | setlocal indentexpr=GetScalaIndent() |
| 15 | setlocal indentkeys=0{,0},0),!^F,<>>,o,O,e,=case,<CR> |
| 16 | |
| 17 | if exists("*GetScalaIndent") |
| 18 | finish |
| 19 | endif |
| 20 | let s:keepcpo= &cpo |
| 21 | set cpo&vim |
| 22 | |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 23 | let s:annotationMatcher = '@[A-Za-z._]\+\s\+' |
| 24 | let s:modifierMatcher = s:annotationMatcher . '\|\%(private\|protected\)\%(\[[^\]]*\]\)\?\s\+\|abstract\s\+\|override\s\+\|final\s\+' |
| 25 | let s:defMatcher = '\%(' . s:modifierMatcher . '\)*\<def\>' |
| 26 | let s:valMatcher = '\%(' . s:modifierMatcher . '\|lazy\s\+\)*\<va[lr]\>' |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 27 | let s:funcNameMatcher = '\w\+' |
| 28 | let s:typeSpecMatcher = '\%(\s*\[\_[^\]]*\]\)' |
| 29 | let s:defArgMatcher = '\%((\_.\{-})\)' |
| 30 | let s:returnTypeMatcher = '\%(:\s*\w\+' . s:typeSpecMatcher . '\?\)' |
| 31 | let g:fullDefMatcher = '^\s*' . s:defMatcher . '\s\+' . s:funcNameMatcher . '\s*' . s:typeSpecMatcher . '\?\s*' . s:defArgMatcher . '\?\s*' . s:returnTypeMatcher . '\?\s*[={]' |
| 32 | |
| 33 | function! scala#ConditionalConfirm(msg) |
| 34 | if 0 |
| 35 | call confirm(a:msg) |
| 36 | endif |
| 37 | endfunction |
| 38 | |
| 39 | function! scala#GetLine(lnum) |
| 40 | let line = substitute(getline(a:lnum), '//.*$', '', '') |
| 41 | let line = substitute(line, '"\(.\|\\"\)\{-}"', '""', 'g') |
| 42 | return line |
| 43 | endfunction |
| 44 | |
| 45 | function! scala#CountBrackets(line, openBracket, closedBracket) |
| 46 | let line = substitute(a:line, '"\(.\|\\"\)\{-}"', '', 'g') |
| 47 | let open = substitute(line, '[^' . a:openBracket . ']', '', 'g') |
| 48 | let close = substitute(line, '[^' . a:closedBracket . ']', '', 'g') |
| 49 | return strlen(open) - strlen(close) |
| 50 | endfunction |
| 51 | |
| 52 | function! scala#CountParens(line) |
| 53 | return scala#CountBrackets(a:line, '(', ')') |
| 54 | endfunction |
| 55 | |
| 56 | function! scala#CountCurlies(line) |
| 57 | return scala#CountBrackets(a:line, '{', '}') |
| 58 | endfunction |
| 59 | |
| 60 | function! scala#LineEndsInIncomplete(line) |
| 61 | if a:line =~ '[.,]\s*$' |
| 62 | return 1 |
| 63 | else |
| 64 | return 0 |
| 65 | endif |
| 66 | endfunction |
| 67 | |
| 68 | function! scala#LineIsAClosingXML(line) |
| 69 | if a:line =~ '^\s*</\w' |
| 70 | return 1 |
| 71 | else |
| 72 | return 0 |
| 73 | endif |
| 74 | endfunction |
| 75 | |
| 76 | function! scala#LineCompletesXML(lnum, line) |
| 77 | let savedpos = getpos('.') |
| 78 | call setpos('.', [savedpos[0], a:lnum, 0, savedpos[3]]) |
| 79 | let tag = substitute(a:line, '^.*</\([^>]*\)>.*$', '\1', '') |
| 80 | let [lineNum, colnum] = searchpairpos('<' . tag . '>', '', '</' . tag . '>', 'Wbn') |
| 81 | call setpos('.', savedpos) |
| 82 | let pline = scala#GetLine(prevnonblank(lineNum - 1)) |
| 83 | if pline =~ '=\s*$' |
| 84 | return 1 |
| 85 | else |
| 86 | return 0 |
| 87 | endif |
| 88 | endfunction |
| 89 | |
| 90 | function! scala#IsParentCase() |
| 91 | let savedpos = getpos('.') |
| 92 | call setpos('.', [savedpos[0], savedpos[1], 0, savedpos[3]]) |
| 93 | let [l, c] = searchpos('^\s*\%(' . s:defMatcher . '\|\%(\<case\>\)\)', 'bnW') |
| 94 | let retvalue = -1 |
| 95 | if l != 0 && search('\%' . l . 'l\s*\<case\>', 'bnW') |
| 96 | let retvalue = l |
| 97 | endif |
| 98 | call setpos('.', savedpos) |
| 99 | return retvalue |
| 100 | endfunction |
| 101 | |
| 102 | function! scala#CurlyMatcher() |
| 103 | let matchline = scala#GetLineThatMatchesBracket('{', '}') |
| 104 | if scala#CountParens(scala#GetLine(matchline)) < 0 |
| 105 | let savedpos = getpos('.') |
| 106 | call setpos('.', [savedpos[0], matchline, 9999, savedpos[3]]) |
| 107 | call searchpos('{', 'Wbc') |
| 108 | call searchpos(')', 'Wb') |
| 109 | let [lnum, colnum] = searchpairpos('(', '', ')', 'Wbn') |
| 110 | call setpos('.', savedpos) |
| 111 | let line = scala#GetLine(lnum) |
| 112 | if line =~ '^\s*' . s:defMatcher |
| 113 | return lnum |
| 114 | else |
| 115 | return matchline |
| 116 | endif |
| 117 | else |
| 118 | return matchline |
| 119 | endif |
| 120 | endfunction |
| 121 | |
| 122 | function! scala#GetLineAndColumnThatMatchesCurly() |
| 123 | return scala#GetLineAndColumnThatMatchesBracket('{', '}') |
| 124 | endfunction |
| 125 | |
| 126 | function! scala#GetLineAndColumnThatMatchesParen() |
| 127 | return scala#GetLineAndColumnThatMatchesBracket('(', ')') |
| 128 | endfunction |
| 129 | |
| 130 | function! scala#GetLineAndColumnThatMatchesBracket(openBracket, closedBracket) |
| 131 | let savedpos = getpos('.') |
| 132 | let curline = scala#GetLine(line('.')) |
| 133 | if curline =~ a:closedBracket . '.*' . a:openBracket . '.*' . a:closedBracket |
| 134 | call setpos('.', [savedpos[0], savedpos[1], 0, savedpos[3]]) |
| 135 | call searchpos(a:closedBracket . '\ze[^' . a:closedBracket . a:openBracket . ']*' . a:openBracket, 'W') |
| 136 | else |
| 137 | call setpos('.', [savedpos[0], savedpos[1], 9999, savedpos[3]]) |
| 138 | call searchpos(a:closedBracket, 'Wbc') |
| 139 | endif |
| 140 | let [lnum, colnum] = searchpairpos(a:openBracket, '', a:closedBracket, 'Wbn') |
| 141 | call setpos('.', savedpos) |
| 142 | return [lnum, colnum] |
| 143 | endfunction |
| 144 | |
| 145 | function! scala#GetLineThatMatchesCurly() |
| 146 | return scala#GetLineThatMatchesBracket('{', '}') |
| 147 | endfunction |
| 148 | |
| 149 | function! scala#GetLineThatMatchesParen() |
| 150 | return scala#GetLineThatMatchesBracket('(', ')') |
| 151 | endfunction |
| 152 | |
| 153 | function! scala#GetLineThatMatchesBracket(openBracket, closedBracket) |
| 154 | let [lnum, colnum] = scala#GetLineAndColumnThatMatchesBracket(a:openBracket, a:closedBracket) |
| 155 | return lnum |
| 156 | endfunction |
| 157 | |
| 158 | function! scala#NumberOfBraceGroups(line) |
| 159 | let line = substitute(a:line, '[^()]', '', 'g') |
| 160 | if strlen(line) == 0 |
| 161 | return 0 |
| 162 | endif |
| 163 | let line = substitute(line, '^)*', '', 'g') |
| 164 | if strlen(line) == 0 |
| 165 | return 0 |
| 166 | endif |
| 167 | let line = substitute(line, '^(', '', 'g') |
| 168 | if strlen(line) == 0 |
| 169 | return 0 |
| 170 | endif |
| 171 | let c = 1 |
| 172 | let counter = 0 |
| 173 | let groupCount = 0 |
| 174 | while counter < strlen(line) |
| 175 | let char = strpart(line, counter, 1) |
| 176 | if char == '(' |
| 177 | let c = c + 1 |
| 178 | elseif char == ')' |
| 179 | let c = c - 1 |
| 180 | endif |
| 181 | if c == 0 |
| 182 | let groupCount = groupCount + 1 |
| 183 | endif |
| 184 | let counter = counter + 1 |
| 185 | endwhile |
| 186 | return groupCount |
| 187 | endfunction |
| 188 | |
| 189 | function! scala#MatchesIncompleteDefValr(line) |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 190 | if a:line =~ '^\s*\%(' . s:defMatcher . '\|' . s:valMatcher . '\).*[=({]\s*$' |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 191 | return 1 |
| 192 | else |
| 193 | return 0 |
| 194 | endif |
| 195 | endfunction |
| 196 | |
| 197 | function! scala#LineIsCompleteIf(line) |
| 198 | if scala#CountBrackets(a:line, '{', '}') == 0 && |
| 199 | \ scala#CountBrackets(a:line, '(', ')') == 0 && |
| 200 | \ a:line =~ '^\s*\<if\>\s*([^)]*)\s*\S.*$' |
| 201 | return 1 |
| 202 | else |
| 203 | return 0 |
| 204 | endif |
| 205 | endfunction |
| 206 | |
| 207 | function! scala#LineCompletesIfElse(lnum, line) |
| 208 | if a:line =~ '^\s*\%(\<if\>\|\%(}\s*\)\?\<else\>\)' |
| 209 | return 0 |
| 210 | endif |
| 211 | let result = search('^\%(\s*\<if\>\s*(.*).*\n\|\s*\<if\>\s*(.*)\s*\n.*\n\)\%(\s*\<else\>\s*\<if\>\s*(.*)\s*\n.*\n\)*\%(\s*\<else\>\s*\n\|\s*\<else\>[^{]*\n\)\?\%' . a:lnum . 'l', 'Wbn') |
| 212 | if result != 0 && scala#GetLine(prevnonblank(a:lnum - 1)) !~ '{\s*$' |
| 213 | return result |
| 214 | endif |
| 215 | return 0 |
| 216 | endfunction |
| 217 | |
| 218 | function! scala#GetPrevCodeLine(lnum) |
| 219 | " This needs to skip comment lines |
| 220 | return prevnonblank(a:lnum - 1) |
| 221 | endfunction |
| 222 | |
| 223 | function! scala#InvertBracketType(openBracket, closedBracket) |
| 224 | if a:openBracket == '(' |
| 225 | return [ '{', '}' ] |
| 226 | else |
| 227 | return [ '(', ')' ] |
| 228 | endif |
| 229 | endfunction |
| 230 | |
| 231 | function! scala#Testhelper(lnum, line, openBracket, closedBracket, iteration) |
| 232 | let bracketCount = scala#CountBrackets(a:line, a:openBracket, a:closedBracket) |
| 233 | " There are more '}' braces than '{' on this line so it may be completing the function definition |
| 234 | if bracketCount < 0 |
| 235 | let [matchedLNum, matchedColNum] = scala#GetLineAndColumnThatMatchesBracket(a:openBracket, a:closedBracket) |
| 236 | if matchedLNum == a:lnum |
| 237 | return -1 |
| 238 | endif |
| 239 | let matchedLine = scala#GetLine(matchedLNum) |
| 240 | if ! scala#MatchesIncompleteDefValr(matchedLine) |
| 241 | let bracketLine = substitute(substitute(matchedLine, '\%' . matchedColNum . 'c.*$', '', ''), '[^{}()]', '', 'g') |
| 242 | if bracketLine =~ '}$' |
| 243 | return scala#Testhelper(matchedLNum, matchedLine, '{', '}', a:iteration + 1) |
| 244 | elseif bracketLine =~ ')$' |
| 245 | return scala#Testhelper(matchedLNum, matchedLine, '(', ')', a:iteration + 1) |
| 246 | else |
| 247 | let prevCodeLNum = scala#GetPrevCodeLine(matchedLNum) |
| 248 | if scala#MatchesIncompleteDefValr(scala#GetLine(prevCodeLNum)) |
| 249 | return prevCodeLNum |
| 250 | else |
| 251 | return -1 |
| 252 | endif |
| 253 | endif |
| 254 | else |
| 255 | " return indent value instead |
| 256 | return matchedLNum |
| 257 | endif |
| 258 | " There's an equal number of '{' and '}' on this line so it may be a single line function definition |
| 259 | elseif bracketCount == 0 |
| 260 | if a:iteration == 0 |
| 261 | let otherBracketType = scala#InvertBracketType(a:openBracket, a:closedBracket) |
| 262 | return scala#Testhelper(a:lnum, a:line, otherBracketType[0], otherBracketType[1], a:iteration + 1) |
| 263 | else |
| 264 | let prevCodeLNum = scala#GetPrevCodeLine(a:lnum) |
| 265 | let prevCodeLine = scala#GetLine(prevCodeLNum) |
| 266 | if scala#MatchesIncompleteDefValr(prevCodeLine) && prevCodeLine !~ '{\s*$' |
| 267 | return prevCodeLNum |
| 268 | else |
| 269 | let possibleIfElse = scala#LineCompletesIfElse(a:lnum, a:line) |
| 270 | if possibleIfElse != 0 |
| 271 | let defValrLine = prevnonblank(possibleIfElse - 1) |
| 272 | let possibleDefValr = scala#GetLine(defValrLine) |
| 273 | if scala#MatchesIncompleteDefValr(possibleDefValr) && possibleDefValr =~ '^.*=\s*$' |
| 274 | return possibleDefValr |
| 275 | else |
| 276 | return -1 |
| 277 | endif |
| 278 | else |
| 279 | return -1 |
| 280 | endif |
| 281 | endif |
| 282 | endif |
| 283 | else |
| 284 | return -1 |
| 285 | endif |
| 286 | endfunction |
| 287 | |
| 288 | function! scala#Test(lnum, line, openBracket, closedBracket) |
| 289 | return scala#Testhelper(a:lnum, a:line, a:openBracket, a:closedBracket, 0) |
| 290 | endfunction |
| 291 | |
| 292 | function! scala#LineCompletesDefValr(lnum, line) |
| 293 | let bracketCount = scala#CountBrackets(a:line, '{', '}') |
| 294 | if bracketCount < 0 |
| 295 | let matchedBracket = scala#GetLineThatMatchesBracket('{', '}') |
| 296 | if ! scala#MatchesIncompleteDefValr(scala#GetLine(matchedBracket)) |
| 297 | let possibleDefValr = scala#GetLine(prevnonblank(matchedBracket - 1)) |
| 298 | if matchedBracket != -1 && scala#MatchesIncompleteDefValr(possibleDefValr) |
| 299 | return 1 |
| 300 | else |
| 301 | return 0 |
| 302 | endif |
| 303 | else |
| 304 | return 0 |
| 305 | endif |
| 306 | elseif bracketCount == 0 |
| 307 | let bracketCount = scala#CountBrackets(a:line, '(', ')') |
| 308 | if bracketCount < 0 |
| 309 | let matchedBracket = scala#GetLineThatMatchesBracket('(', ')') |
| 310 | if ! scala#MatchesIncompleteDefValr(scala#GetLine(matchedBracket)) |
| 311 | let possibleDefValr = scala#GetLine(prevnonblank(matchedBracket - 1)) |
| 312 | if matchedBracket != -1 && scala#MatchesIncompleteDefValr(possibleDefValr) |
| 313 | return 1 |
| 314 | else |
| 315 | return 0 |
| 316 | endif |
| 317 | else |
| 318 | return 0 |
| 319 | endif |
| 320 | elseif bracketCount == 0 |
| 321 | let possibleDefValr = scala#GetLine(prevnonblank(a:lnum - 1)) |
| 322 | if scala#MatchesIncompleteDefValr(possibleDefValr) && possibleDefValr =~ '^.*=\s*$' |
| 323 | return 1 |
| 324 | else |
| 325 | let possibleIfElse = scala#LineCompletesIfElse(a:lnum, a:line) |
| 326 | if possibleIfElse != 0 |
| 327 | let possibleDefValr = scala#GetLine(prevnonblank(possibleIfElse - 1)) |
| 328 | if scala#MatchesIncompleteDefValr(possibleDefValr) && possibleDefValr =~ '^.*=\s*$' |
| 329 | return 2 |
| 330 | else |
| 331 | return 0 |
| 332 | endif |
| 333 | else |
| 334 | return 0 |
| 335 | endif |
| 336 | endif |
| 337 | else |
| 338 | return 0 |
| 339 | endif |
| 340 | endif |
| 341 | endfunction |
| 342 | |
| 343 | function! scala#SpecificLineCompletesBrackets(lnum, openBracket, closedBracket) |
| 344 | let savedpos = getpos('.') |
| 345 | call setpos('.', [savedpos[0], a:lnum, 9999, savedpos[3]]) |
| 346 | let retv = scala#LineCompletesBrackets(a:openBracket, a:closedBracket) |
| 347 | call setpos('.', savedpos) |
| 348 | |
| 349 | return retv |
| 350 | endfunction |
| 351 | |
| 352 | function! scala#LineCompletesBrackets(openBracket, closedBracket) |
| 353 | let savedpos = getpos('.') |
| 354 | let offline = 0 |
| 355 | while offline == 0 |
| 356 | let [lnum, colnum] = searchpos(a:closedBracket, 'Wb') |
| 357 | let [lnumA, colnumA] = searchpairpos(a:openBracket, '', a:closedBracket, 'Wbn') |
| 358 | if lnum != lnumA |
| 359 | let [lnumB, colnumB] = searchpairpos(a:openBracket, '', a:closedBracket, 'Wbnr') |
| 360 | let offline = 1 |
| 361 | endif |
| 362 | endwhile |
| 363 | call setpos('.', savedpos) |
| 364 | if lnumA == lnumB && colnumA == colnumB |
| 365 | return lnumA |
| 366 | else |
| 367 | return -1 |
| 368 | endif |
| 369 | endfunction |
| 370 | |
| 371 | function! GetScalaIndent() |
| 372 | " Find a non-blank line above the current line. |
| 373 | let prevlnum = prevnonblank(v:lnum - 1) |
| 374 | |
| 375 | " Hit the start of the file, use zero indent. |
| 376 | if prevlnum == 0 |
| 377 | return 0 |
| 378 | endif |
| 379 | |
| 380 | let ind = indent(prevlnum) |
| 381 | let originalIndentValue = ind |
| 382 | let prevline = scala#GetLine(prevlnum) |
| 383 | let curlnum = v:lnum |
| 384 | let curline = scala#GetLine(curlnum) |
| 385 | if get(g:, 'scala_scaladoc_indent', 0) |
| 386 | let star_indent = 2 |
| 387 | else |
| 388 | let star_indent = 1 |
| 389 | end |
| 390 | |
| 391 | if prevline =~ '^\s*/\*\*' |
| 392 | if prevline =~ '\*/\s*$' |
| 393 | return ind |
| 394 | else |
| 395 | return ind + star_indent |
| 396 | endif |
| 397 | endif |
| 398 | |
| 399 | if curline =~ '^\s*\*' |
| 400 | return cindent(curlnum) |
| 401 | endif |
| 402 | |
| 403 | " If this line starts with a { then make it indent the same as the previous line |
| 404 | if curline =~ '^\s*{' |
| 405 | call scala#ConditionalConfirm("1") |
| 406 | " Unless, of course, the previous one is a { as well |
| 407 | if prevline !~ '^\s*{' |
| 408 | call scala#ConditionalConfirm("2") |
| 409 | return indent(prevlnum) |
| 410 | endif |
| 411 | endif |
| 412 | |
| 413 | " '.' continuations |
| 414 | if curline =~ '^\s*\.' |
| 415 | if prevline =~ '^\s*\.' |
| 416 | return ind |
| 417 | else |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 418 | return ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 419 | endif |
| 420 | endif |
| 421 | |
| 422 | " Indent html literals |
| 423 | if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$' |
| 424 | call scala#ConditionalConfirm("3") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 425 | return ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 426 | endif |
| 427 | |
| 428 | " assumes curly braces around try-block |
| 429 | if curline =~ '^\s*}\s*\<catch\>' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 430 | return ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 431 | elseif curline =~ '^\s*\<catch\>' |
| 432 | return ind |
| 433 | endif |
| 434 | |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 435 | " Add a shiftwidth()' after lines that start a block |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 436 | " If 'if', 'for' or 'while' end with ), this is a one-line block |
| 437 | " If 'val', 'var', 'def' end with =, this is a one-line block |
| 438 | if (prevline =~ '^\s*\<\%(\%(}\?\s*else\s\+\)\?if\|for\|while\)\>.*[)=]\s*$' && scala#NumberOfBraceGroups(prevline) <= 1) |
| 439 | \ || prevline =~ '^\s*' . s:defMatcher . '.*=\s*$' |
Bram Moolenaar | 89a9c15 | 2021-08-29 21:55:35 +0200 | [diff] [blame] | 440 | \ || prevline =~ '^\s*' . s:valMatcher . '.*[=]\s*$' |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 441 | \ || prevline =~ '^\s*\%(}\s*\)\?\<else\>\s*$' |
| 442 | \ || prevline =~ '=\s*$' |
| 443 | call scala#ConditionalConfirm("4") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 444 | let ind = ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 445 | elseif prevline =~ '^\s*\<\%(}\?\s*else\s\+\)\?if\>' && curline =~ '^\s*}\?\s*\<else\>' |
| 446 | return ind |
| 447 | endif |
| 448 | |
| 449 | let lineCompletedBrackets = 0 |
| 450 | let bracketCount = scala#CountBrackets(prevline, '{', '}') |
| 451 | if bracketCount > 0 || prevline =~ '.*{\s*$' |
| 452 | call scala#ConditionalConfirm("5b") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 453 | let ind = ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 454 | elseif bracketCount < 0 |
| 455 | call scala#ConditionalConfirm("6b") |
| 456 | " if the closing brace actually completes the braces entirely, then we |
| 457 | " have to indent to line that started the whole thing |
| 458 | let completeLine = scala#LineCompletesBrackets('{', '}') |
| 459 | if completeLine != -1 |
| 460 | call scala#ConditionalConfirm("8b") |
| 461 | let prevCompleteLine = scala#GetLine(prevnonblank(completeLine - 1)) |
| 462 | " However, what actually started this part looks like it was a function |
| 463 | " definition, so we need to indent to that line instead. This is |
| 464 | " actually pretty weak at the moment. |
| 465 | if prevCompleteLine =~ '=\s*$' |
| 466 | call scala#ConditionalConfirm("9b") |
| 467 | let ind = indent(prevnonblank(completeLine - 1)) |
| 468 | else |
| 469 | call scala#ConditionalConfirm("10b") |
| 470 | let ind = indent(completeLine) |
| 471 | endif |
| 472 | else |
| 473 | let lineCompletedBrackets = 1 |
| 474 | endif |
| 475 | endif |
| 476 | |
| 477 | if ind == originalIndentValue |
| 478 | let bracketCount = scala#CountBrackets(prevline, '(', ')') |
| 479 | if bracketCount > 0 || prevline =~ '.*(\s*$' |
| 480 | call scala#ConditionalConfirm("5a") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 481 | let ind = ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 482 | elseif bracketCount < 0 |
| 483 | call scala#ConditionalConfirm("6a") |
| 484 | " if the closing brace actually completes the braces entirely, then we |
| 485 | " have to indent to line that started the whole thing |
| 486 | let completeLine = scala#LineCompletesBrackets('(', ')') |
| 487 | if completeLine != -1 && prevline !~ '^.*{\s*$' |
| 488 | call scala#ConditionalConfirm("8a") |
| 489 | let prevCompleteLine = scala#GetLine(prevnonblank(completeLine - 1)) |
| 490 | " However, what actually started this part looks like it was a function |
| 491 | " definition, so we need to indent to that line instead. This is |
| 492 | " actually pretty weak at the moment. |
| 493 | if prevCompleteLine =~ '=\s*$' |
| 494 | call scala#ConditionalConfirm("9a") |
| 495 | let ind = indent(prevnonblank(completeLine - 1)) |
| 496 | else |
| 497 | call scala#ConditionalConfirm("10a") |
| 498 | let ind = indent(completeLine) |
| 499 | endif |
| 500 | else |
| 501 | " This is the only part that's different from from the '{', '}' one below |
| 502 | " Yup... some refactoring is necessary at some point. |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 503 | let ind = ind + (bracketCount * shiftwidth()) |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 504 | let lineCompletedBrackets = 1 |
| 505 | endif |
| 506 | endif |
| 507 | endif |
| 508 | |
| 509 | if curline =~ '^\s*}\?\s*\<else\>\%(\s\+\<if\>\s*(.*)\)\?\s*{\?\s*$' && |
| 510 | \ ! scala#LineIsCompleteIf(prevline) && |
| 511 | \ prevline !~ '^.*}\s*$' |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 512 | let ind = ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 513 | endif |
| 514 | |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 515 | " Subtract a shiftwidth()' on '}' or html |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 516 | let curCurlyCount = scala#CountCurlies(curline) |
| 517 | if curCurlyCount < 0 |
| 518 | call scala#ConditionalConfirm("14a") |
| 519 | let matchline = scala#CurlyMatcher() |
| 520 | return indent(matchline) |
| 521 | elseif curline =~ '^\s*</[a-zA-Z][^>]*>' |
| 522 | call scala#ConditionalConfirm("14c") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 523 | return ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 524 | endif |
| 525 | |
| 526 | let prevParenCount = scala#CountParens(prevline) |
| 527 | if prevline =~ '^\s*\<for\>.*$' && prevParenCount > 0 |
| 528 | call scala#ConditionalConfirm("15") |
| 529 | let ind = indent(prevlnum) + 5 |
| 530 | endif |
| 531 | |
| 532 | let prevCurlyCount = scala#CountCurlies(prevline) |
| 533 | if prevCurlyCount == 0 && prevline =~ '^.*\%(=>\|⇒\)\s*$' && prevline !~ '^\s*this\s*:.*\%(=>\|⇒\)\s*$' && curline !~ '^\s*\<case\>' |
| 534 | call scala#ConditionalConfirm("16") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 535 | let ind = ind + shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 536 | endif |
| 537 | |
| 538 | if ind == originalIndentValue && curline =~ '^\s*\<case\>' |
| 539 | call scala#ConditionalConfirm("17") |
| 540 | let parentCase = scala#IsParentCase() |
| 541 | if parentCase != -1 |
| 542 | call scala#ConditionalConfirm("17a") |
| 543 | return indent(parentCase) |
| 544 | endif |
| 545 | endif |
| 546 | |
| 547 | if prevline =~ '^\s*\*/' |
| 548 | \ || prevline =~ '*/\s*$' |
| 549 | call scala#ConditionalConfirm("18") |
| 550 | let ind = ind - star_indent |
| 551 | endif |
| 552 | |
| 553 | if scala#LineEndsInIncomplete(prevline) |
| 554 | call scala#ConditionalConfirm("19") |
| 555 | return ind |
| 556 | endif |
| 557 | |
| 558 | if scala#LineIsAClosingXML(prevline) |
| 559 | if scala#LineCompletesXML(prevlnum, prevline) |
| 560 | call scala#ConditionalConfirm("20a") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 561 | return ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 562 | else |
| 563 | call scala#ConditionalConfirm("20b") |
| 564 | return ind |
| 565 | endif |
| 566 | endif |
| 567 | |
| 568 | if ind == originalIndentValue |
| 569 | "let indentMultiplier = scala#LineCompletesDefValr(prevlnum, prevline) |
| 570 | "if indentMultiplier != 0 |
| 571 | " call scala#ConditionalConfirm("19a") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 572 | " let ind = ind - (indentMultiplier * shiftwidth()) |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 573 | let defValrLine = scala#Test(prevlnum, prevline, '{', '}') |
| 574 | if defValrLine != -1 |
| 575 | call scala#ConditionalConfirm("21a") |
| 576 | let ind = indent(defValrLine) |
| 577 | elseif lineCompletedBrackets == 0 |
| 578 | call scala#ConditionalConfirm("21b") |
| 579 | if scala#GetLine(prevnonblank(prevlnum - 1)) =~ '^.*\<else\>\s*\%(//.*\)\?$' |
| 580 | call scala#ConditionalConfirm("21c") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 581 | let ind = ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 582 | elseif scala#LineCompletesIfElse(prevlnum, prevline) |
| 583 | call scala#ConditionalConfirm("21d") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 584 | let ind = ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 585 | elseif scala#CountParens(curline) < 0 && curline =~ '^\s*)' && scala#GetLine(scala#GetLineThatMatchesBracket('(', ')')) =~ '.*(\s*$' |
| 586 | " Handles situations that look like this: |
| 587 | " |
| 588 | " val a = func( |
| 589 | " 10 |
| 590 | " ) |
| 591 | " |
| 592 | " or |
| 593 | " |
| 594 | " val a = func( |
| 595 | " 10 |
| 596 | " ).somethingHere() |
| 597 | call scala#ConditionalConfirm("21e") |
Bram Moolenaar | 036986f | 2017-03-16 17:41:02 +0100 | [diff] [blame] | 598 | let ind = ind - shiftwidth() |
Bram Moolenaar | e4a3bcf | 2016-08-26 19:52:37 +0200 | [diff] [blame] | 599 | endif |
| 600 | endif |
| 601 | endif |
| 602 | |
| 603 | call scala#ConditionalConfirm("returning " . ind) |
| 604 | |
| 605 | return ind |
| 606 | endfunction |
| 607 | |
| 608 | let &cpo = s:keepcpo |
| 609 | unlet s:keepcpo |
| 610 | |
| 611 | " vim:set sw=2 sts=2 ts=8 et: |
| 612 | " vim600:fdm=marker fdl=1 fdc=0: |