Detect Language

Any way to check the state’s language facet and determine which language is active?

For instance: state.facet(language) returns the LRLanguage but that doesn’t really contain any identifying information other than directly comparing to a language.

import { language } from '@codemirror/language';
import { cssLanguage } from '@codemirror/lang-css';
import { htmlLanguage } from '@codemirror/lang-html';

function whatLanguage(state){
  let stateLanguage = state.facet(language);
  if ( stateLanguage === cssLangauge ) { ... }
  else if ( stateLanguage === htmlLanguage ) { ... }

That’s cumbersome but kinda works.

However, two cases that would be great to support:

  1. If you’re lazy loading languages via the language-data pack. You don’t directly have a LRLanguage to compare to.
  2. “sub” languages and their parent language: TypeScript, Less, Sass, etc.

Perhaps some metadata on the LRLanguages?

Related Issue: Emmet Plugin can’t detect supported languages

Indeed, that’s not something languages provide. But it should be workable to set up your code that adds the language to also add some custom facet that stores a language name alongside it.

I do have my own custom facet that stores a usable reference to what language is active for my own purposes, however that doesn’t much help in terms of a general extension like Emmet.

For the author of Emmet, are there any reasonable ways to detect what language is in use to allow for selectively enabling/disabling the extension’s features?

Comparing in a way that considers sub-languages equivalent can be done by comparing (which is copied over to reconfigured versions of a language). For doing the test without having access to the actual language objects, I don’t have a solution.