Test code - how to fold line with custom rangeFinder?

I tried to test the custom folding by the rangeFinder function to foldOptions (see below for setup), but it doesn’t work because onGutterClick function checks only the options of cm.state.foldGutter (which doesn’t take the foldOptions into account). How to do it right in a simple way, with restrictions as below?

My sample code executed on CodeMirror: Code Folding Demo page - pasted in dev console, also with test line <ref>test1</ref><ref>test2</ref><ref>test3</ref> in editor_html window. I wanted (for testing purposes) to fold entire line into <ref>...</ref> [to the first ref and the last ref]):

window.editor_html.options.foldOptions={
  rangeFinder: (cm, pos) => {
    var line=window.editor_html.getLine(pos.line);
    var match=line.match(/<ref>(.*)<\/ref>/);
    if (match==null) {
      return CodeMirror.fold.auto(cm,pos);
    }
    else {
      match=match[1];
      var startPos=CodeMirror.Pos(pos.line, match.index);
      var endPos=CodeMirror.Pos(pos.line, match.index+match.length);
      return endPos && CodeMirror.cmpPos(endPos, startPos) > 0 ? {from: startPos, to: endPos} : null;
    }
  }
}

doFold uses getOption, which does take foldOptions into account.

Sorry, I was in a hurry when I wrote the above question and wasn’t precise enough.

I try to fold the line by clicking where the gutter should be - it invokes onGutterClick, which reads options from cm.state.foldGutter (it doesn’t contain custom rangeFinder from foldOptions).
So I guess I need to fold the line by directly calling the doFold function, or somehow propagate custom rangeFinder into cm.state.foldGutter (don’t really know how).

I also tried to assign the same to window.editor_html.options.foldGutter, with the same result as above.

And then calls through to foldCode which will fill in options missing in the object it is passed from foldOptions.

I did the debugging more carefully this time, and of course you’re right.
Corrections that I needed to do to make it work:

  • in regex: var match=line.match(/(?<=<ref>).*(?=<\/ref>)/);
  • in else:
//removed match=match[1];
...
var endPos=CodeMirror.Pos(pos.line, match.index+match[0].length);