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

Custom value autocompletion #67

Open
mindofmatthew opened this issue Oct 17, 2023 · 3 comments
Open

Custom value autocompletion #67

mindofmatthew opened this issue Oct 17, 2023 · 3 comments

Comments

@mindofmatthew
Copy link

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
Copy link
Owner

acao commented Oct 20, 2023

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
Copy link
Author

mindofmatthew commented Oct 20, 2023

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
Copy link
Owner

acao commented Oct 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants