void (@NotNull AnActionEvent e) { final Project project = e.getProject(); final Editor editor = e.getData(CommonDataKeys.EDITOR); final VirtualFile virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE); if (project == null || editor == null || virtualFile == null) return; FileDocumentManager.getInstance().saveAllDocuments(); final Document document = editor.getDocument(); final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor); final String command; if (selectedRange.isEmpty()) { command = document.getText(); // whole document } else { StringBuilder commandBuilder = new StringBuilder(); PsiFile file = e.getData(CommonDataKeys.PSI_FILE); if (file instanceof GroovyFile) { GrImportStatement[] statements = ((GroovyFile)file).getImportStatements(); for (GrImportStatement statement : statements) { if (!selectedRange.contains(statement.getTextRange())) { commandBuilder.append(statement.getText()).append("\n"); } } } commandBuilder.append(document.getText(selectedRange)); command = commandBuilder.toString(); } GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command)); }