Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for spec middleware (form, example) #335

Merged
merged 13 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion doc/fireplace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ DOCUMENTATION *fireplace-documentation*
:Javadoc {class} Open the java docs for the given class in a browser.

*fireplace-K*
K Look up docs for symbol under cursor.
K Look up the doc, javadoc, or spec-form for the
identifier under the cursor.

*fireplace-:FindDoc*
:FindDoc {arg} Wrapper around (clojure.repl/find-doc ...).

*fireplace-:SpecForm*
:SpecForm {keyword} Show the form of the spec given.
Like (spec/form keyword).

*fireplace-:SpecExample*
:SpecExample {keyword} Generate one example for the spec given.
Like (gen/generate keyword).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:SpecForm {keyword}, and the wording needs tweaking but I'm not sure to what. Do these roughly correspond to clojure.spec functions? If so, it might make sense to reference that like we do for :FindDoc. I worry that just saying "spec" is confusing to those not familiar (it was for me).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use some codified version like (spec/form) - they are at least all named the same for now in all three versions of spec, that are in the wild.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used spec/form and gen/generate - the later is the alias used in the main docs https://clojure.org/guides/spec#_sampling_generators; the first is a bit more clear than just s

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we go all in and use the full namespaces? That should make it easier to look up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with the namespace would be to name all two currently (and soon three) supported by orchard/cider (spec "0", spec alpha, spec alpha 2) - which is a problem on it's on (see answer to "error handling" comment below)

*fireplace-:Source*
:Source {symbol} Show the source for the given symbol.

Expand Down
61 changes: 61 additions & 0 deletions plugin/fireplace.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ function! s:map(mode, lhs, rhs, ...) abort
endif
endfunction

function! s:pr(obj) abort
if type(a:obj) == v:t_string
return a:obj
elseif type(a:obj) == v:t_list
return '(' . join(map(copy(a:obj), 's:pr(v:val)'), ' ') . ')'
elseif type(a:obj) == v:t_dict
return '{' . join(map(keys(a:obj), 's:pr(v:val) . " " . s:pr(a:obj[v:val])'), ', ') . '}'
else
return string(a:obj)
endif
endfunction

" Section: Escaping

function! s:str(string) abort
Expand Down Expand Up @@ -1666,6 +1678,53 @@ augroup fireplace_go_to_file
autocmd FileType clojure call s:set_up_go_to_file()
augroup END

" Section: Spec

function! fireplace#qualify_keyword(kw) abort
if a:kw =~# '^::.\+/'
let kw = ':' . fireplace#resolve_alias(matchstr(a:kw, '^::\zs[^/]\+')) . matchstr(a:kw, '/.*')
elseif a:kw =~# '^::'
let kw = ':' . fireplace#ns() . '/' . strpart(a:kw, 2)
else
let kw = a:kw
endif
return kw
endfunction

function! s:SpecForm(kw) abort
let op = "spec-form"
if fireplace#op_available(op)
let symbol = fireplace#qualify_keyword(a:kw)
let response = fireplace#message({'op': op, 'spec-name': symbol})[0]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new fireplace#message({...}, v:t_dict) API here to get back a single dictionary, rather than relying on the response to be a single message.

if !empty(get(response, op))
echo s:pr(get(response, op))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unknown op error includes the original op, so this should be adequate for error handling:

elseif has_key(response, 'op')
  return 'echoerr ' . string('Fireplace: no nREPL op available for ' . op)

Use this same echoerr pattern for any other error handling.

endif
endif
Copy link
Owner

@tpope tpope Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop the conditional and wrap everything in a try block to handle the case when we're not connected:

try
  ...
catch /^Fireplace:/
  return 'echoerr ' . v:exception
endtry

return ''
endfunction

function! s:SpecExample(kw) abort
let op = "spec-example"
if fireplace#op_available(op)
let symbol = fireplace#qualify_keyword(a:kw)
let response = fireplace#message({'op': op, 'spec-name': symbol})[0]
if !empty(get(response, op))
echo get(response, op)
endif
endif
return ''
endfunction

function! s:set_up_fireplace_spec() abort
command! -buffer -bar -nargs=1 -complete=customlist,fireplace#eval_complete SpecForm :exe s:SpecForm(<q-args>)
command! -buffer -bar -nargs=1 -complete=customlist,fireplace#eval_complete SpecExample :exe s:SpecExample(<q-args>)
endfunction

augroup fireplace_spec
autocmd!
autocmd FileType clojure call s:set_up_fireplace_spec()
augroup END

" Section: Formatting

function! fireplace#format(lnum, count, char) abort
Expand Down Expand Up @@ -1772,6 +1831,8 @@ function! s:K() abort
let java_candidate = matchstr(word, '^\%(\w\+\.\)*\u\l[[:alnum:]$]*\ze\%(\.\|\/\w\+\)\=$')
if java_candidate !=# ''
return 'Javadoc '.java_candidate
elseif word =~# '^:'
return 'SpecForm '.word
else
return 'Doc '.word
endif
Expand Down