feat: foldable add ensure to use ensureSyntaxTree #8

Closed
CahierX wants to merge 1 commit from main into main
CahierX commented 2025-08-02 10:48:04 +02:00 (Migrated from github.com)

When I use ensureSyntaxTree to get the complete syntax tree, I want to fold all regions. So I call foldable to get the from and to positions. However, foldable internally still uses syntaxTree, which may return an incomplete tree, resulting in inaccurate foldRanges.

Therefore, I added a fourth parameter ensure to foldable, allowing it to internally use ensureSyntaxTree.

This is my fold code, It does not work normally, if add ensure, it can work normally

function foldAllRecursive(view: EditorView) {
  const state = view.state;

  const foldRanges: { from: number, to: number }[] = [];
  ensureSyntaxTree(state, state.doc.length, Infinity)iterate({
    enter(node) {
      const isFoldable = foldable(state, node.from, node.to)
      if (isFoldable) {
        foldRanges.push({ from: isFoldable.from, to: isFoldable.to });
      }
    }
  });

  view.dispatch({
    effects: foldRanges.map(range => foldEffect.of({ from: range.from, to: range.to }))
  });
}
When I use `ensureSyntaxTree` to get the complete syntax tree, I want to fold all regions. So I call `foldable` to get the from and to positions. However, `foldable` internally still uses `syntaxTree`, which may return an incomplete tree, resulting in inaccurate foldRanges. Therefore, I added a fourth parameter `ensure` to `foldable`, allowing it to internally use `ensureSyntaxTree`. This is my fold code, It does not work normally, if add ensure, it can work normally ```javascript function foldAllRecursive(view: EditorView) { const state = view.state; const foldRanges: { from: number, to: number }[] = []; ensureSyntaxTree(state, state.doc.length, Infinity)iterate({ enter(node) { const isFoldable = foldable(state, node.from, node.to) if (isFoldable) { foldRanges.push({ from: isFoldable.from, to: isFoldable.to }); } } }); view.dispatch({ effects: foldRanges.map(range => foldEffect.of({ from: range.from, to: range.to })) }); } ```
marijnh commented 2025-08-02 17:59:58 +02:00 (Migrated from github.com)

I suggest you use forceParsing here, to move the state forward before you look for foldable ranges. Adding an extra boolean parameter that makes finding a fold range potentially take a large amount of time seems too messy and risky.

I suggest you use [`forceParsing`](https://codemirror.net/docs/ref/#language.forceParsing) here, to move the state forward before you look for foldable ranges. Adding an extra boolean parameter that makes finding a fold range potentially take a large amount of time seems too messy and risky.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
codemirror/language!8
No description provided.