Equivalent of getStateAfter in CM6

What is the equivalent of calling cm.getStateAfter(null, true) when migrating from CodeMirror 5 to CodeMirror 6, using StreamParser?

Thanks for a great library!

This doesn’t exist anymore, since parsers work differently now, and the stream parser doesn’t export a feature like this. What were you using it for?

I’ve been using CodeMirror 5 for command line parsing, which I understand is quite different from the intended use. But it has been working quite well, with the exception of lack of spell check (which is why I try to migrate).

Example:

codemirror_console

My custom mode parses the command to both highlighting it with color, but also to interpret and store the parsed parts in the state object. Later (when pressing Enter), I retrieve the state with getStateAfter and use it to execute the command.

In the screenshot example above, the state would, at the end of the last token (which is “Creator of the realm”) contain something similiar to:

{
    cmd: "setTag",
    tagName: "creator",
    tagAttr: "desc",
    desc: "Creator of the realm"
}

A different use case, I know :slight_smile:

In CM6, I have seen that the state does exist inside the syntax tree:

syntaxTree(editorState).resolve(editorState.doc.length)).node.children[0].props[11]

Now… that line of code is clearly not very useful (see the “arbitrary” 11), but at least it is there. But how to get it in a correct way?

I think I’ve gotten it to work by replicating the internal findState function in @codemirror/stream-parser:

Hopefully I haven’t misunderstood anything important :sweat_smile:

The recommended way to do this in CM6 would be to switch to a Lezer-based parser, and see what nodes exist in the parse tree (see syntaxTree) of the line you’re interested in. You could also look at the tree emitted by the stream parser, though that’ll be flat and somewhat harder to interpret.

1 Like