Skip to content

Commit 10f3452

Browse files
author
Darrick Wiebe
committed
Merge commit 'wes/master'
* commit 'wes/master': Fixes a bug with inverting rename_column Seperate ruby code from vimscript More rubyization Conflicts: autoload/rails.vim
2 parents 658599c + 68432dd commit 10f3452

File tree

3 files changed

+123
-113
lines changed

3 files changed

+123
-113
lines changed

autoload/rails.vim

Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,119 +3031,10 @@ endfunction
30313031

30323032
function! 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+
31473038
EOF
31483039
endfunction
31493040

autoload/vim.rb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
module VIM
2+
def self.comment(str)
3+
# grabs comments
4+
str.match(/\s*#[^{].*/) ? $~[0] : ''
5+
end
6+
7+
def self.args(str, i)
8+
VIM::evaluate("s:mextargs(\"#{str}\", #{i})")
9+
end
10+
11+
def self.indentation(str)
12+
str.match(/^\s*/) ? $~[0] : ''
13+
end
14+
15+
class Buffer
16+
def each
17+
1.upto(length) do |i|
18+
yield self[i]
19+
end
20+
end
21+
22+
def each_with_index
23+
1.upto(length) do |i|
24+
yield self[i], i
25+
end
26+
end
27+
28+
def index_of(pattern)
29+
self.each_with_index {|line, i| return i if line.match(pattern) }
30+
end
31+
32+
def end_of(i)
33+
VIM::evaluate("s:endof(#{i})").to_i
34+
end
35+
36+
def error(msg)
37+
VIM::evaluate("s:error(\"#{msg}\")")
38+
end
39+
40+
def open_fold_at(i)
41+
# VIM::evaluate("
42+
# if foldclosed(#{i}) > 0
43+
# exe '#{i}foldopen!'
44+
# endif"
45+
end
46+
47+
def remove_between(start, finish)
48+
return if start + 1 == finish
49+
(start+1..finish-1).to_a.reverse.each {|i| self.delete(i) }
50+
end
51+
52+
def multi_append(start, lines)
53+
(start..(start+lines.length-1)).each {|i| self.append(i, lines[i-start]) }
54+
end
55+
end
56+
57+
class Rails
58+
def self.invert_range(start, finish)
59+
inverted_cmds = []
60+
i = start
61+
while(i <= finish)
62+
line = $curbuf[i]
63+
inverted_cmds << case line
64+
when /^\s*#[^{].*$/
65+
line
66+
when /\bcreate_table\b/
67+
i = $curbuf.end_of(i)
68+
"#{VIM::indentation(line)}drop_table#{VIM::args(line, 1)}#{VIM::comment(line)}"
69+
when /\bdrop_table\b/
70+
inverted_cmds << line.gsub(/drop_table\s*([^,){ ]*).*/, "create_table #{'\1'} do |t|")
71+
"#{VIM::indentation(line)}end#{VIM::comment(line)}"
72+
when /\badd_column\b/
73+
"#{VIM::indentation(line)}remove_column#{VIM::args(line, 2)}#{VIM::comment(line)}"
74+
when /\bremove_column\b/
75+
line.gsub(/\bremove_column\b/, 'add_column')
76+
when /\badd_index\b(.*)/
77+
"#{VIM::indentation(line)}remove_index#{$~[1]}"
78+
when /\bremove_index\b(.*)/
79+
line.gsub(/\bremove_index\b/, 'add_index').gsub(/:column\s*=>\s*/, '')
80+
when /\brename_(table|column)\b/
81+
line.gsub(/(\brename_(table|column)\b)\s+([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*/, '\1 \3, \5, \4')
82+
when /\.update_all$/, /\bchange_column(\b|_default\b)/
83+
# bleh, it's not worth it
84+
"##{line}"
85+
when /^\s*(if|unless|while|until|for)/
86+
i = $curbuf.end_of(i)
87+
end
88+
raise "Error parsing migration, aborting" if i < 1
89+
i += 1
90+
end
91+
inverted_cmds.compact
92+
end
93+
94+
def self.rinvert!
95+
up_start = $curbuf.index_of(/def\s*self.up/)
96+
up_end = $curbuf.end_of(up_start)
97+
unless up_start > 0 && up_end > up_start
98+
return VIM::evaluate("s:error('Couldn't parse self.up method')")
99+
end
100+
inverted_cmds = []
101+
begin
102+
inverted_cmds = VIM::Rails::invert_range(up_start+1, up_end+1)
103+
rescue Exception => e
104+
return $curbuf.error(e.message)
105+
end
106+
down_start = $curbuf.index_of(/def\s*self.down/)
107+
down_end = $curbuf.end_of(down_start)
108+
unless down_start > 0 && down_end > down_start
109+
return $curbuf.error("Couldn't parse self.down method")
110+
end
111+
112+
$curbuf.open_fold_at(down_start)
113+
$curbuf.remove_between(down_start, down_end)
114+
$curbuf.multi_append(down_start, inverted_cmds)
115+
end
116+
end
117+
end

install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cp autoload/* ~/.vim/autoload/.
2+
cp plugin/* ~/.vim/plugin/.

0 commit comments

Comments
 (0)