[groovy] console: include imports in command if they are not selected

(IDEA-172217)
This commit is contained in:
Daniil Ovchinnikov
2017-05-04 17:30:23 +03:00
parent 833aaadccc
commit f0d191b044
@@ -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) {