Skip to content

Commit

Permalink
A better understanding of what function definitions look like
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwyatt committed Mar 20, 2011
1 parent 8de39ef commit af7baa5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
8 changes: 8 additions & 0 deletions doc/scala_info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2765,6 +2765,14 @@ And you can combine this with a {for} expression.
sign: null, integer: 99, decimal: null
sign: null, integer: 3, decimal: null
!/sc!
The "date" example is cool too:
!sc!
scala> val regex = """(\d+)/(\d+)/(\d+)""".r
scala> val regex(year, month, day) = "2010/1/13"
year: String = 2010
month: String = 1
day: String = 13
!/sc!

==============================================================================
10. Packages and Access Modifiers *scala-packages* {{{1
Expand Down
30 changes: 26 additions & 4 deletions indent/scala.vim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ setlocal autoindent
" finish
"endif

let s:defMatcher = '\%(\%(private\|protected\)\%(\[[^\]]*\]\)\?\s\+\|abstract\s\+\|override\s\+\)*\<def\>'

function! scala#ConditionalConfirm(msg)
if 0
call confirm(a:msg)
Expand Down Expand Up @@ -47,7 +49,7 @@ endfunction
function! scala#IsParentCase()
let savedpos = getpos('.')
call setpos('.', [savedpos[0], savedpos[1], 0, savedpos[3]])
let [l, c] = searchpos('^\s*\%(\%(\%(abstract\s\+\)\?\%(override\s\+\)\?\<def\>\)\|\%(\<case\>\)\)', 'bnW')
let [l, c] = searchpos('^\s*\%(' . s:defMatcher . '\|\%(\<case\>\)\)', 'bnW')
let retvalue = -1
if l != 0 && search('\%' . l . 'l\s*\<case\>', 'bnW')
let retvalue = l
Expand All @@ -56,6 +58,26 @@ function! scala#IsParentCase()
return retvalue
endfunction

function! scala#CurlyMatcher()
let matchline = scala#GetLineThatMatchesBracket('{', '}')
if scala#CountParens(scala#GetLine(matchline)) < 0
let savedpos = getpos('.')
call setpos('.', [savedpos[0], matchline, 9999, savedpos[3]])
call searchpos('{', 'Wb')
call searchpos(')', 'Wb')
let [lnum, colnum] = searchpairpos('(', '', ')', 'Wbn')
call setpos('.', savedpos)
let line = scala#GetLine(lnum)
if line =~ '^\s*' . s:defMatcher
return lnum
else
return matchline
endif
else
return matchline
endif
endfunction

function! scala#GetLineThatMatchesBracket(openBracket, closedBracket)
let savedpos = getpos('.')
let curline = scala#GetLine(line('.'))
Expand Down Expand Up @@ -103,7 +125,7 @@ function! scala#NumberOfBraceGroups(line)
endfunction

function! scala#MatchesIncompleteDefValr(line)
if a:line =~ '^\s*\%(\%(\%(abstract\s\+\)\?\%(override\s\+\)\?\<def\>\)\|\<va[lr]\>\).*[=({]\s*$'
if a:line =~ '^\s*\%(' . s:defMatcher . '\|\<va[lr]\>\).*[=({]\s*$'
return 1
else
return 0
Expand Down Expand Up @@ -262,7 +284,7 @@ function! GetScalaIndent()
" If 'if', 'for' or 'while' end with ), this is a one-line block
" If 'val', 'var', 'def' end with =, this is a one-line block
if (prevline =~ '^\s*\<\%(\%(}\?\s*else\s\+\)\?if\|for\|while\)\>.*[)=]\s*$' && scala#NumberOfBraceGroups(prevline) <= 1)
\ || prevline =~ '^\s*\%(abstract\s\+\)\?\%(override\s\+\)\?\<def\>.*=\s*$'
\ || prevline =~ '^\s*' . s:defMatcher . '.*=\s*$'
\ || prevline =~ '^\s*\<va[lr]\>.*[=]\s*$'
\ || prevline =~ '^\s*\%(}\s*\)\?\<else\>\s*$'
\ || prevline =~ '=\s*$'
Expand Down Expand Up @@ -340,7 +362,7 @@ function! GetScalaIndent()
let curCurlyCount = scala#CountCurlies(curline)
if curCurlyCount < 0
call scala#ConditionalConfirm("14a")
let matchline = scala#GetLineThatMatchesBracket('{', '}')
let matchline = scala#CurlyMatcher()
return indent(matchline)
elseif curline =~ '^\s*</[a-zA-Z][^>]*>'
call scala#ConditionalConfirm("14c")
Expand Down
27 changes: 25 additions & 2 deletions indent/testfile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,28 @@ class SomeClass {
case There =>
}

def somethingWithDots = {
def func = oneliner
private def func = oneliner
private[thing] def func = oneliner
protected def func = oneliner
protected[thing] def func = oneliner
override def func = oneliner
abstract override def func = oneliner
override abstract def func = oneliner
protected override def func = oneliner
protected abstract override def func = oneliner
protected override abstract def func = oneliner
protected[thing] override def func = oneliner
protected[thing] abstract override def func = oneliner
protected[thing] override abstract def func = oneliner
private override def func = oneliner
private abstract override def func = oneliner
private override abstract def func = oneliner
private[thing] override def func = oneliner
private[thing] abstract override def func = oneliner
private[thing] override abstract def func = oneliner

private[somepackage] abstract override def somethingWithDots = {
SomeObject.build
.withSomething
.withSomethingElse
Expand All @@ -288,5 +309,7 @@ class SomeClass {
some stuff here
}
}
}
}

private[this] def followingFunction = oneliner
}

0 comments on commit af7baa5

Please sign in to comment.