Search in sources :

Example 1 with ScObject

use of org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject in project intellij by bazelbuild.

the class BlazeScalaMainClassRunConfigurationProducer method getTarget.

@Nullable
private static TargetIdeInfo getTarget(Project project, ScObject mainObject) {
    File mainObjectFile = RunUtil.getFileForClass(mainObject);
    if (mainObjectFile == null) {
        return null;
    }
    Collection<TargetIdeInfo> scalaBinaryTargets = findScalaBinaryTargets(project, mainObjectFile);
    // Scala objects are basically singletons with a '$' appended to the class name.
    // The trunced qualified name removes the '$',
    // so it matches the main class specified in the scala_binary rule.
    String qualifiedName = mainObject.getTruncedQualifiedName();
    if (qualifiedName == null) {
        // out of date psi element; just take the first match
        return Iterables.getFirst(scalaBinaryTargets, null);
    }
    // Can't use getName because of the '$'.
    String className = qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
    // first look for a matching main_class
    TargetIdeInfo match = scalaBinaryTargets.stream().filter(target -> target.javaIdeInfo != null && qualifiedName.equals(target.javaIdeInfo.javaBinaryMainClass)).findFirst().orElse(null);
    if (match != null) {
        return match;
    }
    match = scalaBinaryTargets.stream().filter(target -> className.equals(target.key.label.targetName().toString())).findFirst().orElse(null);
    if (match != null) {
        return match;
    }
    return Iterables.getFirst(scalaBinaryTargets, null);
}
Also used : TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Iterables(com.google.common.collect.Iterables) ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) FilteredTargetMap(com.google.idea.blaze.base.run.testmap.FilteredTargetMap) ScalaMainMethodUtil(org.jetbrains.plugins.scala.runner.ScalaMainMethodUtil) BlazeCommandRunConfigurationType(com.google.idea.blaze.base.run.BlazeCommandRunConfigurationType) BlazeCommandRunConfiguration(com.google.idea.blaze.base.run.BlazeCommandRunConfiguration) Kind(com.google.idea.blaze.base.model.primitives.Kind) BlazeProjectData(com.google.idea.blaze.base.model.BlazeProjectData) PsiClass(com.intellij.psi.PsiClass) ImmutableList(com.google.common.collect.ImmutableList) PsiElement(com.intellij.psi.PsiElement) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) Project(com.intellij.openapi.project.Project) RunUtil(com.google.idea.blaze.java.run.RunUtil) ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) BlazeRunConfigurationProducer(com.google.idea.blaze.base.run.producers.BlazeRunConfigurationProducer) Nullable(javax.annotation.Nullable) PsiMethod(com.intellij.psi.PsiMethod) Collection(java.util.Collection) Option(scala.Option) ScObject(org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject) BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) File(java.io.File) BlazeCommandName(com.google.idea.blaze.base.command.BlazeCommandName) Objects(java.util.Objects) SyncCache(com.google.idea.blaze.base.sync.SyncCache) JavaExecutionUtil(com.intellij.execution.JavaExecutionUtil) Location(com.intellij.execution.Location) Ref(com.intellij.openapi.util.Ref) ScalaFile(org.jetbrains.plugins.scala.lang.psi.api.ScalaFile) File(java.io.File) Nullable(javax.annotation.Nullable)

Example 2 with ScObject

use of org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject in project intellij by bazelbuild.

the class BlazeScalaMainClassRunConfigurationProducer method doSetupConfigFromContext.

@Override
protected boolean doSetupConfigFromContext(BlazeCommandRunConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    ScObject mainObject = getMainObject(context);
    if (mainObject == null) {
        return false;
    }
    Option<PsiMethod> mainMethod = ScalaMainMethodUtil.findMainMethod(mainObject);
    if (mainMethod.isEmpty()) {
        sourceElement.set(mainObject);
    } else {
        sourceElement.set(mainMethod.get());
    }
    TargetIdeInfo target = getTarget(context.getProject(), mainObject);
    if (target == null) {
        return false;
    }
    configuration.setTargetInfo(target.toTargetInfo());
    BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (handlerState == null) {
        return false;
    }
    handlerState.getCommandState().setCommand(BlazeCommandName.RUN);
    configuration.setGeneratedName();
    return true;
}
Also used : TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) PsiMethod(com.intellij.psi.PsiMethod) ScObject(org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject)

Example 3 with ScObject

use of org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject in project intellij by bazelbuild.

the class BlazeScalaMainClassRunConfigurationProducer method doIsConfigFromContext.

@Override
protected boolean doIsConfigFromContext(BlazeCommandRunConfiguration configuration, ConfigurationContext context) {
    BlazeCommandRunConfigurationCommonState handlerState = configuration.getHandlerStateIfType(BlazeCommandRunConfigurationCommonState.class);
    if (handlerState == null) {
        return false;
    }
    if (!Objects.equals(handlerState.getCommandState().getCommand(), BlazeCommandName.RUN)) {
        return false;
    }
    ScObject mainObject = getMainObject(context);
    if (mainObject == null) {
        return false;
    }
    TargetIdeInfo target = getTarget(context.getProject(), mainObject);
    return target != null && Objects.equals(configuration.getTarget(), target.key.label);
}
Also used : BlazeCommandRunConfigurationCommonState(com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState) TargetIdeInfo(com.google.idea.blaze.base.ideinfo.TargetIdeInfo) ScObject(org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject)

Aggregations

TargetIdeInfo (com.google.idea.blaze.base.ideinfo.TargetIdeInfo)3 BlazeCommandRunConfigurationCommonState (com.google.idea.blaze.base.run.state.BlazeCommandRunConfigurationCommonState)3 ScObject (org.jetbrains.plugins.scala.lang.psi.api.toplevel.typedef.ScObject)3 PsiMethod (com.intellij.psi.PsiMethod)2 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 BlazeCommandName (com.google.idea.blaze.base.command.BlazeCommandName)1 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)1 Kind (com.google.idea.blaze.base.model.primitives.Kind)1 BlazeCommandRunConfiguration (com.google.idea.blaze.base.run.BlazeCommandRunConfiguration)1 BlazeCommandRunConfigurationType (com.google.idea.blaze.base.run.BlazeCommandRunConfigurationType)1 BlazeRunConfigurationProducer (com.google.idea.blaze.base.run.producers.BlazeRunConfigurationProducer)1 FilteredTargetMap (com.google.idea.blaze.base.run.testmap.FilteredTargetMap)1 SyncCache (com.google.idea.blaze.base.sync.SyncCache)1 RunUtil (com.google.idea.blaze.java.run.RunUtil)1 JavaExecutionUtil (com.intellij.execution.JavaExecutionUtil)1 Location (com.intellij.execution.Location)1 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)1 Project (com.intellij.openapi.project.Project)1 Ref (com.intellij.openapi.util.Ref)1