diff --git a/autoload/sj/ruby.vim b/autoload/sj/ruby.vim index dbf86e86..45699847 100644 --- a/autoload/sj/ruby.vim +++ b/autoload/sj/ruby.vim @@ -358,14 +358,19 @@ function! sj#ruby#SplitBlock() if search('\S\%#', 'Wbn') let multiline_block = ' '.multiline_block endif - - let body = join(split(body, '\s*;\s*'), "\n") + let replacement = substitute(body, '^'.pattern.'$', multiline_block, '') + " remove leftover whitespace let replacement = substitute(replacement, '\s*\n', '\n', 'g') call sj#ReplaceMotion('Va{', replacement) + normal! j0 + while sj#SearchSkip(';', sj#SkipSyntax(['rubyString']), 'W', line('.')) > 0 + call execute("normal! r\") + endwhile + return 1 endfunction diff --git a/spec/plugin/ruby_spec.rb b/spec/plugin/ruby_spec.rb index 6040fbce..ba5666db 100644 --- a/spec/plugin/ruby_spec.rb +++ b/spec/plugin/ruby_spec.rb @@ -955,6 +955,86 @@ module Bar EOF end + it "doesn't get confused by ; when joining" do + set_file_contents <<~EOF + foo do + example1 + example2 + end + EOF + + vim.search 'do' + join + + assert_file_contents <<~EOF + foo { example1; example2 } + EOF + end + + it "doesn't get confused by ; when splitting" do + set_file_contents <<~EOF + foo { example1; example2 } + EOF + + vim.search 'example1;' + split + + assert_file_contents <<~EOF + foo do + example1 + example2 + end + EOF + end + + it "doesn't get confused when ; is in a string literal(single quotes) when splitting" do + set_file_contents <<~EOF + foo { example1; 'some string ; literal' } + EOF + + vim.search 'example1;' + split + + assert_file_contents <<~EOF + foo do + example1 + 'some string ; literal' + end + EOF + end + + it "doesn't get confused when ; is in a string literal(double quotes) when splitting" do + set_file_contents <<~EOF + foo { example1; "some string ; literal" } + EOF + + vim.search 'example1;' + split + + assert_file_contents <<~EOF + foo do + example1 + "some string ; literal" + end + EOF + end + + it "doesn't get confused when ; is in a string literal(percent strings) when splitting" do + set_file_contents <<~EOF + foo { example1; %(some string ; literal) } + EOF + + vim.search 'example1;' + split + + assert_file_contents <<~EOF + foo do + example1 + %(some string ; literal) + end + EOF + end + it "migrates inline comments when joining" do set_file_contents <<~EOF foo do