Skip to content

Commit cb5b5b2

Browse files
committed
Map gf to prevent editing directories
1 parent 7ff47b7 commit cb5b5b2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

ftplugin/ruby.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
144144
\."| sil! exe 'nunmap <buffer> <C-W>g<C-]>'| sil! exe 'nunmap <buffer> <C-W>g]'"
145145
\."| sil! exe 'nunmap <buffer> <C-W>}'| sil! exe 'nunmap <buffer> <C-W>g}'"
146146
endif
147+
148+
if maparg("gf",'n') == ''
149+
" By using findfile() rather than gf's normal behavior, we prevent
150+
" erroneously editing a directory.
151+
nnoremap <silent> <buffer> gf :<C-U>exe <SID>gf(v:count1,"gf",'edit')<CR>
152+
nnoremap <silent> <buffer> <C-W>f :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>f",'split')<CR>
153+
nnoremap <silent> <buffer> <C-W><C-F> :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>\<Lt>C-F>",'split')<CR>
154+
nnoremap <silent> <buffer> <C-W>gf :<C-U>exe <SID>gf(v:count1,"\<Lt>C-W>gf",'tabedit')<CR>
155+
let b:undo_ftplugin = b:undo_ftplugin
156+
\."| 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'"
157+
endif
147158
endif
148159

149160
let &cpo = s:cpo_save
@@ -243,6 +254,19 @@ function! RubyCursorIdentifier()
243254
return stripped == '' ? expand("<cword>") : stripped
244255
endfunction
245256

257+
function! s:gf(count,map,edit) abort
258+
let target = expand('<cfile>')
259+
if target =~# '^\%(require\|load\)$' && getline('.') =~# '^\s*\%(require\|load\) \(["'']\).*\1'
260+
let target = matchstr(getline('.'),'^\s*\%(require\|load\) \(["'']\)\zs.\{-\}\ze\1')
261+
endif
262+
let found = findfile(target, &path, a:count)
263+
if found ==# ''
264+
return 'norm! '.a:count.a:map
265+
else
266+
return a:edit.' '.fnameescape(found)
267+
endif
268+
endfunction
269+
246270
"
247271
" Instructions for enabling "matchit" support:
248272
"

0 commit comments

Comments
 (0)