Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Docks

  • Loading branch information...
commit 2591fefbc78ee7448da8da4a0537a531f710c743 1 parent 7a3c4f5
@kchmck authored
Showing with 15 additions and 11 deletions.
  1. +15 −11 indent/coffee.vim
View
26 indent/coffee.vim
@@ -50,7 +50,7 @@ let s:COMPOUND_ASSIGNMENT = '[:=]\s*\%(if\|unless\|for\|while\|until\|'
" A postfix condition like `return ... if ...`.
let s:POSTFIX_CONDITION = '\S\s\+\zs\<\%(if\|unless\)\>'
-" A single-line else statement like `else ...` but not `else if ...
+" A single line else statement like `else ...` but not `else if ...`
let s:SINGLE_LINE_ELSE = '^else\s\+\%(\<\%(if\|unless\)\>\)\@!'
" Max lines to look back for a match
@@ -92,8 +92,7 @@ function! s:IsCommentLine(linenum)
return s:IsComment(a:linenum, indent(a:linenum) + 1)
endfunction
-" Repeatedly search a line for a regex until one is found outside a string or
-" comment.
+" Search a line for a regex until one is found outside a string or comment.
function! s:SmartSearch(linenum, regex)
" Start at the first column.
let col = 0
@@ -117,18 +116,19 @@ function! s:SmartSearch(linenum, regex)
return 0
endfunction
-" Skip a match if it's in a comment or string, is a single-line statement that
-" isn't adjacent, or is a postfix condition.
+" Check if a match should be skipped.
function! s:ShouldSkip(startlinenum, linenum, col)
+ " Skip if in a comment or string.
if s:IsCommentOrString(a:linenum, a:col)
return 1
endif
- " Check for a single-line statement that isn't adjacent.
+ " Skip if a single line statement that isn't adjacent.
if s:SmartSearch(a:linenum, '\<then\>') && a:startlinenum - a:linenum > 1
return 1
endif
+ " Skip if a postfix condition.
if s:SmartSearch(a:linenum, s:POSTFIX_CONDITION) &&
\ !s:SmartSearch(a:linenum, s:COMPOUND_ASSIGNMENT)
return 1
@@ -245,16 +245,17 @@ function! s:GetCoffeeIndent(curlinenum)
return -1
endif
+ " Indent based on the current line.
let curline = s:GetTrimmedLine(a:curlinenum)
- " Try to find a previous matching statement. This handles outdenting.
+ " Try to find a matching statement. This handles outdenting.
let matchlinenum = s:GetMatch(curline)
if matchlinenum
return indent(matchlinenum)
endif
- " Try to find a matching `when`.
+ " Try to find a matching when.
if curline =~ '^when\>' && !s:SmartSearch(prevlinenum, '\<switch\>')
let linenum = a:curlinenum
@@ -269,6 +270,7 @@ function! s:GetCoffeeIndent(curlinenum)
return -1
endif
+ " Indent based on the previous line.
let prevline = s:GetTrimmedLine(prevlinenum)
let previndent = indent(prevlinenum)
@@ -291,13 +293,15 @@ function! s:GetCoffeeIndent(curlinenum)
let prevprevlinenum = s:GetPrevNormalLine(prevlinenum)
- " If the continuation is the first in the file, don't run the other checks.
+ " If the continuation is the first in the file, there can't be others before
+ " it.
if !prevprevlinenum
return previndent + &shiftwidth
endif
let prevprevline = s:GetTrimmedLine(prevprevlinenum)
+ " Only indent after the first continuation.
if prevprevline !~ s:CONTINUATION && prevprevline !~ s:CONTINUATION_BLOCK
return previndent + &shiftwidth
endif
@@ -306,7 +310,7 @@ function! s:GetCoffeeIndent(curlinenum)
endif
" Indent after these keywords and compound assignments if they aren't a
- " single-line statement.
+ " single line statement.
if prevline =~ s:INDENT_AFTER_KEYWORD || prevline =~ s:COMPOUND_ASSIGNMENT
if !s:SmartSearch(prevlinenum, '\<then\>') && prevline !~ s:SINGLE_LINE_ELSE
return previndent + &shiftwidth
@@ -329,7 +333,7 @@ function! s:GetCoffeeIndent(curlinenum)
endif
endif
- " No indenting or outdenting is needed.
+ " Keep the current indent.
return -1
endfunction
Please sign in to comment.
Something went wrong with that request. Please try again.