diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index 9531d66..efef9ab 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -6,6 +6,13 @@ if g:rubyhash_map_keys==1 nnoremap rs :call ToSymbolKeysLinewise() nnoremap rt :call ToStringKeysLinewise() nnoremap rr :call To19KeysLinewise() + nnoremap rq :call ToSingleQuotedStringKeysLinewise() + + " add bindings that work in visual line mode + vnoremap rs :call ToSymbolKeysLinewise() + vnoremap rt :call ToStringKeysLinewise() + vnoremap rr :call To19KeysLinewise() + vnoremap rq :call ToSingleQuotedStringKeysLinewise() endif function! ToSymbolKeysLinewise() @@ -20,6 +27,10 @@ function! ToStringKeysLinewise() :ruby Convert::to_strings endfunction +function! ToSingleQuotedStringKeysLinewise() + :ruby Convert::to_single_quoted_strings +endfunction + ruby << EOF module Convert def self.to_symbols @@ -36,6 +47,13 @@ module Convert ]) end + def self.to_single_quoted_strings + search_and_replace([ + { :search => /:(\w+)(?=\s*=>)/, :replace => "'\\1'"}, + { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => "\\1'\\2' =>"}, + ]) + end + def self.to_19 search_and_replace([ { :search => /:(\w+)\s*=>/, :replace => '\1:'}, diff --git a/spec/plugin/rubyhash_spec.rb b/spec/plugin/rubyhash_spec.rb index 06cddec..a8e87fc 100644 --- a/spec/plugin/rubyhash_spec.rb +++ b/spec/plugin/rubyhash_spec.rb @@ -3,13 +3,15 @@ describe "rubyhash plugin" do before(:each) do @plugin_path = File.join(File.dirname(__FILE__), '..', '..', 'plugin', 'rubyhash.vim') - @runner = RobotVim::Runner.new + # use a fake vimrc file so we don't confuse the tests by executing the contents of + # our real .vimrc file + @runner = RobotVim::Runner.new(:vimrc => "spec/test_vimrc") @commands = [ ":source #{@plugin_path}"] end describe "operating on a single line containing a complete hash" do - describe "converting to symbol keys" do + describe "ToSymbolKeysLinewise" do it "converts string hash keys to symbol hash keys" do input_buffer = "{'my_key'=>'my_value', \"new_key\"=>'new_value', 'final_key' => 'final_value'}" @@ -37,7 +39,7 @@ end - describe "converting to ruby1.9 keys" do + describe "To19KeysLinewise" do it "converts symbol keys" do input_buffer = %q{{:key_one=>'val_one', :key_two => "val_two"}} @@ -65,7 +67,8 @@ end - describe "converting to string keys" do + describe "ToStringKeysLinewise" do + it "converts symbol keys" do input_buffer = %q{{:key_one=>'val_one', :key_two => "val_two"}} @commands += [ ":call ToStringKeysLinewise()"] @@ -74,7 +77,7 @@ result.body.should == %q|{"key_one"=>'val_one', "key_two" => "val_two"}| end - it "converts Ruby 1.9 style keys to symbols" do + it "converts Ruby 1.9 style keys to strings" do input_buffer = "{key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class}" @commands += [ ":call ToStringKeysLinewise()"] @@ -84,10 +87,32 @@ end + describe "ToSingleQuotedStringKeysLinewise" do + + it "converts symbol keys" do + input_buffer = %q{{:key_one=>'val_one', :key_two => "val_two"}} + @commands += [ ":call ToSingleQuotedStringKeysLinewise()"] + + result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) + result.body.should == %q|{'key_one'=>'val_one', 'key_two' => "val_two"}| + end + + it "converts Ruby 1.9 style keys to strings" do + input_buffer = "{key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class}" + @commands += [ ":call ToSingleQuotedStringKeysLinewise()"] + + result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) + result.body.should == %q|{'key_one' =>'value_one', 'keytwo' => 'value:two', 'keythree' => 'value3', 'key4' => My::Class}| + end + + end + end describe "operating on a single line containing a snippet of a hash" do - describe "converting to symbol keys" do + + describe "ToSymbolKeysLinewise" do + it "converts string hash keys to symbol hash keys" do input_buffer = " 'my_key'=>'my_value', \"new_key\"=>'new_value', 'final_key' => 'final_value'" @commands += [ ":call ToSymbolKeysLinewise()"] @@ -114,7 +139,8 @@ end - describe "converting to ruby1.9 keys" do + describe "To19KeysLinewise" do + it "converts symbol keys" do input_buffer = %q|:key_one=>'val_one', :key_two => "val_two"| @commands += [ ":call To19KeysLinewise()"] @@ -141,7 +167,8 @@ end - describe "converting to string keys" do + describe "ToStringKeysLinewise" do + it "converts symbol keys" do input_buffer = %q|:key_one=>'val_one', :key_two => "val_two"| @commands += [ ":call ToStringKeysLinewise()"] @@ -160,6 +187,26 @@ end + describe "ToSingleQuotedStringKeysLinewise" do + + it "converts symbol keys" do + input_buffer = %q|:key_one=>'val_one', :key_two => "val_two"| + @commands += [ ":call ToSingleQuotedStringKeysLinewise()"] + + result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) + result.body.should == %q|'key_one'=>'val_one', 'key_two' => "val_two"| + end + + it "converts Ruby 1.9 style keys to symbols" do + input_buffer = "key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class" + @commands += [ ":call ToSingleQuotedStringKeysLinewise()"] + + result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) + result.body.should == %q|'key_one' =>'value_one', 'keytwo' => 'value:two', 'keythree' => 'value3', 'key4' => My::Class| + end + + end + end describe "default keymappings" do @@ -183,6 +230,14 @@ result.body.should == %q|"key_one" => 'one'| end + it "converts to single-quoted strings with the sequence rq" do + input_buffer = %q|:key_one => 'one'| + @commands += [ '\rq' ] + + result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) + result.body.should == %q|'key_one' => 'one'| + end + it "converts to Ruby1.9 keys with the sequence rr" do input_buffer = %q|:key_one => 'one'| @commands += [ '\rr' ] @@ -197,8 +252,8 @@ it "does not set the keymappings if rubyhash_map_keys is set to 0" do input_buffer = %q|:key_one => 'one'| @commands.unshift(":let rubyhash_map_keys=0") + @commands << ":let mapleader='\'" @commands += [ '\rr' ] - result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) result.body.should_not == %q|key_one: 'one'| end diff --git a/spec/test_vimrc b/spec/test_vimrc new file mode 100644 index 0000000..e69de29