Separate user from system evals

This keeps the user's session clean by not using it for things the user
did not directly dispatch (such as omnicomplete).  On the fence but
currently included in the user session is commands like :Doc.
This commit is contained in:
Tim Pope 2013-01-04 22:26:50 -05:00
parent 39d1e296cf
commit e62540fef9
2 changed files with 62 additions and 42 deletions

View File

@ -144,13 +144,20 @@ endfunction
function! s:nrepl_eval(expr, ...) dict abort function! s:nrepl_eval(expr, ...) dict abort
let payload = {"op": "eval", "code": a:expr} let payload = {"op": "eval", "code": a:expr}
if a:0 let options = a:0 ? a:1 : {}
let payload.ns = a:1 if has_key(options, 'ns')
let payload.ns = options.ns
elseif has_key(self, 'ns') elseif has_key(self, 'ns')
let payload.ns = self.ns let payload.ns = self.ns
endif endif
if has_key(self, 'session') if get(options, 'session', 1)
let payload.session = self.session 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
endif endif
let response = self.process(payload) let response = self.process(payload)
if has_key(response, 'ns') && !a:0 if has_key(response, 'ns') && !a:0

View File

@ -145,9 +145,9 @@ function! s:repl.path() dict abort
return self.connection.path() return self.connection.path()
endfunction endfunction
function! s:repl.eval(expr, ns) dict abort function! s:repl.eval(expr, options) dict abort
try try
let result = self.connection.eval(a:expr, a:ns) let result = self.connection.eval(a:expr, a:options)
catch /^\w\+: Connection/ catch /^\w\+: Connection/
call filter(s:repl_paths, 'v:val isnot self') call filter(s:repl_paths, 'v:val isnot self')
call filter(s:repls, 'v:val isnot self') call filter(s:repls, 'v:val isnot self')
@ -160,7 +160,7 @@ function! s:repl.require(lib) dict abort
if a:lib !~# '^\%(user\)\=$' && !get(self.requires, a:lib, 0) if a:lib !~# '^\%(user\)\=$' && !get(self.requires, a:lib, 0)
let reload = has_key(self.requires, a:lib) ? ' :reload' : '' let reload = has_key(self.requires, a:lib) ? ' :reload' : ''
let self.requires[a:lib] = 0 let self.requires[a:lib] = 0
let result = self.eval('(doto '.s:qsym(a:lib).' (require'.reload.') the-ns)', 'user') let result = self.eval('(doto '.s:qsym(a:lib).' (require'.reload.') the-ns)', {'ns': 'user', 'session': 0})
let self.requires[a:lib] = !has_key(result, 'ex') let self.requires[a:lib] = !has_key(result, 'ex')
endif endif
return '' return ''
@ -287,14 +287,14 @@ let s:oneoff_in = tempname()
let s:oneoff_out = tempname() let s:oneoff_out = tempname()
let s:oneoff_err = tempname() let s:oneoff_err = tempname()
function! s:oneoff.eval(expr, ns) dict abort function! s:oneoff.eval(expr, options) dict abort
if &verbose if &verbose && get(options, 'session', 1)
echohl WarningMSG echohl WarningMSG
echomsg "No REPL found. Running java clojure.main ..." echomsg "No REPL found. Running java clojure.main ..."
echohl None echohl None
endif endif
if a:ns !=# '' && a:ns !=# 'user' if a:options.ns !=# '' && a:options.ns !=# 'user'
let ns = '(require '.s:qsym(a:ns).') (in-ns '.s:qsym(a:ns).') ' let ns = '(require '.s:qsym(options).') (in-ns '.s:qsym(options).') '
else else
let ns = '' let ns = ''
endif endif
@ -370,36 +370,46 @@ function! foreplay#local_client(...)
return extend({'classpath': cp}, s:oneoff) return extend({'classpath': cp}, s:oneoff)
endfunction endfunction
function! foreplay#eval(expr, ...) abort function! s:output_response(response) abort
let c = s:client() if get(a:response, 'err', '') !=# ''
if !a:0 && foreplay#ns() !~# '^\%(user\)$'
call c.require(foreplay#ns())
endif
let result = c.eval(a:expr, a:0 ? a:1 : foreplay#ns())
if get(result, 'err', '') !=# ''
echohl ErrorMSG echohl ErrorMSG
echo substitute(result.err, '\n$', '', '') echo substitute(a:response.err, '\n$', '', '')
echohl NONE echohl NONE
endif endif
if get(result, 'out', '') !=# '' if get(a:response, 'out', '') !=# ''
echo substitute(result.out, '\n$', '', '') echo substitute(a:response.out, '\n$', '', '')
endif endif
endfunction
if get(result, 'ex', '') !=# '' function! s:eval(expr, ...) abort
let err = 'Clojure: '.result.ex let options = a:0 ? copy(a:1) : {}
elseif has_key(result, 'value') let client = get(options, 'client', s:client())
return result.value if !has_key(options, 'ns')
if foreplay#ns() !~# '^\%(user\)$'
call client.require(foreplay#ns())
endif
let options.ns = foreplay#ns()
endif
return client.eval(a:expr, options)
endfunction
function! foreplay#eval(expr) abort
let response = s:eval(a:expr, {'session': 1})
call s:output_response(response)
if get(response, 'ex', '') !=# ''
let err = 'Clojure: '.response.ex
elseif has_key(response, 'value')
return response.value
else else
let err = 'foreplay.vim: Something went wrong: '.string(result) let err = 'foreplay.vim: Something went wrong: '.string(response)
endif endif
throw err throw err
endfunction endfunction
function! foreplay#evalparse(expr) abort function! foreplay#evalparse(expr) abort
let body = foreplay#eval( let response = s:eval(
\ '(symbol ((fn *vimify [x]' . \ '(symbol ((fn *vimify [x]' .
\ ' (cond' . \ ' (cond' .
\ ' (map? x) (str "{" (apply str (interpose ", " (map (fn [[k v]] (str (*vimify k) ": " (*vimify v))) x))) "}")' . \ ' (map? x) (str "{" (apply str (interpose ", " (map (fn [[k v]] (str (*vimify k) ": " (*vimify v))) x))) "}")' .
@ -407,12 +417,17 @@ function! foreplay#evalparse(expr) abort
\ ' (number? x) (pr-str x)' . \ ' (number? x) (pr-str x)' .
\ ' (keyword? x) (pr-str (name x))' . \ ' (keyword? x) (pr-str (name x))' .
\ ' :else (pr-str (str x)))) '.a:expr.'))', \ ' :else (pr-str (str x)))) '.a:expr.'))',
\ a:0 ? a:1 : foreplay#ns()) \ {'session': 0})
if body ==# '' call s:output_response(response)
return ''
if get(response, 'ex', '') !=# ''
let err = 'Clojure: '.response.ex
elseif has_key(response, 'value')
return empty(response.value) ? '' : eval(response.value)
else else
return eval(body) let err = 'foreplay.vim: Something went wrong: '.string(response)
endif endif
throw err
endfunction endfunction
" }}}1 " }}}1
@ -453,7 +468,7 @@ function! s:filterop(type) abort
let reg_save = @@ let reg_save = @@
try try
let expr = s:opfunc(a:type) let expr = s:opfunc(a:type)
let @@ = matchstr(expr, '^\n\+').foreplay#eval(expr, foreplay#ns()).matchstr(expr, '\n\+$') let @@ = matchstr(expr, '^\n\+').foreplay#eval(expr).matchstr(expr, '\n\+$')
if @@ !~# '^\n*$' if @@ !~# '^\n*$'
normal! gvp normal! gvp
endif endif
@ -684,8 +699,7 @@ augroup END
" Go to source {{{1 " Go to source {{{1
function! foreplay#source(symbol) abort function! foreplay#source(symbol) abort
let c = foreplay#local_client() let options = {'client': foreplay#local_client(), 'ns': foreplay#ns(), 'session': 0}
call c.require(foreplay#ns())
let cmd = let cmd =
\ " (when-let [v (resolve " . s:qsym(a:symbol) .')]' . \ " (when-let [v (resolve " . s:qsym(a:symbol) .')]' .
\ ' (when-let [filepath (:file (meta v))]' . \ ' (when-let [filepath (:file (meta v))]' .
@ -694,7 +708,7 @@ function! foreplay#source(symbol) abort
\ ' (if (= "jar" (.getProtocol url))' . \ ' (if (= "jar" (.getProtocol url))' .
\ ' (str "zip" (.replaceFirst (.getFile url) "!/" "::"))' . \ ' (str "zip" (.replaceFirst (.getFile url) "!/" "::"))' .
\ ' (.getFile url)))))))' \ ' (.getFile url)))))))'
let result = get(split(c.eval(cmd, foreplay#ns()).value, "\n"), 0, '') let result = get(split(s:eval(cmd, options).value, "\n"), 0, '')
return result ==# 'nil' ? '' : result return result ==# 'nil' ? '' : result
endfunction endfunction
@ -739,8 +753,7 @@ augroup END
" Go to file {{{1 " Go to file {{{1
function! foreplay#findfile(path) abort function! foreplay#findfile(path) abort
let c = foreplay#local_client() let options = {'client': foreplay#local_client(), 'ns': foreplay#ns(), 'session': 0}
call c.require(foreplay#ns())
let cmd = let cmd =
\ '(symbol' . \ '(symbol' .
@ -758,7 +771,7 @@ function! foreplay#findfile(path) abort
\ '(if-let [ns ((ns-aliases *ns*) '.s:qsym(path).')]' . \ '(if-let [ns ((ns-aliases *ns*) '.s:qsym(path).')]' .
\ ' (str (.replace (.replace (str (ns-name ns)) "-" "_") "." "/") ".clj")' . \ ' (str (.replace (.replace (str (ns-name ns)) "-" "_") "." "/") ".clj")' .
\ ' "'.path.'.clj")') \ ' "'.path.'.clj")')
let result = get(split(c.eval(aliascmd, foreplay#ns()).value, "\n"), 0, '') let result = get(split(s:eval(aliascmd, options).value, "\n"), 0, '')
else else
if path !~# '/' if path !~# '/'
let path = tr(path, '.-', '/_') let path = tr(path, '.-', '/_')
@ -767,7 +780,7 @@ function! foreplay#findfile(path) abort
let path .= '.clj' let path .= '.clj'
endif endif
let result = get(split(c.eval(printf(cmd, '"'.escape(path, '"').'"'), foreplay#ns()).value, "\n"), 0, '') let result = get(split(s:eval(printf(cmd, '"'.escape(path, '"').'"'), options).value, "\n"), 0, '')
endif endif
if result ==# '' if result ==# ''