Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Ruby |
| 3 | " Maintainer: Gavin Sinclair <gsinclair@soyabean.com.au> |
| 4 | " Last Change: 2003 May 11 |
| 5 | " URL: www.soyabean.com.au/gavin/vim/index.html |
| 6 | " Changes: (since vim 6.1) |
| 7 | " - indentation after a line ending in comma, etc, (even in a comment) was |
| 8 | " broken, now fixed (2002/08/14) |
| 9 | |
| 10 | " Only load this indent file when no other was loaded. |
| 11 | if exists("b:did_indent") |
| 12 | finish |
| 13 | endif |
| 14 | let b:did_indent = 1 |
| 15 | |
| 16 | setlocal indentexpr=GetRubyIndent() |
| 17 | setlocal nolisp |
| 18 | setlocal nosmartindent |
| 19 | setlocal autoindent |
| 20 | setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue |
| 21 | |
| 22 | " Only define the function once. |
| 23 | if exists("*GetRubyIndent") |
| 24 | finish |
| 25 | endif |
| 26 | |
| 27 | function GetRubyIndent() |
| 28 | " Find a non-blank line above the current line. |
| 29 | let lnum = prevnonblank(v:lnum - 1) |
| 30 | |
| 31 | " At the start of the file use zero indent. |
| 32 | if lnum == 0 |
| 33 | return 0 |
| 34 | endif |
| 35 | |
| 36 | " If the line trailed with [*+,.(] - but not in a comment - trust the user |
| 37 | if getline(lnum) =~ '\(\[^#\].*\)?\(\*\|\.\|+\|,\|(\)\(\s*#.*\)\=$' |
| 38 | return -1 |
| 39 | endif |
| 40 | |
| 41 | " Add a 'shiftwidth' after lines beginning with: |
| 42 | " module, class, dev, if, for, while, until, else, elsif, case, when, { |
| 43 | let ind = indent(lnum) |
| 44 | let flag = 0 |
| 45 | if getline(lnum) =~ '^\s*\(module\>\|class\>\|def\>\|if\>\|for\>\|while\>\|until\>\|else\>\|elsif\>\|case\>\|when\>\|unless\|begin\|ensure\>\|rescue\>\)' |
| 46 | \ || getline(lnum) =~ '{\s*$' |
| 47 | \ || getline(lnum) =~ '\({\|\<do\>\).*|.*|\s*$' |
| 48 | \ || getline(lnum) =~ '\<do\>\(\s*#.*\)\=$' |
| 49 | let ind = ind + &sw |
| 50 | let flag = 1 |
| 51 | endif |
| 52 | |
| 53 | " Subtract a 'shiftwidth' after lines ending with |
| 54 | " "end" when they begin with while, if, for, until |
| 55 | if flag == 1 && getline(lnum) =~ '\<end\>\(\s*#.*\)\=$' |
| 56 | let ind = ind - &sw |
| 57 | endif |
| 58 | |
| 59 | " Subtract a 'shiftwidth' on end, else and, elsif, when and } |
| 60 | if getline(v:lnum) =~ '^\s*\(end\>\|else\>\|elsif\>\|when\>\|ensure\>\|rescue\>\|}\)' |
| 61 | let ind = ind - &sw |
| 62 | endif |
| 63 | |
| 64 | return ind |
| 65 | endfunction |
| 66 | |
| 67 | " vim:sw=2 |