blob: 6b9363e480407011cd3d81e655dd40884ea89635 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim filetype plugin
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00002" Language: Ruby
Bram Moolenaar91170f82006-05-05 21:15:17 +00003" Maintainer: Gavin Sinclair <gsinclair at gmail.com>
Bram Moolenaar1d689522010-05-28 20:54:39 +02004" Last Change: 2010 Mar 15
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00005" URL: http://vim-ruby.rubyforge.org
6" Anon CVS: See above site
Bram Moolenaar756ec0f2007-05-05 17:59:48 +00007" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Bram Moolenaar60a795a2005-09-16 21:55:43 +00008" ----------------------------------------------------------------------------
9"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000010" Original matchit support thanks to Ned Konz. See his ftplugin/ruby.vim at
Bram Moolenaar60a795a2005-09-16 21:55:43 +000011" http://bike-nomad.com/vim/ruby.vim.
12" ----------------------------------------------------------------------------
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14" Only do this when not done yet for this buffer
15if (exists("b:did_ftplugin"))
Bram Moolenaar60a795a2005-09-16 21:55:43 +000016 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000017endif
18let b:did_ftplugin = 1
19
Bram Moolenaar60a795a2005-09-16 21:55:43 +000020let s:cpo_save = &cpo
21set cpo&vim
22
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000023if has("gui_running") && !has("gui_win32")
24 setlocal keywordprg=ri\ -T
25else
26 setlocal keywordprg=ri
27endif
28
Bram Moolenaar60a795a2005-09-16 21:55:43 +000029" Matchit support
30if exists("loaded_matchit") && !exists("b:match_words")
31 let b:match_ignorecase = 0
32
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000033 let b:match_words =
Bram Moolenaar79166c42007-05-10 18:29:51 +000034 \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000035 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000036 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000037 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000038 \ '\<end\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000039 \ ',{:},\[:\],(:)'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000040
41 let b:match_skip =
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000042 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
Bram Moolenaar79166c42007-05-10 18:29:51 +000043 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
44 \ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
45 \ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
46 \ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
47 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000048endif
49
50setlocal formatoptions-=t formatoptions+=croql
51
52setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
53setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
54setlocal suffixesadd=.rb
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000055
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000056if exists("&ofu") && has("ruby")
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000057 setlocal omnifunc=rubycomplete#Complete
58endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000059
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000060" To activate, :set ballooneval
61if has('balloon_eval') && exists('+balloonexpr')
62 setlocal balloonexpr=RubyBalloonexpr()
63endif
64
65
Bram Moolenaar60a795a2005-09-16 21:55:43 +000066" TODO:
67"setlocal define=^\\s*def
68
69setlocal comments=:#
70setlocal commentstring=#\ %s
71
Bram Moolenaar1d689522010-05-28 20:54:39 +020072if !exists("s:ruby_path")
73 if exists("g:ruby_path")
74 let s:ruby_path = g:ruby_path
75 elseif has("ruby") && has("win32")
76 ruby VIM::command( 'let s:ruby_path = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
77 let s:ruby_path = '.,' . substitute(s:ruby_path, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000078 elseif executable("ruby")
Bram Moolenaard5cdbeb2005-10-10 20:59:28 +000079 let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000080 if &shellxquote == "'"
Bram Moolenaar1d689522010-05-28 20:54:39 +020081 let s:ruby_path = system('ruby -e "' . s:code . '"')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000082 else
Bram Moolenaar1d689522010-05-28 20:54:39 +020083 let s:ruby_path = system("ruby -e '" . s:code . "'")
Bram Moolenaar60a795a2005-09-16 21:55:43 +000084 endif
Bram Moolenaar1d689522010-05-28 20:54:39 +020085 let s:ruby_path = '.,' . substitute(s:ruby_path, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
Bram Moolenaar60a795a2005-09-16 21:55:43 +000086 else
87 " If we can't call ruby to get its path, just default to using the
88 " current directory and the directory of the current file.
Bram Moolenaar1d689522010-05-28 20:54:39 +020089 let s:ruby_path = ".,,"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000090 endif
91endif
92
Bram Moolenaar1d689522010-05-28 20:54:39 +020093let &l:path = s:ruby_path
Bram Moolenaar60a795a2005-09-16 21:55:43 +000094
95if has("gui_win32") && !exists("b:browsefilter")
96 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000097 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000098endif
99
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000100let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
Bram Moolenaar79166c42007-05-10 18:29:51 +0000101 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
102 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
103 \."| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
104
105if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
106
Bram Moolenaar1d689522010-05-28 20:54:39 +0200107 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b','n')<CR>
108 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','','n')<CR>
109 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b','n')<CR>
110 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','','n')<CR>
111 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b','v')<CR>
112 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','','v')<CR>
113 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b','v')<CR>
114 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000115
Bram Moolenaar1d689522010-05-28 20:54:39 +0200116 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b','n')<CR>
117 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','','n')<CR>
118 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b','n')<CR>
119 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','','n')<CR>
120 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b','v')<CR>
121 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','','v')<CR>
122 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b','v')<CR>
123 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000124
125 let b:undo_ftplugin = b:undo_ftplugin
126 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
127 \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
Bram Moolenaar1d689522010-05-28 20:54:39 +0200128
129 if maparg("\<C-]>",'n') == ''
130 nnoremap <silent> <buffer> <C-]> :<C-U>exe v:count1."tag <C-R>=RubyCursorIdentifier()<CR>"<CR>
131 nnoremap <silent> <buffer> g<C-]> :<C-U>exe "tjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
132 nnoremap <silent> <buffer> g] :<C-U>exe "tselect <C-R>=RubyCursorIdentifier()<CR>"<CR>
133 nnoremap <silent> <buffer> <C-W>] :<C-U>exe v:count1."stag <C-R>=RubyCursorIdentifier()<CR>"<CR>
134 nnoremap <silent> <buffer> <C-W><C-]> :<C-U>exe v:count1."stag <C-R>=RubyCursorIdentifier()<CR>"<CR>
135 nnoremap <silent> <buffer> <C-W>g<C-]> :<C-U>exe "stjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
136 nnoremap <silent> <buffer> <C-W>g] :<C-U>exe "stselect <C-R>=RubyCursorIdentifier()<CR>"<CR>
137 nnoremap <silent> <buffer> <C-W>} :<C-U>exe "ptag <C-R>=RubyCursorIdentifier()<CR>"<CR>
138 nnoremap <silent> <buffer> <C-W>g} :<C-U>exe "ptjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
139 let b:undo_ftplugin = b:undo_ftplugin
140 \."| sil! exe 'nunmap <buffer> <C-]>'| sil! exe 'nunmap <buffer> g<C-]>'| sil! exe 'nunmap <buffer> g]'"
141 \."| sil! exe 'nunmap <buffer> <C-W>]'| sil! exe 'nunmap <buffer> <C-W><C-]>'"
142 \."| sil! exe 'nunmap <buffer> <C-W>g<C-]>'| sil! exe 'nunmap <buffer> <C-W>g]'"
143 \."| sil! exe 'nunmap <buffer> <C-W>}'| sil! exe 'nunmap <buffer> <C-W>g}'"
144 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000145endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000146
147let &cpo = s:cpo_save
148unlet s:cpo_save
149
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000150if exists("g:did_ruby_ftplugin_functions")
151 finish
152endif
153let g:did_ruby_ftplugin_functions = 1
154
155function! RubyBalloonexpr()
156 if !exists('s:ri_found')
157 let s:ri_found = executable('ri')
158 endif
159 if s:ri_found
160 let line = getline(v:beval_lnum)
161 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
162 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
163 let str = b.a
164 let before = strpart(line,0,v:beval_col-strlen(b))
165 let after = strpart(line,v:beval_col+strlen(a))
166 if str =~ '^\.'
167 let str = substitute(str,'^\.','#','g')
168 if before =~ '\]\s*$'
169 let str = 'Array'.str
170 elseif before =~ '}\s*$'
171 " False positives from blocks here
172 let str = 'Hash'.str
173 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
174 let str = 'String'.str
175 elseif before =~ '\$\d\+\.\d\+\s*$'
176 let str = 'Float'.str
177 elseif before =~ '\$\d\+\s*$'
178 let str = 'Integer'.str
179 elseif before =~ '/\s*$'
180 let str = 'Regexp'.str
181 else
182 let str = substitute(str,'^#','.','')
183 endif
184 endif
185 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
186 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
187 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
188 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
189 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
190 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
191 if str !~ '^\w'
192 return ''
193 endif
194 silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000195 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000196 return ''
197 endif
198 return res
199 else
200 return ""
201 endif
202endfunction
203
Bram Moolenaar1d689522010-05-28 20:54:39 +0200204function! s:searchsyn(pattern,syn,flags,mode)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000205 norm! m'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200206 if a:mode ==# 'v'
207 norm! gv
208 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000209 let i = 0
210 let cnt = v:count ? v:count : 1
211 while i < cnt
212 let i = i + 1
213 let line = line('.')
214 let col = col('.')
215 let pos = search(a:pattern,'W'.a:flags)
216 while pos != 0 && s:synname() !~# a:syn
217 let pos = search(a:pattern,'W'.a:flags)
218 endwhile
219 if pos == 0
220 call cursor(line,col)
221 return
222 endif
223 endwhile
224endfunction
225
226function! s:synname()
227 return synIDattr(synID(line('.'),col('.'),0),'name')
228endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000229
Bram Moolenaar1d689522010-05-28 20:54:39 +0200230function! RubyCursorIdentifier()
231 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
232 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
233 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
234 let method = '\%(\<[_a-zA-Z]\w*\>\%([?!]\|\s*=>\@!\)\=\)'
235 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
236 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
237 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
238 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
239 let raw = matchstr(getline('.')[col-1 : ],pattern)
240 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*:\=','','')
241 return stripped == '' ? expand("<cword>") : stripped
242endfunction
243
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000244"
245" Instructions for enabling "matchit" support:
246"
247" 1. Look for the latest "matchit" plugin at
248"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000249" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000250"
251" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
252"
253" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
254"
255" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
256"
257" 4. Ensure this file (ftplugin/ruby.vim) is installed.
258"
259" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000260" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000261"
262" 6. Restart Vim and create the matchit documentation:
263"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000264" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000265"
266" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000267" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000268"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000269" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000270" locations of plugin directories, etc., as there are several options, and it
271" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
272"
273
Bram Moolenaar446cb832008-06-24 21:56:24 +0000274" vim: nowrap sw=2 sts=2 ts=8: