ruby.vim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. " Vim filetype plugin
  2. " Language: Ruby
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " URL: https://github.com/vim-ruby/vim-ruby
  5. " Last Change: 2023 Dec 31
  6. " 2024 Jan 14 by Vim Project (browsefilter)
  7. if (exists("b:did_ftplugin"))
  8. finish
  9. endif
  10. let b:did_ftplugin = 1
  11. let s:cpo_save = &cpo
  12. set cpo&vim
  13. if has("gui_running") && !has("gui_win32")
  14. setlocal keywordprg=ri\ -T\ -f\ bs
  15. else
  16. setlocal keywordprg=ri
  17. endif
  18. " Matchit support
  19. if exists("loaded_matchit") && !exists("b:match_words")
  20. let b:match_ignorecase = 0
  21. let b:match_words =
  22. \ '{\|\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|=\@<!begin\)\>=\@!' .
  23. \ ':' .
  24. \ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
  25. \ ':' .
  26. \ '}\|\%(^\|[^.\:@$=]\)\@<=\<end\:\@!\>' .
  27. \ ',^=begin\>:^=end\>,' .
  28. \ ',\[:\],(:)'
  29. let b:match_skip =
  30. \ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
  31. \ "\\<ruby\\%(String\\|.\+Delimiter\\|Character\\|.\+Escape\\|" .
  32. \ "Regexp\\|Interpolation\\|Comment\\|Documentation\\|" .
  33. \ "ConditionalModifier\\|RepeatModifier\\|RescueModifier\\|OptionalDo\\|" .
  34. \ "MethodName\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
  35. \ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
  36. endif
  37. setlocal formatoptions-=t formatoptions+=croql
  38. setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
  39. setlocal suffixesadd=.rb
  40. if exists("&ofu") && has("ruby")
  41. setlocal omnifunc=rubycomplete#Complete
  42. endif
  43. " TODO:
  44. "setlocal define=^\\s*def
  45. setlocal comments=b:#
  46. setlocal commentstring=#\ %s
  47. if !exists('g:ruby_version_paths')
  48. let g:ruby_version_paths = {}
  49. endif
  50. let s:path_split = has('win32') ? ';' : ':'
  51. function! s:query_path(root) abort
  52. " Disabled by default for security reasons.
  53. if !get(g:, 'ruby_exec', get(g:, 'plugin_exec', 0)) || empty(a:root)
  54. return map(split($RUBYLIB, s:path_split), 'v:val ==# "." ? "" : v:val')
  55. endif
  56. let code = "print $:.join %q{,}"
  57. if &shellxquote == "'"
  58. let args = ' --disable-gems -e "' . code . '"'
  59. else
  60. let args = " --disable-gems -e '" . code . "'"
  61. endif
  62. let cd = haslocaldir() ? 'lcd' : exists(':tcd') && haslocaldir(-1) ? 'tcd' : 'cd'
  63. let cwd = fnameescape(getcwd())
  64. try
  65. exe cd fnameescape(a:root)
  66. for dir in split($PATH, s:path_split)
  67. if dir !=# '.' && executable(dir . '/ruby') == 1
  68. let exepath = dir . '/ruby'
  69. break
  70. endif
  71. endfor
  72. if exists('l:exepath')
  73. let path = split(system(exepath . args),',')
  74. if v:shell_error
  75. let path = []
  76. endif
  77. else
  78. let path = []
  79. endif
  80. exe cd cwd
  81. return path
  82. finally
  83. exe cd cwd
  84. endtry
  85. endfunction
  86. function! s:build_path(path) abort
  87. let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
  88. if &g:path =~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
  89. let path = path . ',.,,'
  90. elseif &g:path =~# ',\.,,$'
  91. let path = &g:path[0:-4] . path . ',.,,'
  92. elseif &g:path =~# ',,$'
  93. let path = &g:path[0:-2] . path . ',,'
  94. else
  95. let path = substitute(&g:path, '[^,]\zs$', ',', '') . path
  96. endif
  97. return path
  98. endfunction
  99. if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
  100. let s:version_file = findfile('.ruby-version', '.;')
  101. if !empty(s:version_file) && filereadable(s:version_file)
  102. let b:ruby_version = get(readfile(s:version_file, '', 1), '')
  103. if !has_key(g:ruby_version_paths, b:ruby_version)
  104. let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
  105. endif
  106. endif
  107. endif
  108. if exists("g:ruby_path")
  109. let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
  110. elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
  111. let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
  112. let s:ruby_path = s:build_path(s:ruby_paths)
  113. else
  114. if !exists('g:ruby_default_path')
  115. if has("ruby") && has("win32")
  116. ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
  117. else
  118. let g:ruby_default_path = s:query_path($HOME)
  119. endif
  120. endif
  121. let s:ruby_paths = g:ruby_default_path
  122. let s:ruby_path = s:build_path(s:ruby_paths)
  123. endif
  124. if stridx(&l:path, s:ruby_path) == -1
  125. let &l:path = s:ruby_path
  126. endif
  127. if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
  128. let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
  129. endif
  130. if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
  131. let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n"
  132. if has("win32")
  133. let b:browsefilter .= "All Files (*.*)\t*\n"
  134. else
  135. let b:browsefilter .= "All Files (*)\t*\n"
  136. endif
  137. endif
  138. let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
  139. \."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
  140. \."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
  141. if get(g:, 'ruby_recommended_style', 1)
  142. setlocal shiftwidth=2 softtabstop=2 expandtab
  143. let b:undo_ftplugin .= ' | setl sw< sts< et<'
  144. endif
  145. " To activate, :set ballooneval
  146. if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
  147. setlocal balloonexpr=RubyBalloonexpr()
  148. let b:undo_ftplugin .= "| setl bexpr="
  149. endif
  150. function! s:map(mode, flags, map) abort
  151. let from = matchstr(a:map, '\S\+')
  152. if empty(mapcheck(from, a:mode))
  153. exe a:mode.'map' '<buffer>' a:flags a:map
  154. let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
  155. endif
  156. endfunction
  157. cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
  158. cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
  159. let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
  160. if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
  161. nmap <buffer><script> <SID>: :<C-U>
  162. nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
  163. cmap <buffer> <SID><cfile> <Plug><cfile>
  164. cmap <buffer> <SID><ctag> <Plug><ctag>
  165. nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
  166. nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
  167. nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
  168. nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
  169. xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
  170. xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
  171. xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
  172. xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
  173. nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
  174. nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
  175. nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
  176. nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
  177. xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
  178. xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
  179. xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
  180. xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
  181. let b:undo_ftplugin = b:undo_ftplugin
  182. \."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
  183. \."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
  184. if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
  185. onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
  186. onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
  187. xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
  188. xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
  189. let b:undo_ftplugin = b:undo_ftplugin
  190. \."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
  191. \."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
  192. endif
  193. if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
  194. onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
  195. onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
  196. xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
  197. xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
  198. let b:undo_ftplugin = b:undo_ftplugin
  199. \."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
  200. \."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
  201. endif
  202. call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
  203. cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
  204. call s:map('n', '<script><silent>', '<C-]> <SID>:exe v:count1."tag <SID><ctag>"<SID>tagzv<CR>')
  205. call s:map('n', '<script><silent>', 'g<C-]> <SID>:exe "tjump <SID><ctag>"<SID>tagzv<CR>')
  206. call s:map('n', '<script><silent>', 'g] <SID>:exe "tselect <SID><ctag>"<SID>tagzv<CR>')
  207. call s:map('n', '<script><silent>', '<C-W>] <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
  208. call s:map('n', '<script><silent>', '<C-W><C-]> <SID>:exe v:count1."stag <SID><ctag>"<SID>tagzv<CR>')
  209. call s:map('n', '<script><silent>', '<C-W>g<C-]> <SID>:exe "stjump <SID><ctag>"<SID>tagzv<CR>')
  210. call s:map('n', '<script><silent>', '<C-W>g] <SID>:exe "stselect <SID><ctag>"<SID>tagzv<CR>')
  211. call s:map('n', '<script><silent>', '<C-W>} <SID>:exe v:count1."ptag <SID><ctag>"<CR>')
  212. call s:map('n', '<script><silent>', '<C-W>g} <SID>:exe "ptjump <SID><ctag>"<CR>')
  213. call s:map('n', '<script><silent>', 'gf <SID>c:find <SID><cfile><CR>')
  214. call s:map('n', '<script><silent>', '<C-W>f <SID>c:sfind <SID><cfile><CR>')
  215. call s:map('n', '<script><silent>', '<C-W><C-F> <SID>c:sfind <SID><cfile><CR>')
  216. call s:map('n', '<script><silent>', '<C-W>gf <SID>c:tabfind <SID><cfile><CR>')
  217. endif
  218. let &cpo = s:cpo_save
  219. unlet s:cpo_save
  220. if exists("g:did_ruby_ftplugin_functions")
  221. finish
  222. endif
  223. let g:did_ruby_ftplugin_functions = 1
  224. function! RubyBalloonexpr() abort
  225. if !exists('s:ri_found')
  226. let s:ri_found = executable('ri')
  227. endif
  228. if s:ri_found
  229. let line = getline(v:beval_lnum)
  230. let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
  231. let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
  232. let str = b.a
  233. let before = strpart(line,0,v:beval_col-strlen(b))
  234. let after = strpart(line,v:beval_col+strlen(a))
  235. if str =~ '^\.'
  236. let str = substitute(str,'^\.','#','g')
  237. if before =~ '\]\s*$'
  238. let str = 'Array'.str
  239. elseif before =~ '}\s*$'
  240. " False positives from blocks here
  241. let str = 'Hash'.str
  242. elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
  243. let str = 'String'.str
  244. elseif before =~ '\$\d\+\.\d\+\s*$'
  245. let str = 'Float'.str
  246. elseif before =~ '\$\d\+\s*$'
  247. let str = 'Integer'.str
  248. elseif before =~ '/\s*$'
  249. let str = 'Regexp'.str
  250. else
  251. let str = substitute(str,'^#','.','')
  252. endif
  253. endif
  254. let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
  255. let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
  256. let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
  257. let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
  258. let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
  259. let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
  260. if str !~ '^\w'
  261. return ''
  262. endif
  263. silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
  264. if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
  265. return ''
  266. endif
  267. return res
  268. else
  269. return ""
  270. endif
  271. endfunction
  272. function! s:searchsyn(pattern, syn, flags, mode) abort
  273. let cnt = v:count1
  274. norm! m'
  275. if a:mode ==# 'v'
  276. norm! gv
  277. endif
  278. let i = 0
  279. call map(a:syn, 'hlID(v:val)')
  280. while i < cnt
  281. let i = i + 1
  282. let line = line('.')
  283. let col = col('.')
  284. let pos = search(a:pattern,'W'.a:flags)
  285. while pos != 0 && index(a:syn, s:synid()) < 0
  286. let pos = search(a:pattern,'W'.a:flags)
  287. endwhile
  288. if pos == 0
  289. call cursor(line,col)
  290. return
  291. endif
  292. endwhile
  293. endfunction
  294. function! s:synid() abort
  295. return synID(line('.'),col('.'),0)
  296. endfunction
  297. function! s:wrap_i(back,forward) abort
  298. execute 'norm! k'
  299. execute 'norm '.a:forward
  300. let line = line('.')
  301. execute 'norm '.a:back
  302. if line('.') == line - 1
  303. return s:wrap_a(a:back,a:forward)
  304. endif
  305. execute 'norm! jV'
  306. execute 'norm '.a:forward
  307. execute 'norm! k'
  308. endfunction
  309. function! s:wrap_a(back,forward) abort
  310. execute 'norm '.a:forward
  311. if line('.') < line('$') && getline(line('.')+1) ==# ''
  312. let after = 1
  313. endif
  314. execute 'norm '.a:back
  315. while getline(line('.')-1) =~# '^\s*#' && line('.')
  316. -
  317. endwhile
  318. if exists('after')
  319. execute 'norm! V'
  320. execute 'norm '.a:forward
  321. execute 'norm! j'
  322. elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
  323. execute 'norm! kV'
  324. execute 'norm '.a:forward
  325. else
  326. execute 'norm! V'
  327. execute 'norm '.a:forward
  328. endif
  329. endfunction
  330. function! RubyCursorIdentifier() abort
  331. let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
  332. let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
  333. let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
  334. let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
  335. let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
  336. let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
  337. let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
  338. let [lnum, col] = searchpos(pattern,'bcn',line('.'))
  339. let raw = matchstr(getline('.')[col-1 : ],pattern)
  340. let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
  341. return stripped == '' ? expand("<cword>") : stripped
  342. endfunction
  343. function! RubyCursorTag() abort
  344. return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
  345. endfunction
  346. function! RubyCursorFile() abort
  347. let isfname = &isfname
  348. try
  349. set isfname+=:
  350. let cfile = expand('<cfile>')
  351. finally
  352. let isfname = &isfname
  353. endtry
  354. let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
  355. let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
  356. if s:synid() ==# hlID('rubyConstant')
  357. let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
  358. let cfile = substitute(cfile,'^::','','')
  359. let cfile = substitute(cfile,'::','/','g')
  360. let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
  361. let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
  362. return tolower(cfile) . '.rb'
  363. elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
  364. let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
  365. let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
  366. elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
  367. let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
  368. let cfile = expand('%:p:h') . target
  369. let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
  370. elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
  371. let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
  372. let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
  373. elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
  374. let cfile = expand('%:p:h') . strpart(cfile, 2)
  375. else
  376. return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
  377. endif
  378. let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
  379. let cfile = substitute(cfile, cwdpat, '.', '')
  380. if fnameescape(cfile) !=# cfile
  381. return '+ '.fnameescape(cfile)
  382. else
  383. return cfile
  384. endif
  385. endfunction
  386. "
  387. " Instructions for enabling "matchit" support:
  388. "
  389. " 1. Look for the latest "matchit" plugin at
  390. "
  391. " http://www.vim.org/scripts/script.php?script_id=39
  392. "
  393. " It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
  394. "
  395. " 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
  396. "
  397. " 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
  398. "
  399. " 4. Ensure this file (ftplugin/ruby.vim) is installed.
  400. "
  401. " 5. Ensure you have this line in your $HOME/.vimrc:
  402. " filetype plugin on
  403. "
  404. " 6. Restart Vim and create the matchit documentation:
  405. "
  406. " :helptags ~/.vim/doc
  407. "
  408. " Now you can do ":help matchit", and you should be able to use "%" on Ruby
  409. " keywords. Try ":echo b:match_words" to be sure.
  410. "
  411. " Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
  412. " locations of plugin directories, etc., as there are several options, and it
  413. " differs on Windows. Email gsinclair@soyabean.com.au if you need help.
  414. "
  415. " vim: nowrap sw=2 sts=2 ts=8: