I have two questions:
Question 1
I am using the language mode from CodeMirror v5 in CodeMirror v6 by utilizing StreamLanguage.define(new OldLangMode())
Here The OldLangMode class provides functions like token(stream, state), startState(), and copyState() to CodeMirror v6. However, whenever a line is changed, the token() function is called from the very beginning of the document instead of starting from the modified line. This triggers a re-parsing of the entire document from the start.
In CodeMirror v5, the parser knew where to resume parsing. When I set a breakpoint in the token() function, I can see that the line number corresponds to the line where the change occurred.
In CodeMirror v6, is there a way to make the token() function start parsing from the line where the document was changed, rather than re-parsing the entire document from the beginning?
Question 2:
When the document is updated and the token(stream, state) function is called, is it possible to access the most recently updated content? The stream parameter provides access to the current line, but the complex logic in the OldLangMode class (used in CodeMirror v5) requires knowledge of the content of subsequent lines.
In CodeMirror v5, this was achieved using the codemirror.doc object, which allowed calling doc.getLine(n) to retrieve the content of specific lines.
Is there a way to pass this information to the OldLangMode class in CodeMirror v6?
Here is a snippet of what I have tried:
const oldLangMode = new OldLangMode(tokenTable, name);
// Defined a global variable in OldLangMode and updating it here
oldLangMode.setCurrentScript(value);
const extensions = [
languageCompartment.of(StreamLanguage.define(oldLangMode)),
EditorView.updateListener.of((update) => {
if (update.docChanged && onChange) {
const newValue = update.state.doc.toString();
onChange(newValue);
// Update the most recent script when doc changes.
oldLangMode.setCurrentScript(newValue);
update.view.dispatch({
effects: languageCompartment.reconfigure(StreamLanguage.define(oldLangMode)),
});
}
}),
// other extensions...
];
const state = EditorState.create({
doc: value,
extensions,
});
From the code you can probably tell that this is not the right way. I have defined a variable in OldLangMode and updating it whenever the doc changes. My goal was to use that most updated doc in the parsing logic. But the problem is that that update listener gets triggered after the token(stream, state) call is trigerred. So I am calling update.view.dispatch({..}) in the updateListener to retrigger the token() call, so that now I have access to the most recent doc…as you can see I am in a mess.
Any help would be highly appreciated. Thanks.