diff options
Diffstat (limited to 'indent/ruby.vim')
-rw-r--r-- | indent/ruby.vim | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/indent/ruby.vim b/indent/ruby.vim index 8fa929be..3617a925 100644 --- a/indent/ruby.vim +++ b/indent/ruby.vim @@ -49,22 +49,21 @@ set cpo&vim " 1. Variables {{{1 " ============ -" Regex of syntax group names that are or delimit strings/symbols or are comments. -let s:syng_strcom = '\<ruby\%(Regexp\|RegexpDelimiter\|RegexpEscape' . - \ '\|Symbol\|String\|StringDelimiter\|StringEscape\|ASCIICode' . - \ '\|Interpolation\|InterpolationDelimiter\|NoInterpolation\|Comment\|Documentation\)\>' - -" Regex of syntax group names that are strings. +" Syntax group names that are strings. let s:syng_string = - \ '\<ruby\%(String\|Interpolation\|InterpolationDelimiter\|NoInterpolation\|StringEscape\)\>' + \ ['String', 'Interpolation', 'InterpolationDelimiter', 'NoInterpolation', 'StringEscape'] + +" Syntax group names that are strings or documentation. +let s:syng_stringdoc = s:syng_string + ['Documentation'] -" Regex of syntax group names that are strings or documentation. -let s:syng_stringdoc = - \ '\<ruby\%(String\|Interpolation\|InterpolationDelimiter\|NoInterpolation\|StringEscape\|Documentation\)\>' +" Syntax group names that are or delimit strings/symbols/regexes or are comments. +let s:syng_strcom = s:syng_stringdoc + + \ ['Regexp', 'RegexpDelimiter', 'RegexpEscape', + \ 'Symbol', 'StringDelimiter', 'ASCIICode', 'Comment'] " Expression used to check whether we should skip a match with searchpair(). let s:skip_expr = - \ "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'" + \ 'index(map('.string(s:syng_strcom).',"hlID(''ruby''.v:val)"), synID(line("."),col("."),1)) >= 0' " Regex used for words that, at the start of a line, add a level of indent. let s:ruby_indent_keywords = @@ -152,7 +151,7 @@ let s:leading_operator_regex = '^\s*[.]' " 2. GetRubyIndent Function {{{1 " ========================= -function GetRubyIndent(...) +function! GetRubyIndent(...) abort " 2.1. Setup {{{2 " ---------- @@ -255,7 +254,7 @@ endfunction " 3. Indenting Logic Callbacks {{{1 " ============================ -function! s:AccessModifier(cline_info) +function! s:AccessModifier(cline_info) abort let info = a:cline_info " If this line is an access modifier keyword, align according to the closest @@ -279,7 +278,7 @@ function! s:AccessModifier(cline_info) return -1 endfunction -function! s:ClosingBracketOnEmptyLine(cline_info) +function! s:ClosingBracketOnEmptyLine(cline_info) abort let info = a:cline_info " If we got a closing bracket on an empty line, find its match and indent @@ -308,7 +307,7 @@ function! s:ClosingBracketOnEmptyLine(cline_info) return -1 endfunction -function! s:BlockComment(cline_info) +function! s:BlockComment(cline_info) abort " If we have a =begin or =end set indent to first column. if match(a:cline_info.cline, '^\s*\%(=begin\|=end\)$') != -1 return 0 @@ -316,7 +315,7 @@ function! s:BlockComment(cline_info) return -1 endfunction -function! s:DeindentingKeyword(cline_info) +function! s:DeindentingKeyword(cline_info) abort let info = a:cline_info " If we have a deindenting keyword, find its match and indent to its level. @@ -357,7 +356,7 @@ function! s:DeindentingKeyword(cline_info) return -1 endfunction -function! s:MultilineStringOrLineComment(cline_info) +function! s:MultilineStringOrLineComment(cline_info) abort let info = a:cline_info " If we are in a multi-line string or line-comment, don't do anything to it. @@ -367,7 +366,7 @@ function! s:MultilineStringOrLineComment(cline_info) return -1 endfunction -function! s:ClosingHeredocDelimiter(cline_info) +function! s:ClosingHeredocDelimiter(cline_info) abort let info = a:cline_info " If we are at the closing delimiter of a "<<" heredoc-style string, set the @@ -381,7 +380,7 @@ function! s:ClosingHeredocDelimiter(cline_info) return -1 endfunction -function! s:LeadingOperator(cline_info) +function! s:LeadingOperator(cline_info) abort " If the current line starts with a leading operator, add a level of indent. if s:Match(a:cline_info.clnum, s:leading_operator_regex) return indent(s:GetMSL(a:cline_info.clnum)) + a:cline_info.sw @@ -389,7 +388,7 @@ function! s:LeadingOperator(cline_info) return -1 endfunction -function! s:EmptyInsideString(pline_info) +function! s:EmptyInsideString(pline_info) abort " If the line is empty and inside a string (plnum would not be the real " prevnonblank in that case), use the previous line's indent let info = a:pline_info @@ -400,7 +399,7 @@ function! s:EmptyInsideString(pline_info) return -1 endfunction -function! s:StartOfFile(pline_info) +function! s:StartOfFile(pline_info) abort " At the start of the file use zero indent. if a:pline_info.plnum == 0 return 0 @@ -408,7 +407,7 @@ function! s:StartOfFile(pline_info) return -1 endfunction -function! s:AfterAccessModifier(pline_info) +function! s:AfterAccessModifier(pline_info) abort let info = a:pline_info if g:ruby_indent_access_modifier_style == 'indent' @@ -434,7 +433,7 @@ endfunction " puts "foo" " end " -function! s:ContinuedLine(pline_info) +function! s:ContinuedLine(pline_info) abort let info = a:pline_info let col = s:Match(info.plnum, s:ruby_indent_keywords) @@ -456,7 +455,7 @@ function! s:ContinuedLine(pline_info) return -1 endfunction -function! s:AfterBlockOpening(pline_info) +function! s:AfterBlockOpening(pline_info) abort let info = a:pline_info " If the previous line ended with a block opening, add a level of indent. @@ -482,7 +481,7 @@ function! s:AfterBlockOpening(pline_info) return -1 endfunction -function! s:AfterLeadingOperator(pline_info) +function! s:AfterLeadingOperator(pline_info) abort " If the previous line started with a leading operator, use its MSL's level " of indent if s:Match(a:pline_info.plnum, s:leading_operator_regex) @@ -491,7 +490,7 @@ function! s:AfterLeadingOperator(pline_info) return -1 endfunction -function! s:AfterHangingSplat(pline_info) +function! s:AfterHangingSplat(pline_info) abort let info = a:pline_info " If the previous line ended with the "*" of a splat, add a level of indent @@ -501,7 +500,7 @@ function! s:AfterHangingSplat(pline_info) return -1 endfunction -function! s:AfterUnbalancedBracket(pline_info) +function! s:AfterUnbalancedBracket(pline_info) abort let info = a:pline_info " If the previous line contained unclosed opening brackets and we are still @@ -541,7 +540,7 @@ function! s:AfterUnbalancedBracket(pline_info) return -1 endfunction -function! s:AfterEndKeyword(pline_info) +function! s:AfterEndKeyword(pline_info) abort let info = a:pline_info " If the previous line ended with an "end", match that "end"s beginning's " indent. @@ -562,7 +561,7 @@ function! s:AfterEndKeyword(pline_info) return -1 endfunction -function! s:AfterIndentKeyword(pline_info) +function! s:AfterIndentKeyword(pline_info) abort let info = a:pline_info let col = s:Match(info.plnum, s:ruby_indent_keywords) @@ -589,7 +588,7 @@ function! s:AfterIndentKeyword(pline_info) return -1 endfunction -function! s:PreviousNotMSL(msl_info) +function! s:PreviousNotMSL(msl_info) abort let info = a:msl_info " If the previous line wasn't a MSL @@ -608,7 +607,7 @@ function! s:PreviousNotMSL(msl_info) return -1 endfunction -function! s:IndentingKeywordInMSL(msl_info) +function! s:IndentingKeywordInMSL(msl_info) abort let info = a:msl_info " If the MSL line had an indenting keyword in it, add a level of indent. " TODO: this does not take into account contrived things such as @@ -632,7 +631,7 @@ function! s:IndentingKeywordInMSL(msl_info) return -1 endfunction -function! s:ContinuedHangingOperator(msl_info) +function! s:ContinuedHangingOperator(msl_info) abort let info = a:msl_info " If the previous line ended with [*+/.,-=], but wasn't a block ending or a @@ -652,32 +651,37 @@ endfunction " 4. Auxiliary Functions {{{1 " ====================== +function! s:IsInRubyGroup(groups, lnum, col) abort + let ids = map(copy(a:groups), 'hlID("ruby".v:val)') + return index(ids, synID(a:lnum, a:col, 1)) >= 0 +endfunction + " Check if the character at lnum:col is inside a string, comment, or is ascii. -function s:IsInStringOrComment(lnum, col) - return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom +function! s:IsInStringOrComment(lnum, col) abort + return s:IsInRubyGroup(s:syng_strcom, a:lnum, a:col) endfunction " Check if the character at lnum:col is inside a string. -function s:IsInString(lnum, col) - return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string +function! s:IsInString(lnum, col) abort + return s:IsInRubyGroup(s:syng_string, a:lnum, a:col) endfunction " Check if the character at lnum:col is inside a string or documentation. -function s:IsInStringOrDocumentation(lnum, col) - return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc +function! s:IsInStringOrDocumentation(lnum, col) abort + return s:IsInRubyGroup(s:syng_stringdoc, a:lnum, a:col) endfunction " Check if the character at lnum:col is inside a string delimiter -function s:IsInStringDelimiter(lnum, col) - return synIDattr(synID(a:lnum, a:col, 1), 'name') == 'rubyStringDelimiter' +function! s:IsInStringDelimiter(lnum, col) abort + return s:IsInRubyGroup(['StringDelimiter'], a:lnum, a:col) endfunction -function s:IsAssignment(str, pos) +function! s:IsAssignment(str, pos) abort return strpart(a:str, 0, a:pos - 1) =~ '=\s*$' endfunction " Find line above 'lnum' that isn't empty, in a comment, or in a string. -function s:PrevNonBlankNonString(lnum) +function! s:PrevNonBlankNonString(lnum) abort let in_block = 0 let lnum = prevnonblank(a:lnum) while lnum > 0 @@ -702,7 +706,7 @@ function s:PrevNonBlankNonString(lnum) endfunction " Find line above 'lnum' that started the continuation 'lnum' may be part of. -function s:GetMSL(lnum) +function! s:GetMSL(lnum) abort " Start on the line we're at and use its indent. let msl = a:lnum let lnum = s:PrevNonBlankNonString(a:lnum - 1) @@ -807,7 +811,7 @@ function s:GetMSL(lnum) endfunction " Check if line 'lnum' has more opening brackets than closing ones. -function s:ExtraBrackets(lnum) +function! s:ExtraBrackets(lnum) abort let opening = {'parentheses': [], 'braces': [], 'brackets': []} let closing = {'parentheses': [], 'braces': [], 'brackets': []} @@ -869,7 +873,7 @@ function s:ExtraBrackets(lnum) return [rightmost_opening, rightmost_closing] endfunction -function s:Match(lnum, regex) +function! s:Match(lnum, regex) abort let line = getline(a:lnum) let offset = match(line, '\C'.a:regex) let col = offset + 1 @@ -889,7 +893,7 @@ endfunction " Locates the containing class/module's definition line, ignoring nested classes " along the way. " -function! s:FindContainingClass() +function! s:FindContainingClass() abort let saved_position = getpos('.') while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW', |