clojure-lsp / clojure-lsp

Clojure & ClojureScript Language Server (LSP) implementation
https://clojure-lsp.io
MIT License
1.17k stars 154 forks source link

Display schema output in `lsp-describe-thing-at-point` #181

Open Andre0991 opened 4 years ago

Andre0991 commented 4 years ago

Consider this function:

(schema.core/defn foo :- s/Uuid
  [bar :- s/Int])

It uses the plumatic schema library.

This is the result of lsp-describe-thing-at-point:

my.ns/foo [bar :- s/Int]

lsp: :declare :public

Note that it displays the input schemas, but it does not do the same for the output (which is s/Uuid).

My company heavily uses plumatic schema, so this is a bit annoying because it requires you to go to the function definition if you want to know the expected output.

ericdallo commented 4 years ago

I'm not sure if we have some easy way to find the output schema on hover/documentation request given the function name, WDYT @snoe ?

snoe commented 4 years ago

@Andre0991 @ericdallo

My first thought is we'd have to allow macro-defs to add something like an :extra-info or :return-type key to the add-reference call, it's possible it has overlap with type-hinting. Then that key could be used in the hover/documentation commands.

I'm hesitant to do this, since we'd like to move to a new parser (kondo) and we'll likely have to do things differently with macros in that world. But if it's important, I'll take a PR.

snoe commented 4 years ago

Oh and you should reuse :signature-style :typed for schema when figuring out how to find the return-type value.

ericdallo commented 3 years ago

We now rely on clj-kondo analysis to know what is that code, and the analysis related to that is:

{:fixed-arities #{1},
  :end-row 18,
  :name-end-col 12,
  :name-end-row 17,
  :name-row 17,
  :ns clojure-sample.core,
  :name foo,
  :filename
  "/home/greg/dev/clojure-sample/src/clojure_sample/core.clj",
  :col 1,
  :name-col 9,
  :end-col 18,
  :arglist-strs ["[bar]"],
  :bucket :var-definitions,
  :row 17}

@borkdude, do you think we could add some info about that on arglists-strs or a new field? It's seems hard to think in a generic field not specific for that issue though

snoe commented 3 years ago

I think this and ^Type hint returns (:tag iirc in clojure.core parlance) could fit well together.

ericdallo commented 3 years ago

Ping @borkdude, if you agree, I can raise a clj-kondo issue to help track that

borkdude commented 3 years ago

The reason the input schema shows up is just because the raw arglist source string is preserved.

I think we could add return types to the analysis, but we would have to come up with a format which offers this information on a per arity basis, since functions can have different return types per arity.

Needs hammock time and probably not high priority.

ericdallo commented 3 years ago

@borkdude I'm trying to implement that on clj-kondo side, AFAIK schema.core can have only one return schema for var, right?

borkdude commented 3 years ago

I don't know, read the schema docs :)

ericdallo commented 3 years ago

From schema.core docs

  • The output schema always goes on the fn name, not the arg vector. This means that all arities must share the same output schema.

@borkdude I don't see a better name, I intend to add to the :var-definitions as :return-schema-str "s/Int" do you have any suggestions?

Or maybe return-schema-strs for future compatibility with other libs? (not sure it's an optimization unnecessary for now)

ericdallo commented 1 year ago

Blocked by https://github.com/clj-kondo/clj-kondo/issues/1268