blob: 8c1f47731c351bb0b388722bd3b0ea6d4c63c742 [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>
Bram Moolenaar46eea442022-03-30 10:51:39 +01006" Last Change: 2022 Mar 21
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)
80 let path = split(system(path_check),',')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020081 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020082 return path
83 finally
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020084 exe cd cwd
Bram Moolenaarec7944a2013-06-12 21:29:15 +020085 endtry
86endfunction
87
Bram Moolenaar89bcfda2016-08-30 23:26:57 +020088function! s:build_path(path) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +020089 let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
Bram Moolenaar46eea442022-03-30 10:51:39 +010090 if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
91 let path = path . ',.,,'
92 elseif &g:path =~# ',\.,,$'
93 let path = &g:path[0:-4] . path . ',.,,'
94 elseif &g:path =~# ',,$'
95 let path = &g:path[0:-2] . path . ',,'
96 else
97 let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
Bram Moolenaarec7944a2013-06-12 21:29:15 +020098 endif
99 return path
100endfunction
101
102if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
103 let s:version_file = findfile('.ruby-version', '.;')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200104 if !empty(s:version_file) && filereadable(s:version_file)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200105 let b:ruby_version = get(readfile(s:version_file, '', 1), '')
106 if !has_key(g:ruby_version_paths, b:ruby_version)
107 let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
108 endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000109 endif
110endif
111
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200112if exists("g:ruby_path")
113 let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
114elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
115 let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
116 let s:ruby_path = s:build_path(s:ruby_paths)
117else
118 if !exists('g:ruby_default_path')
119 if has("ruby") && has("win32")
120 ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
Bram Moolenaar4d8f4762021-06-27 15:18:56 +0200121 elseif executable('ruby') && !empty($HOME)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200122 let g:ruby_default_path = s:query_path($HOME)
123 else
124 let g:ruby_default_path = map(split($RUBYLIB,':'), 'v:val ==# "." ? "" : v:val')
125 endif
126 endif
127 let s:ruby_paths = g:ruby_default_path
128 let s:ruby_path = s:build_path(s:ruby_paths)
129endif
130
131if stridx(&l:path, s:ruby_path) == -1
132 let &l:path = s:ruby_path
133endif
134if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
135 let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
136endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000137
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200138if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000139 let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000140 \ "All Files (*.*)\t*.*\n"
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000141endif
142
Bram Moolenaard09091d2019-01-17 16:07:22 +0100143let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
Bram Moolenaar79166c42007-05-10 18:29:51 +0000144 \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
145 \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
Bram Moolenaard09091d2019-01-17 16:07:22 +0100146
147if get(g:, 'ruby_recommended_style', 1)
148 setlocal shiftwidth=2 softtabstop=2 expandtab
149 let b:undo_ftplugin .= ' | setl sw< sts< et<'
150endif
151
152" To activate, :set ballooneval
153if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
154 setlocal balloonexpr=RubyBalloonexpr()
155 let b:undo_ftplugin .= "| setl bexpr="
156endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000157
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200158function! s:map(mode, flags, map) abort
159 let from = matchstr(a:map, '\S\+')
160 if empty(mapcheck(from, a:mode))
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100161 exe a:mode.'map' '<buffer>' a:flags a:map
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200162 let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
163 endif
164endfunction
165
Bram Moolenaard09091d2019-01-17 16:07:22 +0100166cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200167cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100168let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200169
Bram Moolenaar79166c42007-05-10 18:29:51 +0000170if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200171 nmap <buffer><script> <SID>: :<C-U>
172 nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
Bram Moolenaar46eea442022-03-30 10:51:39 +0100173 cmap <buffer> <SID><cfile> <Plug><cfile>
174 cmap <buffer> <SID><ctag> <Plug><ctag>
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200175
Bram Moolenaard09091d2019-01-17 16:07:22 +0100176 nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
177 nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
178 nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
179 nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
180 xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
181 xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
182 xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
183 xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000184
Bram Moolenaard09091d2019-01-17 16:07:22 +0100185 nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
186 nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
187 nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
188 nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
189 xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
190 xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
191 xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
192 xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
Bram Moolenaar79166c42007-05-10 18:29:51 +0000193
194 let b:undo_ftplugin = b:undo_ftplugin
195 \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
196 \."| 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 +0200197
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200198 if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200199 onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
200 onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
201 xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
202 xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
203 let b:undo_ftplugin = b:undo_ftplugin
204 \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
205 \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
206 endif
207
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200208 if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200209 onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
210 onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
211 xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
212 xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
213 let b:undo_ftplugin = b:undo_ftplugin
214 \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
215 \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
216 endif
217
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200218 call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200219
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200220 cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
Bram Moolenaar46eea442022-03-30 10:51:39 +0100221 call s:map('n', '<script><silent>', '<C-]> <SID>:exe v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
222 call s:map('n', '<script><silent>', 'g<C-]> <SID>:exe "tjump <SID><ctag>"<SID>tagzv<CR>')
223 call s:map('n', '<script><silent>', 'g] <SID>:exe "tselect <SID><ctag>"<SID>tagzv<CR>')
224 call s:map('n', '<script><silent>', '<C-W>] <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
225 call s:map('n', '<script><silent>', '<C-W><C-]> <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
226 call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe "stjump <SID><ctag>"<SID>tagzv<CR>')
227 call s:map('n', '<script><silent>', '<C-W>g] <SID>:exe "stselect <SID><ctag>"<SID>tagzv<CR>')
228 call s:map('n', '<script><silent>', '<C-W>} <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
229 call s:map('n', '<script><silent>', '<C-W>g} <SID>:exe "ptjump <SID><ctag>"<CR>')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200230
Bram Moolenaar46eea442022-03-30 10:51:39 +0100231 call s:map('n', '<script><silent>', 'gf <SID>c:find <SID><cfile><CR>')
232 call s:map('n', '<script><silent>', '<C-W>f <SID>c:sfind <SID><cfile><CR>')
233 call s:map('n', '<script><silent>', '<C-W><C-F> <SID>c:sfind <SID><cfile><CR>')
234 call s:map('n', '<script><silent>', '<C-W>gf <SID>c:tabfind <SID><cfile><CR>')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000235endif
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000236
237let &cpo = s:cpo_save
238unlet s:cpo_save
239
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000240if exists("g:did_ruby_ftplugin_functions")
241 finish
242endif
243let g:did_ruby_ftplugin_functions = 1
244
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200245function! RubyBalloonexpr() abort
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000246 if !exists('s:ri_found')
247 let s:ri_found = executable('ri')
248 endif
249 if s:ri_found
250 let line = getline(v:beval_lnum)
251 let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
252 let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
253 let str = b.a
254 let before = strpart(line,0,v:beval_col-strlen(b))
255 let after = strpart(line,v:beval_col+strlen(a))
256 if str =~ '^\.'
257 let str = substitute(str,'^\.','#','g')
258 if before =~ '\]\s*$'
259 let str = 'Array'.str
260 elseif before =~ '}\s*$'
261 " False positives from blocks here
262 let str = 'Hash'.str
263 elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
264 let str = 'String'.str
265 elseif before =~ '\$\d\+\.\d\+\s*$'
266 let str = 'Float'.str
267 elseif before =~ '\$\d\+\s*$'
268 let str = 'Integer'.str
269 elseif before =~ '/\s*$'
270 let str = 'Regexp'.str
271 else
272 let str = substitute(str,'^#','.','')
273 endif
274 endif
275 let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
276 let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
277 let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
278 let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
279 let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
280 let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
281 if str !~ '^\w'
282 return ''
283 endif
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200284 silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
Bram Moolenaar79166c42007-05-10 18:29:51 +0000285 if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000286 return ''
287 endif
288 return res
289 else
290 return ""
291 endif
292endfunction
293
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200294function! s:searchsyn(pattern, syn, flags, mode) abort
295 let cnt = v:count1
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200296 norm! m'
297 if a:mode ==# 'v'
298 norm! gv
299 endif
300 let i = 0
Bram Moolenaard09091d2019-01-17 16:07:22 +0100301 call map(a:syn, 'hlID(v:val)')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200302 while i < cnt
303 let i = i + 1
304 let line = line('.')
305 let col = col('.')
306 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaard09091d2019-01-17 16:07:22 +0100307 while pos != 0 && index(a:syn, s:synid()) < 0
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200308 let pos = search(a:pattern,'W'.a:flags)
Bram Moolenaar79166c42007-05-10 18:29:51 +0000309 endwhile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200310 if pos == 0
311 call cursor(line,col)
312 return
313 endif
314 endwhile
Bram Moolenaar79166c42007-05-10 18:29:51 +0000315endfunction
316
Bram Moolenaard09091d2019-01-17 16:07:22 +0100317function! s:synid() abort
318 return synID(line('.'),col('.'),0)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200319endfunction
320
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200321function! s:wrap_i(back,forward) abort
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100322 execute 'norm! k'
323 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200324 let line = line('.')
325 execute 'norm '.a:back
326 if line('.') == line - 1
327 return s:wrap_a(a:back,a:forward)
328 endif
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100329 execute 'norm! jV'
330 execute 'norm '.a:forward
331 execute 'norm! k'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200332endfunction
333
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200334function! s:wrap_a(back,forward) abort
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200335 execute 'norm '.a:forward
336 if line('.') < line('$') && getline(line('.')+1) ==# ''
337 let after = 1
338 endif
339 execute 'norm '.a:back
340 while getline(line('.')-1) =~# '^\s*#' && line('.')
341 -
342 endwhile
343 if exists('after')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100344 execute 'norm! V'
345 execute 'norm '.a:forward
346 execute 'norm! j'
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200347 elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100348 execute 'norm! kV'
349 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200350 else
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100351 execute 'norm! V'
352 execute 'norm '.a:forward
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200353 endif
Bram Moolenaar79166c42007-05-10 18:29:51 +0000354endfunction
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000355
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200356function! RubyCursorIdentifier() abort
Bram Moolenaar1d689522010-05-28 20:54:39 +0200357 let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
358 let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
359 let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200360 let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
Bram Moolenaar1d689522010-05-28 20:54:39 +0200361 let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
362 let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
363 let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
364 let [lnum, col] = searchpos(pattern,'bcn',line('.'))
365 let raw = matchstr(getline('.')[col-1 : ],pattern)
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200366 let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
Bram Moolenaar1d689522010-05-28 20:54:39 +0200367 return stripped == '' ? expand("<cword>") : stripped
368endfunction
369
Bram Moolenaard09091d2019-01-17 16:07:22 +0100370function! RubyCursorTag() abort
371 return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
372endfunction
373
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200374function! RubyCursorFile() abort
375 let isfname = &isfname
376 try
377 set isfname+=:
378 let cfile = expand('<cfile>')
379 finally
380 let isfname = &isfname
381 endtry
382 let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
383 let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100384 if s:synid() ==# hlID('rubyConstant')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200385 let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
Bram Moolenaard09091d2019-01-17 16:07:22 +0100386 let cfile = substitute(cfile,'^::','','')
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200387 let cfile = substitute(cfile,'::','/','g')
388 let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
389 let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
390 return tolower(cfile) . '.rb'
391 elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100392 let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
393 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200394 elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
395 let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100396 let cfile = expand('%:p:h') . target
397 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200398 elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
Bram Moolenaar2ed639a2019-12-09 23:11:18 +0100399 let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
400 let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200401 elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
402 let cfile = expand('%:p:h') . strpart(cfile, 2)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200403 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200404 return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200405 endif
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200406 let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
407 let cfile = substitute(cfile, cwdpat, '.', '')
408 if fnameescape(cfile) !=# cfile
409 return '+ '.fnameescape(cfile)
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200410 else
Bram Moolenaar89bcfda2016-08-30 23:26:57 +0200411 return cfile
Bram Moolenaarec7944a2013-06-12 21:29:15 +0200412 endif
413endfunction
414
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000415"
416" Instructions for enabling "matchit" support:
417"
418" 1. Look for the latest "matchit" plugin at
419"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000420" http://www.vim.org/scripts/script.php?script_id=39
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000421"
422" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
423"
424" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
425"
426" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
427"
428" 4. Ensure this file (ftplugin/ruby.vim) is installed.
429"
430" 5. Ensure you have this line in your $HOME/.vimrc:
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000431" filetype plugin on
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000432"
433" 6. Restart Vim and create the matchit documentation:
434"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000435" :helptags ~/.vim/doc
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000436"
437" Now you can do ":help matchit", and you should be able to use "%" on Ruby
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000438" keywords. Try ":echo b:match_words" to be sure.
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000439"
Bram Moolenaar756ec0f2007-05-05 17:59:48 +0000440" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
Bram Moolenaar60a795a2005-09-16 21:55:43 +0000441" locations of plugin directories, etc., as there are several options, and it
442" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
443"
444
Bram Moolenaar446cb832008-06-24 21:56:24 +0000445" vim: nowrap sw=2 sts=2 ts=8: