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 8if exists("b:did_indent") 9 finish 10endif 11let b:did_indent = 1 12 13setlocal autoindent 14setlocal indentexpr=GetScalaIndent() 15setlocal indentkeys=0{,0},0),!^F,<>>,o,O,e,=case,<CR> 16 17if exists("*GetScalaIndent") 18 finish 19endif 20let s:keepcpo= &cpo 21set cpo&vim 22 23let s:annotationMatcher = '@[A-Za-z._]\+\s\+' 24let s:modifierMatcher = s:annotationMatcher . '\|\%(private\|protected\)\%(\[[^\]]*\]\)\?\s\+\|abstract\s\+\|override\s\+\|final\s\+' 25let s:defMatcher = '\%(' . s:modifierMatcher . '\)*\<def\>' 26let s:valMatcher = '\%(' . s:modifierMatcher . '\|lazy\s\+\)*\<va[lr]\>' 27let s:funcNameMatcher = '\w\+' 28let s:typeSpecMatcher = '\%(\s*\[\_[^\]]*\]\)' 29let s:defArgMatcher = '\%((\_.\{-})\)' 30let s:returnTypeMatcher = '\%(:\s*\w\+' . s:typeSpecMatcher . '\?\)' 31let g:fullDefMatcher = '^\s*' . s:defMatcher . '\s\+' . s:funcNameMatcher . '\s*' . s:typeSpecMatcher . '\?\s*' . s:defArgMatcher . '\?\s*' . s:returnTypeMatcher . '\?\s*[={]' 32 33function! scala#ConditionalConfirm(msg) 34 if 0 35 call confirm(a:msg) 36 endif 37endfunction 38 39function! scala#GetLine(lnum) 40 let line = substitute(getline(a:lnum), '//.*$', '', '') 41 let line = substitute(line, '"\(.\|\\"\)\{-}"', '""', 'g') 42 return line 43endfunction 44 45function! 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) 50endfunction 51 52function! scala#CountParens(line) 53 return scala#CountBrackets(a:line, '(', ')') 54endfunction 55 56function! scala#CountCurlies(line) 57 return scala#CountBrackets(a:line, '{', '}') 58endfunction 59 60function! scala#LineEndsInIncomplete(line) 61 if a:line =~ '[.,]\s*$' 62 return 1 63 else 64 return 0 65 endif 66endfunction 67 68function! scala#LineIsAClosingXML(line) 69 if a:line =~ '^\s*</\w' 70 return 1 71 else 72 return 0 73 endif 74endfunction 75 76function! 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 88endfunction 89 90function! 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 100endfunction 101 102function! 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 120endfunction 121 122function! scala#GetLineAndColumnThatMatchesCurly() 123 return scala#GetLineAndColumnThatMatchesBracket('{', '}') 124endfunction 125 126function! scala#GetLineAndColumnThatMatchesParen() 127 return scala#GetLineAndColumnThatMatchesBracket('(', ')') 128endfunction 129 130function! 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] 143endfunction 144 145function! scala#GetLineThatMatchesCurly() 146 return scala#GetLineThatMatchesBracket('{', '}') 147endfunction 148 149function! scala#GetLineThatMatchesParen() 150 return scala#GetLineThatMatchesBracket('(', ')') 151endfunction 152 153function! scala#GetLineThatMatchesBracket(openBracket, closedBracket) 154 let [lnum, colnum] = scala#GetLineAndColumnThatMatchesBracket(a:openBracket, a:closedBracket) 155 return lnum 156endfunction 157 158function! 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 187endfunction 188 189function! scala#MatchesIncompleteDefValr(line) 190 if a:line =~ '^\s*\%(' . s:defMatcher . '\|' . s:valMatcher . '\).*[=({]\s*$' 191 return 1 192 else 193 return 0 194 endif 195endfunction 196 197function! 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 205endfunction 206 207function! 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 216endfunction 217 218function! scala#GetPrevCodeLine(lnum) 219 " This needs to skip comment lines 220 return prevnonblank(a:lnum - 1) 221endfunction 222 223function! scala#InvertBracketType(openBracket, closedBracket) 224 if a:openBracket == '(' 225 return [ '{', '}' ] 226 else 227 return [ '(', ')' ] 228 endif 229endfunction 230 231function! 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 286endfunction 287 288function! scala#Test(lnum, line, openBracket, closedBracket) 289 return scala#Testhelper(a:lnum, a:line, a:openBracket, a:closedBracket, 0) 290endfunction 291 292function! 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 341endfunction 342 343function! 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 350endfunction 351 352function! 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 369endfunction 370 371function! 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 418 return ind + shiftwidth() 419 endif 420 endif 421 422 " Indent html literals 423 if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$' 424 call scala#ConditionalConfirm("3") 425 return ind + shiftwidth() 426 endif 427 428 " assumes curly braces around try-block 429 if curline =~ '^\s*}\s*\<catch\>' 430 return ind - shiftwidth() 431 elseif curline =~ '^\s*\<catch\>' 432 return ind 433 endif 434 435 " Add a shiftwidth()' after lines that start a block 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*$' 440 \ || prevline =~ '^\s*' . s:valMatcher . '.*[=]\s*$' 441 \ || prevline =~ '^\s*\%(}\s*\)\?\<else\>\s*$' 442 \ || prevline =~ '=\s*$' 443 call scala#ConditionalConfirm("4") 444 let ind = ind + shiftwidth() 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") 453 let ind = ind + shiftwidth() 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") 481 let ind = ind + shiftwidth() 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. 503 let ind = ind + (bracketCount * shiftwidth()) 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*$' 512 let ind = ind - shiftwidth() 513 endif 514 515 " Subtract a shiftwidth()' on '}' or html 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") 523 return ind - shiftwidth() 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") 535 let ind = ind + shiftwidth() 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") 561 return ind - shiftwidth() 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") 572 " let ind = ind - (indentMultiplier * shiftwidth()) 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") 581 let ind = ind - shiftwidth() 582 elseif scala#LineCompletesIfElse(prevlnum, prevline) 583 call scala#ConditionalConfirm("21d") 584 let ind = ind - shiftwidth() 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") 598 let ind = ind - shiftwidth() 599 endif 600 endif 601 endif 602 603 call scala#ConditionalConfirm("returning " . ind) 604 605 return ind 606endfunction 607 608let &cpo = s:keepcpo 609unlet s:keepcpo 610 611" vim:set sw=2 sts=2 ts=8 et: 612" vim600:fdm=marker fdl=1 fdc=0: 613