Bram Moolenaar | 4a74803 | 2010-09-30 21:47:56 +0200 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: Falcon |
| 3 | " Maintainer: Steven Oliver <oliver.steven@gmail.com> |
| 4 | " Website: https://steveno@github.com/steveno/falconpl-vim.git |
| 5 | " Credits: Thanks to the ruby.vim authors, I borrow a lot! |
| 6 | " Previous Maintainer: Brent A. Fulgham <bfulgham@debian.org> |
| 7 | " ----------------------------------------------------------- |
Bram Moolenaar | 4a74803 | 2010-09-30 21:47:56 +0200 | [diff] [blame] | 8 | |
| 9 | "====================================== |
| 10 | " SETUP |
| 11 | "====================================== |
| 12 | |
| 13 | " Only load this indent file when no other was loaded. |
| 14 | if exists("b:did_indent") |
| 15 | finish |
| 16 | endif |
| 17 | let b:did_indent = 1 |
| 18 | |
| 19 | setlocal nosmartindent |
| 20 | |
| 21 | " Setup indent function and when to use it |
| 22 | setlocal indentexpr=FalconGetIndent() |
| 23 | setlocal indentkeys=0{,0},0),0],!^F,o,O,e |
| 24 | setlocal indentkeys+==~case,=~catch,=~default,=~elif,=~else,=~end,=~\" |
| 25 | |
| 26 | " Define the appropriate indent function but only once |
| 27 | if exists("*FalconGetIndent") |
| 28 | finish |
| 29 | endif |
| 30 | |
| 31 | let s:cpo_save = &cpo |
| 32 | set cpo&vim |
| 33 | |
| 34 | "====================================== |
| 35 | " VARIABLES |
| 36 | "====================================== |
| 37 | |
| 38 | " Regex of syntax group names that are strings AND comments |
| 39 | let s:syng_strcom = '\<falcon\%(String\|StringEscape\|Comment\)\>' |
| 40 | |
| 41 | " Regex of syntax group names that are strings |
| 42 | let s:syng_string = '\<falcon\%(String\|StringEscape\)\>' |
| 43 | |
| 44 | " Keywords to indent on |
| 45 | let s:falcon_indent_keywords = '^\s*\(case\|catch\|class\|enum\|default\|elif\|else' . |
| 46 | \ '\|for\|function\|if.*"[^"]*:.*"\|if \(\(:\)\@!.\)*$\|loop\|object\|select' . |
| 47 | \ '\|switch\|try\|while\|\w*\s*=\s*\w*([$\)' |
| 48 | |
| 49 | " Keywords to deindent on |
| 50 | let s:falcon_deindent_keywords = '^\s*\(case\|catch\|default\|elif\|else\|end\)' |
| 51 | |
| 52 | "====================================== |
| 53 | " FUNCTIONS |
| 54 | "====================================== |
| 55 | |
| 56 | " Check if the character at lnum:col is inside a string |
| 57 | function s:IsInStringOrComment(lnum, col) |
| 58 | return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom |
| 59 | endfunction |
| 60 | |
| 61 | "====================================== |
| 62 | " INDENT ROUTINE |
| 63 | "====================================== |
| 64 | |
| 65 | function FalconGetIndent() |
| 66 | " Get the line to be indented |
| 67 | let cline = getline(v:lnum) |
| 68 | |
| 69 | " Don't reindent comments on first column |
| 70 | if cline =~ '^\/\/' |
| 71 | return 0 |
| 72 | endif |
| 73 | |
| 74 | " Find the previous non-blank line |
| 75 | let lnum = prevnonblank(v:lnum - 1) |
| 76 | |
| 77 | " Use zero indent at the top of the file |
| 78 | if lnum == 0 |
| 79 | return 0 |
| 80 | endif |
| 81 | |
| 82 | let prevline=getline(lnum) |
| 83 | let ind = indent(lnum) |
| 84 | let chg = 0 |
| 85 | |
| 86 | " If we are in a multi-line string or line-comment, don't do anything |
| 87 | if s:IsInStringOrComment(v:lnum, matchend(cline, '^\s*') + 1 ) |
| 88 | return indent('.') |
| 89 | endif |
| 90 | |
| 91 | " If the start of the line equals a double quote, then indent to the |
| 92 | " previous lines first double quote |
| 93 | if cline =~? '^\s*"' |
| 94 | let chg = chg + &sw |
| 95 | endif |
| 96 | |
| 97 | " If previous line started with a double quote and this one |
| 98 | " doesn't, unindent |
| 99 | if prevline =~? '^\s*"' && cline =~? '^\s*' |
| 100 | let chg = chg - &sw |
| 101 | endif |
| 102 | |
| 103 | " Indent if proper keyword |
| 104 | if prevline =~? s:falcon_indent_keywords |
| 105 | let chg = &sw |
| 106 | " If previous line opened a parenthesis, and did not close it, indent |
| 107 | elseif prevline =~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 108 | " Make sure this isn't just a function split between two lines |
| 109 | if prevline =~ ',\s*$' |
| 110 | return indent(prevnonblank(v:lnum - 1)) + &sw |
| 111 | else |
| 112 | return match(prevline, '(.*\((.*)\|[^)]\)*.*$') + 1 |
| 113 | endif |
| 114 | elseif prevline =~ '^[^(]*)\s*$' |
| 115 | " This line closes a parenthesis. Finds opening. |
| 116 | let curr_line = prevnonblank(lnum - 1) |
| 117 | while curr_line >= 0 |
| 118 | let str = getline(curr_line) |
| 119 | if str !~ '^.*(\s*[^)]*\((.*)\)*[^)]*$' |
| 120 | let curr_line = prevnonblank(curr_line - 1) |
| 121 | else |
| 122 | break |
| 123 | endif |
| 124 | endwhile |
| 125 | if curr_line < 0 |
| 126 | return -1 |
| 127 | endif |
| 128 | let ind = indent(curr_line) |
| 129 | endif |
| 130 | |
| 131 | " If previous line ends in a semi-colon reset indent to previous |
| 132 | " lines setting |
| 133 | if prevline =~? ';\s*$' && prevnonblank(prevline) =~? ',\s*$' |
Bram Moolenaar | 34700a6 | 2013-03-07 13:20:54 +0100 | [diff] [blame] | 134 | let chg = chg - (2 * &sw) |
Bram Moolenaar | 4a74803 | 2010-09-30 21:47:56 +0200 | [diff] [blame] | 135 | endif |
| 136 | |
| 137 | " If previous line ended in a comma, indent again |
| 138 | if prevline =~? ',\s*$' |
| 139 | let chg = chg + &sw |
| 140 | endif |
| 141 | |
| 142 | " If previous line ended in a =>, indent again |
| 143 | if prevline =~? '=>\s*$' |
| 144 | let chg = chg + &sw |
| 145 | endif |
| 146 | |
| 147 | " Deindent on proper keywords |
| 148 | if cline =~? s:falcon_deindent_keywords |
| 149 | let chg = chg - &sw |
| 150 | endif |
| 151 | |
| 152 | return ind + chg |
| 153 | endfunction |
| 154 | |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 155 | let &cpo = s:cpo_save |
| 156 | unlet s:cpo_save |
| 157 | |
Bram Moolenaar | 4a74803 | 2010-09-30 21:47:56 +0200 | [diff] [blame] | 158 | " vim: set sw=4 sts=4 et tw=80 : |