svelte-jsoneditor icon indicating copy to clipboard operation
svelte-jsoneditor copied to clipboard

Autocompletion based on JSON schema

Open rxxy opened this issue 3 years ago • 4 comments
trafficstars

rxxy avatar Aug 13 '22 14:08 rxxy

what?

josdejong avatar Aug 14 '22 12:08 josdejong

image

Like intellij idea, when schema is specified, input 'n' can be completed to 'name'

rxxy avatar Aug 15 '22 14:08 rxxy

Ah, now I understand you, you mean auto-completion based on a JSON Schema. That's not yet supported but would be very nice to have.

Anyone interested in picking this up?

josdejong avatar Aug 16 '22 11:08 josdejong

I need!

developergunny avatar Sep 15 '22 04:09 developergunny

Nice to have

AlexRMU avatar Jun 12 '23 13:06 AlexRMU

Please share more details about autocomplete in svelte-jsoneditor / vanilla-jsoneditor. As I understand, the previous version, jsoneditor, supports some kind of autocomplete, https://codesandbox.io/s/z65fp?file=/src/App.vue. The newer version uses @codemirror/autocomplete somewhere, but there're no examples and there's nothing about autocomplete in the docs.

Our use case is that we have a schema for createAjvValidator with enums, validation works just fine, but we want to have autocomplete functionality both for keys and enum values.

Kasheftin avatar Jun 19 '23 09:06 Kasheftin

@Kasheftin There is no autocompletion for JSON Schema implemented yet in svelte-jsoneditor like there is in jsoneditor, hence this issue. Help would be welcome.

josdejong avatar Jun 20 '23 08:06 josdejong

@josdejong If it could help, I saw this project https://github.com/acao/codemirror-json-schema . They might not have the ctrl-space trick users known from monaco-editor but it is better than nothing from what I saw on https://codemirror-json-schema.netlify.app/.

image

They seem to have documented something for custom usage that could be added to your .create call

// This approach allows you to configure the json mode and parse linter, as well as our linter, hovers, etc more specifically.

import { EditorState } from "@codemirror/state";
import { linter } from "@codemirror/lint";
import { hoverTooltip } from "@codemirror/view";
import { json, jsonLanguage, jsonParseLinter } from "@codemirror/lang-json";
import { jsonCompletion jsonSchemaLinter, jsonSchemaHover } from "codemirror-json-schema";

const schema = {
  type: "object",
  properties: {
    example: {
      type: "boolean",
    },
  },
};

const state = EditorState.create({
  doc: `{ "example": true }`,
  extensions: [
    json(),
    linter(jsonParseLinter(), {
      // default is 750ms
      delay: 300
    }),
    linter(jsonSchemaLinter(schema)),
    jsonLanguage.data.of({
      autocomplete: jsonCompletion(schema),
    }),
    hoverTooltip(jsonSchemaHover(schema)),
  ];
})

Notice : It has currently an open MR called ".js file imports and schema loading as statefield" so that you don't have to provide the schema in the create step

jy95 avatar Oct 22 '23 21:10 jy95

Thanks for sharing @jy95 , this can be very useful indeed!

Anyone interested in working this out for svelte-jsoneditor?

josdejong avatar Oct 25 '23 10:10 josdejong

@josdejong I set up a stackblitz for POC that is uncomplete for the time being , mostly because I am not used to the prop drilling in Svelte codebase (export let schema: any).

jy95 avatar Nov 01 '23 17:11 jy95

Thanks @jy95!

josdejong avatar Nov 08 '23 07:11 josdejong

@josdejong In the meantime, codemirror-json-schema got one interesting release . What do you think ?

extensions: [
  json(),
  linter(jsonParseLinter()),
  linter(jsonSchemaLinter(), {
    needsRefresh: handleRefresh,
  }),
  jsonLanguage.data.of({
    autocomplete: jsonCompletion(),
  }),
  hoverTooltip(jsonSchemaHover()),
  // this is where we pass the schema!
  // very important!!!!
  stateExtensions(schema),
];

jy95 avatar Nov 11 '23 14:11 jy95

That looks very promising indeed! Either to just use codemirror-json-schema, or to see how they solved autocompletion.

josdejong avatar Nov 15 '23 07:11 josdejong

Moving this idea to the Discussions > Ideas section since we are not actively working on it. Help implementing this would be very welcome.

josdejong avatar Dec 13 '23 08:12 josdejong