Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix reindenting problem with coffeescript

  • Loading branch information...
commit 05bdb32ae8bf2aeefed0598e44be43d99df23b7a 1 parent 4afe248
@AndrewRadev authored
Showing with 16 additions and 4 deletions.
  1. +14 −4 autoload/sj/coffee.vim
  2. +2 −0  examples/test.coffee
View
18 autoload/sj/coffee.vim
@@ -23,16 +23,17 @@ endfunction
function! sj#coffee#SplitIfClause()
let line = getline('.')
+ let base_indent = indent('.')
let suffix_pattern = '\v(.*\S.*) (if|unless|while|until) (.*)'
let postfix_pattern = '\v(if|unless|while|until) (.*) then (.*)'
if line =~ suffix_pattern
call sj#ReplaceMotion('V', substitute(line, suffix_pattern, '\2 \3\n\1', ''))
- normal! j==
+ call s:SetBaseIndent(line('.'), line('.') + 1, base_indent)
return 1
elseif line =~ postfix_pattern
call sj#ReplaceMotion('V', substitute(line, postfix_pattern, '\1 \2\n\3', ''))
- normal! j==
+ call s:SetBaseIndent(line('.'), line('.') + 1, base_indent)
return 1
else
return 0
@@ -40,8 +41,9 @@ function! sj#coffee#SplitIfClause()
endfunction
function! sj#coffee#JoinIfClause()
- let line = getline('.')
- let pattern = '\v^\s*(if|unless|while|until)'
+ let line = getline('.')
+ let base_indent = indent('.')
+ let pattern = '\v^\s*(if|unless|while|until)'
if line !~ pattern
return 0
@@ -55,6 +57,7 @@ function! sj#coffee#JoinIfClause()
else
call sj#ReplaceMotion('Vj', if_clause.' then '.body)
endif
+ call s:SetBaseIndent(line('.'), line('.'), base_indent)
return 1
endfunction
@@ -127,3 +130,10 @@ function! s:IndentedLinesBelow(line)
return [first_line, current_line]
endfunction
+
+function! s:SetBaseIndent(from, to, indent)
+ let current_whitespace = matchstr(getline(a:from), '^\s*')
+ let new_whitespace = repeat(' ', a:indent)
+
+ exe a:from.','.a:to.'s/^'.current_whitespace.'/'.new_whitespace
+endfunction
View
2  examples/test.coffee
@@ -1,4 +1,6 @@
(foo, bar) -> console.log foo
+(foo, bar) ->
+ console.log foo
do ->
console.log bar if foo?
Please sign in to comment.
Something went wrong with that request. Please try again.