Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,11 @@ public void setTarget(@Nullable TargetExpression target) {

private void updateHandler() {
BlazeCommandRunConfigurationHandlerProvider handlerProvider =
BlazeCommandRunConfigurationHandlerProvider.findHandlerProvider(
getTargetState(), getTargetKind());
BlazeCommandRunConfigurationHandlerProvider.findHandlerProvider(this);
updateHandlerIfDifferentProvider(handlerProvider);
}

private TargetState getTargetState() {
public TargetState getTargetState() {
return targetPatterns.isEmpty() && pendingContext != null
? TargetState.PENDING
: TargetState.KNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ static BlazeCommandRunConfigurationHandlerProvider findHandlerProvider(
"No BlazeCommandRunConfigurationHandlerProvider found for Kind " + kind);
}

/**
* Find a {@link BlazeCommandRunConfigurationHandlerProvider} applicable to the given configuration. Providers that
* need richer context than just the {@link Kind} can override {@link canHandleConfig} to make use of it.
*/
static BlazeCommandRunConfigurationHandlerProvider findHandlerProvider(
BlazeCommandRunConfiguration configuration) {
for (BlazeCommandRunConfigurationHandlerProvider handlerProvider : EP_NAME.getExtensions()) {
if (handlerProvider.canHandleConfig(configuration)) {
return handlerProvider;
}
}

throw new RuntimeException(
"No BlazeCommandRunConfigurationHandlerProvider found for configuration " + configuration.getName());
}

/** Get the BlazeCommandRunConfigurationHandlerProvider with the given ID, if one exists. */
@Nullable
static BlazeCommandRunConfigurationHandlerProvider getHandlerProvider(@Nullable String id) {
Expand All @@ -65,6 +81,14 @@ static BlazeCommandRunConfigurationHandlerProvider getHandlerProvider(@Nullable
/** Whether this extension is applicable to the kind. */
boolean canHandleKind(TargetState state, @Nullable Kind kind);

/**
* Whether this extension is applicable to the given configuration. Defaults to delegating to
* {@link #canHandleKind}; override when the decision depends on details beyond the rule kind.
*/
default boolean canHandleConfig(BlazeCommandRunConfiguration configuration) {
return canHandleKind(configuration.getTargetState(), configuration.getTargetKind());
}

/** Returns the corresponding {@link BlazeCommandRunConfigurationHandler}. */
BlazeCommandRunConfigurationHandler createHandler(BlazeCommandRunConfiguration configuration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.idea.blaze.base.command.BlazeCommandName;
import com.google.idea.blaze.base.logging.EventLoggingService;
import com.google.idea.blaze.base.model.primitives.Kind;
import com.google.idea.blaze.base.run.BlazeCommandRunConfiguration;
import com.intellij.debugger.impl.GenericDebuggerRunner;
import com.intellij.execution.ExecutionException;
Expand Down Expand Up @@ -49,8 +48,7 @@ public boolean canRun(final String executorId, final RunProfile profile) {
return false;
}
BlazeCommandRunConfiguration configuration = (BlazeCommandRunConfiguration) profile;
Kind kind = configuration.getTargetKind();
if (kind == null || !BlazeJavaRunConfigurationHandlerProvider.supportsKind(kind)) {
if (!BlazeJavaRunConfigurationHandlerProvider.supports(configuration)) {
return false;
}
return canDebug(configuration.getHandler().getCommandName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@
package com.google.idea.blaze.java.run;

import com.google.common.collect.ImmutableSet;
import com.google.idea.blaze.base.ideinfo.TargetIdeInfo;
import com.google.idea.blaze.base.ideinfo.TargetKey;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.primitives.Kind;
import com.google.idea.blaze.base.model.primitives.Label;
import com.google.idea.blaze.base.model.primitives.RuleType;
import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.run.BlazeCommandRunConfiguration;
import com.google.idea.blaze.base.run.confighandler.BlazeCommandRunConfigurationHandler;
import com.google.idea.blaze.base.run.confighandler.BlazeCommandRunConfigurationHandlerProvider;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.google.idea.blaze.java.sync.source.JavaLikeLanguage;
import javax.annotation.Nullable;

Expand All @@ -30,15 +37,61 @@ public class BlazeJavaRunConfigurationHandlerProvider
private static final ImmutableSet<Kind> RELEVANT_RULE_KINDS =
JavaLikeLanguage.getAllDebuggableKinds();

/**
* A fallback for {@link supports} that can be used when synced target data isn't available. Prefer to call
* {@link supports} instead.
*/
static boolean supportsKind(@Nullable Kind kind) {
return RELEVANT_RULE_KINDS.contains(kind);
}

/**
* Returns whether the Java handler should drive this configuration. True if either (a) the
* target's synced {@link TargetIdeInfo} carries a {@code JavaIdeInfo} and is a test or binary, or (b) the rule kind
* is explicitly registered as Java-like.
*/
public static boolean supports(BlazeCommandRunConfiguration configuration) {
TargetIdeInfo target = resolveTarget(configuration);

if (target != null && target.getJavaIdeInfo() != null) {
RuleType ruleType = target.getKind().getRuleType();

if (ruleType == RuleType.TEST || ruleType == RuleType.BINARY) {
return true;
}
}

return supportsKind(configuration.getTargetKind());
}

@Nullable
private static TargetIdeInfo resolveTarget(BlazeCommandRunConfiguration configuration) {
TargetExpression target = configuration.getSingleTarget();

if (!(target instanceof Label)) {
return null;
}

BlazeProjectData projectData =
BlazeProjectDataManager.getInstance(configuration.getProject()).getBlazeProjectData();

if (projectData == null) {
return null;
}

return projectData.getTargetMap().get(TargetKey.forPlainTarget((Label) target));
}

@Override
public boolean canHandleKind(TargetState state, @Nullable Kind kind) {
return supportsKind(kind);
}

@Override
public boolean canHandleConfig(BlazeCommandRunConfiguration configuration) {
return supports(configuration);
}

@Override
public BlazeCommandRunConfigurationHandler createHandler(BlazeCommandRunConfiguration config) {
return new BlazeJavaRunConfigurationHandler(config);
Expand Down
Loading