blob: ab0a32576c3652eb0992b93666e89d67dbd767ad [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
dkearnsd08059a2024-01-02 04:58:57 +11005" Last Change: 2023 Dec 31
Doug Kearns93197fd2024-01-14 20:59:02 +01006" 2024 Jan 14 by Vim Project (browsefilter)
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
dkearnsd08059a2024-01-02 04:58:57 +110063let s:path_split = has('win32') ? ';' : ':'
64
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020065function! s:query_path(root) abort
dkearnsd08059a2024-01-02 04:58:57 +110066 " Disabled by default for security reasons.
67 if !get(g:, 'ruby_exec', get(g:, 'plugin_exec', 0)) || empty(a:root)
68 return map(split($RUBYLIB, s:path_split), 'v:val ==# "." ? "" : v:val')
Christian Brabandtf7ac0ef2023-09-06 20:41:25 +020069 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +020070 let code = "print $:.join %q{,}"
Bram Moolenaarec7944a2013-06-12 21:29:15 +020071 if &shellxquote == "'"
dkearnsd08059a2024-01-02 04:58:57 +110072 let args = ' --disable-gems -e "' . code . '"'
Bram Moolenaarec7944a2013-06-12 21:29:15 +020073 else
dkearnsd08059a2024-01-02 04:58:57 +110074 let args = " --disable-gems -e '" . code . "'"
Bram Moolenaarec7944a2013-06-12 21:29:15 +020075 endif
76
dkearnsd08059a2024-01-02 04:58:57 +110077 let cd = haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020078 let cwd = fnameescape(getcwd())
Bram Moolenaarec7944a2013-06-12 21:29:15 +020079 try
80 exe cd fnameescape(a:root)
dkearnsd08059a2024-01-02 04:58:57 +110081 for dir in split($PATH, s:path_split)
82 if dir !=# '.' && executable(dir . '/ruby') == 1
83 let exepath = dir . '/ruby'
84 break
85 endif
86 endfor
87 if exists('l:exepath')
88 let path = split(system(exepath . args),',')
89 if v:shell_error
90 let path = []
91 endif
Doug Kearnsda16a1b2023-09-01 18:33:33 +020092 else
dkearnsd08059a2024-01-02 04:58:57 +110093 let path = []
Doug Kearnsda16a1b2023-09-01 18:33:33 +020094 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020095 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020096 return path
97 finally
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020098 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020099 endtry
100endfunction
101
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200102function! s:build_path(path) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200103 let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
Bram Moolenaar46eea442022-03-30 10:51:39 +0100104 if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
105 let path = path . ',.,,'
106 elseif &g:path =~# ',\.,,$'
107 let path = &g:path[0:-4] . path . ',.,,'
108 elseif &g:path =~# ',,$'
109 let path = &g:path[0:-2] . path . ',,'
110 else
111 let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200112 endif
113 return path
114endfunction
115
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200116if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
117 let s:version_file = findfile('.ruby-version', '.;')
118 if !empty(s:version_file) && filereadable(s:version_file)
119 let b:ruby_version = get(readfile(s:version_file, '', 1), '')
120 if !has_key(g:ruby_version_paths, b:ruby_version)
121 let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
122 endif
123 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000124endif
125
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200126if exists("g:ruby_path")
127 let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
128elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
129 let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
130 let s:ruby_path = s:build_path(s:ruby_paths)
131else
132 if !exists('g:ruby_default_path')
133 if has("ruby") && has("win32")
134 ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200135 else
dkearnsd08059a2024-01-02 04:58:57 +1100136 let g:ruby_default_path = s:query_path($HOME)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200137 endif
138 endif
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200139 let s:ruby_paths = g:ruby_default_path
140 let s:ruby_path = s:build_path(s:ruby_paths)
141endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200142
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200143if stridx(&l:path, s:ruby_path) == -1
144 let &l:path = s:ruby_path
145endif
146if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
147 let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
148endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000149
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200150if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Doug Kearns93197fd2024-01-14 20:59:02 +0100151 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n"
152 if has("win32")
153 let b:browsefilter .= "All Files (*.*)\t*\n"
154 else
155 let b:browsefilter .= "All Files (*)\t*\n"
156 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000157endif
158
Bram Moolenaard09091d2019-01-17 16:07:22 +0100159let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
Bram Moolenaar79166c42007-05-10 18:29:51 +0000160 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
161 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
Bram Moolenaard09091d2019-01-17 16:07:22 +0100162
163if get(g:, 'ruby_recommended_style', 1)
164 setlocal shiftwidth=2 softtabstop=2 expandtab
165 let b:undo_ftplugin .= ' | setl sw< sts< et<'
166endif
167
168" To activate, :set ballooneval
169if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
170 setlocal balloonexpr=RubyBalloonexpr()
171 let b:undo_ftplugin .= "| setl bexpr="
172endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000173
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200174function! s:map(mode, flags, map) abort
175 let from = matchstr(a:map, '\S\+')
176 if empty(mapcheck(from, a:mode))
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100177 exe a:mode.'map' '<buffer>' a:flags a:map
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200178 let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
179 endif
180endfunction
181
Bram Moolenaard09091d2019-01-17 16:07:22 +0100182cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200183cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100184let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200185
Bram Moolenaar79166c42007-05-10 18:29:51 +0000186if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200187 nmap <buffer><script> <SID>: :<C-U>
188 nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
Bram Moolenaar46eea442022-03-30 10:51:39 +0100189 cmap <buffer> <SID><cfile> <Plug><cfile>
190 cmap <buffer> <SID><ctag> <Plug><ctag>
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200191
Bram Moolenaard09091d2019-01-17 16:07:22 +0100192 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
193 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
194 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
195 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
196 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
197 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
198 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
199 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000200
Bram Moolenaard09091d2019-01-17 16:07:22 +0100201 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
202 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
203 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
204 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
205 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
206 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
207 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
208 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000209
210 let b:undo_ftplugin = b:undo_ftplugin
211 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
212 \."| 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 +0200213
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200214 if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200215 onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
216 onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
217 xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
218 xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
219 let b:undo_ftplugin = b:undo_ftplugin
220 \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
221 \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
222 endif
223
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200224 if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200225 onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
226 onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
227 xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
228 xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
229 let b:undo_ftplugin = b:undo_ftplugin
230 \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
231 \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
232 endif
233
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200234 call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200235
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200236 cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
Bram Moolenaar46eea442022-03-30 10:51:39 +0100237 call s:map('n', '<script><silent>', '<C-]> <SID>:exe v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
238 call s:map('n', '<script><silent>', 'g<C-]> <SID>:exe "tjump <SID><ctag>"<SID>tagzv<CR>')
239 call s:map('n', '<script><silent>', 'g] <SID>:exe "tselect <SID><ctag>"<SID>tagzv<CR>')
240 call s:map('n', '<script><silent>', '<C-W>] <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
241 call s:map('n', '<script><silent>', '<C-W><C-]> <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
242 call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe "stjump <SID><ctag>"<SID>tagzv<CR>')
243 call s:map('n', '<script><silent>', '<C-W>g] <SID>:exe "stselect <SID><ctag>"<SID>tagzv<CR>')
244 call s:map('n', '<script><silent>', '<C-W>} <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
245 call s:map('n', '<script><silent>', '<C-W>g} <SID>:exe "ptjump <SID><ctag>"<CR>')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200246
Bram Moolenaar46eea442022-03-30 10:51:39 +0100247 call s:map('n', '<script><silent>', 'gf <SID>c:find <SID><cfile><CR>')
248 call s:map('n', '<script><silent>', '<C-W>f <SID>c:sfind <SID><cfile><CR>')
249 call s:map('n', '<script><silent>', '<C-W><C-F> <SID>c:sfind <SID><cfile><CR>')
250 call s:map('n', '<script><silent>', '<C-W>gf <SID>c:tabfind <SID><cfile><CR>')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000251endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000252
253let &cpo = s:cpo_save
254unlet s:cpo_save
255
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000256if exists("g:did_ruby_ftplugin_functions")
257 finish
258endif
259let g:did_ruby_ftplugin_functions = 1
260
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200261function! RubyBalloonexpr() abort
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000262 if !exists('s:ri_found')
263 let s:ri_found = executable('ri')
264 endif
265 if s:ri_found
266 let line = getline(v:beval_lnum)
267 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
268 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
269 let str = b.a
270 let before = strpart(line,0,v:beval_col-strlen(b))
271 let after = strpart(line,v:beval_col+strlen(a))
272 if str =~ '^\.'
273 let str = substitute(str,'^\.','#','g')
274 if before =~ '\]\s*$'
275 let str = 'Array'.str
276 elseif before =~ '}\s*$'
277 " False positives from blocks here
278 let str = 'Hash'.str
279 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
280 let str = 'String'.str
281 elseif before =~ '\$\d\+\.\d\+\s*$'
282 let str = 'Float'.str
283 elseif before =~ '\$\d\+\s*$'
284 let str = 'Integer'.str
285 elseif before =~ '/\s*$'
286 let str = 'Regexp'.str
287 else
288 let str = substitute(str,'^#','.','')
289 endif
290 endif
291 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
292 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
293 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
294 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
295 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
296 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
297 if str !~ '^\w'
298 return ''
299 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200300 silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000301 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000302 return ''
303 endif
304 return res
305 else
306 return ""
307 endif
308endfunction
309
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200310function! s:searchsyn(pattern, syn, flags, mode) abort
311 let cnt = v:count1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200312 norm! m'
313 if a:mode ==# 'v'
314 norm! gv
315 endif
316 let i = 0
Bram Moolenaard09091d2019-01-17 16:07:22 +0100317 call map(a:syn, 'hlID(v:val)')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200318 while i < cnt
319 let i = i + 1
320 let line = line('.')
321 let col = col('.')
322 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaard09091d2019-01-17 16:07:22 +0100323 while pos != 0 && index(a:syn, s:synid()) < 0
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200324 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000325 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200326 if pos == 0
327 call cursor(line,col)
328 return
329 endif
330 endwhile
Bram Moolenaar79166c42007-05-10 18:29:51 +0000331endfunction
332
Bram Moolenaard09091d2019-01-17 16:07:22 +0100333function! s:synid() abort
334 return synID(line('.'),col('.'),0)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200335endfunction
336
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200337function! s:wrap_i(back,forward) abort
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100338 execute 'norm! k'
339 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200340 let line = line('.')
341 execute 'norm '.a:back
342 if line('.') == line - 1
343 return s:wrap_a(a:back,a:forward)
344 endif
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100345 execute 'norm! jV'
346 execute 'norm '.a:forward
347 execute 'norm! k'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200348endfunction
349
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200350function! s:wrap_a(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200351 execute 'norm '.a:forward
352 if line('.') < line('$') && getline(line('.')+1) ==# ''
353 let after = 1
354 endif
355 execute 'norm '.a:back
356 while getline(line('.')-1) =~# '^\s*#' && line('.')
357 -
358 endwhile
359 if exists('after')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100360 execute 'norm! V'
361 execute 'norm '.a:forward
362 execute 'norm! j'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200363 elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100364 execute 'norm! kV'
365 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200366 else
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100367 execute 'norm! V'
368 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200369 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000370endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000371
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200372function! RubyCursorIdentifier() abort
Bram Moolenaar1d689522010-05-28 20:54:39 +0200373 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
374 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
375 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200376 let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200377 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
378 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
379 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
380 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
381 let raw = matchstr(getline('.')[col-1 : ],pattern)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200382 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
Bram Moolenaar1d689522010-05-28 20:54:39 +0200383 return stripped == '' ? expand("<cword>") : stripped
384endfunction
385
Bram Moolenaard09091d2019-01-17 16:07:22 +0100386function! RubyCursorTag() abort
387 return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
388endfunction
389
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200390function! RubyCursorFile() abort
391 let isfname = &isfname
392 try
393 set isfname+=:
394 let cfile = expand('<cfile>')
395 finally
396 let isfname = &isfname
397 endtry
398 let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
399 let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100400 if s:synid() ==# hlID('rubyConstant')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200401 let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100402 let cfile = substitute(cfile,'^::','','')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200403 let cfile = substitute(cfile,'::','/','g')
404 let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
405 let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
406 return tolower(cfile) . '.rb'
407 elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100408 let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
409 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200410 elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
411 let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100412 let cfile = expand('%:p:h') . target
413 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200414 elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100415 let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
416 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200417 elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
418 let cfile = expand('%:p:h') . strpart(cfile, 2)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200419 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200420 return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200421 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200422 let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
423 let cfile = substitute(cfile, cwdpat, '.', '')
424 if fnameescape(cfile) !=# cfile
425 return '+ '.fnameescape(cfile)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200426 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200427 return cfile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200428 endif
429endfunction
430
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000431"
432" Instructions for enabling "matchit" support:
433"
434" 1. Look for the latest "matchit" plugin at
435"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000436" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000437"
438" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
439"
440" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
441"
442" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
443"
444" 4. Ensure this file (ftplugin/ruby.vim) is installed.
445"
446" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000447" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000448"
449" 6. Restart Vim and create the matchit documentation:
450"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000451" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000452"
453" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000454" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000455"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000456" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000457" locations of plugin directories, etc., as there are several options, and it
458" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
459"
460
Bram Moolenaar446cb832008-06-24 21:56:24 +0000461" vim: nowrap sw=2 sts=2 ts=8: