package com.jetbrains.python.run;

import com.intellij.execution.Location;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.junit.RuntimeConfigurationProducer;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.python.PythonFileType;

/**
 * @author yole
 */
public class PythonRunConfigurationProducer extends RuntimeConfigurationProducer implements Cloneable {
  private PsiFile mySourceFile = null;

  public PythonRunConfigurationProducer() {
    super(PythonConfigurationType.getInstance());
  }

  public PsiElement getSourceElement() {
    return mySourceFile;
  }

  protected RunnerAndConfigurationSettings createConfigurationByElement(final Location location, final ConfigurationContext context) {
    PsiFile script = location.getPsiElement().getContainingFile();
    if (script == null || script.getFileType() != PythonFileType.INSTANCE) {
      return null;
    }
    Module module = ModuleUtil.findModuleForPsiElement(script);
    if (module != null) {
      for (RunnableScriptFilter f : Extensions.getExtensions(RunnableScriptFilter.EP_NAME)) {
        if (f.isRunnableScript(script, module)) {
          return null;
        }
      }
    }
    mySourceFile = script;

    final Project project = mySourceFile.getProject();
    RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(project, context);
    PythonRunConfiguration configuration = (PythonRunConfiguration) settings.getConfiguration();
    final VirtualFile vFile = mySourceFile.getVirtualFile();
    if (vFile == null) return null;
    configuration.setScriptName(vFile.getPath());
    final VirtualFile parent = vFile.getParent();
    if (parent != null) {
      configuration.setWorkingDirectory(parent.getPath());
    }
    configuration.setName(configuration.suggestedName());
    if (module != null) {
      configuration.setUseModuleSdk(true);
      configuration.setModule(module);
    }
    return settings;
  }

  public int compareTo(final Object o) {
    return PREFERED;
  }
}

@@ -256,16 +256,67 @@
 oducer;%0A
+import com.intellij.openapi.extensions.Extensions;%0A
 import c
@@ -346,24 +346,24 @@
 ule.Module;%0A
-
 import com.i
@@ -470,37 +470,32 @@
 openapi.
-extensions.Extensions
+util.io.FileUtil
 ;%0Aimport
@@ -598,24 +598,24 @@
 si.PsiFile;%0A
-
 import com.j
@@ -645,16 +645,58 @@
 ileType;
+%0Aimport org.jetbrains.annotations.NotNull;
 %0A%0A/**%0A *
@@ -2366,24 +2366,933 @@
 tings;%0A  %7D%0A%0A
+  @Override%0A  protected RunnerAndConfigurationSettings findExistingByElement(Location location,%0A                                                                 @NotNull RunnerAndConfigurationSettings%5B%5D existingConfigurations,%0A                                                                 ConfigurationContext context) %7B%0A    PsiFile script = location.getPsiElement().getContainingFile();%0A    if (script == null) %7B%0A      return null;%0A    %7D%0A    final VirtualFile vFile = script.getVirtualFile();%0A    if (vFile == null) %7B%0A      return null;%0A    %7D%0A    String path = vFile.getPath();%0A    for (RunnerAndConfigurationSettings configuration : existingConfigurations) %7B%0A      final String scriptName = ((PythonRunConfiguration)configuration.getConfiguration()).getScriptName();%0A      if (FileUtil.toSystemIndependentName(scriptName).equals(path)) %7B%0A        return configuration;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A
   public int

implement findExistingByElement() for Python script run configuration (PY-324)