Questions on Facet -> of

I’m trying to understand the code and am struggling with the following documentation

Facet → of says

Returns an extension that adds the given value to this facet

In the code it returns a FacetProvider - and from what I can tell FacetProvider doesn’t interact with its facet other than hold a reference to it.

So I’m wondering two things

  1. What does “Returns an extension that adds the given value to this facet” mean ? i.e. where in the code is FacetProvider adding the value to the facet ?

  2. What does it mean for an extension to add a value to a facet ? If a Facet “is a labeled value that is associated with an editor state” and we’re supposed to be using facets to compute a value based off extensions as inputs, then does adding a value mean we’re initializing or setting the facet’s value ? The phrase “add a value” also confuses me - it’s my understanding the facet returns a single value, so what does it mean to add one ?

  1. What does “Returns an extension that adds the given value to this facet” mean ? i.e. where in the code is FacetProvider adding the value to the facet ?

It does exactly what it says, “an extension that adds the given value to the facet”. Facet can hold multiple input values, which you can input like this:

EditorState.create({
  extensions: [
    EditorState.tabSize.of(2),
    EditorState.tabSize.of(4),
    EditorState.tabSize.of(8)
  ]
})

tabSize is a facet, which in this particular state holds 3 values (2, 4, 8). The facet takes multiple input values and is configured to return a single, coherent output value (that’s what Facet.define() is for). Obviously you can’t use multiple tab sizes at once, so somehow the end size must be decided. That’s what tabSize facet is doing - it’s defined to chose one of them (don’t remember whether it choses the first or the last one).

Facets don’t have to pick one value. Other facets can merge them - for example multiple extensions can provide multiple keybindings, and keymap facet combines them all into big map of key mappings that are then later used.

Think of them like this: multiple extensions can configure a value - how do you decide which value to use? That’s what facet is doing - it accepts multiple values, and combines them to one that can be used in the library.

More info here: CodeMirror System Guide

1 Like

Thanks much for your help

Facet can hold multiple input values

So are you saying “an extension that adds the given value to the facet” is saying it’s adding an input value ? If so then I’ll create a PR to update that line because I don’t read “value” as “input value” there. It’s also confusing since it’s worded here as if it adds the value to the facet - where the facet says the extension itself is the input. So an extension adding the value as input feels inconsistent.

and I think that resolves my question #2, as adding an input makes sense considering the facet is designed to have multiple inputs.

where the facet says the extension itself is the input.

Where did you read that? Can you share a link?

Did you try reading https://codemirror.net/docs/guide/#facets?

Where did you read that

I was referring to this note in the Facet reference

It takes inputs from any number of extensions

Which I now realize is my own misunderstanding as an input from an extension doesn’t mean the extension is the input. Thanks for pointing that out.

regarding my question

So are you saying … it’s adding an input value ?

I’ll assume that’s correct

I think the docs are correct in that combining the “input” values into one “output” value is optional in Facet.define. For example, the updateListener facet just takes every value (a function) given to it, and iirc it doesn’t do any kind of combination to a single output. So anything provided to it is indeed a final value.

It’s supposed to be an Input value - I looked at the reference again yesterday and the type assigned is Input. I was too focused on the words below to notice the type.

Because of that, I didn’t create a PR. If I’m the only person to get hung up on it then a one word PR isn’t worth the maintainer’s time