Is there a way to access the index of the cursors when multiple selection cursors are active?
Example :
Say I had the following text, with 5 cursors
lo|rem
ip|sum
do|lor
si|t
am|et
With access to the index of the cursors, I could easily make turn it into
lo1rem
ip2sum
do3lor
si4t
am5et
You can do this with a plugin by simply iterating over each caret/cursor from getCaretModel().getAllCarets()
and inserting a running index. The getAllCarets()
method always returns carets sorted by visual order:
public class CaretIndexAction extends AnAction {
public CaretIndexAction() {
super("Insert Caret Index(es)");
}
public void actionPerformed(AnActionEvent event) {
Editor editor = PlatformDataKeys.EDITOR.getData(event.getDataContext());
Document doc = editor.getDocument();
WriteCommandAction.runWriteCommandAction(event.getProject(), () -> {
int i = 1;
for (Caret c : editor.getCaretModel().getAllCarets()) {
doc.replaceString(c.getSelectionStart(), c.getSelectionEnd(), String.valueOf(i));
i++;
}
});
}
}
Result:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With