Support .nrepl-port outside of Leiningen

This commit is contained in:
Tim Pope 2014-04-03 21:03:26 -04:00
parent 1bfb5aadcf
commit ab5eee34d8

View File

@ -134,6 +134,7 @@ let s:repl = {"requires": {}}
if !exists('s:repls') if !exists('s:repls')
let s:repls = [] let s:repls = []
let s:repl_paths = {} let s:repl_paths = {}
let s:repl_portfiles = {}
endif endif
function! s:repl.user_ns() abort function! s:repl.user_ns() abort
@ -236,6 +237,34 @@ endfunction
function! s:unregister_connection(conn) abort function! s:unregister_connection(conn) abort
call filter(s:repl_paths, 'v:val.connection.transport isnot# a:conn.transport') call filter(s:repl_paths, 'v:val.connection.transport isnot# a:conn.transport')
call filter(s:repls, 'v:val.connection.transport isnot# a:conn.transport') call filter(s:repls, 'v:val.connection.transport isnot# a:conn.transport')
call filter(s:repl_portfiles, 'v:val.connection.transport isnot# a:conn.transport')
endfunction
function! s:register_portfile(portfile, ...) abort
let old = get(s:repl_portfiles, a:portfile, {})
if has_key(old, 'time') && getftime(a:portfile) !=# old.time
call s:unregister_connection(old.connection)
let old = {}
endif
if empty(old) && getfsize(a:portfile) > 0
let port = matchstr(readfile(a:portfile, 'b', 1)[0], '\d\+')
let s:repl_portfiles[a:portfile] = {'time': getftime(a:portfile)}
try
let conn = nrepl#fireplace_connection#open(port)
let s:repl_portfiles[a:portfile].connection = conn
call s:register_connection(conn, a:0 ? a:1 : '')
return conn
catch /^nREPL Connection Error:/
if &verbose
echohl WarningMSG
echomsg v:exception
echohl None
endif
return {}
endtry
else
return get(old, 'connection', {})
endif
endfunction endfunction
" }}}1 " }}}1
@ -437,7 +466,18 @@ function! fireplace#path(...) abort
endfunction endfunction
function! fireplace#platform(...) abort function! fireplace#platform(...) abort
for [k, v] in items(s:repl_portfiles)
if getftime(k) != v.time
call s:unregister_connection(v.connection)
endif
endfor
let portfile = findfile('.nrepl-port', '.;/')
if !empty(portfile)
call s:register_portfile(portfile, fnamemodify(portfile, ':h'))
endif
silent doautocmd User FireplacePreConnect silent doautocmd User FireplacePreConnect
let buf = a:0 ? a:1 : s:buf() let buf = a:0 ? a:1 : s:buf()
let root = simplify(fnamemodify(bufname(buf), ':p:s?[\/]$??')) let root = simplify(fnamemodify(bufname(buf), ':p:s?[\/]$??'))
let previous = "" let previous = ""
@ -1320,13 +1360,13 @@ if !exists('s:leiningen_repls')
let s:leiningen_paths = {} let s:leiningen_paths = {}
endif endif
function! s:portfile() abort function! s:leiningen_portfile() abort
if !exists('b:leiningen_root') if !exists('b:leiningen_root')
return '' return ''
endif endif
let root = b:leiningen_root let root = b:leiningen_root
let portfiles = [root.'/target/repl-port', root.'/target/repl/repl-port', root.'/.nrepl-port'] let portfiles = [root.'/.nrepl-port', root.'/target/repl-port', root.'/target/repl/repl-port']
for f in portfiles for f in portfiles
if filereadable(f) if filereadable(f)
@ -1336,7 +1376,6 @@ function! s:portfile() abort
return '' return ''
endfunction endfunction
function! s:leiningen_connect() abort function! s:leiningen_connect() abort
for [k, v] in items(s:leiningen_repls) for [k, v] in items(s:leiningen_repls)
if getfsize(v.file) <= 0 if getfsize(v.file) <= 0
@ -1345,26 +1384,13 @@ function! s:leiningen_connect() abort
endif endif
endfor endfor
let portfile = s:portfile() let portfile = s:leiningen_portfile()
if empty(portfile) if empty(portfile)
return return
endif endif
let conn = s:register_portfile(portfile, b:leiningen_root)
if getfsize(portfile) > 0 && getftime(portfile) !=# get(get(s:leiningen_repls, b:leiningen_root, {}), 'time', -1) if has_key(conn, 'path')
let port = matchstr(readfile(portfile, 'b', 1)[0], '\d\+')
let s:leiningen_repls[b:leiningen_root] = {'time': getftime(portfile), 'file': portfile}
try
let conn = nrepl#fireplace_connection#open(port)
let s:leiningen_repls[b:leiningen_root].connection = conn
call s:register_connection(conn, b:leiningen_root)
let s:leiningen_paths[b:leiningen_root] = conn.path() let s:leiningen_paths[b:leiningen_root] = conn.path()
catch /^nREPL Connection Error:/
if &verbose
echohl WarningMSG
echomsg v:exception
echohl None
endif
endtry
endif endif
endfunction endfunction