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.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.extensions.Extensions;
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 RunnerAndConfigurationSettingsImpl 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();
    RunnerAndConfigurationSettingsImpl settings = cloneTemplateConfiguration(project, context);
    PythonRunConfiguration configuration = (PythonRunConfiguration) settings.getConfiguration();
    configuration.setScriptName(mySourceFile.getVirtualFile().getPath());
    configuration.setName(configuration.suggestedName());
    if (module != null) {
      configuration.setUseModuleSdk(true);
      configuration.setModule(module);
    }
    copyStepsBeforeRun(project, configuration);
    return settings;
  }

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

@@ -451,16 +451,61 @@
 nsions;%0A
+import com.intellij.openapi.vfs.VirtualFile;%0A
 import c
@@ -1821,65 +1821,273 @@
     
-configuration.setScriptName(mySourceFile.getVirtualFile()
+final VirtualFile vFile = mySourceFile.getVirtualFile();%0A    if (vFile == null) return null;%0A    configuration.setScriptName(vFile.getPath());%0A    final VirtualFile parent = vFile.getParent();%0A    if (parent != null) %7B%0A      configuration.setWorkingDirectory(parent
 .get
@@ -2091,24 +2091,30 @@
 getPath());%0A
+    %7D%0A
     configur

set correct working dir for run configurations created by Ctrl-Shift-F10 (PY-324)