ensureSyntaxTree does not give tree but it's present in editorView.state.tree.

hi i’m using the following to get the tree syntax.

const tree: Tree = ensureSyntaxTree(editorView.state, editorView.state.doc.length) 

this does not work always and i have to write this to make sure the tree is there.

const tree: Tree = ensureSyntaxTree(editorView.state, editorView.state.doc.length) ?? (editorView.state.tree);

However there is not EditorState.tree proerty attached in types but is there at runtime.
Thanks for help :slight_smile:

Yes, that is expected. As document, ensureSyntaxTree will only return a tree if it was able to finish parsing up to the position you gave it. state.tree (or rather syntaxTree(state)) will always have a tree, but if the state is behind on parsing, that may not cover the entire document.

Thanks for your reply. I have two followups

  • can i somehow wait for it to complete?
  • Can i depend on editorView.state.tree ? to be there as part of api contract since it doesn’t apprear in types or documentation.
    Regards

Have you read the documentation for this function at all? The third argument allows you to specify how long you’re prepared to wait.

No you cannot. Use syntaxTree(state), which will return the same object.

Thanks for quick reply. I was focused on the fact that i want full parsed tree and want to wait for that.
Have a great day. :slight_smile:

hi sorry to bother again,

ensureSyntaxTree(editorView.state, editorView.state.doc.length,20000) ?? syntaxTree(editorView.state)

even with high timeout none of these work. it always return null.

syntaxTree returns null? That’s not really possible. Can you show a minimal piece of example code that shows this happening?

hi sorry for late reply.
I tried to reproduce but the thing works fine on code sandbox
And the setup i use has layers upon layers of company package it’s really hard to give better debugging information.
But upon debugging i’ve found in case of the correct scenario i’m getting this as fields data


but in my implementation i’m getting this array

i can understand if you think it’s not worth the time to debug this for you and i’ll find another way if you believe this is from our side