package com.jetbrains.python.run;

import com.intellij.execution.Location;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.execution.junit.RuntimeConfigurationProducer;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.openapi.project.Project;
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 RunnerAndConfigurationSettingsImpl createConfigurationByElement(final Location location, final ConfigurationContext context) {
    mySourceFile = location.getPsiElement().getContainingFile();
    if (mySourceFile == null || mySourceFile.getFileType() != PythonFileType.INSTANCE) {
      return null;
    }

    final Project project = mySourceFile.getProject();
    RunnerAndConfigurationSettingsImpl settings = cloneTemplateConfiguration(project, context);
    PythonRunConfiguration configuration = (PythonRunConfiguration) settings.getConfiguration();
    configuration.setScriptName(mySourceFile.getVirtualFile().getPath());
    configuration.setName(configuration.suggestedName());
    copyStepsBeforeRun(project, configuration);
    return settings;
  }

  public int compareTo(final Object o) {
    return 0;  //To change body of implemented methods use File | Settings | File Templates.
  }
}

@@ -289,22 +289,29 @@
 lij.
-psi.PsiElement
+openapi.module.Module
 ;%0Aim
@@ -332,19 +332,33 @@
 lij.
-psi.PsiF
+openapi.module.ModuleUt
 il
-e
 ;%0Aim
@@ -400,16 +400,85 @@
 roject;%0A
+import com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiFile;%0A
 import c
@@ -1547,24 +1547,220 @@
 tedName());%0A
+    Module module = ModuleUtil.findModuleForPsiElement(location.getPsiElement());%0A    if (module != null) %7B%0A      configuration.setUseModuleSdk(true);%0A      configuration.setModule(module);%0A    %7D%0A
     copyStep

use module SDK in new Python script run configurations