jsonnext / codemirror-json-schema

A JSONSchema enabled mode for codemirror 6, for json4 and json5, inspired by monaco-json
https://codemirror-json-schema.netlify.app
MIT License
57 stars 9 forks source link

Custom value autocompletion #67

Open mindofmatthew opened 9 months ago

mindofmatthew commented 9 months ago

It would be great to have the ability to specify custom autocompletion sources for specific values. These sources could either be associated with a specific JSON Schema format tag, or specified as an array of completion functions that would receive the schema for the given property.

I'd be interested in implementing this as a PR, but wanted to reach out, as I'm still figuring out how CM6 autocompletions work, much less how they're specifically set up in this extension.

Perhaps something like this?

type JSONCompletionSource = (schema: JSONSchema7, ctx: CompletionContext) => CompletionResult | Promise <CompletionResult | null> | null;

type JSONCompletionOptions = {
  mode?: "json" | "json5";
  custom?: JSONCompletionSource[];
};
acao commented 9 months ago

I'm trying to understand your case better, is there a reason this can't be acheived with json schema? is there a remotely defined json schema definition that's not loading?

mindofmatthew commented 9 months ago

My specific use-case is a configuration object where certain fields are file paths on the users' system. In this case, autocompletion of file paths (plus theoretical linting of missing files, etc) would be awesome.

I'm not super well-versed in JSON Schema, but it seems like the way to represent these semantics would be through a custom format field, which can be used for implementation-specific validation or annotation of fields. json-schema-library allows custom validators for this purpose, though I'm not sure if that's how you'd want to approach it.

Perhaps the better approach would be a separate autocompletion source extension that combines with/overrides the standard JSON Schema autocompletion using the usual CM6 mechanisms for combining extensions. For that, is there already a way to query relevant JSON Schema information about a given code range or syntax node?

acao commented 9 months ago

format could be used, but for example, generating a jsonschema enum on the fly might work better. for your case it could just resolve the results of readdir() on complete directory paths as you type, though we would need to provide some API callback to let you hook into our autocomplete without needing to write your own, then you could be provided with a functional interface to append to the autocomplete options, because you would need the input value to generate your results, rather than providing it as source to the mode invocation config. #63 improves the dynamic schema state changes so you will just be able to call updateSchema() every time you change the incoming schema value, and codemirror's state field will update without reloading the editor

similarly, for example, with monaco-graphql, we feed dynamically generated json schema to monaco-json based on user input

If you choose the additional autocomplete idea, which is also sound, I wonder if we can't provide additional API hooks to make it easier to author your own completion logic for certain cases 🤔 there has to be a more codemirror 6 appropriate way to solve this problem. I will open a ticket on the cm6 forum to ask the authors what they think is ideal - I'm sure other language modes have come across this problem.