/*
 * Copyright 2000-2016 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.groovy.console.actions;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.util.EditorUtil;
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 org.jetbrains.plugins.groovy.console.GroovyConsole;

public class GrExecuteCommandAction extends AnAction {

  public GrExecuteCommandAction() {
    super(AllIcons.Toolwindows.ToolWindowRun);
  }

  @Override
  public void actionPerformed(AnActionEvent e) {
    final Project project = e.getProject();
    final Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    final VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
    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 = (selectedRange.isEmpty() ? document.getText() : document.getText(selectedRange));

    final GroovyConsole existingConsole = virtualFile.getUserData(GroovyConsole.GROOVY_CONSOLE);
    if (existingConsole == null) {
      GroovyConsole.getOrCreateConsole(project, virtualFile, console -> console.execute(command));
    }
    else {
      existingConsole.execute(command);
    }
  }
}

@@ -21,9 +21,9 @@
 -201
-6
+7
  Jet
@@ -1186,16 +1186,49 @@
 alFile;%0A
+import com.intellij.psi.PsiFile;%0A
 import o
@@ -1277,16 +1277,158 @@
 Console;
+%0Aimport org.jetbrains.plugins.groovy.lang.psi.GroovyFile;%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
 %0A%0Apublic
@@ -2128,18 +2128,24 @@
  command
- =
+;%0A    if
  (select
@@ -2165,65 +2165,654 @@
 ty()
- ? document.getText() : document.getText(selectedRange));
+) %7B%0A      command = document.getText(); // whole document%0A    %7D%0A    else %7B%0A      StringBuilder commandBuilder = new StringBuilder();%0A      PsiFile file = e.getData(CommonDataKeys.PSI_FILE);%0A      if (file instanceof GroovyFile) %7B%0A        GrImportStatement%5B%5D statements = ((GroovyFile)file).getImportStatements();%0A        for (GrImportStatement statement : statements) %7B%0A          if (!selectedRange.contains(statement.getTextRange())) %7B%0A            commandBuilder.append(statement.getText()).append(%22%5Cn%22);%0A          %7D%0A        %7D%0A      %7D%0A      commandBuilder.append(document.getText(selectedRange));%0A      command = commandBuilder.toString();%0A    %7D
 %0A%0A  

include imports in command if they are not selected (IDEA-172217)