@@ -3031,119 +3031,10 @@ endfunction
30313031
30323032function ! s: Invert (bang )
30333033 ruby << EOF
3034- module VIM
3035- def self .comment (str)
3036- # grabs comments
3037- str.match (/\s*#[^{].*/ ) ? $ ~[0 ] : ' '
3038- end
3039-
3040- def self .args (str, i )
3041- VIM:: evaluate (" s:mextargs(\" #{str}\" , #{i})" )
3042- end
3043-
3044- def self .indentation (str)
3045- str.match (/^\s*/ ) ? $ ~[0 ] : ' '
3046- end
3047-
3048- class Buffer
3049- def each
3050- 1 .upto (length) do |i |
3051- yield self [i ]
3052- end
3053- end
3054-
3055- def each_with_index
3056- 1 .upto (length) do |i |
3057- yield self [i ], i
3058- end
3059- end
3060-
3061- def index_of (pattern)
3062- self .each_with_index {|line , i | return i if line .match (pattern) }
3063- end
3064-
3065- def end_of (i )
3066- VIM:: evaluate (" s:endof(#{i})" ).to_i
3067- end
3068-
3069- def error (msg)
3070- VIM:: evaluate (" s:error(\" #{msg}\" )" )
3071- end
3072-
3073- def open_fold_at (i )
3074- # VIM:: evaluate ("
3075- # if foldclosed (#{i }) > 0
3076- # exe ' #{i}foldopen!'
3077- # endif "
3078- end
3079-
3080- def remove_between (start , finish )
3081- return if start + 1 == finish
3082- (start + 1 ..finish - 1 ).to_a.reverse .each {|i | self .delete (i ) }
3083- end
3084-
3085- def multi_append (start , lines )
3086- (start ..(start + lines .length- 1 )).each {|i | self .append (i , lines [i - start ]) }
3087- end
3088- end
3089- end
3090-
3091- def invert_range (start , finish )
3092- inverted_cmds = []
3093- i = start
3094- while (i <= finish )
3095- line = $curbuf [i ]
3096- inverted_cmds << case line
3097- when /^\s*#[^{].*$/
3098- line
3099- when /\bcreate_table\b/
3100- i = $curbuf .end_of (i )
3101- " #{VIM::indentation(line)}drop_table#{VIM::args(line, 1)}#{VIM::comment(line)}"
3102- when /\bdrop_table\b/
3103- inverted_cmds << line .gsub (/drop_table\s*([^,){ ]*).*/ , " create_table #{'\1 '} do |t|" )
3104- " #{VIM::indentation(line)}end#{VIM::comment(line)}"
3105- when /\badd_column\b/
3106- " #{VIM::indentation(line)}remove_column#{VIM::args(line, 2)}#{VIM::comment(line)}"
3107- when /\bremove_column\b/
3108- line .gsub (/\bremove_column\b/ , ' add_column' )
3109- when /\badd_index\b(.*)/
3110- " #{VIM::indentation(line)}remove_index#{$~[1]}"
3111- when /\bremove_index\b(.*)/
3112- line .gsub (/\bremove_index\b/ , ' add_index' ).gsub (/:column\s*=>\s*/ , ' ' )
3113- when /\brename_(table|column)\b/
3114- line .gsub (/(\brename_(table|column)\b)\s+([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*/ , ' \1 \3 \5 \4' )
3115- when /\.update_all$/ , /\bchange_column(\b|_default\b)/
3116- # bleh, it's not worth it
3117- " ##{line}"
3118- when /^\s*(if|unless|while|until|for)/
3119- i = $curbuf .end_of (i )
3120- end
3121- raise " Error parsing migration, aborting" if i < 1
3122- i += 1
3123- end
3124- inverted_cmds.compact
3125- end
3126-
3127- up_start = $curbuf .index_of (/def\s*self.up/ )
3128- up_end = $curbuf .end_of (up_start)
3129- unless up_start > 0 && up_end > up_start
3130- return VIM:: evaluate (" s:error('Couldn't parse self.up method')" )
3131- end
3132- inverted_cmds = []
3133- begin
3134- inverted_cmds = invert_range (up_start+ 1 , up_end+ 1 )
3135- rescue Exception = > e
3136- return $curbuf .error (e .message)
3137- end
3138- down_start = $curbuf .index_of (/def\s*self.down/ )
3139- down_end = $curbuf .end_of (down_start)
3140- unless down_start > 0 && down_end > down_start
3141- return $curbuf .error (" Couldn't parse self.down method" )
3142- end
3143-
3144- $curbuf .open_fold_at (down_start)
3145- $curbuf .remove_between (down_start, down_end)
3146- $curbuf .multi_append (down_start, inverted_cmds)
3034+
3035+ require ' ~/.vim/autoload/vim.rb' # make the ruby lib into a gem to solve this problem
3036+ Vim::Rails: :rinvert!
3037+
31473038EOF
31483039endfunction
31493040
0 commit comments