blob: b61c1765d97ee9969fd98ebd24c6fc3b0e01eb74 [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 Moolenaarec7944a2013-06-12 21:29:15 +02003" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
4" URL: https://github.com/vim-ruby/vim-ruby
Bram Moolenaard09091d2019-01-17 16:07:22 +01005" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
Doug Kearnsda16a1b2023-09-01 18:33:33 +02006" Last Change: 2023 Sep 1st
Bram Moolenaar071d4272004-06-13 20:20:40 +00007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008if (exists("b:did_ftplugin"))
Bram Moolenaar60a795a2005-09-16 21:55:43 +00009 finish
Bram Moolenaar071d4272004-06-13 20:20:40 +000010endif
11let b:did_ftplugin = 1
12
Bram Moolenaar60a795a2005-09-16 21:55:43 +000013let s:cpo_save = &cpo
14set cpo&vim
15
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000016if has("gui_running") && !has("gui_win32")
Bram Moolenaarec7944a2013-06-12 21:29:15 +020017 setlocal keywordprg=ri\ -T\ -f\ bs
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000018else
19 setlocal keywordprg=ri
20endif
21
Bram Moolenaar60a795a2005-09-16 21:55:43 +000022" Matchit support
23if exists("loaded_matchit") && !exists("b:match_words")
24 let b:match_ignorecase = 0
25
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000026 let b:match_words =
Bram Moolenaar2ed639a2019-12-09 23:11:18 +010027 \ '{\|\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|=\@<!begin\)\>=\@!' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000028 \ ':' .
Bram Moolenaar79166c42007-05-10 18:29:51 +000029 \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000030 \ ':' .
Bram Moolenaar2ed639a2019-12-09 23:11:18 +010031 \ '}\|\%(^\|[^.\:@$=]\)\@<=\<end\:\@!\>' .
32 \ ',^=begin\>:^=end\>,' .
33 \ ',\[:\],(:)'
Bram Moolenaar60a795a2005-09-16 21:55:43 +000034
35 let b:match_skip =
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000036 \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
Bram Moolenaar2ed639a2019-12-09 23:11:18 +010037 \ "\\<ruby\\%(String\\|.\+Delimiter\\|Character\\|.\+Escape\\|" .
38 \ "Regexp\\|Interpolation\\|Comment\\|Documentation\\|" .
39 \ "ConditionalModifier\\|RepeatModifier\\|RescueModifier\\|OptionalDo\\|" .
40 \ "MethodName\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
Bram Moolenaar79166c42007-05-10 18:29:51 +000041 \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
Bram Moolenaar60a795a2005-09-16 21:55:43 +000042endif
43
44setlocal formatoptions-=t formatoptions+=croql
45
Bram Moolenaarec7944a2013-06-12 21:29:15 +020046setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
Bram Moolenaar60a795a2005-09-16 21:55:43 +000047setlocal suffixesadd=.rb
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000048
Bram Moolenaar756ec0f2007-05-05 17:59:48 +000049if exists("&ofu") && has("ruby")
Bram Moolenaarc6249bb2006-04-15 20:25:09 +000050 setlocal omnifunc=rubycomplete#Complete
51endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +000052
53" TODO:
54"setlocal define=^\\s*def
55
Bram Moolenaar46eea442022-03-30 10:51:39 +010056setlocal comments=b:#
Bram Moolenaar60a795a2005-09-16 21:55:43 +000057setlocal commentstring=#\ %s
58
Bram Moolenaarec7944a2013-06-12 21:29:15 +020059if !exists('g:ruby_version_paths')
60 let g:ruby_version_paths = {}
61endif
62
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020063function! s:query_path(root) abort
Christian Brabandtf7ac0ef2023-09-06 20:41:25 +020064 " Disabled by default for security reasons.
65 if !get(g:, 'ruby_exec', get(g:, 'plugin_exec', 0))
66 return []
67 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020068 let code = "print $:.join %q{,}"
Bram Moolenaard09091d2019-01-17 16:07:22 +010069 if &shell =~# 'sh' && empty(&shellxquote)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020070 let prefix = 'env PATH='.shellescape($PATH).' '
Bram Moolenaar60a795a2005-09-16 21:55:43 +000071 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +020072 let prefix = ''
73 endif
74 if &shellxquote == "'"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020075 let path_check = prefix.'ruby --disable-gems -e "' . code . '"'
Bram Moolenaarec7944a2013-06-12 21:29:15 +020076 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020077 let path_check = prefix."ruby --disable-gems -e '" . code . "'"
Bram Moolenaarec7944a2013-06-12 21:29:15 +020078 endif
79
80 let cd = haslocaldir() ? 'lcd' : 'cd'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020081 let cwd = fnameescape(getcwd())
Bram Moolenaarec7944a2013-06-12 21:29:15 +020082 try
83 exe cd fnameescape(a:root)
Anton Sharonov67c951d2023-09-05 21:03:27 +020084 let s:tmp_cwd = getcwd()
85 if (fnamemodify(exepath('ruby'), ':p:h') ==# cwd
86 \ && (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) == -1 || s:tmp_cwd == '.'))
Doug Kearnsda16a1b2023-09-01 18:33:33 +020087 let path = []
88 else
89 let path = split(system(path_check),',')
90 endif
Christian Brabandtf7ac0ef2023-09-06 20:41:25 +020091 unlet! s:tmp_cwd
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020092 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020093 return path
94 finally
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020095 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020096 endtry
97endfunction
98
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020099function! s:build_path(path) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200100 let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
Bram Moolenaar46eea442022-03-30 10:51:39 +0100101 if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
102 let path = path . ',.,,'
103 elseif &g:path =~# ',\.,,$'
104 let path = &g:path[0:-4] . path . ',.,,'
105 elseif &g:path =~# ',,$'
106 let path = &g:path[0:-2] . path . ',,'
107 else
108 let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200109 endif
110 return path
111endfunction
112
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200113if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
114 let s:version_file = findfile('.ruby-version', '.;')
115 if !empty(s:version_file) && filereadable(s:version_file)
116 let b:ruby_version = get(readfile(s:version_file, '', 1), '')
117 if !has_key(g:ruby_version_paths, b:ruby_version)
118 let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
119 endif
120 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000121endif
122
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200123if exists("g:ruby_path")
124 let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
125elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
126 let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
127 let s:ruby_path = s:build_path(s:ruby_paths)
128else
129 if !exists('g:ruby_default_path')
130 if has("ruby") && has("win32")
131 ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
132 elseif executable('ruby') && !empty($HOME)
133 let g:ruby_default_path = s:query_path($HOME)
134 else
135 let g:ruby_default_path = map(split($RUBYLIB,':'), 'v:val ==# "." ? "" : v:val')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200136 endif
137 endif
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200138 let s:ruby_paths = g:ruby_default_path
139 let s:ruby_path = s:build_path(s:ruby_paths)
140endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200141
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200142if stridx(&l:path, s:ruby_path) == -1
143 let &l:path = s:ruby_path
144endif
145if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
146 let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
147endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000148
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200149if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000150 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000151 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000152endif
153
Bram Moolenaard09091d2019-01-17 16:07:22 +0100154let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
Bram Moolenaar79166c42007-05-10 18:29:51 +0000155 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
156 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
Bram Moolenaard09091d2019-01-17 16:07:22 +0100157
158if get(g:, 'ruby_recommended_style', 1)
159 setlocal shiftwidth=2 softtabstop=2 expandtab
160 let b:undo_ftplugin .= ' | setl sw< sts< et<'
161endif
162
163" To activate, :set ballooneval
164if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
165 setlocal balloonexpr=RubyBalloonexpr()
166 let b:undo_ftplugin .= "| setl bexpr="
167endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000168
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200169function! s:map(mode, flags, map) abort
170 let from = matchstr(a:map, '\S\+')
171 if empty(mapcheck(from, a:mode))
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100172 exe a:mode.'map' '<buffer>' a:flags a:map
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200173 let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
174 endif
175endfunction
176
Bram Moolenaard09091d2019-01-17 16:07:22 +0100177cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200178cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100179let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200180
Bram Moolenaar79166c42007-05-10 18:29:51 +0000181if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200182 nmap <buffer><script> <SID>: :<C-U>
183 nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
Bram Moolenaar46eea442022-03-30 10:51:39 +0100184 cmap <buffer> <SID><cfile> <Plug><cfile>
185 cmap <buffer> <SID><ctag> <Plug><ctag>
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200186
Bram Moolenaard09091d2019-01-17 16:07:22 +0100187 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
188 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
189 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
190 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
191 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
192 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
193 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
194 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000195
Bram Moolenaard09091d2019-01-17 16:07:22 +0100196 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
197 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
198 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
199 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
200 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
201 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
202 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
203 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000204
205 let b:undo_ftplugin = b:undo_ftplugin
206 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
207 \."| 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 +0200208
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200209 if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200210 onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
211 onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
212 xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
213 xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
214 let b:undo_ftplugin = b:undo_ftplugin
215 \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
216 \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
217 endif
218
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200219 if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200220 onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
221 onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
222 xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
223 xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
224 let b:undo_ftplugin = b:undo_ftplugin
225 \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
226 \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
227 endif
228
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200229 call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200230
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200231 cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
Bram Moolenaar46eea442022-03-30 10:51:39 +0100232 call s:map('n', '<script><silent>', '<C-]> <SID>:exe v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
233 call s:map('n', '<script><silent>', 'g<C-]> <SID>:exe "tjump <SID><ctag>"<SID>tagzv<CR>')
234 call s:map('n', '<script><silent>', 'g] <SID>:exe "tselect <SID><ctag>"<SID>tagzv<CR>')
235 call s:map('n', '<script><silent>', '<C-W>] <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
236 call s:map('n', '<script><silent>', '<C-W><C-]> <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
237 call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe "stjump <SID><ctag>"<SID>tagzv<CR>')
238 call s:map('n', '<script><silent>', '<C-W>g] <SID>:exe "stselect <SID><ctag>"<SID>tagzv<CR>')
239 call s:map('n', '<script><silent>', '<C-W>} <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
240 call s:map('n', '<script><silent>', '<C-W>g} <SID>:exe "ptjump <SID><ctag>"<CR>')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200241
Bram Moolenaar46eea442022-03-30 10:51:39 +0100242 call s:map('n', '<script><silent>', 'gf <SID>c:find <SID><cfile><CR>')
243 call s:map('n', '<script><silent>', '<C-W>f <SID>c:sfind <SID><cfile><CR>')
244 call s:map('n', '<script><silent>', '<C-W><C-F> <SID>c:sfind <SID><cfile><CR>')
245 call s:map('n', '<script><silent>', '<C-W>gf <SID>c:tabfind <SID><cfile><CR>')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000246endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000247
248let &cpo = s:cpo_save
249unlet s:cpo_save
250
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000251if exists("g:did_ruby_ftplugin_functions")
252 finish
253endif
254let g:did_ruby_ftplugin_functions = 1
255
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200256function! RubyBalloonexpr() abort
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000257 if !exists('s:ri_found')
258 let s:ri_found = executable('ri')
259 endif
260 if s:ri_found
261 let line = getline(v:beval_lnum)
262 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
263 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
264 let str = b.a
265 let before = strpart(line,0,v:beval_col-strlen(b))
266 let after = strpart(line,v:beval_col+strlen(a))
267 if str =~ '^\.'
268 let str = substitute(str,'^\.','#','g')
269 if before =~ '\]\s*$'
270 let str = 'Array'.str
271 elseif before =~ '}\s*$'
272 " False positives from blocks here
273 let str = 'Hash'.str
274 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
275 let str = 'String'.str
276 elseif before =~ '\$\d\+\.\d\+\s*$'
277 let str = 'Float'.str
278 elseif before =~ '\$\d\+\s*$'
279 let str = 'Integer'.str
280 elseif before =~ '/\s*$'
281 let str = 'Regexp'.str
282 else
283 let str = substitute(str,'^#','.','')
284 endif
285 endif
286 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
287 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
288 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
289 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
290 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
291 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
292 if str !~ '^\w'
293 return ''
294 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200295 silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000296 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000297 return ''
298 endif
299 return res
300 else
301 return ""
302 endif
303endfunction
304
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200305function! s:searchsyn(pattern, syn, flags, mode) abort
306 let cnt = v:count1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200307 norm! m'
308 if a:mode ==# 'v'
309 norm! gv
310 endif
311 let i = 0
Bram Moolenaard09091d2019-01-17 16:07:22 +0100312 call map(a:syn, 'hlID(v:val)')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200313 while i < cnt
314 let i = i + 1
315 let line = line('.')
316 let col = col('.')
317 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaard09091d2019-01-17 16:07:22 +0100318 while pos != 0 && index(a:syn, s:synid()) < 0
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200319 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000320 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200321 if pos == 0
322 call cursor(line,col)
323 return
324 endif
325 endwhile
Bram Moolenaar79166c42007-05-10 18:29:51 +0000326endfunction
327
Bram Moolenaard09091d2019-01-17 16:07:22 +0100328function! s:synid() abort
329 return synID(line('.'),col('.'),0)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200330endfunction
331
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200332function! s:wrap_i(back,forward) abort
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100333 execute 'norm! k'
334 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200335 let line = line('.')
336 execute 'norm '.a:back
337 if line('.') == line - 1
338 return s:wrap_a(a:back,a:forward)
339 endif
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100340 execute 'norm! jV'
341 execute 'norm '.a:forward
342 execute 'norm! k'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200343endfunction
344
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200345function! s:wrap_a(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200346 execute 'norm '.a:forward
347 if line('.') < line('$') && getline(line('.')+1) ==# ''
348 let after = 1
349 endif
350 execute 'norm '.a:back
351 while getline(line('.')-1) =~# '^\s*#' && line('.')
352 -
353 endwhile
354 if exists('after')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100355 execute 'norm! V'
356 execute 'norm '.a:forward
357 execute 'norm! j'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200358 elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100359 execute 'norm! kV'
360 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200361 else
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100362 execute 'norm! V'
363 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200364 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000365endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000366
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200367function! RubyCursorIdentifier() abort
Bram Moolenaar1d689522010-05-28 20:54:39 +0200368 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
369 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
370 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200371 let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200372 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
373 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
374 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
375 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
376 let raw = matchstr(getline('.')[col-1 : ],pattern)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200377 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
Bram Moolenaar1d689522010-05-28 20:54:39 +0200378 return stripped == '' ? expand("<cword>") : stripped
379endfunction
380
Bram Moolenaard09091d2019-01-17 16:07:22 +0100381function! RubyCursorTag() abort
382 return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
383endfunction
384
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200385function! RubyCursorFile() abort
386 let isfname = &isfname
387 try
388 set isfname+=:
389 let cfile = expand('<cfile>')
390 finally
391 let isfname = &isfname
392 endtry
393 let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
394 let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100395 if s:synid() ==# hlID('rubyConstant')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200396 let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100397 let cfile = substitute(cfile,'^::','','')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200398 let cfile = substitute(cfile,'::','/','g')
399 let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
400 let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
401 return tolower(cfile) . '.rb'
402 elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100403 let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
404 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200405 elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
406 let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100407 let cfile = expand('%:p:h') . target
408 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200409 elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100410 let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
411 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200412 elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
413 let cfile = expand('%:p:h') . strpart(cfile, 2)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200414 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200415 return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200416 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200417 let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
418 let cfile = substitute(cfile, cwdpat, '.', '')
419 if fnameescape(cfile) !=# cfile
420 return '+ '.fnameescape(cfile)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200421 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200422 return cfile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200423 endif
424endfunction
425
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000426"
427" Instructions for enabling "matchit" support:
428"
429" 1. Look for the latest "matchit" plugin at
430"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000431" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000432"
433" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
434"
435" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
436"
437" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
438"
439" 4. Ensure this file (ftplugin/ruby.vim) is installed.
440"
441" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000442" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000443"
444" 6. Restart Vim and create the matchit documentation:
445"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000446" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000447"
448" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000449" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000450"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000451" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000452" locations of plugin directories, etc., as there are several options, and it
453" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
454"
455
Bram Moolenaar446cb832008-06-24 21:56:24 +0000456" vim: nowrap sw=2 sts=2 ts=8: