Module name in language-data

Hi Marijn,
I’m building a language list inside a CM based editor:

To link the module name to the language I’ve added a module field to language-data. See the code below.

Would you be interested in adding something like this? Happy to provide as PR. Or should I fork instead?

function ofLegacy(spec) {
  let ld: LanguageDescription
  if (!spec.load) {
    if (spec.loadName) {
      const name: string = spec.loadName.toLowerCase()
      spec.load = () => import("@codemirror/legacy-modes/mode/" + name).then(m => legacy(m[spec.loadName]))
    }
    else {
      const name: string = spec.name.toLowerCase()
      spec.load = () => import("@codemirror/legacy-modes/mode/" + name).then(m => legacy(m[name]))
    }
  }
  ld = LanguageDescription.of(spec)
  ld.module = "@codemirror/legacy-modes"
  return ld
}

function of(spec) {
  let ld: LanguageDescription
  const module: string = spec.module || ("@codemirror/lang-" + spec.name.toLowerCase())
  if (!spec.load) {
    if (spec.loadName)
      spec.load = () => import(module).then(m => m[spec.loadName]())
    else
      spec.load = () => import(module).then(m => m[spec.name.toLowerCase()]())
  }
  ld = LanguageDescription.of(spec)
  ld.module = module
  return ld
}

/// An array of language descriptions for known language packages.
export const languages = [
  // New-style language modes
  of({
    name: "C",
    extensions: ["c","h","ino"],
    module: "@codemirror/lang-cpp",
    loadName: "cpp"
  }),
  //...
  ofLegacy({
    name: "APL",
    extensions: ["dyalog","apl"],
  }),
  ofLegacy({
    name: "PGP",
    alias: ["asciiarmor"],
    extensions: ["asc","pgp","sig"],
    loadName: "asciiArmor"
  }),
// etc

This sounds similar to @codemirror/language-data, except that you’re also including packages not directly maintained by me. I’ve considered maintaining such a list, but I decided that I don’t really want to take on the work of keeping it up to date (and verifying fitness of purpose and continued non-maliciousness of all the linked packages).

Ah no, all I want is to add a module field to the languages that are already in language-data.

This module field will simply contain the module name, like “@codemirror/lang-cpp” for C.

The other languages in the image are just examples. That info will be maintained elsewhere.

Oh sorry, I didn’t read your post very closely. This doesn’t seem a generally useful addition (use cases where the package name is relevant seem rather rare), so I’d say I don’t want this in @codemirror/language-data. You may not need to fork, you could just keep a mapping from language names in that list to package names and combine that with the objects from my package.

1 Like