Skip to content

Commit

Permalink
Simple haml support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed May 9, 2013
1 parent 41303f8 commit 7cc3114
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions autoload/sj/haml.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
" TODO (2013-05-09) Only works for very simple things, needs work
function! sj#haml#SplitInterpolation()
let lineno = line('.')
let line = getline('.')
let indent = indent('.')
let pattern = '^\s*%.\{-}\zs='

if line !~ pattern
return 0
endif

exe 's/'.pattern.'/\r=/'
call s:SetIndent(lineno + 1, lineno + 1, indent + &sw)

return 1
endfunction

function! sj#haml#JoinInterpolation()
if line('.') == line('$')
return 0
endif

let line = getline('.')
let next_line = getline(line('.') + 1)

if !(line =~ '^\s*%\k\+\s*$' && next_line =~ '^\s*=')
return 0
end

s/\n\s*//
return 1
endfunction

" Sets the absolute indent of the given range of lines to the given indent
function! s:SetIndent(from, to, indent)
let new_whitespace = repeat(' ', a:indent)
exe a:from.','.a:to.'s/^\s*/'.new_whitespace
endfunction
3 changes: 3 additions & 0 deletions examples/test.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%div
%span
= 1 + 2
7 changes: 7 additions & 0 deletions ftplugin/haml/splitjoin.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let b:splitjoin_split_callbacks = [
\ 'sj#haml#SplitInterpolation'
\ ]

let b:splitjoin_join_callbacks = [
\ 'sj#haml#JoinInterpolation'
\ ]
28 changes: 28 additions & 0 deletions spec/plugin/haml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'spec_helper'

describe "haml" do
let(:filename) { 'test.haml' }

# Haml is not built-in, so let's set it up manually
def setup_haml_filetype
vim.set(:filetype, 'haml')
vim.set(:expandtab)
vim.set(:shiftwidth, 2)
end

specify "interpolation" do
set_file_contents '%div= 1 + 2'
setup_haml_filetype

split

assert_file_contents <<-EOF
%div
= 1 + 2
EOF

join

assert_file_contents '%div= 1 + 2'
end
end

0 comments on commit 7cc3114

Please sign in to comment.