Skip to content
Browse files

Added :Rcommand.

:runtime! macros/rails.vim on buffer initialization.
Documented :Rrefresh.
  • Loading branch information...
1 parent aec0d9a commit ac07afc09f294b595b9454f201bea30a4e413882 @tpope committed Apr 16, 2007
Showing with 138 additions and 15 deletions.
  1. +49 −5 doc/rails.txt
  2. +89 −10 plugin/rails.vim
View
54 doc/rails.txt
@@ -11,6 +11,7 @@ Author: Tim Pope <vimNOSPAM@tpope.info> |rails-plugin-author|
|rails-model-navigation| Model Navigation Commands
|rails-controller-navigation| Controller Navigation Commands
|rails-misc-navigation| Miscellaneous Navigation Commands
+|rails-custom-navigation| Custom Navigation Commands
|rails-scripts| Script Wrappers
|rails-partials| Partial Extraction
|rails-integration| Plugin Integration
@@ -173,6 +174,14 @@ actually edit a file from a Rails application.
:Rtags Calls ctags -R on the current application root.
Exuberant ctags must be installed.
+ *rails-:Rrefresh*
+:Rrefresh Refreshes certain cached settings. Most noticeably,
+ this clears the cached list of classes that are syntax
+ highlighted as railsUserClass.
+
+ *rails-:Rrefresh!*
+:Rrefresh! As above, and also reloads rails.vim.
+
*rails-:OpenURL*
:OpenURL {url} This is not a command provided by the plugin, but
rather provided by user and utilized by other plugin
@@ -260,13 +269,12 @@ Two commands, :A and :R, are used quickly jump to an "alternate" and a
:AE a.vim. Briefly, they edit the "alternate" file, in
:AS either the same window (:A and :AE), a new split
:AV window (:AS), a new vertically split window (:AV), or
-:AT a new tab (:AT). An experimental mapping for :A is
- [f .
+:AT a new tab (:AT). A mapping for :A is [f .
*rails-:R* *rails-:RE* *rails-:RS* *rails-:RV* *:rails-:RT*
:R These are similar |rails-:A| and friends above, only
:RE they jump to the "related" file rather than the
-:RS "alternate." An experimental mapping for :R is ]f .
+:RS "alternate." A mapping for :R is ]f .
:RV
:RT
@@ -290,7 +298,7 @@ Suggestions for further contexts to consider for the alternate file, related
file, and file under the cursor are welcome. They are subtly tweaked from
release to release.
-For the more uncommon cases, a more deliberate set of commands are provided.
+For the less common cases, a more deliberate set of commands are provided.
Each of the following takes an optional argument (with tab completion) but
defaults to a reasonable guess that follows Rails conventions. For example,
when editing app/models/employee.rb, :Rcontroller will default to
@@ -432,6 +440,36 @@ The following commands are not clearly associated with models or controllers.
is based on the current controller or model, with no
singularization or pluralization done.
+Custom Navigation Commands ~
+ *rails-custom-navigation*
+
+It is also possible to create custom navigation commands. This is best done
+in an initialization routine of some sort (e.g., an autocommand); see
+|rails-configuration| for details.
+
+ *rails-:Rcommand*
+:Rcommand [options] {name} [{path} ...]
+ Create a navigation command with the supplied
+ name, looking in the supplied paths, using the
+ supplied options. The -suffix option specifies what
+ suffix to filter on, and strip from the filename, and
+ defaults to -suffix=.rb . The -glob option specifies
+ a file glob to use to find files, _excluding_ the
+ suffix. Useful values include -glob=* and -glob=**/*.
+ The -default option specifies a default argument (not
+ a full path). If it is specified as -default=model(),
+ -default=controller(), or -default=both(), the current
+ model, controller, or both (as with :Rintegrationtest)
+ is used as a default.
+
+:Rcommand is still under development and far from fully documented, but the
+following examples should illustrate the basics:
+>
+ Rcommand api app/apis -glob=**/* -suffix=_api.rb
+ Rcommand config config -glob=*.* -suffix= -default=routes.rb
+ Rcommand environment config/environments -default=../environment
+ Rcommand concern app/concerns -glob=**/*
+
Finally, one Vim feature that proves helpful in conjunction with all of the
above is |CTRL-^|. This keystroke edits the previous file, and is helpful to
back out of any of the above commands.
@@ -618,7 +656,7 @@ most terminals send <M-o> as <Esc>o anyways.
>
imap <M-o> <Esc>o
<
-One can also use the "e" surrounding in a plain Ruby file to append a bare
+One can also use the <C-E> surrounding in a plain Ruby file to append a bare
"end" on the following line.
==============================================================================
@@ -693,6 +731,12 @@ specification. There is also a filename matching syntax: >
Use the filetype based syntax whenever possible, reserving the filename based
syntax for more advanced cases.
+ *macros/rails.vim*
+If you have several commands to run on initialization for all file types, they
+can be placed in a "macros/rails.vim" file in the 'runtimepath' (for example,
+"~/.vim/macros/rails.vim"). This file is sourced by rails.vim each time a
+Rails file is loaded.
+
*config/rails.vim*
If you have settings particular to a specific project, they can be put in a
config/rails.vim file in the root directory of the application. The file is
View
99 plugin/rails.vim
@@ -1,7 +1,7 @@
" rails.vim - Detect a rails application
" Author: Tim Pope <vimNOSPAM@tpope.info>
" GetLatestVimScripts: 1567 1 :AutoInstall: rails.vim
-" URL: http://svn.tpope.net/rails/vim/railsvim
+" URL: http://rails.vim.tpope.net/
" $Id$
" See doc/rails.txt for details. Grab it from the URL above if you don't have it
@@ -623,7 +623,7 @@ function! s:BufCommands()
command! -buffer -bar -nargs=* -bang -complete=custom,s:SetComplete Rset :call s:Set(<bang>0,<f-args>)
command! -buffer -bar -nargs=0 Rtags :call s:Tags(<bang>0)
command! -buffer -bar -nargs=0 -bang Rdoc :if <bang>0 | call s:prephelp() | help rails | else | call s:Doc(<bang>0) | endif
- command! -buffer -bar -nargs=0 -bang Refresh :Rrefresh<bang>
+ "command! -buffer -bar -nargs=0 -bang Refresh :Rrefresh<bang>
command! -buffer -bar -nargs=0 -bang Rrefresh :if <bang>0|unlet! g:loaded_rails|source `=s:file`|endif|call s:Refresh(<bang>0)
if exists(":Project")
command! -buffer -bar -nargs=? -bang Rproject :call s:Project(<bang>0,<q-args>)
@@ -1573,6 +1573,7 @@ function! s:addfilecmds(type)
endfunction
function! s:BufFinderCommands()
+ command! -buffer -bar -nargs=+ Rcommand :call s:Command(<bang>0,<f-args>)
call s:addfilecmds("model")
call s:addfilecmds("view")
call s:addfilecmds("controller")
@@ -1758,6 +1759,83 @@ function! s:libList(A,L,P)
return s:autocamelize(all,a:A)
endfunction
+function! s:Command(bang,name,...)
+ if a:name !~ '^[A-Za-z]\+$'
+ return s:error("E182: Invalid command name")
+ endif
+ let suffix = ".rb"
+ let filter = "**/*"
+ let prefix = ""
+ let default = ""
+ let i = 0
+ while i < a:0
+ let i = i + 1
+ let arg = a:{i}
+ if arg =~# '^-suffix='
+ let suffix = matchstr(arg,'-suffix=\zs.*')
+ elseif arg =~# '^-default='
+ let default = matchstr(arg,'-default=\zs.*')
+ elseif arg =~# '^-\%(glob\|filter)='
+ let filter = matchstr(arg,'-\w*=\zs.*')
+ elseif arg !~# '^-'
+ " A literal '\n'. For evaluation below
+ let prefix = prefix."\\n".s:sub(arg,'/\=$','/')
+ endif
+ endwhile
+ let prefix = s:sub(prefix,'^\\n','')
+ let name = a:name
+ let cmds = 'ESVT '
+ let cmd = ''
+ while cmds != ''
+ exe 'command! -buffer -bar -bang -nargs=* -complete=custom,s:CommandList R'.cmd.name." :call s:CommandEdit(<bang>0,'".cmd."','".name."',\"".prefix."\",".s:string(suffix).",".s:string(filter).",".s:string(default).",<f-args>)"
+ let cmd = strpart(cmds,0,1)
+ let cmds = strpart(cmds,1)
+ endwhile
+endfunction
+
+function! s:CommandList(A,L,P)
+ let cmd = matchstr(a:L,'\CR[A-Z]\=\w\+')
+ exe cmd." &"
+ let lp = s:last_prefix . "\n"
+ let res = ""
+ while lp != ""
+ let p = matchstr(lp,'.\{-\}\ze\n')
+ let lp = s:sub(lp,'.\{-\}\n','')
+ let res = res . s:relglob(p,s:last_filter,s:last_suffix)."\n"
+ endwhile
+ let res = s:sub(s:sub(res,'^\n',''),'\n$','')
+ let res = s:gsub(res,'\n\n\+','\n')
+ if s:last_camelize
+ return s:autocamelize(res,a:A)
+ else
+ return res
+ endif
+endfunction
+
+function! s:CommandEdit(bang,cmd,name,prefix,suffix,filter,default,...)
+ if a:0 && a:1 == "&"
+ let s:last_prefix = a:prefix
+ let s:last_suffix = a:suffix
+ let s:last_filter = a:filter
+ let s:last_camelize = (a:suffix =~# '\.rb$')
+ else
+ if a:default == "both()"
+ if s:model() != ""
+ let default = s:model()
+ else
+ let default = s:controller()
+ endif
+ elseif a:default == "model()"
+ let default = s:model(1)
+ elseif a:default == "controller()"
+ let default = s:controller(1)
+ else
+ let default = a:default
+ endif
+ call s:EditSimpleRb(a:bang,a:cmd,a:name,a:0 ? a:1 : default,a:prefix,a:suffix)
+ endif
+endfunction
+
function! s:EditSimpleRb(bang,cmd,name,target,prefix,suffix)
let cmd = s:findcmdfor(a:cmd.(a:bang?'!':''))
if a:target == ""
@@ -1772,7 +1850,7 @@ function! s:EditSimpleRb(bang,cmd,name,target,prefix,suffix)
else
let f = f.a:suffix
if a:suffix !~ '\.'
- let f = f.".rb"
+ "let f = f.".rb"
endif
endif
let f = s:gsub(a:prefix,'\n',f.'\n').f
@@ -1840,11 +1918,11 @@ function! s:fixturesEdit(bang,cmd,...)
endfunction
function! s:modelEdit(bang,cmd,...)
- call s:EditSimpleRb(a:bang,a:cmd,"model",a:0? a:1 : s:model(1),"app/models/","")
+ call s:EditSimpleRb(a:bang,a:cmd,"model",a:0? a:1 : s:model(1),"app/models/",".rb")
endfunction
function! s:observerEdit(bang,cmd,...)
- call s:EditSimpleRb(a:bang,a:cmd,"observer",a:0? a:1 : s:model(1),"app/models/","_observer")
+ call s:EditSimpleRb(a:bang,a:cmd,"observer",a:0? a:1 : s:model(1),"app/models/","_observer.rb")
endfunction
function! s:viewEdit(bang,cmd,...)
@@ -1926,11 +2004,11 @@ function! s:controllerEdit(bang,cmd,...)
endfunction
function! s:helperEdit(bang,cmd,...)
- return s:EditSimpleRb(a:bang,a:cmd,"helper",a:0? a:1 : s:controller(1),"app/helpers/\ncomponents/","_helper")
+ return s:EditSimpleRb(a:bang,a:cmd,"helper",a:0? a:1 : s:controller(1),"app/helpers/\ncomponents/","_helper.rb")
endfunction
function! s:apiEdit(bang,cmd,...)
- return s:EditSimpleRb(a:bang,a:cmd,"api",a:0 ? a:1 : s:controller(1),"app/apis/","_api")
+ return s:EditSimpleRb(a:bang,a:cmd,"api",a:0 ? a:1 : s:controller(1),"app/apis/","_api.rb")
endfunction
function! s:stylesheetEdit(bang,cmd,...)
@@ -1948,7 +2026,7 @@ function! s:unittestEdit(bang,cmd,...)
let f = f . "_observer"
endif
endif
- return s:EditSimpleRb(a:bang,a:cmd,"unittest",f,"test/unit/","_test")
+ return s:EditSimpleRb(a:bang,a:cmd,"unittest",f,"test/unit/","_test.rb")
endfunction
function! s:functionaltestEdit(bang,cmd,...)
@@ -1964,7 +2042,7 @@ function! s:functionaltestEdit(bang,cmd,...)
let f = f . "_api"
endif
endif
- return s:EditSimpleRb(a:bang,a:cmd,"functionaltest",f,"test/functional/","_test")
+ return s:EditSimpleRb(a:bang,a:cmd,"functionaltest",f,"test/functional/","_test.rb")
endfunction
function! s:integrationtestEdit(bang,cmd,...)
@@ -1975,7 +2053,7 @@ function! s:integrationtestEdit(bang,cmd,...)
else
let f = s:controller()
endif
- return s:EditSimpleRb(a:bang,a:cmd,"integrationtest",f,"test/integration/","_test")
+ return s:EditSimpleRb(a:bang,a:cmd,"integrationtest",f,"test/integration/","_test.rb")
endfunction
function! s:pluginEdit(bang,cmd,...)
@@ -4049,6 +4127,7 @@ function! s:BufInit(path)
if f =~ '[ !#$%\,]'
let f = ''
endif
+ runtime! macros/rails.vim
silent doautocmd User Rails
if t != '-'
exe "silent doautocmd User Rails".s:gsub(t,'-','.')

0 comments on commit ac07afc

Please sign in to comment.
Something went wrong with that request. Please try again.