Decoration Set Creation

Hi, I’m creating a new Decoration set from an array of Decorations, and I had a couple of questions regarding this.

Here is my array of Decorations that I created from some positions of interest:

Decoration {from: 3, to: 9, type: InlineType}
Decoration {from: 9, to: 17, type: InlineType}
Decoration {from: 17, to: 25, type: InlineType}
Decoration {from: 25, to: 33, type: InlineType}
Decoration {from: 33, to: 41, type: InlineType}

And here is what I get after doing let decorationSet = DecorationSet.create(tr.doc, newDecorations) in a plugin and fetching the decorations from the new set, decorationSet.find():

Decoration {from: 9, to: 17, type: InlineType}
Decoration {from: 17, to: 25, type: InlineType}
Decoration {from: 25, to: 33, type: InlineType}
Decoration {from: 33, to: 41, type: InlineType}
Decoration {from: 3, to: 9, type: InlineType}

So the first decoration now appears last, and from skimming the source code and how the DecorationSet is implemented using a tree, I would think that the array should be ordered, which is essential to how I plan to iterate through the decorations in my use case. I could sort the results from .find() of course but I wanted to check if this is an intended behavior.

One note that may be relevant is that the decoration from 3 to 9 is inside of a node (as in 3 is the $post.start() and 9 is $pos.end()), and then the rest have the from at the end of the previous node and the to is at the end of the currentNode. An example is diagrammed below where I is a decoration position, and are opening and closing node tokens.

<w>Iword II</w><w>word II</w><w>word I</w>

1 Like

No, it’s not, and find doesn’t guarantee ordering (by design, to avoid an expensive sort). The tree structure is only partially sorted, so it doesn’t provide a trivial way to iterate in order.

1 Like

I see, that makes sense. Thank you!