Skip to content

Commit

Permalink
Basic "<<"-style heredoc indenting
Browse files Browse the repository at this point in the history
If the closing delimiter is at column 0, keep it that way.
  • Loading branch information
AndrewRadev committed May 12, 2013
1 parent 5c3213a commit beb1289
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions indent/ruby.vim
Expand Up @@ -124,6 +124,11 @@ function s:IsInStringOrDocumentation(lnum, col)
return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
endfunction 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'
endfunction

" Find line above 'lnum' that isn't empty, in a comment, or in a string. " Find line above 'lnum' that isn't empty, in a comment, or in a string.
function s:PrevNonBlankNonString(lnum) function s:PrevNonBlankNonString(lnum)
let in_block = 0 let in_block = 0
Expand Down Expand Up @@ -376,6 +381,14 @@ function GetRubyIndent(...)
return indent('.') return indent('.')
endif endif


" If we are at the closing delimiter of a "<<" heredoc-style string, set the
" indent to 0.
if line =~ '^\k\+\s*$'
\ && s:IsInStringDelimiter(clnum, 1)
\ && search('\V<<'.line, 'nbW') > 0
return 0
endif

" 3.3. Work on the previous line. {{{2 " 3.3. Work on the previous line. {{{2
" ------------------------------- " -------------------------------


Expand Down
18 changes: 18 additions & 0 deletions spec/indent/basic_spec.rb
Expand Up @@ -21,4 +21,22 @@
something_else something_else
EOF EOF
end end

specify "heredocs" do
assert_correct_indenting <<-EOF
def one
two = <<-THREE
four
THREE
end
EOF

assert_correct_indenting <<-EOF
def one
two = <<THREE
four
THREE
end
EOF
end
end end

0 comments on commit beb1289

Please sign in to comment.