schemaCompletionSource Example

I have a CodeMirror setup to edit SQL and would like schema auto completion, however I’d like to load the schema in parts after the editor is ready.

Reading the docs it looks like I need to implement schemaCompletionSource, but I can’t figure out where this is added to the setup; can I have a pointer please.

I have:

        this.editor = new EditorView({
            doc: this.value || '',
            extensions: [
                    dialect: MySQL,
                    upperCaseKeywords: true
            parent: this.hostElement.nativeElement,

I’ve tried adding this:

let mysqlLanguage = MySQL;{ schemaCompletionSource: this.schemaSource })
//schemaSource returns { schema: { 'abc.table': [ 'id', 'name' ] } }

and setting the dialect to mysqlLanguage; but (I guess obviously) it does nothing

The easiest way to do this is to add schema: mySchema to the options to sql(). If you want to add this separately, you need to add an extension like{
  autocomplete: schemaCompletionSource({schema: mySchema}))

I agree the easiest way is adding to the options of sql(), but I think I can only do this at initialisation, is that correct?

I’ve tried adding after init using your code, but it doesn’t seem to work - I’ve added to the try page; what’s wrong with this? Thanks!

You’re just creating the extension at the end of the program and then doing nothing with it. To make it affect the editor, you have to add it, via something like .dispatch({effects: StateEffect.appendConfig(myExtension)}).

Still not there, sorry if I’m being slow!!

I’ve added this to the bottom of the script, but it doesn’t seem to have done anything:

  effects: StateEffect.appendConfig.of([
      dialect: mysqlLanguage

Look does nothing except create an extension. If you don’t do anything with the resulting value, it has zero effect anywhere. So put that extension into appendConfig.

Got it! Thank you!

Update the schema using:

let mySchema = { 'abc.person': [ 'id', 'name' ] };
  effects: StateEffect.appendConfig.of({
      autocomplete: schemaCompletionSource({schema: mySchema})