From f09f88989948eabb7d9bc6e47451cb9f03c1c124 Mon Sep 17 00:00:00 2001 From: MacKinley Smith Date: Fri, 2 Oct 2015 12:59:17 -0600 Subject: [PATCH 1/5] Implemented to19 method in pure vimscript --- .gitignore | 1 + plugin/rubyhash.vim | 73 +++++++++++++++++++++++---------------------- 2 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ead8445 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/*.swp diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index 9531d66..70b719a 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -9,46 +9,49 @@ if g:rubyhash_map_keys==1 endif function! ToSymbolKeysLinewise() - :ruby Convert::to_symbols + " :ruby Convert::to_symbols endfunction function! To19KeysLinewise() - :ruby Convert::to_19 + " :ruby Convert::to_19 + let line=getline('.') + let line19 = substitute(substitute(line, ':\(\w\+\)\s*=>', '\1:', ''), '[''"]\(\w\+\)[''"]\s*=>', '', '') + call setline('.', line19) endfunction function! ToStringKeysLinewise() - :ruby Convert::to_strings + " :ruby Convert::to_strings endfunction -ruby << EOF -module Convert - def self.to_symbols - search_and_replace([ - { :search => /['"](\w+)['"](?=\s*=>)/, :replace => ':\1' }, - { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => '\1:\2 =>'} - ]) - end - - def self.to_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:'}, - { :search => /['"](\w+)['"]\s*=>/, :replace => '\1:'}, - ]) - end - - private - - def self.search_and_replace(patterns=[]) - contents = VIM::Buffer.current.line - patterns.each { |params| contents.gsub!(params[:search], params[:replace]) } - VIM::Buffer.current.line = contents - end -end -EOF +" ruby << EOF +" module Convert +" def self.to_symbols +" search_and_replace([ +" { :search => /['"](\w+)['"](?=\s*=>)/, :replace => ':\1' }, +" { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => '\1:\2 =>'} +" ]) +" end +" +" def self.to_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:'}, +" { :search => /['"](\w+)['"]\s*=>/, :replace => '\1:'}, +" ]) +" end +" +" private +" +" def self.search_and_replace(patterns=[]) +" contents = VIM::Buffer.current.line +" patterns.each { |params| contents.gsub!(params[:search], params[:replace]) } +" VIM::Buffer.current.line = contents +" end +" end +" EOF From 707a40e62a6ebfe7c017bd4b483b722d91765d4a Mon Sep 17 00:00:00 2001 From: MacKinley Smith Date: Fri, 2 Oct 2015 13:01:12 -0600 Subject: [PATCH 2/5] Removed commented heredoc --- plugin/rubyhash.vim | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index 70b719a..bc06099 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -23,7 +23,6 @@ function! ToStringKeysLinewise() " :ruby Convert::to_strings endfunction -" ruby << EOF " module Convert " def self.to_symbols " search_and_replace([ @@ -54,4 +53,3 @@ endfunction " VIM::Buffer.current.line = contents " end " end -" EOF From 379b167fa6cbbb34639a794b933bff2113899ec6 Mon Sep 17 00:00:00 2001 From: MacKinley Smith Date: Fri, 2 Oct 2015 13:02:37 -0600 Subject: [PATCH 3/5] Completed to19 function, added global flag --- plugin/rubyhash.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index bc06099..f3945ec 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -15,7 +15,7 @@ endfunction function! To19KeysLinewise() " :ruby Convert::to_19 let line=getline('.') - let line19 = substitute(substitute(line, ':\(\w\+\)\s*=>', '\1:', ''), '[''"]\(\w\+\)[''"]\s*=>', '', '') + let line19 = substitute(substitute(line, ':\(\w\+\)\s*=>', '\1:', 'g'), '[''"]\(\w\+\)[''"]\s*=>', '\1:', 'g') call setline('.', line19) endfunction From 7d72b38970c3ec5989251f282721d8953423b7aa Mon Sep 17 00:00:00 2001 From: MacKinley Smith Date: Fri, 2 Oct 2015 13:14:11 -0600 Subject: [PATCH 4/5] Implemented the remaining functions in pure Vimscript --- plugin/rubyhash.vim | 46 +++++++++------------------------------------ 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index f3945ec..c10d1f8 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -9,47 +9,19 @@ if g:rubyhash_map_keys==1 endif function! ToSymbolKeysLinewise() - " :ruby Convert::to_symbols + call setline('.', substitute(substitute(getline('.'), + '[''"]\(\w\+\)[''"]\(?=\s*=>\)', '\1:', 'g'), + '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1:\2 =>', 'g')) endfunction function! To19KeysLinewise() - " :ruby Convert::to_19 - let line=getline('.') - let line19 = substitute(substitute(line, ':\(\w\+\)\s*=>', '\1:', 'g'), '[''"]\(\w\+\)[''"]\s*=>', '\1:', 'g') - call setline('.', line19) + call setline('.', substitute(substitute(getline('.'), + ':\(\w\+\)\s*=>', '\1:', 'g'), + '[''"]\(\w\+\)[''"]\s*=>', '\1:', 'g')) endfunction function! ToStringKeysLinewise() - " :ruby Convert::to_strings + call setline('.', substitute(substitute(getline('.'), + ':\(\w\+\)\(?=\s*=>\)', '"\1"', 'g'), + '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1"\2" =>', 'g')) endfunction - -" module Convert -" def self.to_symbols -" search_and_replace([ -" { :search => /['"](\w+)['"](?=\s*=>)/, :replace => ':\1' }, -" { :search => /((?:\{|,|^)\s*)(\w+):/, :replace => '\1:\2 =>'} -" ]) -" end -" -" def self.to_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:'}, -" { :search => /['"](\w+)['"]\s*=>/, :replace => '\1:'}, -" ]) -" end -" -" private -" -" def self.search_and_replace(patterns=[]) -" contents = VIM::Buffer.current.line -" patterns.each { |params| contents.gsub!(params[:search], params[:replace]) } -" VIM::Buffer.current.line = contents -" end -" end From 5db038b2fa8c7271b02710ddae31a4da8658af50 Mon Sep 17 00:00:00 2001 From: MacKinley Smith Date: Fri, 2 Oct 2015 13:26:08 -0600 Subject: [PATCH 5/5] Apparently everything has to be on one line... oops. --- plugin/rubyhash.vim | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/plugin/rubyhash.vim b/plugin/rubyhash.vim index c10d1f8..5828d88 100644 --- a/plugin/rubyhash.vim +++ b/plugin/rubyhash.vim @@ -9,19 +9,13 @@ if g:rubyhash_map_keys==1 endif function! ToSymbolKeysLinewise() - call setline('.', substitute(substitute(getline('.'), - '[''"]\(\w\+\)[''"]\(?=\s*=>\)', '\1:', 'g'), - '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1:\2 =>', 'g')) + call setline('.', substitute(substitute(getline('.'), '[''"]\(\w\+\)[''"]\(?=\s*=>\)', '\1:', 'g'), '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1:\2 =>', 'g')) endfunction function! To19KeysLinewise() - call setline('.', substitute(substitute(getline('.'), - ':\(\w\+\)\s*=>', '\1:', 'g'), - '[''"]\(\w\+\)[''"]\s*=>', '\1:', 'g')) + call setline('.', substitute(substitute(getline('.'), ':\(\w\+\)\s*=>', '\1:', 'g'), '[''"]\(\w\+\)[''"]\s*=>', '\1:', 'g')) endfunction function! ToStringKeysLinewise() - call setline('.', substitute(substitute(getline('.'), - ':\(\w\+\)\(?=\s*=>\)', '"\1"', 'g'), - '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1"\2" =>', 'g')) + call setline('.', substitute(substitute(getline('.'), ':\(\w\+\)\(?=\s*=>\)', '"\1"', 'g'), '\(\(?:\{|,|^\)\s*\)\(\w\+\):', '\1"\2" =>', 'g')) endfunction