execute callback for each selection


Is there a way to execute a callback for each selection (in order) when multiple ranges selected?

I.e. the same behavior as in ace editor’s ace.forEachSelection( cb ), which to my knowledge executes the callback for each selection in order, removing (hiding) & restoring the other selections so that when the callback is invoked, the editor state is as if only one range is selected.

The closest API I could find in cm is replaceSelections, but that one requires an array of strings to be supplied.


No, that doesn’t exist.


IMO it’s necessary in order to implement more advanced multiselection commands (well technically not necessary, but it drastically simplifies workflow). Would you be interested in PR/addon?

WTBS, for an addon, I guess I’d at least need an access to computeSelAfterChange helper which is not exposed if I’m not mistaken.

Proposed algo:

  1. before iteration, store all active ranges
  2. deselect all except ranges[i] range
  3. execute callback with ranges[i]
  4. keep track of changes being done inside the callback, and shift inactive ranges via computeSelAfterChange
  5. recache current range that may have been modified in the callback
  6. repeat from (2)
  7. restore cached ranges