Java Code Examples for com.intellij.execution.actions.ConfigurationContext#getPsiLocation()

The following examples show how to use com.intellij.execution.actions.ConfigurationContext#getPsiLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlutterTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * If the current file looks like a Flutter test, initializes the run config to run it.
 * <p>
 * Returns true if successfully set up.
 */
@Override
protected boolean setupConfigurationFromContext(TestConfig config, ConfigurationContext context, Ref<PsiElement> sourceElement) {
  if (!isFlutterContext(context)) return false;

  final PsiElement elt = context.getPsiLocation();
  if (elt instanceof PsiDirectory) {
    return setupForDirectory(config, (PsiDirectory)elt);
  }

  final DartFile file = FlutterRunConfigurationProducer.getDartFile(context);
  if (file == null) {
    return false;
  }

  if (supportsFiltering(config.getSdk())) {
    final String testName = testConfigUtils.findTestName(elt);
    if (testName != null) {
      return setupForSingleTest(config, context, file, testName);
    }
  }

  return setupForDartFile(config, context, file);
}
 
Example 2
Source File: ScenarioExecutionProducer.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
private String getScenarioName(ConfigurationContext context) {
    PsiElement selectedElement = context.getPsiLocation();
    String scenarioName;

    if (selectedElement == null) return null;
    if (selectedElement.getClass().equals(SpecScenarioImpl.class)) {
        scenarioName = selectedElement.getText();
    } else {
        String text = getScenarioHeading(selectedElement).trim();
        if (text.equals("*")) {
            scenarioName = selectedElement.getParent().getParent().getNode().getFirstChildNode().getText();
        } else scenarioName = text;
    }
    if (scenarioName.startsWith("##"))
        scenarioName = scenarioName.replaceFirst("##", "");
    scenarioName = scenarioName.trim();
    return scenarioName.contains("\n") ? scenarioName.substring(0, scenarioName.indexOf("\n")) : scenarioName;
}
 
Example 3
Source File: MultipleJavaClassesTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
  boolean outsideProject = context.getModule() == null;
  if (outsideProject) {
    // TODO(brendandouglas): resolve PSI asynchronously for files outside the project
    return null;
  }
  PsiElement location = context.getPsiLocation();
  if (location instanceof PsiDirectory) {
    PsiDirectory dir = (PsiDirectory) location;
    ListenableFuture<TargetInfo> future = getTargetContext(dir);
    return futureEmpty(future) ? null : fromDirectory(future, dir);
  }
  Set<PsiClass> testClasses = selectedTestClasses(context);
  if (testClasses.size() <= 1) {
    return null;
  }
  ListenableFuture<TargetInfo> target = getTestTargetIfUnique(context.getProject(), testClasses);
  if (futureEmpty(target)) {
    return null;
  }
  testClasses = ProducerUtils.includeInnerTestClasses(testClasses);
  return fromClasses(target, testClasses);
}
 
Example 4
Source File: AllInPackageTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
  PsiElement location = context.getPsiLocation();
  if (!(location instanceof PsiDirectory)) {
    return null;
  }
  WorkspaceRoot root = WorkspaceRoot.fromProject(context.getProject());
  PsiDirectory dir = (PsiDirectory) location;
  if (!isInWorkspace(root, dir)) {
    return null;
  }
  // only check if the directory itself is a blaze package
  // TODO(brendandouglas): otherwise check off the EDT, and return PendingRunConfigurationContext?
  return BlazePackage.isBlazePackage(dir) ? fromDirectory(root, dir) : null;
}
 
Example 5
Source File: VirtualFileTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
  PsiElement psi = context.getPsiLocation();
  if (!(psi instanceof PsiFileSystemItem) || !(psi instanceof FakePsiElement)) {
    return null;
  }
  VirtualFile vf = ((PsiFileSystemItem) psi).getVirtualFile();
  if (vf == null) {
    return null;
  }
  WorkspacePath path = getWorkspacePath(context.getProject(), vf);
  if (path == null) {
    return null;
  }
  return CachedValuesManager.getCachedValue(
      psi,
      () ->
          CachedValueProvider.Result.create(
              doFindTestContext(context, vf, psi, path),
              PsiModificationTracker.MODIFICATION_COUNT,
              BlazeSyncModificationTracker.getInstance(context.getProject())));
}
 
