@@ -1634,12 +1634,12 @@ function! s:BufNavCommands()
16341634 command ! - buffer - bar -nargs =* - bang -complete =customlist ,s: Complete_edit RVedit :call s: Edit (<count> ,' V<bang>' ,<f-args> )
16351635 command ! - buffer - bar -nargs =* - bang -complete =customlist ,s: Complete_edit RTedit :call s: Edit (<count> ,' T<bang>' ,<f-args> )
16361636 command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_edit RDedit :call s: Edit (<count> ,' <line1>D<bang>' ,<f-args> )
1637- command ! - buffer - bar -nargs =* -complete =customlist ,s: Complete_find A :call s: Alternate (' <bang>' , <f-args> )
1638- command ! - buffer - bar -nargs =* -complete =customlist ,s: Complete_find AE :call s: Alternate (' E<bang>' ,<f-args> )
1639- command ! - buffer - bar -nargs =* -complete =customlist ,s: Complete_find AS :call s: Alternate (' S<bang>' ,<f-args> )
1640- command ! - buffer - bar -nargs =* -complete =customlist ,s: Complete_find AV :call s: Alternate (' V<bang>' ,<f-args> )
1641- command ! - buffer - bar -nargs =* -complete =customlist ,s: Complete_find AT :call s: Alternate (' T<bang>' ,<f-args> )
1642- command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_find AD :call s: Alternate (' <line1> D<bang>' ,<f-args> )
1637+ command ! - buffer - bar -nargs =* - range = 0 -complete =customlist ,s: Complete_related A :call s: Alternate (' <bang>' , <line1> , <line2> , <count> , <f-args> )
1638+ command ! - buffer - bar -nargs =* - range = 0 -complete =customlist ,s: Complete_related AE :call s: Alternate (' E<bang>' , <line1> , <line2> , <count> ,<f-args> )
1639+ command ! - buffer - bar -nargs =* - range = 0 -complete =customlist ,s: Complete_related AS :call s: Alternate (' S<bang>' , <line1> , <line2> , <count> ,<f-args> )
1640+ command ! - buffer - bar -nargs =* - range = 0 -complete =customlist ,s: Complete_related AV :call s: Alternate (' V<bang>' , <line1> , <line2> , <count> ,<f-args> )
1641+ command ! - buffer - bar -nargs =* - range = 0 -complete =customlist ,s: Complete_related AT :call s: Alternate (' T<bang>' , <line1> , <line2> , <count> ,<f-args> )
1642+ command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_related AD :call s: Alternate (' D<bang>' , <line1> , <line2> , <count> ,<f-args> )
16431643 command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_related AN :call s: Related (' <bang>' ,<line1> ,<line2> ,<count> ,<f-args> )
16441644 command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_related R :call s: Related (' <bang>' ,<line1> ,<line2> ,<count> ,<f-args> )
16451645 command ! - buffer - bar -nargs =* -range =0 -complete =customlist ,s: Complete_related RE :call s: Related (' E<bang>' ,<line1> ,<line2> ,<count> ,<f-args> )
@@ -2803,26 +2803,104 @@ function! s:edit(cmd,file,...)
28032803 endif
28042804endfunction
28052805
2806- function ! s: Alternate (cmd,... )
2806+ function ! s: Alternate (cmd,line1,line2, count , ... )
28072807 if a: 0
2808- return call (' s:Find' ,[1 ,a: cmd ]+ a: 000 )
2808+ if a: count && a: cmd !~# ' D'
2809+ return call (' s:Find' ,[1 ,a: line1 .a: cmd ]+ a: 000 )
2810+ elseif a: count
2811+ return call (' s:Edit' ,[1 ,a: line1 .a: cmd ]+ a: 000 )
2812+ else
2813+ return call (' s:Edit' ,[1 ,a: cmd ]+ a: 000 )
2814+ endif
28092815 else
2810- let file = s: AlternateFile ()
2811- if file != " "
2816+ let file = s: getopt (a: count ? ' related' : ' alternate' , ' bl' )
2817+ if file == ' '
2818+ let file = rails#buffer ().related (a: count )
2819+ endif
2820+ if file != ' '
28122821 call s: findedit (a: cmd ,file )
28132822 else
28142823 call s: warn (" No alternate file is defined" )
28152824 endif
28162825 endif
28172826endfunction
28182827
2819- function ! s: AlternateFile ()
2820- let f = RailsFilePath ()
2821- let t = RailsFileType ()
2822- let altopt = s: getopt (" alternate" ," bl" )
2823- if altopt != " "
2824- return altopt
2825- elseif f = ~ ' \<config/environments/'
2828+ function ! s: Related (cmd,line1,line2,count ,... )
2829+ if a: count == 0 && a: 0 == 0
2830+ return s: Alternate (a: cmd ,a: line1 ,a: line1 ,a: line1 )
2831+ else
2832+ return call (' s:Alternate' ,[a: cmd ,a: line1 ,a: line2 ,a: count ]+ a: 000 )
2833+ endif
2834+ endfunction
2835+
2836+ function ! s: Complete_related (A,L,P )
2837+ if a: L = ~# ' ^[[:alpha:]]'
2838+ return s: Complete_edit (a: A ,a: L ,a: P )
2839+ else
2840+ return s: Complete_find (a: A ,a: L ,a: P )
2841+ endif
2842+ endfunction
2843+
2844+ function ! s: readable_related (... ) dict abort
2845+ let f = self .name ()
2846+ let t = self .type_name ()
2847+ if a: 0 && a: 1
2848+ let lastmethod = self .last_method (a: 1 )
2849+ if t = ~ ' ^\%(controller\|model-mailer\)\>' && lastmethod != " "
2850+ let root = s: sub (s: sub (s: sub (f ,' /application\.rb$' ,' /shared_controller.rb' ),' /%(controllers|models)/' ,' /views/' ),' %(_controller)=\.rb$' ,' /' .lastmethod)
2851+ let format = self .last_format (a: 1 )
2852+ if format == ' ' | let format = ' html' | endif
2853+ if glob (self .app ().path ().' /' .root.' .' .format.' .*[^~]' ) != ' '
2854+ return root . ' .' . format
2855+ else
2856+ return root
2857+ endif
2858+ elseif f = ~ ' \<config/environments/'
2859+ return " config/database.yml#" . fnamemodify (f ,' :t:r' )
2860+ elseif f = ~ ' \<config/database\.yml$'
2861+ if lastmethod != " "
2862+ return " config/environments/" .lastmethod." .rb"
2863+ else
2864+ return " config/environment.rb"
2865+ endif
2866+ elseif f = ~ ' \<config/routes\.rb$' | return " config/database.yml"
2867+ elseif f = ~ ' \<config/environment\.rb$' | return " config/routes.rb"
2868+ elseif f = ~ ' \<db/migrate/\d\d\d_'
2869+ let num = matchstr (f ,' \<db/migrate/0*\zs\d\+\ze_' )+ 1
2870+ let migr = self .app ().migration (num)
2871+ return migr == ' ' ? " db/schema.rb" : migr
2872+ elseif t = ~ ' ^view-layout\>'
2873+ return s: sub (s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /layouts/(\k+)\..*$' ,' /\1_controller.rb' ),' <application_controller\.rb$' ,' application.rb' )
2874+ elseif t = ~ ' ^view\>'
2875+ let controller = s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /(\k+%(\.\k+)=)\..*$' ,' _controller.rb#\1' )
2876+ let controller2 = s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /(\k+%(\.\k+)=)\..*$' ,' .rb#\1' )
2877+ let model = s: sub (s: sub (f ,' /views/' ,' /models/' ),' /(\k+)\..*$' ,' .rb#\1' )
2878+ if self .app ().has_file (s: sub (controller,' #.{-}$' ,' ' ))
2879+ return controller
2880+ elseif self .app ().has_file (s: sub (controller2,' #.{-}$' ,' ' ))
2881+ return controller2
2882+ elseif self .app ().has_file (s: sub (model,' #.{-}$' ,' ' )) || model = ~ ' _mailer\.rb#'
2883+ return model
2884+ else
2885+ return controller
2886+ endif
2887+ elseif t = ~ ' ^controller\>'
2888+ return s: sub (s: sub (f ,' /controllers/' ,' /helpers/' ),' %(_controller)=\.rb$' ,' _helper.rb' )
2889+ " elseif t=~ '^helper\>'
2890+ " return s:findlayout(s:controller())
2891+ elseif t = ~ ' ^model-arb\>'
2892+ let table_name = matchstr (join (self .getline (1 ,50 )," \n " ),' \n\s*set_table_name\s*[:"'' ]\zs\w\+' )
2893+ if table_name == ' '
2894+ let table_name = rails#pluralize (s: gsub (s: sub (fnamemodify (f ,' :r' ),' .{-}<app/models/' ,' ' ),' /' ,' _' ))
2895+ endif
2896+ return self .app ().migration (' 0#' .table_name)
2897+ elseif t = ~ ' ^model-aro\>'
2898+ return s: sub (f ,' _observer\.rb$' ,' .rb' )
2899+ elseif f = ~ ' \<db/schema\.rb$'
2900+ return self .app ().migration (1 )
2901+ endif
2902+ endif
2903+ if f = ~ ' \<config/environments/'
28262904 return " config/environment.rb"
28272905 elseif f == ' README'
28282906 return " config/database.yml"
@@ -2831,24 +2909,24 @@ function! s:AlternateFile()
28312909 elseif f = ~ ' \<config/environment\.rb$' | return " config/database.yml"
28322910 elseif f = ~ ' \<db/migrate/\d\d\d_'
28332911 let num = matchstr (f ,' \<db/migrate/0*\zs\d\+\ze_' )-1
2834- return rails# app ().migration (num)
2912+ return self . app ().migration (num)
28352913 elseif f = ~ ' \<application\.js$'
28362914 return " app/helpers/application_helper.rb"
2837- elseif t = ~ ' ^js \>'
2915+ elseif t = ~ ' ^javascript \>'
28382916 return " public/javascripts/application.js"
28392917 elseif f = ~ ' \<db/schema\.rb$'
2840- return rails# app ().migration (' ' )
2918+ return self . app ().migration (' ' )
28412919 elseif t = ~ ' ^view\>'
28422920 let spec1 = fnamemodify (f ,' :s?\<app/?spec/?' )." _spec.rb"
28432921 let spec2 = fnamemodify (f ,' :r:s?\<app/?spec/?' )." _spec.rb"
28442922 let spec3 = fnamemodify (f ,' :r:r:s?\<app/?spec/?' )." _spec.rb"
2845- if rails# app ().has_file (spec1)
2923+ if self . app ().has_file (spec1)
28462924 return spec1
2847- elseif rails# app ().has_file (spec2)
2925+ elseif self . app ().has_file (spec2)
28482926 return spec2
2849- elseif rails# app ().has_file (spec3)
2927+ elseif self . app ().has_file (spec3)
28502928 return spec3
2851- elseif rails# app ().has (' spec' )
2929+ elseif self . app ().has (' spec' )
28522930 return spec2
28532931 else
28542932 if t = ~ ' \<layout\>'
@@ -2861,20 +2939,22 @@ function! s:AlternateFile()
28612939 elseif t = ~ ' ^controller-api\>'
28622940 let api = s: sub (s: sub (f ,' /controllers/' ,' /apis/' ),' _controller\.rb$' ,' _api.rb' )
28632941 return api
2942+ elseif t = ~ ' ^api\>'
2943+ return s: sub (s: sub (f ,' /apis/' ,' /controllers/' ),' _api\.rb$' ,' _controller.rb' )
28642944 elseif t = ~ ' ^helper\>'
28652945 let controller = s: sub (s: sub (f ,' /helpers/' ,' /controllers/' ),' _helper\.rb$' ,' _controller.rb' )
28662946 let controller = s: sub (controller,' application_controller' ,' application' )
28672947 let spec = s: sub (s: sub (f ,' <app/' ,' spec/' ),' \.rb$' ,' _spec.rb' )
2868- if rails# app ().has_file (spec)
2948+ if self . app ().has_file (spec)
28692949 return spec
28702950 else
28712951 return controller
28722952 endif
28732953 elseif t = ~ ' \<fixtures\>' && f = ~ ' \<spec/'
2874- let file = rails#singularize (expand ( " % :t:r" )).' _spec.rb'
2954+ let file = rails#singularize (fnamemodify ( f , " :t:r" )).' _spec.rb'
28752955 return file
28762956 elseif t = ~ ' \<fixtures\>'
2877- let file = rails#singularize (expand ( " % :t:r" )).' _test.rb' " .expand('%:e')
2957+ let file = rails#singularize (fnamemodify ( f , " :t:r" )).' _test.rb'
28782958 return file
28792959 elseif f == ' '
28802960 call s: warn (" No filename present" )
@@ -2906,7 +2986,7 @@ function! s:AlternateFile()
29062986 elseif t == ' spec-lib'
29072987 return s: sub (file ,' <spec/' ,' ' )
29082988 elseif t == ' lib'
2909- return s: sub (f , ' <lib/(.*)\.rb$' , ' test/unit/\1_test\ .rb' )." \n " .s: sub (f , ' <lib/(.*)\.rb$' , ' spec/lib/\1_spec\ .rb' )
2989+ return s: sub (f , ' <lib/(.*)\.rb$' , ' test/unit/\1_test.rb' )." \n " .s: sub (f , ' <lib/(.*)\.rb$' , ' spec/lib/\1_spec.rb' )
29102990 elseif t = ~ ' ^spec\>'
29112991 return s: sub (file ,' <spec/' ,' app/' )
29122992 elseif file = ~ ' \<vendor/.*/lib/'
@@ -2921,107 +3001,7 @@ function! s:AlternateFile()
29213001 endif
29223002endfunction
29233003
2924- function ! s: Complete_related (A,L,P )
2925- if a: L = ~# ' ^[[:alpha:]]'
2926- return s: Complete_edit (a: A ,a: L ,a: P )
2927- else
2928- return s: Complete_find (a: A ,a: L ,a: P )
2929- endif
2930- endfunction
2931-
2932- function ! s: Related (cmd,line1,line2,count ,... )
2933- if a: 0
2934- if a: count && a: cmd !~# ' D'
2935- return call (' s:Find' ,[1 ,a: line1 .a: cmd ]+ a: 000 )
2936- elseif a: count
2937- return call (' s:Edit' ,[1 ,a: line1 .a: cmd ]+ a: 000 )
2938- else
2939- return call (' s:Edit' ,[1 ,a: cmd ]+ a: 000 )
2940- endif
2941- else
2942- let file = s: RelatedFile (a: line1 )
2943- if file != " "
2944- call s: findedit (a: cmd ,file )
2945- else
2946- call s: warn (" No related file is defined" )
2947- endif
2948- endif
2949- endfunction
2950-
2951- function ! s: RelatedFile (line )
2952- let f = RailsFilePath ()
2953- let t = RailsFileType ()
2954- let lastmethod = a: line ? s: lastmethod (a: line ) : ' '
2955- if s: getopt (" related" ," l" ) != " "
2956- return s: getopt (" related" ," l" )
2957- elseif t = ~ ' ^\%(controller\|model-mailer\)\>' && lastmethod != " "
2958- let root = s: sub (s: sub (s: sub (f ,' /application\.rb$' ,' /shared_controller.rb' ),' /%(controllers|models)/' ,' /views/' ),' %(_controller)=\.rb$' ,' /' .lastmethod)
2959- let format = rails#buffer ().last_format (a: line )
2960- if format == ' ' | let format = ' html' | endif
2961- if glob (rails#app ().path ().' /' .root.' .' .format.' .*[^~]' ) != ' '
2962- return root . ' .' . format
2963- else
2964- return root
2965- endif
2966- elseif s: getopt (" related" ," b" ) != " "
2967- return s: getopt (" related" ," b" )
2968- elseif f = ~ ' \<config/environments/'
2969- return " config/database.yml#" . expand (" %:t:r" )
2970- elseif f == ' README'
2971- return " config/database.yml"
2972- elseif f = ~ ' \<config/database\.yml$'
2973- if lastmethod != " "
2974- return " config/environments/" .lastmethod." .rb"
2975- else
2976- return " config/environment.rb"
2977- endif
2978- elseif f = ~ ' \<config/routes\.rb$' | return " config/database.yml"
2979- elseif f = ~ ' \<config/environment\.rb$' | return " config/routes.rb"
2980- elseif f = ~ ' \<db/migrate/\d\d\d_'
2981- let num = matchstr (f ,' \<db/migrate/0*\zs\d\+\ze_' )+ 1
2982- let migr = rails#app ().migration (num)
2983- return migr == ' ' ? " db/schema.rb" : migr
2984- elseif t = ~ ' ^test\>' && f = ~ ' \<test/\w\+/'
2985- let target = s: sub (f ,' .*<test/\w+/' ,' test/mocks/test/' )
2986- let target = s: sub (target,' _test\.rb$' ,' .rb' )
2987- return target
2988- elseif f = ~ ' \<application\.js$'
2989- return " app/helpers/application_helper.rb"
2990- elseif t = ~ ' ^js\>'
2991- return " public/javascripts/application.js"
2992- elseif t = ~ ' ^view-layout\>'
2993- return s: sub (s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /layouts/(\k+)\..*$' ,' /\1_controller.rb' ),' <application_controller\.rb$' ,' application.rb' )
2994- elseif t = ~ ' ^view\>'
2995- let controller = s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /(\k+%(\.\k+)=)\..*$' ,' _controller.rb#\1' )
2996- let controller2 = s: sub (s: sub (f ,' /views/' ,' /controllers/' ),' /(\k+%(\.\k+)=)\..*$' ,' .rb#\1' )
2997- let model = s: sub (s: sub (f ,' /views/' ,' /models/' ),' /(\k+)\..*$' ,' .rb#\1' )
2998- if filereadable (s: sub (controller,' #.{-}$' ,' ' ))
2999- return controller
3000- elseif filereadable (s: sub (controller2,' #.{-}$' ,' ' ))
3001- return controller2
3002- elseif filereadable (s: sub (model,' #.{-}$' ,' ' )) || model = ~ ' _mailer\.rb#'
3003- return model
3004- else
3005- return controller
3006- endif
3007- elseif t = ~ ' ^controller-api\>'
3008- return s: sub (s: sub (f ,' /controllers/' ,' /apis/' ),' _controller\.rb$' ,' _api.rb' )
3009- elseif t = ~ ' ^controller\>'
3010- return s: sub (s: sub (f ,' /controllers/' ,' /helpers/' ),' %(_controller)=\.rb$' ,' _helper.rb' )
3011- elseif t = ~ ' ^helper\>'
3012- return s: findlayout (s: controller ())
3013- elseif t = ~ ' ^model-arb\>'
3014- return rails#app ().migration (' 0#' .rails#pluralize (s: gsub (s: model (),' /' ,' _' )))
3015- elseif t = ~ ' ^model-aro\>'
3016- return s: sub (f ,' _observer\.rb$' ,' .rb' )
3017- elseif t = ~ ' ^api\>'
3018- return s: sub (s: sub (f ,' /apis/' ,' /controllers/' ),' _api\.rb$' ,' _controller.rb' )
3019- elseif f = ~ ' \<db/schema\.rb$'
3020- return rails#app ().migration (1 )
3021- else
3022- return " "
3023- endif
3024- endfunction
3004+ call s: add_methods (' readable' ,[' related' ])
30253005
30263006" }}}1
30273007" Partial Extraction {{{1
0 commit comments