Hi,
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.
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)}).
Look mysqlLanguage.language.data.of 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.
Thanks for this example. If I wanted to update the schema – replacing the previous schema entirely – how would I do that? I tried putting it in a timeout here but the original schema remains. I think I could use StateEffect.reconfigure but I would have to load my original state tree. Is there a better way of doing that where I’m just targeting this one extension?
Thanks that’s really helpful. Here’s what I put together in case it helps someone else. There is likely an improvement that can be made where I’m not having to re-instantiate the entire sql extention and I just adjust the schema. Any suggestions welcome!
import {basicSetup, EditorView} from "codemirror"
import {StateEffect, Compartment} from '@codemirror/state';
import {sql, MySQL, schemaCompletionSource} from "@codemirror/lang-sql"
let mysqlLanguage = MySQL;
let comp = new Compartment
let mySchema = { 'my_person': [ 'my_id', 'my_name' ] };
let editor = new EditorView({
doc: "SELECT * FROM my_",
extensions: [
basicSetup,
comp.of(sql({
dialect: mysqlLanguage,
schema: mySchema
}))
],
parent: document.body
})
setTimeout(() => {
alert('resetting')
let otherSchema = { 'other_person': [ 'other_id', 'other_name' ] };
editor.dispatch({
effects: comp.reconfigure(sql({
dialect: mysqlLanguage,
schema: otherSchema
}))
})
}, 5000)