Changing editor language after the fact?

With editor views being state based, it makes a lot of sense to have a single editor view and then change its content based on “which file is supposed to be active”, but I can’t seem to find any information on how to change the view’s language features, e.g. when switching from a file called “index.js” to a file called “index.css”, keeping the JS syntax highlighting etc. results in rather the wrong behaviour. Is there a simple way to switch features, short of just building “a new editor view” with its own extensions property?

Build a new editor state and use setState on your view.

Hm, and there’s nothing to just update language features but leave everything else untouched? (e.g. basic setup, document change listening, etc. would all need to be “redone” too if it’s a full state replacement)

There’s not much to redo. You just use the same code that created the set of extensions for the original state to create the set of extensions for the new state (except for the language-specific parts). You can dynamically reconfigure a state too, but if you’re loading a new document, creating a new state makes more sense (you don’t want to carry over undo history and similar).

Note that we’re not just talking new documents, we’re also talking file renames, e.g. someone starts a sketch for some code in a code block in a file called, then realize it makes more sense to rename the file to “test.js” and work on it properly. That file should now use JS language features but should definitely not be a new editor (or fresh state), the edit history should definitely stay available for undo/redo.