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