package com.fwdekker.randomness;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;


/**
 * Inserts a randomly generated string at the position of the event's editor's caret.
 */
public abstract class InsertRandomSomething extends AnAction {
    /**
     * Inserts the string generated by {@link #generateString()} at the caret in the editor.
     *
     * @param event the performed action
     */
    @Override
    public void actionPerformed(final AnActionEvent event) {
        final Project project = event.getData(CommonDataKeys.PROJECT);
        final Editor editor = event.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            return;
        }
        final Document document = editor.getDocument();
        final SelectionModel selectionModel = editor.getSelectionModel();

        final int start = selectionModel.getSelectionStart();
        final int end = selectionModel.getSelectionEnd();

        final Runnable runnable = () -> document.replaceString(start, end, generateString());
        WriteCommandAction.runWriteCommandAction(project, runnable);
    }


    /**
     * Generates a random string.
     *
     * @return a random string
     */
    protected abstract String generateString();
}

package com.fwdekker.randomness;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;


/**
 * Inserts a randomly generated string at the position of the event's editor's caret.
 */
public abstract class InsertRandomSomething extends AnAction {
    /**
     * Inserts the string generated by {@link #generateString()} at the caret in the editor.
     *
     * @param event the performed action
     */
    @Override
    public void actionPerformed(final AnActionEvent event) {
        final Editor editor = event.getData(CommonDataKeys.EDITOR);
        if (editor == null) {
            return;
        }
        final Project project = event.getData(CommonDataKeys.PROJECT);
        final Document document = editor.getDocument();
        final CaretModel caretModel = editor.getCaretModel();

        caretModel.getAllCarets().forEach(caret -> {
            final int start = caret.getSelectionStart();
            final int end = caret.getSelectionEnd();

            final Runnable runnable = () -> document.replaceString(start, end, generateString());
            WriteCommandAction.runWriteCommandAction(project, runnable);
        });
    }


    /**
     * Generates a random string.
     *
     * @return a random string
     */
    protected abstract String generateString();
}

Support multiple carets