Skip to content

Commit 6020345

Browse files
author
wmoxam
committed
More rubyization
1 parent 00d3308 commit 6020345

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

autoload/rails.vim

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3065,64 +3065,71 @@ function! s:Invert(bang)
30653065
(start..(start+lines.length-1)).each {|i| self.append(i, lines[i-start]) }
30663066
end
30673067
end
3068-
end
30693068

3070-
def invert_range(start, finish)
3071-
inverted_cmds = []
3072-
i = start
3073-
while(i <= finish)
3074-
line = $curbuf[i]
3075-
inverted_cmds << case line
3076-
when /^\s*#[^{].*$/
3077-
line
3078-
when /\bcreate_table\b/
3079-
i = $curbuf.end_of(i)
3080-
"#{VIM::indentation(line)}drop_table#{VIM::args(line, 1)}#{VIM::comment(line)}"
3081-
when /\bdrop_table\b/
3082-
inverted_cmds << line.gsub(/drop_table\s*([^,){ ]*).*/, "create_table #{'\1'} do |t|")
3083-
"#{VIM::indentation(line)}end#{VIM::comment(line)}"
3084-
when /\badd_column\b/
3085-
"#{VIM::indentation(line)}remove_column#{VIM::args(line, 2)}#{VIM::comment(line)}"
3086-
when /\bremove_column\b/
3087-
line.gsub(/\bremove_column\b/, 'add_column')
3088-
when /\badd_index\b(.*)/
3089-
"#{VIM::indentation(line)}remove_index#{$~[1]}"
3090-
when /\bremove_index\b(.*)/
3091-
line.gsub(/\bremove_index\b/, 'add_index').gsub(/:column\s*=>\s*/, '')
3092-
when /\brename_(table|column)\b/
3093-
line.gsub(/(\brename_(table|column)\b)\s+([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*/, '\1 \3 \5 \4')
3094-
when /\.update_all$/, /\bchange_column(\b|_default\b)/
3095-
# bleh, it's not worth it
3096-
"##{line}"
3097-
when /^\s*(if|unless|while|until|for)/
3098-
i = $curbuf.end_of(i)
3069+
class Rails
3070+
def self.invert_range(start, finish)
3071+
inverted_cmds = []
3072+
i = start
3073+
while(i <= finish)
3074+
line = $curbuf[i]
3075+
inverted_cmds << case line
3076+
when /^\s*#[^{].*$/
3077+
line
3078+
when /\bcreate_table\b/
3079+
i = $curbuf.end_of(i)
3080+
"#{VIM::indentation(line)}drop_table#{VIM::args(line, 1)}#{VIM::comment(line)}"
3081+
when /\bdrop_table\b/
3082+
inverted_cmds << line.gsub(/drop_table\s*([^,){ ]*).*/, "create_table #{'\1'} do |t|")
3083+
"#{VIM::indentation(line)}end#{VIM::comment(line)}"
3084+
when /\badd_column\b/
3085+
"#{VIM::indentation(line)}remove_column#{VIM::args(line, 2)}#{VIM::comment(line)}"
3086+
when /\bremove_column\b/
3087+
line.gsub(/\bremove_column\b/, 'add_column')
3088+
when /\badd_index\b(.*)/
3089+
"#{VIM::indentation(line)}remove_index#{$~[1]}"
3090+
when /\bremove_index\b(.*)/
3091+
line.gsub(/\bremove_index\b/, 'add_index').gsub(/:column\s*=>\s*/, '')
3092+
when /\brename_(table|column)\b/
3093+
line.gsub(/(\brename_(table|column)\b)\s+([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*/, '\1 \3 \5 \4')
3094+
when /\.update_all$/, /\bchange_column(\b|_default\b)/
3095+
# bleh, it's not worth it
3096+
"##{line}"
3097+
when /^\s*(if|unless|while|until|for)/
3098+
i = $curbuf.end_of(i)
3099+
end
3100+
raise "Error parsing migration, aborting" if i < 1
3101+
i += 1
3102+
end
3103+
inverted_cmds.compact
3104+
end
3105+
3106+
def self.rinvert!
3107+
up_start = $curbuf.index_of(/def\s*self.up/)
3108+
up_end = $curbuf.end_of(up_start)
3109+
unless up_start > 0 && up_end > up_start
3110+
return VIM::evaluate("s:error('Couldn't parse self.up method')")
3111+
end
3112+
inverted_cmds = []
3113+
begin
3114+
inverted_cmds = VIM::Rails::invert_range(up_start+1, up_end+1)
3115+
rescue Exception => e
3116+
return $curbuf.error(e.message)
3117+
end
3118+
down_start = $curbuf.index_of(/def\s*self.down/)
3119+
down_end = $curbuf.end_of(down_start)
3120+
unless down_start > 0 && down_end > down_start
3121+
return $curbuf.error("Couldn't parse self.down method")
3122+
end
3123+
3124+
$curbuf.open_fold_at(down_start)
3125+
$curbuf.remove_between(down_start, down_end)
3126+
$curbuf.multi_append(down_start, inverted_cmds)
30993127
end
3100-
raise "Error parsing migration, aborting" if i < 1
3101-
i += 1
31023128
end
3103-
inverted_cmds.compact
31043129
end
31053130

3106-
up_start = $curbuf.index_of(/def\s*self.up/)
3107-
up_end = $curbuf.end_of(up_start)
3108-
unless up_start > 0 && up_end > up_start
3109-
return VIM::evaluate("s:error('Couldn't parse self.up method')")
3110-
end
3111-
inverted_cmds = []
3112-
begin
3113-
inverted_cmds = invert_range(up_start+1, up_end+1)
3114-
rescue Exception => e
3115-
return $curbuf.error(e.message)
3116-
end
3117-
down_start = $curbuf.index_of(/def\s*self.down/)
3118-
down_end = $curbuf.end_of(down_start)
3119-
unless down_start > 0 && down_end > down_start
3120-
return $curbuf.error("Couldn't parse self.down method")
3121-
end
3131+
Vim::Rails::rinvert!
31223132

3123-
$curbuf.open_fold_at(down_start)
3124-
$curbuf.remove_between(down_start, down_end)
3125-
$curbuf.multi_append(down_start, inverted_cmds)
31263133
EOF
31273134
endfunction
31283135

0 commit comments

Comments
 (0)