Iterating over all atomicRanges widgets

There are different ways of accessing properties of the state: memory efficient and via cursors/iterators.

How costly is it (internally) to go over 20-50 widgets every change/update using .iter ?

   const rangeSets = view.state.facet(EditorView.atomicRanges).map((f)=>f(view));
    for (let i=0; i<rangeSets.length; ++i) {
      if (rangeSets[i].size > 0) {
          const cursor = rangeSets[i].iter();
          let v;
          do {
            v = cursor.value;
            if (!v) break;
            //get some properties of v.widget...
            cursor.next();
          } while(v);
      }
    };

Or should I bypass facet method and go directly to values of editor view? I believe it is probably considered as a bad practice…

Thanks!

That should be absolutely fine.

And no, you really don’t want to reach into internal representations that aren’t documented. Those can change on any release.

1 Like

Thanks, @marijn !

Now I also noticed if I query atomicRanges from another view plugin during update or first creation the list is empty. Only if I trigger the request during viewport change, then there is something.

I though may be atomic ranges are not yet created, so I lowered the priority of my view plugin, which gets all atomic ranges widgets using Prec.lowest, however it did not help.

Can you set up a tiny example program that shows what you are doing precisely?

While I was setting up a minimal example in the playground, I had realized that it was mistake.
I was checking DOMElement of point decorations widgets, while during update/construction, but it is immediately available, since toDOM is delayed.

These are all related to the discussion An efficient way of decorating all parenthesis in a line - #4 by JerryI

This method

const rangeSets = view.state.facet(EditorView.atomicRanges).map((f)=>f(view))

in the end works perfectly combined with iterating over the existing syntax tree. I just defer the measurements of the DOM element offsetHeight, when they are available.

The result is fantastic!
it all also works nicely for the nested editors as well

Thanks for such efficient and robust architecture of CodeMirror 6. I bet it would not be possible with any other modular editor on the market :slight_smile: