Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM and Scala -- indentation problems?

Tags:

vim

scala

I downloaded Scala 2.8, installed the vim scripts included and tried to type in some Scala code. When I typed in val x = 1 + 2 and hit ENTER, the indentation goes to below the v. When I type in val x = (1 + 2), the indentation is below the x!

If VIM is used by anyone at all for Scala, this bug should've been seen long ago. Or am I the only one seeing this?

like image 458
qrest Avatar asked Aug 31 '10 04:08

qrest


People also ask

How do I change the indentation in Vim?

Fix indentation in the whole fileStart in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.

How do I turn off auto indent in Vim?

How to Turn Off Vim Auto Indent. You can also temporarily turn off automatic indenting by typing :set paste in command mode. (:unset paste to turn it back on again.) This is useful — as you might guess from the command name — if you're pasting in chunks of text or code to avoid getting annoying extraneous indents.

How do I turn on auto indent in Vim?

To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing. If you set the auto-indent feature in Vim in command mode, it does not persist upon closing the editor.


1 Answers

With the indent/scala.vim from the current 2.8.0.final release I have the same outcome... But I know, that it worked in a earlier release, because I have one file here where it works. Here it is:

" Vim indent file
" Language   : Scala (http://scala-lang.org/)
" Maintainer : Stefan Matthias Aust
" Last Change: 2006 Apr 13

if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal indentexpr=GetScalaIndent()

setlocal indentkeys=0{,0},0),!^F,<>>,<CR>

setlocal autoindent sw=2 et

if exists("*GetScalaIndent")
  finish
endif

function! CountParens(line)
  let line = substitute(a:line, '"\(.\|\\"\)*"', '', 'g')
  let open = substitute(line, '[^(]', '', 'g')
  let close = substitute(line, '[^)]', '', 'g')
  return strlen(open) - strlen(close)
endfunction

function! GetScalaIndent()
  " Find a non-blank line above the current line.
  let lnum = prevnonblank(v:lnum - 1)

  " Hit the start of the file, use zero indent.
  if lnum == 0
    return 0
  endif

  let ind = indent(lnum)
  let prevline = getline(lnum)

  "Indent html literals
  if prevline !~ '/>\s*$' && prevline =~ '^\s*<[a-zA-Z][^>]*>\s*$'
    return ind + &shiftwidth
  endif

  "### Taken from mail on scala mailing list
  "### -------------------------------------
  " Add a 'shiftwidth' after lines that start a block
  " 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*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
      "\ || prevline =~ '^\s*\<else\>\s*$'
      "\ || prevline =~ '{\s*$'
      "let ind = ind + &shiftwidth
      "endif
      " Add a 'shiftwidth' after lines that start a block
      " 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*\<\(\(else\s\+\)\?if\|for\|while\)\>.*[)]\s*$'
                  \ || prevline =~ '^\s*\<\(\(va[lr]\|def\)\>.*[=]\s*$'
                  \ || prevline =~ '^\s*\<else\>\s*$'
                  \ || prevline =~ '{\s*$'
          let ind = ind + &shiftwidth
      endif

  " If parenthesis are unbalanced, indent or dedent
  let c = CountParens(prevline)
  echo c
  if c > 0
    let ind = ind + &shiftwidth
  elseif c < 0
    let ind = ind - &shiftwidth
  endif

  "### Taken from mail on scala mailing list
  "### -------------------------------------
  " Dedent after if, for, while and val, var, def without block
  "let pprevline = getline(prevnonblank(lnum - 1))
  "if pprevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\|va[lr]\|def\)\>.*[)=]\s*$'
      "\ || pprevline =~ '^\s*\<else\>\s*$'
      "let ind = ind - &shiftwidth
      "endif
      " Dedent after if, for, while and val, var, def without block
      "let pprevline = getline(prevnonblank(lnum - 1))
      if pprevline =~ '^\s*\<\(\(else\s\+\)\?if\|for\|while\)\>.*[)]\s*$'
                  \ || pprevline =~ '^\s*\<\(\va[lr]\|def\)\>.*[=]\s*$'
                  \ || pprevline =~ '^\s*\<else\>\s*$'
          let ind = ind - &shiftwidth
      endif

  " Align 'for' clauses nicely
  if prevline =~ '^\s*\<for\> (.*;\s*$'
    let ind = ind - &shiftwidth + 5
  endif

  " Subtract a 'shiftwidth' on '}' or html
  let thisline = getline(v:lnum)
  if thisline =~ '^\s*[})]'
        \ || thisline =~ '^\s*</[a-zA-Z][^>]*>'
    let ind = ind - &shiftwidth
  endif

  return ind
endfunction

But I have no clue, where the change was introduced... Tried to find it in the SVN history at https://codereview.scala-lang.org/fisheye/browse/scala-svn/scala-tool-support/trunk/src/vim/indent/scala.vim

like image 122
michael.kebe Avatar answered Oct 06 '22 22:10

michael.kebe