2013-03-12 19:37:37 +00:00
|
|
|
" autoload/nrepl/fireplace_connection.vim
|
2012-12-04 06:06:22 +00:00
|
|
|
" Maintainer: Tim Pope <http://tpo.pe/>
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
if exists("g:autoloaded_nrepl_fireplace_connection") || &cp
|
2012-12-04 06:06:22 +00:00
|
|
|
finish
|
|
|
|
endif
|
2013-03-12 19:37:37 +00:00
|
|
|
let g:autoloaded_nrepl_fireplace_connection = 1
|
2012-12-04 06:06:22 +00:00
|
|
|
|
|
|
|
function! s:function(name) abort
|
|
|
|
return function(substitute(a:name,'^s:',matchstr(expand('<sfile>'), '<SNR>\d\+_'),''))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Bencode {{{1
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! nrepl#fireplace_connection#bencode(value) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
if type(a:value) == type(0)
|
|
|
|
return 'i'.a:value.'e'
|
|
|
|
elseif type(a:value) == type('')
|
|
|
|
return strlen(a:value).':'.a:value
|
|
|
|
elseif type(a:value) == type([])
|
2014-01-01 00:30:54 +00:00
|
|
|
return 'l'.join(map(copy(a:value),'nrepl#fireplace_connection#bencode(v:val)'),'').'e'
|
2012-12-04 06:06:22 +00:00
|
|
|
elseif type(a:value) == type({})
|
2014-01-01 00:30:54 +00:00
|
|
|
return 'd'.join(values(map(copy(a:value),'nrepl#fireplace_connection#bencode(v:key).nrepl#fireplace_connection#bencode(v:val)')),'').'e'
|
2012-12-04 06:06:22 +00:00
|
|
|
else
|
|
|
|
throw "Can't bencode ".string(a:value)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! nrepl#fireplace_connection#bdecode(value) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
return s:bdecode({'pos': 0, 'value': a:value})
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:bdecode(state) abort
|
|
|
|
let value = a:state.value
|
|
|
|
if value[a:state.pos] =~# '\d'
|
|
|
|
let pos = a:state.pos
|
|
|
|
let length = matchstr(value[pos : -1], '^\d\+')
|
|
|
|
let a:state.pos += strlen(length) + length + 1
|
|
|
|
return value[pos+strlen(length)+1 : pos+strlen(length)+length]
|
|
|
|
elseif value[a:state.pos] ==# 'i'
|
|
|
|
let int = matchstr(value[a:state.pos+1:-1], '[^e]*')
|
|
|
|
let a:state.pos += 2 + strlen(int)
|
|
|
|
return str2nr(int)
|
|
|
|
elseif value[a:state.pos] ==# 'l'
|
|
|
|
let values = []
|
|
|
|
let a:state.pos += 1
|
|
|
|
while value[a:state.pos] !=# 'e' && value[a:state.pos] !=# ''
|
|
|
|
call add(values, s:bdecode(a:state))
|
|
|
|
endwhile
|
|
|
|
let a:state.pos += 1
|
|
|
|
return values
|
|
|
|
elseif value[a:state.pos] ==# 'd'
|
|
|
|
let values = {}
|
|
|
|
let a:state.pos += 1
|
|
|
|
while value[a:state.pos] !=# 'e' && value[a:state.pos] !=# ''
|
|
|
|
let key = s:bdecode(a:state)
|
|
|
|
let values[key] = s:bdecode(a:state)
|
|
|
|
endwhile
|
|
|
|
let a:state.pos += 1
|
|
|
|
return values
|
|
|
|
else
|
|
|
|
throw 'bencode parse error: '.string(a:state)
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" }}}1
|
|
|
|
|
|
|
|
function! s:shellesc(arg) abort
|
|
|
|
if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
|
|
|
|
return a:arg
|
|
|
|
elseif &shell =~# 'cmd'
|
2013-01-20 04:10:33 +00:00
|
|
|
return '"'.substitute(substitute(a:arg, '"', '""""', 'g'), '%', '"%"', 'g').'"'
|
2012-12-04 06:06:22 +00:00
|
|
|
else
|
|
|
|
let escaped = shellescape(a:arg)
|
|
|
|
if &shell =~# 'sh' && &shell !~# 'csh'
|
|
|
|
return substitute(escaped, '\\\n', '\n', 'g')
|
|
|
|
else
|
|
|
|
return escaped
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! nrepl#fireplace_connection#prompt() abort
|
|
|
|
return fireplace#input_host_port()
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
function! nrepl#fireplace_connection#open(arg) abort
|
2012-12-04 06:06:22 +00:00
|
|
|
if a:arg =~# '^\d\+$'
|
|
|
|
let host = 'localhost'
|
|
|
|
let port = a:arg
|
|
|
|
elseif a:arg =~# ':\d\+$'
|
|
|
|
let host = matchstr(a:arg, '.*\ze:')
|
|
|
|
let port = matchstr(a:arg, ':\zs.*')
|
|
|
|
else
|
|
|
|
throw "nREPL: Couldn't find [host:]port in " . a:arg
|
|
|
|
endif
|
|
|
|
let client = deepcopy(s:nrepl)
|
|
|
|
let client.host = host
|
|
|
|
let client.port = port
|
2012-12-15 05:07:46 +00:00
|
|
|
let session = client.process({'op': 'clone'})['new-session']
|
|
|
|
let response = client.process({'op': 'eval', 'session': session, 'code':
|
2012-12-11 01:09:13 +00:00
|
|
|
\ '(do (println "success") (symbol (str (System/getProperty "path.separator") (System/getProperty "java.class.path"))))'})
|
2012-12-15 20:36:50 +00:00
|
|
|
let client._path = response.value[-1]
|
2012-12-15 05:07:46 +00:00
|
|
|
if has_key(response, 'out')
|
2012-12-11 01:09:13 +00:00
|
|
|
let client.session = session
|
|
|
|
endif
|
2012-12-04 06:06:22 +00:00
|
|
|
return client
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:nrepl_path() dict abort
|
|
|
|
return split(self._path[1:-1], self._path[0])
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:nrepl_process(payload) dict abort
|
2012-12-15 05:07:46 +00:00
|
|
|
let combined = {'status': [], 'session': []}
|
|
|
|
for response in self.call(a:payload)
|
|
|
|
for key in keys(response)
|
|
|
|
if key ==# 'id' || key ==# 'ns'
|
|
|
|
let combined[key] = response[key]
|
|
|
|
elseif key ==# 'value'
|
|
|
|
let combined.value = extend(get(combined, 'value', []), [response.value])
|
|
|
|
elseif key ==# 'status'
|
|
|
|
for entry in response[key]
|
|
|
|
if index(combined[key], entry) < 0
|
|
|
|
call extend(combined[key], [entry])
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
elseif key ==# 'session'
|
|
|
|
if index(combined[key], response[key]) < 0
|
|
|
|
call extend(combined[key], [response[key]])
|
|
|
|
endif
|
|
|
|
elseif type(response[key]) == type('')
|
|
|
|
let combined[key] = get(combined, key, '') . response[key]
|
|
|
|
else
|
|
|
|
let combined[key] = response[key]
|
|
|
|
endif
|
|
|
|
endfor
|
2012-12-04 06:06:22 +00:00
|
|
|
endfor
|
2012-12-15 05:07:46 +00:00
|
|
|
if index(combined.status, 'error') >= 0
|
|
|
|
throw 'nREPL: ' . tr(combined.status[0], '-', ' ')
|
|
|
|
endif
|
|
|
|
return combined
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! s:nrepl_eval(expr, ...) dict abort
|
2013-01-07 06:51:48 +00:00
|
|
|
let payload = {"op": "eval"}
|
2014-01-01 01:04:33 +00:00
|
|
|
let payload.code = a:expr
|
2013-01-05 03:26:50 +00:00
|
|
|
let options = a:0 ? a:1 : {}
|
|
|
|
if has_key(options, 'ns')
|
|
|
|
let payload.ns = options.ns
|
2012-12-04 06:06:22 +00:00
|
|
|
elseif has_key(self, 'ns')
|
|
|
|
let payload.ns = self.ns
|
|
|
|
endif
|
2013-01-05 03:26:50 +00:00
|
|
|
if get(options, 'session', 1)
|
|
|
|
if has_key(self, 'session')
|
|
|
|
let payload.session = self.session
|
|
|
|
elseif &verbose
|
|
|
|
echohl WarningMSG
|
|
|
|
echo "nREPL: server has bug preventing session support"
|
|
|
|
echohl None
|
|
|
|
endif
|
2014-01-01 01:04:33 +00:00
|
|
|
endif
|
|
|
|
if has_key(options, 'file_path')
|
2013-12-30 19:19:11 +00:00
|
|
|
let payload.op = 'load-file'
|
|
|
|
let payload['file-path'] = options.file_path
|
|
|
|
let payload['file-name'] = fnamemodify(options.file_path, ':t')
|
|
|
|
if has_key(payload, 'ns')
|
|
|
|
let payload.file = "(in-ns '".payload.ns.") ".payload.code
|
|
|
|
call remove(payload, 'ns')
|
|
|
|
else
|
|
|
|
let payload.file = payload.code
|
|
|
|
endif
|
|
|
|
call remove(payload, 'code')
|
2012-12-11 01:09:13 +00:00
|
|
|
endif
|
2012-12-15 05:07:46 +00:00
|
|
|
let response = self.process(payload)
|
|
|
|
if has_key(response, 'ns') && !a:0
|
|
|
|
let self.ns = response.ns
|
|
|
|
endif
|
|
|
|
|
2014-01-01 01:04:33 +00:00
|
|
|
if has_key(response, 'ex') && has_key(payload, 'session')
|
|
|
|
let response.stacktrace = s:extract_last_stacktrace(self)
|
|
|
|
endif
|
|
|
|
|
2012-12-15 06:32:08 +00:00
|
|
|
if has_key(response, 'value')
|
|
|
|
let response.value = response.value[-1]
|
2012-12-15 05:07:46 +00:00
|
|
|
endif
|
2012-12-15 06:32:08 +00:00
|
|
|
return response
|
2012-12-04 06:06:22 +00:00
|
|
|
endfunction
|
|
|
|
|
2014-01-01 01:04:33 +00:00
|
|
|
function! s:extract_last_stacktrace(nrepl)
|
|
|
|
let format_st = '(clojure.core/symbol (clojure.core/str "\n\b" (clojure.core/apply clojure.core/str (clojure.core/interleave (clojure.core/repeat "\n") (clojure.core/map clojure.core/str (.getStackTrace *e)))) "\n\b\n"))'
|
|
|
|
let stacktrace = split(get(split(a:nrepl.process({'op': 'eval', 'code': '['.format_st.' *3 *2 *1]', 'session': a:nrepl.session}).value[0], "\n\b\n"), 1, ""), "\n")
|
|
|
|
call a:nrepl.call({'op': 'eval', 'code': '(nth *1 1)', 'session': a:nrepl.session})
|
|
|
|
call a:nrepl.call({'op': 'eval', 'code': '(nth *2 2)', 'session': a:nrepl.session})
|
|
|
|
call a:nrepl.call({'op': 'eval', 'code': '(nth *3 3)', 'session': a:nrepl.session})
|
|
|
|
return stacktrace
|
|
|
|
endfunction
|
|
|
|
|
2012-12-04 06:06:22 +00:00
|
|
|
function! s:nrepl_call(payload) dict abort
|
|
|
|
let in = 'ruby -rsocket -e '.s:shellesc(
|
|
|
|
\ 'begin;' .
|
|
|
|
\ 'TCPSocket.open(%(' . self.host . '), ' . self.port . ') {|s|' .
|
|
|
|
\ 's.write(ARGV.first); loop {' .
|
2012-12-31 02:20:41 +00:00
|
|
|
\ 'body = s.readpartial(8192);' .
|
|
|
|
\ 'raise %(not an nREPL server: upgrade to Leiningen 2) if body =~ /=> $/;' .
|
|
|
|
\ 'print body;' .
|
2013-12-31 02:36:23 +00:00
|
|
|
\ 'break if body =~ /6:statusl(5:error|14:session-closed)?4:done/ }};' .
|
2012-12-04 06:06:22 +00:00
|
|
|
\ 'rescue; abort $!.to_s;' .
|
|
|
|
\ 'end') . ' ' .
|
2013-03-12 19:37:37 +00:00
|
|
|
\ s:shellesc(nrepl#fireplace_connection#bencode(a:payload))
|
2012-12-04 06:06:22 +00:00
|
|
|
let out = system(in)
|
|
|
|
if !v:shell_error
|
2013-03-12 19:37:37 +00:00
|
|
|
return nrepl#fireplace_connection#bdecode('l'.out.'e')
|
2012-12-04 06:06:22 +00:00
|
|
|
endif
|
|
|
|
throw 'nREPL: '.split(out, "\n")[0]
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let s:nrepl = {
|
|
|
|
\ 'call': s:function('s:nrepl_call'),
|
|
|
|
\ 'eval': s:function('s:nrepl_eval'),
|
|
|
|
\ 'path': s:function('s:nrepl_path'),
|
|
|
|
\ 'process': s:function('s:nrepl_process')}
|
|
|
|
|
2013-01-09 05:41:46 +00:00
|
|
|
if !has('python')
|
|
|
|
finish
|
2013-01-06 21:48:38 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
python << EOF
|
|
|
|
import vim
|
2013-01-11 04:09:54 +00:00
|
|
|
import select
|
2013-01-06 21:48:38 +00:00
|
|
|
import socket
|
|
|
|
import string
|
|
|
|
import re
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
def fireplace_string_encode(input):
|
2013-01-06 21:48:38 +00:00
|
|
|
str_list = []
|
|
|
|
for c in input:
|
|
|
|
if (000 <= ord(c) and ord(c) <= 037) or c == '"' or c == "\\":
|
2013-04-19 15:41:51 +00:00
|
|
|
str_list.append("\\{0:03o}".format(ord(c)))
|
2013-01-06 21:48:38 +00:00
|
|
|
else:
|
|
|
|
str_list.append(c)
|
|
|
|
return '"' + ''.join(str_list) + '"'
|
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
def fireplace_let(var, value):
|
|
|
|
return vim.command('let ' + var + " = " + fireplace_string_encode(value))
|
2013-01-06 21:48:38 +00:00
|
|
|
|
2013-03-12 19:37:37 +00:00
|
|
|
def fireplace_repl_interact():
|
2013-01-06 21:48:38 +00:00
|
|
|
buffer = ''
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2013-01-11 04:09:54 +00:00
|
|
|
host = vim.eval('self.host')
|
|
|
|
port = int(vim.eval('self.port'))
|
|
|
|
s.settimeout(8)
|
2013-01-06 21:48:38 +00:00
|
|
|
try:
|
2013-04-17 17:35:47 +00:00
|
|
|
try:
|
|
|
|
s.connect((host, port))
|
|
|
|
s.setblocking(1)
|
|
|
|
s.sendall(vim.eval('payload'))
|
|
|
|
while True:
|
|
|
|
while len(select.select([s], [], [], 0.1)[0]) == 0:
|
|
|
|
vim.eval('getchar(1)')
|
|
|
|
body = s.recv(8192)
|
|
|
|
if re.search("=> $", body) != None:
|
|
|
|
raise Exception("not an nREPL server: upgrade to Leiningen 2")
|
|
|
|
buffer += body
|
2013-12-31 02:36:23 +00:00
|
|
|
if re.search('6:statusl(5:error|14:session-closed)?4:done', body):
|
2013-04-17 17:35:47 +00:00
|
|
|
break
|
|
|
|
fireplace_let('out', buffer)
|
|
|
|
except Exception, e:
|
|
|
|
fireplace_let('err', str(e))
|
2013-01-06 21:48:38 +00:00
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
EOF
|
|
|
|
|
|
|
|
function! s:nrepl_call(payload) dict abort
|
2013-03-12 19:37:37 +00:00
|
|
|
let payload = nrepl#fireplace_connection#bencode(a:payload)
|
2013-01-06 21:48:38 +00:00
|
|
|
python << EOF
|
2013-03-12 19:37:37 +00:00
|
|
|
fireplace_repl_interact()
|
2013-01-06 21:48:38 +00:00
|
|
|
EOF
|
|
|
|
if !exists('err')
|
2013-03-12 19:37:37 +00:00
|
|
|
return nrepl#fireplace_connection#bdecode('l'.out.'e')
|
2013-01-06 21:48:38 +00:00
|
|
|
endif
|
2013-01-07 04:18:15 +00:00
|
|
|
throw 'nREPL Connection Error: '.err
|
2013-01-06 21:48:38 +00:00
|
|
|
endfunction
|
|
|
|
|
2012-12-04 06:06:22 +00:00
|
|
|
" vim:set et sw=2:
|