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