Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single-quoted hash keys. #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions plugin/rubyhash.vim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ if g:rubyhash_map_keys==1
nnoremap <silent> <Leader>rs :call ToSymbolKeysLinewise()<CR>
nnoremap <silent> <Leader>rt :call ToStringKeysLinewise()<CR>
nnoremap <silent> <Leader>rr :call To19KeysLinewise()<CR>
nnoremap <silent> <Leader>rq :call ToSingleQuotedStringKeysLinewise()<CR>

" add bindings that work in visual line mode
vnoremap <silent> <Leader>rs :call ToSymbolKeysLinewise()<CR>
vnoremap <silent> <Leader>rt :call ToStringKeysLinewise()<CR>
vnoremap <silent> <Leader>rr :call To19KeysLinewise()<CR>
vnoremap <silent> <Leader>rq :call ToSingleQuotedStringKeysLinewise()<CR>
endif

function! ToSymbolKeysLinewise()
Expand All @@ -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
Expand All @@ -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:'},
Expand Down
73 changes: 64 additions & 9 deletions spec/plugin/rubyhash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'}"
Expand Down Expand Up @@ -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"}}
Expand Down Expand Up @@ -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()"]
Expand All @@ -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()"]

Expand All @@ -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()"]
Expand All @@ -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()"]
Expand All @@ -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()"]
Expand All @@ -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
Expand All @@ -183,6 +230,14 @@
result.body.should == %q|"key_one" => 'one'|
end

it "converts to single-quoted strings with the sequence <Leader>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 <Leader>rr" do
input_buffer = %q|:key_one => 'one'|
@commands += [ '\rr' ]
Expand All @@ -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
Expand Down
Empty file added spec/test_vimrc
Empty file.