7
|
1 " Vim filetype plugin
|
831
|
2 " Language: Ruby
|
867
|
3 " Maintainer: Gavin Sinclair <gsinclair at gmail.com>
|
831
|
4 " Info: $Id$
|
|
5 " URL: http://vim-ruby.rubyforge.org
|
|
6 " Anon CVS: See above site
|
1123
|
7 " Release Coordinator: Doug Kearns <dougkearns@gmail.com>
|
530
|
8 " ----------------------------------------------------------------------------
|
|
9 "
|
1123
|
10 " Original matchit support thanks to Ned Konz. See his ftplugin/ruby.vim at
|
530
|
11 " http://bike-nomad.com/vim/ruby.vim.
|
|
12 " ----------------------------------------------------------------------------
|
7
|
13
|
|
14 " Only do this when not done yet for this buffer
|
|
15 if (exists("b:did_ftplugin"))
|
530
|
16 finish
|
7
|
17 endif
|
|
18 let b:did_ftplugin = 1
|
|
19
|
530
|
20 let s:cpo_save = &cpo
|
|
21 set cpo&vim
|
|
22
|
1123
|
23 if has("gui_running") && !has("gui_win32")
|
|
24 setlocal keywordprg=ri\ -T
|
|
25 else
|
|
26 setlocal keywordprg=ri
|
|
27 endif
|
|
28
|
530
|
29 " Matchit support
|
|
30 if exists("loaded_matchit") && !exists("b:match_words")
|
|
31 let b:match_ignorecase = 0
|
|
32
|
1123
|
33 let b:match_words =
|
1215
|
34 \ '\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|begin\)\>=\@!' .
|
1123
|
35 \ ':' .
|
1215
|
36 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
|
1123
|
37 \ ':' .
|
1215
|
38 \ '\<end\>' .
|
1123
|
39 \ ',{:},\[:\],(:)'
|
530
|
40
|
|
41 let b:match_skip =
|
1123
|
42 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
|
1215
|
43 \ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
|
|
44 \ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
|
|
45 \ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
|
|
46 \ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
|
|
47 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
|
530
|
48 endif
|
|
49
|
|
50 setlocal formatoptions-=t formatoptions+=croql
|
|
51
|
|
52 setlocal include=^\\s*\\<\\(load\\\|\w*require\\)\\>
|
|
53 setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
|
|
54 setlocal suffixesadd=.rb
|
831
|
55
|
1123
|
56 if exists("&ofu") && has("ruby")
|
831
|
57 setlocal omnifunc=rubycomplete#Complete
|
|
58 endif
|
530
|
59
|
1123
|
60 " To activate, :set ballooneval
|
|
61 if has('balloon_eval') && exists('+balloonexpr')
|
|
62 setlocal balloonexpr=RubyBalloonexpr()
|
|
63 endif
|
|
64
|
|
65
|
530
|
66 " TODO:
|
|
67 "setlocal define=^\\s*def
|
|
68
|
|
69 setlocal comments=:#
|
|
70 setlocal commentstring=#\ %s
|
|
71
|
|
72 if !exists("s:rubypath")
|
1123
|
73 if has("ruby") && has("win32")
|
|
74 ruby VIM::command( 'let s:rubypath = "%s"' % ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,}) )
|
|
75 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
|
|
76 elseif executable("ruby")
|
548
|
77 let s:code = "print ($: + begin; require %q{rubygems}; Gem.all_load_paths.sort.uniq; rescue LoadError; []; end).join(%q{,})"
|
530
|
78 if &shellxquote == "'"
|
548
|
79 let s:rubypath = system('ruby -e "' . s:code . '"')
|
530
|
80 else
|
548
|
81 let s:rubypath = system("ruby -e '" . s:code . "'")
|
530
|
82 endif
|
548
|
83 let s:rubypath = '.,' . substitute(s:rubypath, '\%(^\|,\)\.\%(,\|$\)', ',,', '')
|
530
|
84 else
|
|
85 " If we can't call ruby to get its path, just default to using the
|
|
86 " current directory and the directory of the current file.
|
|
87 let s:rubypath = ".,,"
|
|
88 endif
|
|
89 endif
|
|
90
|
|
91 let &l:path = s:rubypath
|
|
92
|
|
93 if has("gui_win32") && !exists("b:browsefilter")
|
|
94 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
|
1123
|
95 \ "All Files (*.*)\t*.*\n"
|
530
|
96 endif
|
|
97
|
1123
|
98 let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< kp<"
|
1215
|
99 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
|
|
100 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
|
|
101 \."| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
|
|
102
|
|
103 if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
|
|
104
|
|
105 noremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b')<CR>
|
|
106 noremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','')<CR>
|
|
107 noremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b')<CR>
|
|
108 noremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','')<CR>
|
|
109
|
|
110 noremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','b')<CR>
|
|
111 noremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>','rubyModule\<Bar>rubyClass','')<CR>
|
|
112 noremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','b')<CR>
|
|
113 noremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>','rubyModule\<Bar>rubyClass','')<CR>
|
|
114
|
|
115 let b:undo_ftplugin = b:undo_ftplugin
|
|
116 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
|
|
117 \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
|
|
118 endif
|
530
|
119
|
|
120 let &cpo = s:cpo_save
|
|
121 unlet s:cpo_save
|
|
122
|
1123
|
123 if exists("g:did_ruby_ftplugin_functions")
|
|
124 finish
|
|
125 endif
|
|
126 let g:did_ruby_ftplugin_functions = 1
|
|
127
|
|
128 function! RubyBalloonexpr()
|
|
129 if !exists('s:ri_found')
|
|
130 let s:ri_found = executable('ri')
|
|
131 endif
|
|
132 if s:ri_found
|
|
133 let line = getline(v:beval_lnum)
|
|
134 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
|
|
135 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
|
|
136 let str = b.a
|
|
137 let before = strpart(line,0,v:beval_col-strlen(b))
|
|
138 let after = strpart(line,v:beval_col+strlen(a))
|
|
139 if str =~ '^\.'
|
|
140 let str = substitute(str,'^\.','#','g')
|
|
141 if before =~ '\]\s*$'
|
|
142 let str = 'Array'.str
|
|
143 elseif before =~ '}\s*$'
|
|
144 " False positives from blocks here
|
|
145 let str = 'Hash'.str
|
|
146 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
|
|
147 let str = 'String'.str
|
|
148 elseif before =~ '\$\d\+\.\d\+\s*$'
|
|
149 let str = 'Float'.str
|
|
150 elseif before =~ '\$\d\+\s*$'
|
|
151 let str = 'Integer'.str
|
|
152 elseif before =~ '/\s*$'
|
|
153 let str = 'Regexp'.str
|
|
154 else
|
|
155 let str = substitute(str,'^#','.','')
|
|
156 endif
|
|
157 endif
|
|
158 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
|
|
159 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
|
|
160 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
|
|
161 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
|
|
162 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
|
|
163 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
|
|
164 if str !~ '^\w'
|
|
165 return ''
|
|
166 endif
|
|
167 silent! let res = substitute(system("ri -f simple -T \"".str.'"'),'\n$','','')
|
1215
|
168 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
|
1123
|
169 return ''
|
|
170 endif
|
|
171 return res
|
|
172 else
|
|
173 return ""
|
|
174 endif
|
|
175 endfunction
|
|
176
|
1215
|
177 function! s:searchsyn(pattern,syn,flags)
|
|
178 norm! m'
|
|
179 let i = 0
|
|
180 let cnt = v:count ? v:count : 1
|
|
181 while i < cnt
|
|
182 let i = i + 1
|
|
183 let line = line('.')
|
|
184 let col = col('.')
|
|
185 let pos = search(a:pattern,'W'.a:flags)
|
|
186 while pos != 0 && s:synname() !~# a:syn
|
|
187 let pos = search(a:pattern,'W'.a:flags)
|
|
188 endwhile
|
|
189 if pos == 0
|
|
190 call cursor(line,col)
|
|
191 return
|
|
192 endif
|
|
193 endwhile
|
|
194 endfunction
|
|
195
|
|
196 function! s:synname()
|
|
197 return synIDattr(synID(line('.'),col('.'),0),'name')
|
|
198 endfunction
|
1123
|
199
|
530
|
200 "
|
|
201 " Instructions for enabling "matchit" support:
|
|
202 "
|
|
203 " 1. Look for the latest "matchit" plugin at
|
|
204 "
|
1123
|
205 " http://www.vim.org/scripts/script.php?script_id=39
|
530
|
206 "
|
|
207 " It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
|
|
208 "
|
|
209 " 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
|
|
210 "
|
|
211 " 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
|
|
212 "
|
|
213 " 4. Ensure this file (ftplugin/ruby.vim) is installed.
|
|
214 "
|
|
215 " 5. Ensure you have this line in your $HOME/.vimrc:
|
1123
|
216 " filetype plugin on
|
530
|
217 "
|
|
218 " 6. Restart Vim and create the matchit documentation:
|
|
219 "
|
1123
|
220 " :helptags ~/.vim/doc
|
530
|
221 "
|
|
222 " Now you can do ":help matchit", and you should be able to use "%" on Ruby
|
1123
|
223 " keywords. Try ":echo b:match_words" to be sure.
|
530
|
224 "
|
1123
|
225 " Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
|
530
|
226 " locations of plugin directories, etc., as there are several options, and it
|
|
227 " differs on Windows. Email gsinclair@soyabean.com.au if you need help.
|
|
228 "
|
|
229
|
|
230 " vim: nowrap sw=2 sts=2 ts=8 ff=unix:
|