Skip to content

Commit

Permalink
PHP markers
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Sep 14, 2014
1 parent f06dcdc commit dff5e64
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
36 changes: 36 additions & 0 deletions autoload/sj/php.vim
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,39 @@ function! sj#php#JoinIfClause()

return 1
endfunction

function! sj#php#SplitPhpMarker()
if sj#SearchUnderCursor('<?php.\{-}?>') <= 0
return 0
endif

let start_col = col('.')
let skip = sj#SkipSyntax('phpStringSingle', 'phpStringDouble', 'phpComment')
if sj#SearchSkip('?>', skip, 'We', line('.')) <= 0
return 0
endif
let end_col = col('.')

let body = sj#GetCols(start_col, end_col)
let body = substitute(body, '^<?php', "<?php\n", '')
let body = substitute(body, '\s*?>$', "\n?>", '')

call sj#ReplaceCols(start_col, end_col, body)
return 1
endfunction

function! sj#php#JoinPhpMarker()
if sj#SearchUnderCursor('<?php\s*$') <= 0
return 0
endif

let start_lineno = line('.')
let skip = sj#SkipSyntax('phpStringSingle', 'phpStringDouble', 'phpComment')
if sj#SearchSkip('?>', skip, 'We') <= 0
return 0
endif
let end_lineno = line('.')

exe start_lineno.','.end_lineno.'join'
return 1
endfunction
4 changes: 4 additions & 0 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@
}

?>

<div>
<?php echo "OK"; ?>
</div>
2 changes: 2 additions & 0 deletions ftplugin/php/splitjoin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ let b:splitjoin_split_callbacks = [
\ 'sj#php#SplitArray',
\ 'sj#php#SplitIfClause',
\ 'sj#html#SplitTags',
\ 'sj#php#SplitPhpMarker',
\ ]

let b:splitjoin_join_callbacks = [
\ 'sj#php#JoinPhpMarker',
\ 'sj#js#JoinArray',
\ 'sj#php#JoinArray',
\ 'sj#php#JoinIfClause',
Expand Down
18 changes: 18 additions & 0 deletions spec/plugin/php_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@
?>
EOF
end

specify "<?php markers" do
set_file_contents "<?php example(); ?>"

vim.search('example')
split

assert_file_contents <<-EOF
<?php
example();
?>
EOF

vim.search('php')
join

assert_file_contents "<?php example(); ?>"
end
end

0 comments on commit dff5e64

Please sign in to comment.