summaryrefslogtreecommitdiffstats
path: root/runtime/ftplugin/ruby.vim
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/ftplugin/ruby.vim')
-rw-r--r--runtime/ftplugin/ruby.vim153
1 files changed, 89 insertions, 64 deletions
diff --git a/runtime/ftplugin/ruby.vim b/runtime/ftplugin/ruby.vim
index 9630a940ab..84fb9930a4 100644
--- a/runtime/ftplugin/ruby.vim
+++ b/runtime/ftplugin/ruby.vim
@@ -28,12 +28,13 @@ if exists("loaded_matchit") && !exists("b:match_words")
\ ':' .
\ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
\ ':' .
- \ '\<end\>' .
+ \ '\%(^\|[^.\:@$]\)\@<=\<end\:\@!\>' .
\ ',{:},\[:\],(:)'
let b:match_skip =
\ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
\ "\\<ruby\\%(String\\|StringDelimiter\\|ASCIICode\\|Escape\\|" .
+ \ "Regexp\\|RegexpDelimiter\\|" .
\ "Interpolation\\|NoInterpolation\\|Comment\\|Documentation\\|" .
\ "ConditionalModifier\\|RepeatModifier\\|OptionalDo\\|" .
\ "Function\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
@@ -43,7 +44,7 @@ endif
setlocal formatoptions-=t formatoptions+=croql
setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
-setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.rb','')
+setlocal includeexpr=substitute(substitute(v:fname,'::','/','g'),'\%(\.rb\)\=$','.rb','')
setlocal suffixesadd=.rb
if exists("&ofu") && has("ruby")
@@ -66,32 +67,32 @@ if !exists('g:ruby_version_paths')
let g:ruby_version_paths = {}
endif
-function! s:query_path(root)
+function! s:query_path(root) abort
let code = "print $:.join %q{,}"
- if &shell =~# 'sh' && $PATH !~# '\s'
- let prefix = 'env PATH='.$PATH.' '
+ if &shell =~# 'sh'
+ let prefix = 'env PATH='.shellescape($PATH).' '
else
let prefix = ''
endif
if &shellxquote == "'"
- let path_check = prefix.'ruby -e "' . code . '"'
+ let path_check = prefix.'ruby --disable-gems -e "' . code . '"'
else
- let path_check = prefix."ruby -e '" . code . "'"
+ let path_check = prefix."ruby --disable-gems -e '" . code . "'"
endif
let cd = haslocaldir() ? 'lcd' : 'cd'
- let cwd = getcwd()
+ let cwd = fnameescape(getcwd())
try
exe cd fnameescape(a:root)
let path = split(system(path_check),',')
- exe cd fnameescape(cwd)
+ exe cd cwd
return path
finally
- exe cd fnameescape(cwd)
+ exe cd cwd
endtry
endfunction
-function! s:build_path(path)
+function! s:build_path(path) abort
let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
if &g:path !~# '\v^\.%(,/%(usr|emx)/include)=,,$'
let path = substitute(&g:path,',,$',',','') . ',' . path
@@ -101,7 +102,7 @@ endfunction
if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
let s:version_file = findfile('.ruby-version', '.;')
- if !empty(s:version_file)
+ if !empty(s:version_file) && filereadable(s:version_file)
let b:ruby_version = get(readfile(s:version_file, '', 1), '')
if !has_key(g:ruby_version_paths, b:ruby_version)
let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
@@ -135,7 +136,7 @@ if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val.
let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
endif
-if has("gui_win32") && !exists("b:browsefilter")
+if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
\ "All Files (*.*)\t*.*\n"
endif
@@ -145,7 +146,22 @@ let b:undo_ftplugin = "setl fo< inc< inex< sua< def< com< cms< path< tags< kp<"
\."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
\."| if has('balloon_eval') && exists('+bexpr') | setl bexpr< | endif"
+function! s:map(mode, flags, map) abort
+ let from = matchstr(a:map, '\S\+')
+ if empty(mapcheck(from, a:mode))
+ exe a:mode.'map' '<buffer>'.(a:0 ? a:1 : '') a:map
+ let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
+ endif
+endfunction
+
+cmap <buffer><script><expr> <Plug><cword> substitute(RubyCursorIdentifier(),'^$',"\022\027",'')
+cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
+let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><cword>| sil! cunmap <buffer> <Plug><cfile>"
+
if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
+ nmap <buffer><script> <SID>: :<C-U>
+ nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
+
nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','b','n')<CR>
nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>','rubyDefine','','n')<CR>
nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>','rubyDefine','b','n')<CR>
@@ -168,7 +184,7 @@ if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
\."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
\."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
- if maparg('im','n') == ''
+ if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
@@ -178,7 +194,7 @@ if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
\."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
endif
- if maparg('iM','n') == ''
+ if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
@@ -188,33 +204,24 @@ if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
\."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
endif
- if maparg("\<C-]>",'n') == ''
- nnoremap <silent> <buffer> <C-]> :<C-U>exe v:count1."tag <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> g<C-]> :<C-U>exe "tjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> g] :<C-U>exe "tselect <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W>] :<C-U>exe v:count1."stag <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W><C-]> :<C-U>exe v:count1."stag <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W>g<C-]> :<C-U>exe "stjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W>g] :<C-U>exe "stselect <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W>} :<C-U>exe "ptag <C-R>=RubyCursorIdentifier()<CR>"<CR>
- nnoremap <silent> <buffer> <C-W>g} :<C-U>exe "ptjump <C-R>=RubyCursorIdentifier()<CR>"<CR>
- let b:undo_ftplugin = b:undo_ftplugin
- \."| sil! exe 'nunmap <buffer> <C-]>'| sil! exe 'nunmap <buffer> g<C-]>'| sil! exe 'nunmap <buffer> g]'"
- \."| sil! exe 'nunmap <buffer> <C-W>]'| sil! exe 'nunmap <buffer> <C-W><C-]>'"
- \."| sil! exe 'nunmap <buffer> <C-W>g<C-]>'| sil! exe 'nunmap <buffer> <C-W>g]'"
- \."| sil! exe 'nunmap <buffer> <C-W>}'| sil! exe 'nunmap <buffer> <C-W>g}'"
- endif
-
- if maparg("gf",'n') == ''
- " By using findfile() rather than gf's normal behavior, we prevent
- " erroneously editing a directory.
- nnoremap <silent> <buffer> gf :<C-U>exe <SID>gf(v:count1,"gf",'edit')<CR>
- nnoremap <silent> <buffer> <C-W>f :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>f",'split')<CR>
- nnoremap <silent> <buffer> <C-W><C-F> :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>\<Lt>C-F>",'split')<CR>
- nnoremap <silent> <buffer> <C-W>gf :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>gf",'tabedit')<CR>
- let b:undo_ftplugin = b:undo_ftplugin
- \."| 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'"
- endif
+ call s:map('c', '', '<C-R><C-W> <Plug><cword>')
+ call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
+
+ cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
+ call s:map('n', '<silent>', '<C-]> <SID>:exe v:count1."tag <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', 'g<C-]> <SID>:exe "tjump <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', 'g] <SID>:exe "tselect <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', '<C-W>] <SID>:exe v:count1."stag <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', '<C-W><C-]> <SID>:exe v:count1."stag <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', '<C-W>g<C-]> <SID>:exe "stjump <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', '<C-W>g] <SID>:exe "stselect <Plug><cword>"<SID>tagzv<CR>')
+ call s:map('n', '<silent>', '<C-W>} <SID>:exe v:count1."ptag <Plug><cword>"<CR>')
+ call s:map('n', '<silent>', '<C-W>g} <SID>:exe "ptjump <Plug><cword>"<CR>')
+
+ call s:map('n', '<silent>', 'gf <SID>c:find <Plug><cfile><CR>')
+ call s:map('n', '<silent>', '<C-W>f <SID>c:sfind <Plug><cfile><CR>')
+ call s:map('n', '<silent>', '<C-W><C-F> <SID>c:sfind <Plug><cfile><CR>')
+ call s:map('n', '<silent>', '<C-W>gf <SID>c:tabfind <Plug><cfile><CR>')
endif
let &cpo = s:cpo_save
@@ -225,7 +232,7 @@ if exists("g:did_ruby_ftplugin_functions")
endif
let g:did_ruby_ftplugin_functions = 1
-function! RubyBalloonexpr()
+function! RubyBalloonexpr() abort
if !exists('s:ri_found')
let s:ri_found = executable('ri')
endif
@@ -274,13 +281,13 @@ function! RubyBalloonexpr()
endif
endfunction
-function! s:searchsyn(pattern,syn,flags,mode)
+function! s:searchsyn(pattern, syn, flags, mode) abort
+ let cnt = v:count1
norm! m'
if a:mode ==# 'v'
norm! gv
endif
let i = 0
- let cnt = v:count ? v:count : 1
while i < cnt
let i = i + 1
let line = line('.')
@@ -296,11 +303,11 @@ function! s:searchsyn(pattern,syn,flags,mode)
endwhile
endfunction
-function! s:synname()
+function! s:synname() abort
return synIDattr(synID(line('.'),col('.'),0),'name')
endfunction
-function! s:wrap_i(back,forward)
+function! s:wrap_i(back,forward) abort
execute 'norm k'.a:forward
let line = line('.')
execute 'norm '.a:back
@@ -310,7 +317,7 @@ function! s:wrap_i(back,forward)
execute 'norm jV'.a:forward.'k'
endfunction
-function! s:wrap_a(back,forward)
+function! s:wrap_a(back,forward) abort
execute 'norm '.a:forward
if line('.') < line('$') && getline(line('.')+1) ==# ''
let after = 1
@@ -328,37 +335,55 @@ function! s:wrap_a(back,forward)
endif
endfunction
-function! RubyCursorIdentifier()
+function! RubyCursorIdentifier() abort
let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
- let method = '\%(\<[_a-zA-Z]\w*\>\%([?!]\|\s*=>\@!\)\=\)'
+ let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
let [lnum, col] = searchpos(pattern,'bcn',line('.'))
let raw = matchstr(getline('.')[col-1 : ],pattern)
- let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*:\=','','')
+ let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
return stripped == '' ? expand("<cword>") : stripped
endfunction
-function! s:gf(count,map,edit) abort
- if getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
- let target = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
- return a:edit.' %:h/'.target.'.rb'
- elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
- let target = matchstr(getline('.'),'\(["'']\)\.\./\zs.\{-\}\ze\1')
- return a:edit.' %:h/'.target.'.rb'
+function! RubyCursorFile() abort
+ let isfname = &isfname
+ try
+ set isfname+=:
+ let cfile = expand('<cfile>')
+ finally
+ let isfname = &isfname
+ endtry
+ let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
+ let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
+ let ext = getline('.') =~# '^\s*\%(require\%(_relative\)\=\|autoload\)\>' && cfile !~# '\.rb$' ? '.rb' : ''
+ if s:synname() ==# 'rubyConstant'
+ let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
+ let cfile = substitute(cfile,'::','/','g')
+ let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
+ let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
+ return tolower(cfile) . '.rb'
+ elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
+ let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1') . ext
+ elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
+ let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
+ let cfile = expand('%:p:h') . target . ext
elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
- let target = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
+ let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1') . ext
+ elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
+ let cfile = expand('%:p:h') . strpart(cfile, 2)
else
- let target = expand('<cfile>')
+ return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
endif
- let found = findfile(target, &path, a:count)
- if found ==# ''
- return 'norm! '.a:count.a:map
+ let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
+ let cfile = substitute(cfile, cwdpat, '.', '')
+ if fnameescape(cfile) !=# cfile
+ return '+ '.fnameescape(cfile)
else
- return a:edit.' '.fnameescape(found)
+ return cfile
endif
endfunction