Skip to content

Commit ac63f9a

Browse files
committed
Fix detection in netrw buffers
Ported over from Projectionist.
1 parent a6dce38 commit ac63f9a

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

plugin/rails.vim

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,31 @@ function! s:error(str)
1818
let v:errmsg = a:str
1919
endfunction
2020

21+
let s:slash = exists('+shellslash') ? '\' : '/'
22+
function! s:IsAbs(path) abort
23+
return tr(a:path, s:slash, '/') =~# '^/\|^\a\+:'
24+
endfunction
25+
2126
" }}}1
2227
" Detection {{{1
2328

2429
function! RailsDetect(...) abort
2530
if exists('b:rails_root')
2631
return 1
2732
endif
28-
let path = a:0 ? a:1 : @%
33+
if a:0
34+
let path = a:1
35+
elseif &l:buftype =~# '^\%(nowrite\)\=$' && len(@%) || &l:buftype =~# '^\%(nofile\|acwrite\)' && s:IsAbs(@%)
36+
let path = @%
37+
else
38+
return
39+
endif
40+
if !s:IsAbs(path)
41+
let path = getcwd() . (exists('+shellslash') && !&shellslash ? '\' : '/') . path
42+
endif
43+
let path = substitute(path, '[' . s:slash . '/]$', '', '')
44+
2945
if exists('*ProjectionistHas')
30-
if len(&l:buftype)
31-
let path = fnamemodify(path, ':p')
32-
elseif path !~# '^$\|^/\|^\a\+:\|^\\\\'
33-
let path = getcwd() . '/' . path
34-
endif
3546
let previous = ''
3647
while path !=# previous && path !~# '^\.\=$\|^[\/][\/][^\/]*$'
3748
if ProjectionistHas('config/environment.rb&app/', path)
@@ -43,7 +54,7 @@ function! RailsDetect(...) abort
4354
endwhile
4455
return 0
4556
endif
46-
let file = findfile('config/environment.rb', escape(fnamemodify(path, ':p:h'), ', ').';')
57+
let file = findfile('config/environment.rb', escape(path, ', ').';')
4758
if !empty(file) && isdirectory(fnamemodify(file, ':p:h:h') . '/app')
4859
let b:rails_root = fnamemodify(file, ':p:h:h')
4960
return 1
@@ -83,7 +94,7 @@ augroup railsPluginDetect
8394
autocmd!
8495

8596
autocmd BufNewFile,BufReadPost *
86-
\ if RailsDetect(expand("<afile>")) && empty(&filetype) |
97+
\ if RailsDetect() && empty(&filetype) |
8798
\ call rails#buffer_setup() |
8899
\ endif
89100
autocmd VimEnter *
@@ -93,7 +104,7 @@ augroup railsPluginDetect
93104
\ call s:doau_user('BufEnterRails') |
94105
\ endif
95106
autocmd FileType netrw
96-
\ if RailsDetect() |
107+
\ if RailsDetect(get(b:, 'netrw_curdir', @%)) |
97108
\ call s:doau_user('BufEnterRails') |
98109
\ endif
99110
autocmd FileType * if RailsDetect() | call rails#buffer_setup() | endif

0 commit comments

Comments
 (0)