├── Gemfile ├── README.md ├── doc └── rubyhash.txt ├── plugin └── rubyhash.vim └── spec ├── plugin └── rubyhash_spec.rb └── spec_helper.rb /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem "rspec" 4 | gem "robot-vim" 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | ## About 4 | 5 | vim-rubyhash is a plugin that can convert the keys of a Ruby hash literal to one of the following formats: 6 | 7 | * symbols 8 | * strings 9 | * Ruby-1.9 format 10 | 11 | ## Usage 12 | 13 | To perform linewise conversions, the following are the default mappings: 14 | 15 | * rs to convert all keys to symbols 16 | * rt to convert all keys to strings 17 | * rr to convert all keys to Ruby 1.9 format 18 | 19 | The defaults can be overridden - details in the docs. 20 | 21 | ## Caveat Emptor 22 | 23 | The regular expressions that recognise hash keys are geared towards phrases containing only letters, numbers and 24 | underscores. This is admittedly, very limited, but I am hoping that, for now it will suffice for 80% of use cases. 25 | 26 | ## Installation 27 | 28 | It is recommended that you use Pathogen and include the git repo for this plugin as a submodule in the bundle directory. 29 | 30 | In addition, seeing as this plugin is written in Ruby, you will need a version of vim that is compiled with Ruby. An easy way to 31 | check is to inspect the output of `vim --version` for the text `+ruby`. The plugin has been tested against Ruby-1.8.7 and should 32 | work with 1.9.x as well, but I have yet to test this assumption. 33 | 34 | ## Acknowledgements 35 | 36 | This plugin is based on an idea provided by [@sheldonh](https://twitter.com/#!/sheldonh). He would have built it himself, but he is 37 | currently busy completing his [TPS reports](http://www.youtube.com/watch?v=Fy3rjQGc6lA&feature=related). 38 | 39 | ## License 40 | 41 | rubyhash is licensed under the MIT license 42 | 43 | Copyright (C) 2011 by Rory McKinley 44 | 45 | Permission is hereby granted, free of charge, to any person obtaining a copy 46 | of this software and associated documentation files (the "Software"), to deal 47 | in the Software without restriction, including without limitation the rights 48 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 49 | copies of the Software, and to permit persons to whom the Software is 50 | furnished to do so, subject to the following conditions: 51 | 52 | The above copyright notice and this permission notice shall be included in 53 | all copies or substantial portions of the Software. 54 | 55 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 56 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 57 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 58 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 59 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 60 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 61 | THE SOFTWARE. 62 | 63 | -------------------------------------------------------------------------------- /doc/rubyhash.txt: -------------------------------------------------------------------------------- 1 | rubyhash.txt For Vim version 7.3 Last change: 27 December 2011 2 | *rubyhash* 3 | Rubyhash~ 4 | 5 | This plugin is for the conversion of key "styles" in Ruby hash literals. 6 | The three styles supported are symbols, strings or Ruby 1.9. 7 | 8 | Usage~ 9 | 10 | Rubyhash will convert all hash keys on the current line to the selected 11 | style via the following default keymappings: 12 | 13 | rs Convert all hash keys to symbols 14 | rt Convert all hash keys to strings 15 | rr Convert all hash keys to Ruby1.9 format 16 | 17 | Overriding the default keymappings~ 18 | 19 | Adding the following variable to your .vimrc will disable the default 20 | key mappings: 21 | 22 | let g:rubyhash_map_keys = 0 23 | 24 | You can then define your own keymappings to the following functions: 25 | 26 | ToSymbolKeysLinewise() 27 | ToStringKeysLinewise() 28 | To19KeysLinewise() 29 | 30 | Caveats~ 31 | 32 | The regular expression that identifies a potential key is fairly naive - only considers the following pattern: [a-zA-Z0-9_]. 33 | 34 | Contact~ 35 | 36 | Rory McKinley (rorymckinley at gmail dot com) 37 | 38 | License~ 39 | 40 | rubyhash is licensed under the MIT license 41 | 42 | Copyright (C) 2011 by Rory McKinley 43 | 44 | Permission is hereby granted, free of charge, to any person obtaining a copy 45 | of this software and associated documentation files (the "Software"), to deal 46 | in the Software without restriction, including without limitation the rights 47 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 48 | copies of the Software, and to permit persons to whom the Software is 49 | furnished to do so, subject to the following conditions: 50 | 51 | The above copyright notice and this permission notice shall be included in 52 | all copies or substantial portions of the Software. 53 | 54 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 55 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 56 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 57 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 58 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 59 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 60 | THE SOFTWARE. 61 | vim:tw=72:ft=help: 62 | -------------------------------------------------------------------------------- /plugin/rubyhash.vim: -------------------------------------------------------------------------------- 1 | if !exists('g:rubyhash_map_keys') 2 | let g:rubyhash_map_keys=1 3 | endif 4 | 5 | if g:rubyhash_map_keys==1 6 | nnoremap rs :call ToSymbolKeysLinewise() 7 | nnoremap rt :call ToStringKeysLinewise() 8 | nnoremap rr :call To19KeysLinewise() 9 | endif 10 | 11 | function! ToSymbolKeysLinewise() 12 | :ruby Convert::to_symbols 13 | endfunction 14 | 15 | function! To19KeysLinewise() 16 | :ruby Convert::to_19 17 | endfunction 18 | 19 | function! ToStringKeysLinewise() 20 | :ruby Convert::to_strings 21 | endfunction 22 | 23 | ruby << EOF 24 | module Convert 25 | def self.to_symbols 26 | search_and_replace([ 27 | { :search => /['"](\w+)['"](?=\s*=>)/, :replace => ':\1' }, 28 | { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => '\1:\2 =>'} 29 | ]) 30 | end 31 | 32 | def self.to_strings 33 | search_and_replace([ 34 | { :search => /:(\w+)(?=\s*=>)/, :replace => '"\1"'}, 35 | { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => '\1"\2" =>'}, 36 | ]) 37 | end 38 | 39 | def self.to_19 40 | search_and_replace([ 41 | { :search => /:(\w+)\s*=>/, :replace => '\1:'}, 42 | { :search => /['"](\w+)['"]\s*=>/, :replace => '\1:'}, 43 | ]) 44 | end 45 | 46 | private 47 | 48 | def self.search_and_replace(patterns=[]) 49 | contents = VIM::Buffer.current.line 50 | patterns.each { |params| contents.gsub!(params[:search], params[:replace]) } 51 | VIM::Buffer.current.line = contents 52 | end 53 | end 54 | EOF 55 | -------------------------------------------------------------------------------- /spec/plugin/rubyhash_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe "rubyhash plugin" do 4 | before(:each) do 5 | @plugin_path = File.join(File.dirname(__FILE__), '..', '..', 'plugin', 'rubyhash.vim') 6 | @runner = RobotVim::Runner.new 7 | @commands = [ ":source #{@plugin_path}"] 8 | end 9 | 10 | describe "operating on a single line containing a complete hash" do 11 | 12 | describe "converting to symbol keys" do 13 | 14 | it "converts string hash keys to symbol hash keys" do 15 | input_buffer = "{'my_key'=>'my_value', \"new_key\"=>'new_value', 'final_key' => 'final_value'}" 16 | @commands += [ ":call ToSymbolKeysLinewise()"] 17 | 18 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 19 | result.body.should == "{:my_key=>'my_value', :new_key=>'new_value', :final_key => 'final_value'}" 20 | end 21 | 22 | it "converts Ruby 1.9 style keys to symbols" do 23 | input_buffer = "{key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class}" 24 | @commands += [ ":call ToSymbolKeysLinewise()"] 25 | 26 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 27 | result.body.should == "{:key_one =>'value_one', :keytwo => 'value:two', :keythree => 'value3', :key4 => My::Class}" 28 | end 29 | 30 | it "leaves existing symbol keys untouched when converting to symbol keys" do 31 | input_buffer = "{ change_me: 'ping', :leave_me => 'pang', 'change_me_too' => 'pong' }" 32 | @commands += [ ":call ToSymbolKeysLinewise()"] 33 | 34 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 35 | result.body.should == "{ :change_me => 'ping', :leave_me => 'pang', :change_me_too => 'pong' }" 36 | end 37 | 38 | end 39 | 40 | describe "converting to ruby1.9 keys" do 41 | 42 | it "converts symbol keys" do 43 | input_buffer = %q{{:key_one=>'val_one', :key_two => "val_two"}} 44 | @commands += [ ":call To19KeysLinewise()"] 45 | 46 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 47 | result.body.should == %q|{key_one:'val_one', key_two: "val_two"}| 48 | end 49 | 50 | it "converts string keys" do 51 | input_buffer = %q|{"key_one"=>"val_one", 'key_two' => 'val_two'}| 52 | @commands += [ ":call To19KeysLinewise()"] 53 | 54 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 55 | result.body.should == %q|{key_one:"val_one", key_two: 'val_two'}| 56 | end 57 | 58 | it "leaves 1.9 style keys unchanged" do 59 | input_buffer = %q|{"key_one"=>"val_one", key_two: "val_two"}| 60 | @commands += [ ":call To19KeysLinewise()"] 61 | 62 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 63 | result.body.should == %q|{key_one:"val_one", key_two: "val_two"}| 64 | end 65 | 66 | end 67 | 68 | describe "converting to string keys" do 69 | it "converts symbol keys" do 70 | input_buffer = %q{{:key_one=>'val_one', :key_two => "val_two"}} 71 | @commands += [ ":call ToStringKeysLinewise()"] 72 | 73 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 74 | result.body.should == %q|{"key_one"=>'val_one', "key_two" => "val_two"}| 75 | end 76 | 77 | it "converts Ruby 1.9 style keys to symbols" do 78 | input_buffer = "{key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class}" 79 | @commands += [ ":call ToStringKeysLinewise()"] 80 | 81 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 82 | result.body.should == %q|{"key_one" =>'value_one', "keytwo" => 'value:two', "keythree" => 'value3', "key4" => My::Class}| 83 | end 84 | 85 | end 86 | 87 | end 88 | 89 | describe "operating on a single line containing a snippet of a hash" do 90 | describe "converting to symbol keys" do 91 | it "converts string hash keys to symbol hash keys" do 92 | input_buffer = " 'my_key'=>'my_value', \"new_key\"=>'new_value', 'final_key' => 'final_value'" 93 | @commands += [ ":call ToSymbolKeysLinewise()"] 94 | 95 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 96 | result.body.should == " :my_key=>'my_value', :new_key=>'new_value', :final_key => 'final_value'" 97 | end 98 | 99 | it "converts Ruby 1.9 style keys to symbols" do 100 | input_buffer = " key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class" 101 | @commands += [ ":call ToSymbolKeysLinewise()"] 102 | 103 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 104 | result.body.should == " :key_one =>'value_one', :keytwo => 'value:two', :keythree => 'value3', :key4 => My::Class" 105 | end 106 | 107 | it "leaves existing symbol keys untouched when converting to symbol keys" do 108 | input_buffer = " change_me: 'ping', :leave_me => 'pang', 'change_me_too' => 'pong' " 109 | @commands += [ ":call ToSymbolKeysLinewise()"] 110 | 111 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 112 | result.body.should == " :change_me => 'ping', :leave_me => 'pang', :change_me_too => 'pong' " 113 | end 114 | 115 | end 116 | 117 | describe "converting to ruby1.9 keys" do 118 | it "converts symbol keys" do 119 | input_buffer = %q|:key_one=>'val_one', :key_two => "val_two"| 120 | @commands += [ ":call To19KeysLinewise()"] 121 | 122 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 123 | result.body.should == %q|key_one:'val_one', key_two: "val_two"| 124 | end 125 | 126 | it "converts string keys" do 127 | input_buffer = %q|"key_one"=>"val_one", 'key_two' => 'val_two'| 128 | @commands += [ ":call To19KeysLinewise()"] 129 | 130 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 131 | result.body.should == %q|key_one:"val_one", key_two: 'val_two'| 132 | end 133 | 134 | it "leaves 1.9 style keys unchanged" do 135 | input_buffer = %q|"key_one"=>"val_one", key_two: "val_two"| 136 | @commands += [ ":call To19KeysLinewise()"] 137 | 138 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 139 | result.body.should == %q|key_one:"val_one", key_two: "val_two"| 140 | end 141 | 142 | end 143 | 144 | describe "converting to string keys" do 145 | it "converts symbol keys" do 146 | input_buffer = %q|:key_one=>'val_one', :key_two => "val_two"| 147 | @commands += [ ":call ToStringKeysLinewise()"] 148 | 149 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 150 | result.body.should == %q|"key_one"=>'val_one', "key_two" => "val_two"| 151 | end 152 | 153 | it "converts Ruby 1.9 style keys to symbols" do 154 | input_buffer = "key_one:'value_one', keytwo: 'value:two', keythree: 'value3', key4: My::Class" 155 | @commands += [ ":call ToStringKeysLinewise()"] 156 | 157 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 158 | result.body.should == %q|"key_one" =>'value_one', "keytwo" => 'value:two', "keythree" => 'value3', "key4" => My::Class| 159 | end 160 | 161 | end 162 | 163 | end 164 | 165 | describe "default keymappings" do 166 | before(:each) do 167 | @commands << ":let mapleader='\'" 168 | end 169 | 170 | it "converts to symbols with the sequence rs" do 171 | input_buffer = %q|'key_one' => 'one'| 172 | @commands += [ '\rs' ] 173 | 174 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 175 | result.body.should == %q|:key_one => 'one'| 176 | end 177 | 178 | it "converts to strings with the sequence rt" do 179 | input_buffer = %q|:key_one => 'one'| 180 | @commands += [ '\rt' ] 181 | 182 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 183 | result.body.should == %q|"key_one" => 'one'| 184 | end 185 | 186 | it "converts to Ruby1.9 keys with the sequence rr" do 187 | input_buffer = %q|:key_one => 'one'| 188 | @commands += [ '\rr' ] 189 | 190 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 191 | result.body.should == %q|key_one: 'one'| 192 | end 193 | 194 | end 195 | 196 | describe "overriding default keymappings" do 197 | it "does not set the keymappings if rubyhash_map_keys is set to 0" do 198 | input_buffer = %q|:key_one => 'one'| 199 | @commands.unshift(":let rubyhash_map_keys=0") 200 | @commands += [ '\rr' ] 201 | 202 | result = @runner.run(:commands => @commands.join("\n")+"\n", :input_file => input_buffer) 203 | result.body.should_not == %q|key_one: 'one'| 204 | end 205 | end 206 | end 207 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'robot-vim' 2 | --------------------------------------------------------------------------------