Example 6
Source File: BinaryContextRunConfigurationProducer.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private BinaryRunContext findRunContext(ConfigurationContext context) {
  if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
    // not a binary run context
    return null;
  }
  ContextWrapper wrapper = new ContextWrapper(context);
  PsiElement psi = context.getPsiLocation();
  return psi == null
      ? null
      : CachedValuesManager.getCachedValue(
          psi,
          () ->
              CachedValueProvider.Result.create(
                  doFindRunContext(wrapper.context),
                  PsiModificationTracker.MODIFICATION_COUNT,
                  BlazeSyncModificationTracker.getInstance(wrapper.context.getProject())));
}
 
Example 7
Source File: TestContextRunConfigurationProducer.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private RunConfigurationContext findTestContext(ConfigurationContext context) {
  if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
    // handled by a different producer
    return null;
  }
  ContextWrapper wrapper = new ContextWrapper(context);
  PsiElement psi = context.getPsiLocation();
  return psi == null
      ? null
      : CachedValuesManager.getCachedValue(
          psi,
          cacheKey,
          () ->
              CachedValueProvider.Result.create(
                  doFindTestContext(wrapper.context),
                  PsiModificationTracker.MODIFICATION_COUNT,
                  BlazeSyncModificationTracker.getInstance(wrapper.context.getProject())));
}
 
Example 8
Source File: FlutterTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns true if a run config was already created for this file. If so we will reuse it.
 */
@Override
public boolean isConfigurationFromContext(TestConfig config, ConfigurationContext context) {
  final VirtualFile fileOrDir = config.getFields().getFileOrDir();
  if (fileOrDir == null) return false;

  final PsiElement target = context.getPsiLocation();
  if (target instanceof PsiDirectory) {
    return ((PsiDirectory)target).getVirtualFile().equals(fileOrDir);
  }

  if (!FlutterRunConfigurationProducer.hasDartFile(context, fileOrDir.getPath())) return false;

  final String testName = testConfigUtils.findTestName(context.getPsiLocation());
  if (config.getFields().getScope() == TestFields.Scope.NAME) {
    return testName != null && testName.equals(config.getFields().getTestName());
  }
  else {
    return testName == null;
  }
}
 
Example 9
Source File: GaugeExecutionProducer.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean setupConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {
    VirtualFile[] selectedFiles = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(context.getDataContext());
    if (selectedFiles == null || selectedFiles.length > 1) return false;
    Module module = context.getModule();
    if (context.getPsiLocation() == null || module == null) return false;
    PsiFile file = context.getPsiLocation().getContainingFile();
    if (!isSpecFile(file) || !isInSpecScope(context.getPsiLocation())) return false;
    try {
        VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile == null) return false;
        String name = virtualFile.getCanonicalPath();
        configuration.setName(file.getName());
        ((GaugeRunConfiguration) configuration).setSpecsToExecute(name);
        ((GaugeRunConfiguration) configuration).setModule(module);
        return true;
    } catch (Exception ex) {
        LOG.debug(ex);
        ex.printStackTrace();
    }
    return true;
}
 
Example 10
Source File: FlutterTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * If the current file looks like a Flutter test, initializes the run config to run it.
 * <p>
 * Returns true if successfully set up.
 */
@Override
protected boolean setupConfigurationFromContext(TestConfig config, ConfigurationContext context, Ref<PsiElement> sourceElement) {
  if (!isFlutterContext(context)) return false;

  final PsiElement elt = context.getPsiLocation();
  if (elt instanceof PsiDirectory) {
    return setupForDirectory(config, (PsiDirectory)elt);
  }

  final DartFile file = FlutterRunConfigurationProducer.getDartFile(context);
  if (file == null) {
    return false;
  }

  if (supportsFiltering(config.getSdk())) {
    final String testName = testConfigUtils.findTestName(elt);
    if (testName != null) {
      return setupForSingleTest(config, context, file, testName);
    }
  }

  return setupForDartFile(config, context, file);
}
 
Example 11
Source File: BazelTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Called on existing run configurations to check if one has already been created for the given {@param context}.
 *
 * @return true if a run config was already created for this context. If so we will reuse it.
 */
@Override
public boolean isConfigurationFromContext(@NotNull BazelTestConfig config, @NotNull ConfigurationContext context) {
  // Check if the config is a non-watch producer and the producer is a watch producer or vice versa, then the given configuration
  // is not a replacement for one by this producer.
  if (!StringUtil.equals(getId(config), getConfigurationFactory().getId())) return false;

  final VirtualFile file = config.getFields().getFile();
  if (file == null) return false;

  final PsiElement target = context.getPsiLocation();
  if (target instanceof PsiDirectory) {
    return ((PsiDirectory)target).getVirtualFile().equals(file);
  }

  if (!FlutterRunConfigurationProducer.hasDartFile(context, file.getPath())) return false;

  final String testName = bazelTestConfigUtils.findTestName(context.getPsiLocation());
  if (config.getFields().getTestName() != null) {
    return testName != null && testName.equals(config.getFields().getTestName());
  }
  return testName == null;
}
 
