calva icon indicating copy to clipboard operation
calva copied to clipboard

Feature Request: autocomplete (and validate) known map keys

Open ikitommi opened this issue 2 years ago • 4 comments

Autocomplete (and validate) known map keys

Related to the Slack discussion on #calva and in #clj-kondo: Clojure is a dynamically typed language, but we could leverage the runtime schema/spec data in IDE to provide better type linting and auto-complete of known map keys. This would radically improve developer experience, help people come from ecosystem like TypeScript and lessen the map fatigue.

The below examples are based on malli, but the same approach could work with schema and spec too.

Use Case

  1. describe a data structure as malli schema
  2. attach the schema to one of the following: i. function argument or return value ii. var value iii. edn file (e.g. config file)
  3. instead of just getting a runtime errors, provide static analysis to provide both i. errors without evaluating the code ii. autocomplete on known map keys

Current Status

  1. describe a data structure as malli schema
    • ✅ default malli thingie, https://github.com/metosin/malli
  2. attach the schema to one of the following:
    1. function argument or return value
      • ✅ https://cljdoc.org/d/metosin/malli/CURRENT/doc/function-schemas
    2. var value
      • 🚧 (https://github.com/metosin/malli/pull/776)
    3. edn file (e.g. config file)
      • 🚧 - how to map EDN-files with schemas? similar to how JSON Schema + JSON work with VS Code https://code.visualstudio.com/docs/languages/json ?
  3. instead of just getting a runtime errors, provide static analysis to provide both
    1. errors without evaluating the code
      • ✅ malli emits clj-kondo configs for function inputs & outputs
      • 🚧 https://github.com/clj-kondo/clj-kondo/issues/2070
    2. autocomplete on known map keys
      • 👍 this issue

Suggestion

Add IDE-support for autocompleting map keys in case we know the data form via malli schemas for the data. Integrate this to when creating literal data maps (2ii & 2iii) and when accessing schematized function arguments within the function body.

This might require work on all of malli, clj-kondo, lsp-clojure and calva.

Happy to help from malli side.

Screenshots

Malli + clj-kondo ✅

malli+clj-kondo

Malli Vars 🚧 👍

  • here it would be just literal data, maybe easier to autocomplete?
  • Similar could be used with EDN-files I guess

malli

Imaginary Autocomplete of known keys 👍

  • here, the known keys could be known by partial code evaluation?
  • the suggested keys (from Cursive) are from global keyword autocomplete, don't know anything from context

clj-kondo-lsp-malli-autocomplete

JSON + JSON Schema + VSCode ✅

json

Typescript + VSCode ✅

typescript

Bonus

As Malli supports lite-syntax, the syntax is not far from typescript (in the simple cases):

const Page = (props: { message: string }) => 
  <div>{props.message}</div>;
(mx/defn page [props :- {:message :string}]
  [:div (:message props)])

ikitommi avatar Jul 04 '23 18:07 ikitommi

Thanks for the extensive info. @ericdallo, any thoughts here about clojure-lsp's side of supporting this?

bpringe avatar Jul 04 '23 22:07 bpringe

Very cool! Iiuc, we wouldn't need to do anything on the Calva side of things if clojure-lsp is made to support this. Is that how you understand it too, @bpringe?

PEZ avatar Jul 05 '23 06:07 PEZ

Yes, that seems to be the case to me as well.

bpringe avatar Jul 07 '23 02:07 bpringe

Thanks for the detailed issue description, I do think clojure-lsp with clj-kondo's help should make that work. @ikitommi we have this issue in clojure-lsp similar to this one, but we are blocked because we need clj-kondo analysis (not only findings, but the analysis feature) to be able to provider autocomplete in clojure-lsp.

ericdallo avatar Jul 08 '23 15:07 ericdallo