package org.jetbrains.plugins.clojure.runner;
import com.intellij.execution.Location;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.execution.junit.RuntimeConfigurationProducer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.clojure.psi.api.ClojureFile;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: peter
* Date: Jan 15, 2009
* Time: 9:01:26 AM
* Copyright 2007, 2008, 2009 Red Shark Technology
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
*/
public class ClojureScriptRunConfigurationProducer extends RuntimeConfigurationProducer implements Cloneable {
private PsiElement mySourceElement;
public ClojureScriptRunConfigurationProducer() {
super(ClojureScriptRunConfigurationType.getInstance());
}
public PsiElement getSourceElement() {
return mySourceElement;
}
protected RunnerAndConfigurationSettingsImpl createConfigurationByElement(final Location location, final ConfigurationContext context) {
PsiElement element = location.getPsiElement();
PsiFile file = element.getContainingFile();
if (file instanceof ClojureFile) {
mySourceElement = element;
return ((RunnerAndConfigurationSettingsImpl) ClojureScriptRunConfigurationType.getInstance().createConfigurationByLocation(location));
}
return null;
}
public int compareTo(final Object o) {
return PREFERED;
}
@Override
protected RunnerAndConfigurationSettings findExistingByElement(Location location,
@NotNull List<RunnerAndConfigurationSettings> existingConfigurations,
ConfigurationContext context) {
for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) {
final RunConfiguration configuration = existingConfiguration.getConfiguration();
final ClojureScriptRunConfiguration existing = (ClojureScriptRunConfiguration)configuration;
final String path = existing.getScriptPath();
if (path != null) {
final PsiFile file = location.getPsiElement().getContainingFile();
if (file instanceof ClojureFile) {
final VirtualFile vfile = file.getVirtualFile();
if (vfile != null && FileUtil.toSystemIndependentName(path).equals(vfile.getPath())) {
return existingConfiguration;
}
}
}
}
return null;
}
}