Skip to content

Commit bbdfea4

Browse files
crater2150derekwyatt
authored andcommitted
Fix indentation for defs and vals with modifiers
Fixes indentation for defs, vals and vars with annotation on same line, final defs and vals/vars, and several other keywords for vals/vars (like private/lazy/...)
1 parent fb87a6c commit bbdfea4

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

indent/scala.vim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ if exists("*GetScalaIndent")
1717
finish
1818
endif
1919

20-
let s:defMatcher = '\%(\%(private\|protected\)\%(\[[^\]]*\]\)\?\s\+\|abstract\s\+\|override\s\+\)*\<def\>'
20+
let s:annotationMatcher = '@[A-Za-z._]\+\s\+'
21+
let s:modifierMatcher = s:annotationMatcher . '\|\%(private\|protected\)\%(\[[^\]]*\]\)\?\s\+\|abstract\s\+\|override\s\+\|final\s\+'
22+
let s:defMatcher = '\%(' . s:modifierMatcher . '\)*\<def\>'
23+
let s:valMatcher = '\%(' . s:modifierMatcher . '\|lazy\s\+\)*\<va[lr]\>'
2124
let s:funcNameMatcher = '\w\+'
2225
let s:typeSpecMatcher = '\%(\s*\[\_[^\]]*\]\)'
2326
let s:defArgMatcher = '\%((\_.\{-})\)'
@@ -181,7 +184,7 @@ function! scala#NumberOfBraceGroups(line)
181184
endfunction
182185

183186
function! scala#MatchesIncompleteDefValr(line)
184-
if a:line =~ '^\s*\%(' . s:defMatcher . '\|\<va[lr]\>\).*[=({]\s*$'
187+
if a:line =~ '^\s*\%(' . s:defMatcher . '\|' . s:valMatcher . '\).*[=({]\s*$'
185188
return 1
186189
else
187190
return 0
@@ -431,7 +434,7 @@ function! GetScalaIndent()
431434
" If 'val', 'var', 'def' end with =, this is a one-line block
432435
if (prevline =~ '^\s*\<\%(\%(}\?\s*else\s\+\)\?if\|for\|while\)\>.*[)=]\s*$' && scala#NumberOfBraceGroups(prevline) <= 1)
433436
\ || prevline =~ '^\s*' . s:defMatcher . '.*=\s*$'
434-
\ || prevline =~ '^\s*\<va[lr]\>.*[=]\s*$'
437+
\ || prevline =~ '^\s*' . s:valMatcher . '.*[=]\s*$'
435438
\ || prevline =~ '^\s*\%(}\s*\)\?\<else\>\s*$'
436439
\ || prevline =~ '=\s*$'
437440
call scala#ConditionalConfirm("4")

indent/testfile.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,25 @@ class SomeClass {
380380
}
381381
}
382382

383+
/** annotations should not change indent, also allow modifiers for vals */
384+
@annotation.tailrec def someFunction =
385+
some stuff here
386+
387+
final def foo2 =
388+
some stuff here
389+
390+
final val someValue =
391+
some stuff here
392+
393+
private val someValue =
394+
some stuff here
395+
396+
lazy val someValue =
397+
some stuff here
398+
399+
@annotation.foo val someValue =
400+
some stuff here
401+
383402
private[this] def followingFunction = oneliner
384403

385404
val someFunction: List[(Option[T], Option[U])] = TODO

0 commit comments

Comments
 (0)