From f0d191b04461a50684bdd23fec7492a9aff0ebc2 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Thu, 4 May 2017 17:23:07 +0300 Subject: [PATCH] [groovy] console: include imports in command if they are not selected (IDEA-172217) --- .../actions/GrExecuteCommandAction.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/actions/GrExecuteCommandAction.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/actions/GrExecuteCommandAction.java index 6dc61ad854ee..fa25bf7eddcc 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/console/actions/GrExecuteCommandAction.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/console/actions/GrExecuteCommandAction.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,10 @@ import com.intellij.openapi.fileEditor.FileDocumentManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; import org.jetbrains.plugins.groovy.console.GroovyConsole; +import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; +import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement; public class GrExecuteCommandAction extends AnAction { @@ -45,7 +48,24 @@ public class GrExecuteCommandAction extends AnAction { final Document document = editor.getDocument(); final TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor); - final String command = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange)); + 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(); + } final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE); if (existingConsole == null) {