Skip to content

Commit bcf8413

Browse files
committed
Retool server binding retrieval
Most notably, now include the http:// as part of the binding. References: tpope#592
1 parent cb97739 commit bcf8413

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

autoload/rails.vim

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,31 +1727,37 @@ function! s:app_server_binding() dict abort
17271727
endif
17281728
for app in s:split(glob("~/.pow/*"))
17291729
if resolve(app) ==# resolve(self.path())
1730-
return fnamemodify(app, ':t').'.dev'
1730+
return 'http://' . fnamemodify(app, ':t') . '.dev'
17311731
endif
17321732
endfor
17331733
return ''
17341734
endfunction
17351735

1736-
call s:add_methods('app', ['server_pid', 'server_binding'])
1736+
function! s:app_server_root() dict abort
1737+
return substitute(substitute(self.server_binding(),
1738+
\ '://\zs\%(0\.0\.0\.0\|127\.0\.0\.1\)\>', 'localhost', ''),
1739+
\ '://\zs\[::\]', '[::1]', '')
1740+
endfunction
1741+
1742+
call s:add_methods('app', ['server_pid', 'server_binding', 'server_root'])
17371743

17381744
function! s:Preview(bang, lnum, uri) abort
1739-
let binding = rails#app().server_binding()
1740-
if empty(binding)
1741-
let binding = '0.0.0.0:3000'
1745+
let root = rails#app().server_root()
1746+
if empty(root)
1747+
let root = 'http://localhost:3000'
17421748
endif
1743-
let binding = s:sub(binding, '^0\.0\.0\.0>|^127\.0\.0\.1>', 'localhost')
1744-
let binding = s:sub(binding, '^\[::\]', '[::1]')
17451749
let uri = empty(a:uri) ? get(rails#buffer().preview_urls(a:lnum),0,'') : a:uri
17461750
if uri =~ '://'
17471751
"
17481752
elseif uri =~# '^[[:alnum:]-]\+\.'
1749-
let uri = 'http://'.s:sub(uri, '^[^/]*\zs', matchstr(root, ':\d\+$'))
1753+
let uri = matchstr(root, '^.\{-\}://') . substitute(uri, '^[^/]*\zs', matchstr(root, ':\d\+$'), '')
17501754
elseif uri =~# '^[[:alnum:]-]\+\%(/\|$\)'
1751-
let domain = s:sub(binding, '^localhost>', 'lvh.me')
1752-
let uri = 'http://'.s:sub(uri, '^[^/]*\zs', '.'.domain)
1755+
let domain = substitute(
1756+
\ substitute(matchstr(root, '://\zs.*'), '\C^localhost\>', 'lvh.me', ''),
1757+
\ '^\d\+\.\d\+\.\d\+\.\d\+\ze\%(:\|$\)', '&.nip.io', '')
1758+
let uri = matchstr(root, '^.\{-\}://') . substitute(uri, '^[^/]*\zs', '.' . domain, '')
17531759
else
1754-
let uri = 'http://'.binding.'/'.s:sub(uri,'^/','')
1760+
let uri = root . '/' . substitute(uri, '^/', '', '')
17551761
endif
17561762
call s:initOpenURL()
17571763
if (exists(':OpenURL') == 2) && !a:bang
@@ -1760,9 +1766,9 @@ function! s:Preview(bang, lnum, uri) abort
17601766
" Work around bug where URLs ending in / get handled as FTP
17611767
let url = uri.(uri =~ '/$' ? '?' : '')
17621768
silent exe 'pedit '.url
1763-
let root = rails#app().path()
1769+
let app_path = rails#app().path()
17641770
wincmd w
1765-
let b:rails_root = root
1771+
let b:rails_root = app_path
17661772
if &filetype ==# ''
17671773
if uri =~ '\.css$'
17681774
setlocal filetype=css
@@ -1964,30 +1970,34 @@ function! rails#get_binding_for(pid) abort
19641970
if has('win32')
19651971
let output = system('netstat -anop tcp')
19661972
let binding = matchstr(output, '\n\s*TCP\s\+\zs\S\+\ze\s\+\S\+\s\+LISTENING\s\+'.a:pid.'\>')
1967-
return s:sub(binding, '^([^[]*:.*):', '[\1]:')
1968-
endif
1969-
if executable('lsof')
1970-
let lsof = 'lsof'
1971-
elseif executable('/usr/sbin/lsof')
1972-
let lsof = '/usr/sbin/lsof'
1973-
endif
1974-
if exists('lsof')
1975-
let output = system(lsof.' -anP -i4tcp -sTCP:LISTEN -p'.a:pid)
1976-
let binding = matchstr(output, '\S\+:\d\+\ze\s\+(LISTEN)\n')
1977-
let binding = s:sub(binding, '^\*', '0.0.0.0')
1978-
if empty(binding)
1979-
let output = system(lsof.' -anP -i6tcp -sTCP:LISTEN -p'.a:pid)
1973+
else
1974+
if executable('lsof')
1975+
let lsof = 'lsof'
1976+
elseif executable('/usr/sbin/lsof')
1977+
let lsof = '/usr/sbin/lsof'
1978+
endif
1979+
if exists('lsof')
1980+
let output = system(lsof.' -anP -i4tcp -sTCP:LISTEN -p'.a:pid)
19801981
let binding = matchstr(output, '\S\+:\d\+\ze\s\+(LISTEN)\n')
1981-
let binding = s:sub(binding, '^\*', '[::]')
1982+
let binding = s:sub(binding, '^\*', '0.0.0.0')
1983+
if empty(binding)
1984+
let output = system(lsof.' -anP -i6tcp -sTCP:LISTEN -p'.a:pid)
1985+
let binding = matchstr(output, '\S\+:\d\+\ze\s\+(LISTEN)\n')
1986+
let binding = s:sub(binding, '^\*', '[::]')
1987+
endif
1988+
elseif executable('netstat')
1989+
let output = system('netstat -antp')
1990+
let binding = matchstr(output, '\S\+:\d\+\ze\s\+\S\+\s\+LISTEN\s\+'.a:pid.'/')
1991+
return empty(binding) ? '' : 'http://' . s:sub(binding, '^([^[]*:.*):', '[\1]:')
1992+
else
1993+
let binding = ''
19821994
endif
1983-
return binding
19841995
endif
1985-
if executable('netstat')
1986-
let output = system('netstat -antp')
1987-
let binding = matchstr(output, '\S\+:\d\+\ze\s\+\S\+\s\+LISTEN\s\+'.a:pid.'/')
1988-
return s:sub(binding, '^([^[]*:.*):', '[\1]:')
1996+
let binding = substitute(binding, '^\(^[^[]*:.*):', '[\1]:', '')
1997+
if empty(binding)
1998+
return ''
19891999
endif
1990-
return ''
2000+
return 'http://' . binding
19912001
endfunction
19922002

19932003
function! s:ServerCommand(kill, bg, arg) abort
@@ -2686,9 +2696,9 @@ function! s:app_routes() dict abort
26862696
let cwd = getcwd()
26872697
let routes = []
26882698
let paths = {}
2689-
let binding = self.server_binding()
2690-
if len(binding) && len(s:webcat())
2691-
let html = system(s:webcat() . ' ' . shellescape('http://' . binding . '/rails/info/routes'))
2699+
let root = self.server_root()
2700+
if len(root) && len(s:webcat())
2701+
let html = system(s:webcat() . ' ' . shellescape(root . '/rails/info/routes'))
26922702
for line in split(matchstr(html, '.*<tbody>\zs.*\ze</tbody>'), "\n")
26932703
let val = matchstr(line, '\C<td data-route-name=''\zs[^'']*''\ze>')
26942704
if len(val)

0 commit comments

Comments
 (0)