Example 12
Source File: PantsTestRunConfigurationProducer.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConfigurationFromContext(
  @NotNull ExternalSystemRunConfiguration configuration,
  @NotNull ConfigurationContext context
) {
  final ExternalSystemRunConfiguration tempConfig = new ExternalSystemRunConfiguration(
    PantsConstants.SYSTEM_ID, context.getProject(), configuration.getFactory(), configuration.getName()
  );
  final Ref<PsiElement> locationRef = new Ref<>(context.getPsiLocation());
  setupConfigurationFromContext(tempConfig, context, locationRef);
  return compareSettings(configuration.getSettings(), tempConfig.getSettings());
}
 
Example 13
Source File: ScenarioExecutionProducer.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context) {
    if (!(configuration.getType() instanceof GaugeRunTaskConfigurationType)) return false;
    Location location = context.getLocation();
    if (location == null || location.getVirtualFile() == null || context.getPsiLocation() == null) return false;
    String specsToExecute = ((GaugeRunConfiguration) configuration).getSpecsToExecute();
    int identifier = getScenarioIdentifier(context, context.getPsiLocation().getContainingFile());
    return specsToExecute != null && specsToExecute.equals(location.getVirtualFile().getPath() + Constants.SPEC_SCENARIO_DELIMITER + identifier);
}
 
Example 14
Source File: BlazeJavaAbstractTestCaseConfigurationProducer.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static PsiMethod getTestMethod(ConfigurationContext context) {
  PsiElement psi = context.getPsiLocation();
  if (psi instanceof PsiMethod
      && AnnotationUtil.isAnnotated((PsiMethod) psi, JUnitUtil.TEST_ANNOTATION, false)) {
    return (PsiMethod) psi;
  }
  List<PsiMethod> selectedMethods = TestMethodSelectionUtil.getSelectedMethods(context);
  return selectedMethods != null && selectedMethods.size() == 1 ? selectedMethods.get(0) : null;
}
 
Example 15
Source File: GoBinaryContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiFile getMainFile(ConfigurationContext context) {
  PsiElement element = context.getPsiLocation();
  if (element == null) {
    return null;
  }
  PsiFile file = element.getContainingFile();
  if (file instanceof GoFile && GoRunUtil.isMainGoFile(file)) {
    return file;
  }
  return null;
}
 
Example 16
Source File: PantsConfigurationContext.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
public static Optional<PantsConfigurationContext> validatesAndCreate(ConfigurationContext context) {
  final Module module = context.getModule();
  if (module == null) return Optional.empty();

  final Optional<VirtualFile> buildRoot = PantsUtil.findBuildRoot(module);
  if (!buildRoot.isPresent()) return Optional.empty();

  final List<String> targets = PantsUtil.getNonGenTargetAddresses(module);
  if (targets.isEmpty()) return Optional.empty();

  final PsiElement psiLocation = context.getPsiLocation();
  if (psiLocation == null) return Optional.empty();

  return Optional.of(new PantsConfigurationContext(module, buildRoot.get(), targets, psiLocation));
}
 
Example 17
Source File: FlutterTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isFlutterContext(@NotNull ConfigurationContext context) {
  final PsiElement location = context.getPsiLocation();
  return location != null && FlutterUtils.isInFlutterProject(context.getProject(), location);
}
 
Example 18
Source File: BazelTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean isBazelFlutterContext(@NotNull ConfigurationContext context) {
  final PsiElement location = context.getPsiLocation();
  return location != null && getWorkspace(context.getProject()) != null;
}
 
Example 19
Source File: BazelTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean isBazelFlutterContext(@NotNull ConfigurationContext context) {
  final PsiElement location = context.getPsiLocation();
  return location != null && getWorkspace(context.getProject()) != null;
}
 
Example 20
Source File: FlutterTestConfigProducer.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static boolean isFlutterContext(@NotNull ConfigurationContext context) {
  final PsiElement location = context.getPsiLocation();
  return location != null && FlutterUtils.isInFlutterProject(context.getProject(), location);
}