blob: a424801cd169137bc766090e22e8f032d4ef9711 [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
Bram Moolenaarec7944a2013-06-12 21:29:15 +020064 let code = "print $:.join %q{,}"
Bram Moolenaard09091d2019-01-17 16:07:22 +010065 if &shell =~# 'sh' && empty(&shellxquote)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020066 let prefix = 'env PATH='.shellescape($PATH).' '
Bram Moolenaar60a795a2005-09-16 21:55:43 +000067 else
Bram Moolenaarec7944a2013-06-12 21:29:15 +020068 let prefix = ''
69 endif
70 if &shellxquote == "'"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020071 let path_check = prefix.'ruby --disable-gems -e "' . code . '"'
Bram Moolenaarec7944a2013-06-12 21:29:15 +020072 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020073 let path_check = prefix."ruby --disable-gems -e '" . code . "'"
Bram Moolenaarec7944a2013-06-12 21:29:15 +020074 endif
75
76 let cd = haslocaldir() ? 'lcd' : 'cd'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020077 let cwd = fnameescape(getcwd())
Bram Moolenaarec7944a2013-06-12 21:29:15 +020078 try
79 exe cd fnameescape(a:root)
Anton Sharonov67c951d2023-09-05 21:03:27 +020080 let s:tmp_cwd = getcwd()
81 if (fnamemodify(exepath('ruby'), ':p:h') ==# cwd
82 \ && (index(split($PATH,has("win32")? ';' : ':'), s:tmp_cwd) == -1 || s:tmp_cwd == '.'))
Doug Kearnsda16a1b2023-09-01 18:33:33 +020083 let path = []
84 else
85 let path = split(system(path_check),',')
86 endif
Anton Sharonov67c951d2023-09-05 21:03:27 +020087 unlet s:tmp_cwd
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020089 return path
90 finally
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020091 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020092 endtry
93endfunction
94
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020095function! s:build_path(path) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +020096 let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
Bram Moolenaar46eea442022-03-30 10:51:39 +010097 if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
98 let path = path . ',.,,'
99 elseif &g:path =~# ',\.,,$'
100 let path = &g:path[0:-4] . path . ',.,,'
101 elseif &g:path =~# ',,$'
102 let path = &g:path[0:-2] . path . ',,'
103 else
104 let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200105 endif
106 return path
107endfunction
108
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200109if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
110 let s:version_file = findfile('.ruby-version', '.;')
111 if !empty(s:version_file) && filereadable(s:version_file)
112 let b:ruby_version = get(readfile(s:version_file, '', 1), '')
113 if !has_key(g:ruby_version_paths, b:ruby_version)
114 let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
115 endif
116 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000117endif
118
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200119if exists("g:ruby_path")
120 let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
121elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
122 let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
123 let s:ruby_path = s:build_path(s:ruby_paths)
124else
125 if !exists('g:ruby_default_path')
126 if has("ruby") && has("win32")
127 ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
128 elseif executable('ruby') && !empty($HOME)
129 let g:ruby_default_path = s:query_path($HOME)
130 else
131 let g:ruby_default_path = map(split($RUBYLIB,':'), 'v:val ==# "." ? "" : v:val')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200132 endif
133 endif
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200134 let s:ruby_paths = g:ruby_default_path
135 let s:ruby_path = s:build_path(s:ruby_paths)
136endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200137
Doug Kearnsda16a1b2023-09-01 18:33:33 +0200138if stridx(&l:path, s:ruby_path) == -1
139 let &l:path = s:ruby_path
140endif
141if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
142 let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
143endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000144
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200145if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000146 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000147 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000148endif
149
Bram Moolenaard09091d2019-01-17 16:07:22 +0100150let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
Bram Moolenaar79166c42007-05-10 18:29:51 +0000151 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
152 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
Bram Moolenaard09091d2019-01-17 16:07:22 +0100153
154if get(g:, 'ruby_recommended_style', 1)
155 setlocal shiftwidth=2 softtabstop=2 expandtab
156 let b:undo_ftplugin .= ' | setl sw< sts< et<'
157endif
158
159" To activate, :set ballooneval
160if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
161 setlocal balloonexpr=RubyBalloonexpr()
162 let b:undo_ftplugin .= "| setl bexpr="
163endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000164
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200165function! s:map(mode, flags, map) abort
166 let from = matchstr(a:map, '\S\+')
167 if empty(mapcheck(from, a:mode))
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100168 exe a:mode.'map' '<buffer>' a:flags a:map
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200169 let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
170 endif
171endfunction
172
Bram Moolenaard09091d2019-01-17 16:07:22 +0100173cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200174cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100175let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200176
Bram Moolenaar79166c42007-05-10 18:29:51 +0000177if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200178 nmap <buffer><script> <SID>: :<C-U>
179 nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
Bram Moolenaar46eea442022-03-30 10:51:39 +0100180 cmap <buffer> <SID><cfile> <Plug><cfile>
181 cmap <buffer> <SID><ctag> <Plug><ctag>
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200182
Bram Moolenaard09091d2019-01-17 16:07:22 +0100183 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
184 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
185 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
186 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
187 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
188 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
189 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
190 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000191
Bram Moolenaard09091d2019-01-17 16:07:22 +0100192 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
193 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
194 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
195 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
196 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
197 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
198 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
199 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000200
201 let b:undo_ftplugin = b:undo_ftplugin
202 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
203 \."| 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 +0200204
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200205 if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200206 onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
207 onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
208 xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
209 xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
210 let b:undo_ftplugin = b:undo_ftplugin
211 \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
212 \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
213 endif
214
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200215 if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200216 onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
217 onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
218 xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
219 xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
220 let b:undo_ftplugin = b:undo_ftplugin
221 \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
222 \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
223 endif
224
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200225 call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200226
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200227 cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
Bram Moolenaar46eea442022-03-30 10:51:39 +0100228 call s:map('n', '<script><silent>', '<C-]> <SID>:exe v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
229 call s:map('n', '<script><silent>', 'g<C-]> <SID>:exe "tjump <SID><ctag>"<SID>tagzv<CR>')
230 call s:map('n', '<script><silent>', 'g] <SID>:exe "tselect <SID><ctag>"<SID>tagzv<CR>')
231 call s:map('n', '<script><silent>', '<C-W>] <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
232 call s:map('n', '<script><silent>', '<C-W><C-]> <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
233 call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe "stjump <SID><ctag>"<SID>tagzv<CR>')
234 call s:map('n', '<script><silent>', '<C-W>g] <SID>:exe "stselect <SID><ctag>"<SID>tagzv<CR>')
235 call s:map('n', '<script><silent>', '<C-W>} <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
236 call s:map('n', '<script><silent>', '<C-W>g} <SID>:exe "ptjump <SID><ctag>"<CR>')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200237
Bram Moolenaar46eea442022-03-30 10:51:39 +0100238 call s:map('n', '<script><silent>', 'gf <SID>c:find <SID><cfile><CR>')
239 call s:map('n', '<script><silent>', '<C-W>f <SID>c:sfind <SID><cfile><CR>')
240 call s:map('n', '<script><silent>', '<C-W><C-F> <SID>c:sfind <SID><cfile><CR>')
241 call s:map('n', '<script><silent>', '<C-W>gf <SID>c:tabfind <SID><cfile><CR>')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000242endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000243
244let &cpo = s:cpo_save
245unlet s:cpo_save
246
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000247if exists("g:did_ruby_ftplugin_functions")
248 finish
249endif
250let g:did_ruby_ftplugin_functions = 1
251
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200252function! RubyBalloonexpr() abort
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000253 if !exists('s:ri_found')
254 let s:ri_found = executable('ri')
255 endif
256 if s:ri_found
257 let line = getline(v:beval_lnum)
258 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
259 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
260 let str = b.a
261 let before = strpart(line,0,v:beval_col-strlen(b))
262 let after = strpart(line,v:beval_col+strlen(a))
263 if str =~ '^\.'
264 let str = substitute(str,'^\.','#','g')
265 if before =~ '\]\s*$'
266 let str = 'Array'.str
267 elseif before =~ '}\s*$'
268 " False positives from blocks here
269 let str = 'Hash'.str
270 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
271 let str = 'String'.str
272 elseif before =~ '\$\d\+\.\d\+\s*$'
273 let str = 'Float'.str
274 elseif before =~ '\$\d\+\s*$'
275 let str = 'Integer'.str
276 elseif before =~ '/\s*$'
277 let str = 'Regexp'.str
278 else
279 let str = substitute(str,'^#','.','')
280 endif
281 endif
282 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
283 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
284 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
285 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
286 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
287 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
288 if str !~ '^\w'
289 return ''
290 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200291 silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000292 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000293 return ''
294 endif
295 return res
296 else
297 return ""
298 endif
299endfunction
300
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200301function! s:searchsyn(pattern, syn, flags, mode) abort
302 let cnt = v:count1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200303 norm! m'
304 if a:mode ==# 'v'
305 norm! gv
306 endif
307 let i = 0
Bram Moolenaard09091d2019-01-17 16:07:22 +0100308 call map(a:syn, 'hlID(v:val)')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200309 while i < cnt
310 let i = i + 1
311 let line = line('.')
312 let col = col('.')
313 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaard09091d2019-01-17 16:07:22 +0100314 while pos != 0 && index(a:syn, s:synid()) < 0
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200315 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000316 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200317 if pos == 0
318 call cursor(line,col)
319 return
320 endif
321 endwhile
Bram Moolenaar79166c42007-05-10 18:29:51 +0000322endfunction
323
Bram Moolenaard09091d2019-01-17 16:07:22 +0100324function! s:synid() abort
325 return synID(line('.'),col('.'),0)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200326endfunction
327
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200328function! s:wrap_i(back,forward) abort
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100329 execute 'norm! k'
330 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200331 let line = line('.')
332 execute 'norm '.a:back
333 if line('.') == line - 1
334 return s:wrap_a(a:back,a:forward)
335 endif
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100336 execute 'norm! jV'
337 execute 'norm '.a:forward
338 execute 'norm! k'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200339endfunction
340
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200341function! s:wrap_a(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200342 execute 'norm '.a:forward
343 if line('.') < line('$') && getline(line('.')+1) ==# ''
344 let after = 1
345 endif
346 execute 'norm '.a:back
347 while getline(line('.')-1) =~# '^\s*#' && line('.')
348 -
349 endwhile
350 if exists('after')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100351 execute 'norm! V'
352 execute 'norm '.a:forward
353 execute 'norm! j'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200354 elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100355 execute 'norm! kV'
356 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200357 else
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100358 execute 'norm! V'
359 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200360 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000361endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000362
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200363function! RubyCursorIdentifier() abort
Bram Moolenaar1d689522010-05-28 20:54:39 +0200364 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
365 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
366 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200367 let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200368 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
369 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
370 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
371 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
372 let raw = matchstr(getline('.')[col-1 : ],pattern)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200373 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
Bram Moolenaar1d689522010-05-28 20:54:39 +0200374 return stripped == '' ? expand("<cword>") : stripped
375endfunction
376
Bram Moolenaard09091d2019-01-17 16:07:22 +0100377function! RubyCursorTag() abort
378 return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
379endfunction
380
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200381function! RubyCursorFile() abort
382 let isfname = &isfname
383 try
384 set isfname+=:
385 let cfile = expand('<cfile>')
386 finally
387 let isfname = &isfname
388 endtry
389 let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
390 let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100391 if s:synid() ==# hlID('rubyConstant')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200392 let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100393 let cfile = substitute(cfile,'^::','','')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200394 let cfile = substitute(cfile,'::','/','g')
395 let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
396 let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
397 return tolower(cfile) . '.rb'
398 elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100399 let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
400 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200401 elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
402 let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100403 let cfile = expand('%:p:h') . target
404 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200405 elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100406 let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
407 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200408 elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
409 let cfile = expand('%:p:h') . strpart(cfile, 2)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200410 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200411 return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200412 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200413 let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
414 let cfile = substitute(cfile, cwdpat, '.', '')
415 if fnameescape(cfile) !=# cfile
416 return '+ '.fnameescape(cfile)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200417 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200418 return cfile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200419 endif
420endfunction
421
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000422"
423" Instructions for enabling "matchit" support:
424"
425" 1. Look for the latest "matchit" plugin at
426"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000427" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000428"
429" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
430"
431" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
432"
433" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
434"
435" 4. Ensure this file (ftplugin/ruby.vim) is installed.
436"
437" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000438" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000439"
440" 6. Restart Vim and create the matchit documentation:
441"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000442" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000443"
444" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000445" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000446"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000447" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000448" locations of plugin directories, etc., as there are several options, and it
449" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
450"
451
Bram Moolenaar446cb832008-06-24 21:56:24 +0000452" vim: nowrap sw=2 sts=2 ts=8: