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

The following examples show how to use com.intellij.execution.actions.ConfigurationContext#getLocation() . 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: MuleApplicationConfigurationProducer.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean setupConfigurationFromContext(MuleConfiguration muleConfiguration, ConfigurationContext configurationContext, Ref<PsiElement> ref)
{
    final Location location = configurationContext.getLocation();
    if (location != null)
    {
        final boolean muleFile = MuleConfigUtils.isMuleFile(location.getPsiElement().getContainingFile());
        final Module module = configurationContext.getModule();
        if (muleFile && module != null)
        {
            muleConfiguration.setModule(module);
            muleConfiguration.setName(StringUtils.capitalize(module.getName()));
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: TestMethodSelectionUtil.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * Get a test method which is considered selected in the given context, belonging to a
 * non-abstract class. The context location may be the method itself, or anywhere inside the
 * method.
 *
 * @param context The context to search for a test method in.
 * @return A test method, or null if none are found.
 */
@Nullable
public static PsiMethod getIndirectlySelectedMethod(@NotNull ConfigurationContext context) {
  final Location<?> contextLocation = context.getLocation();
  if (contextLocation == null) {
    return null;
  }
  Iterator<Location<PsiMethod>> locationIterator =
      contextLocation.getAncestors(PsiMethod.class, false);
  while (locationIterator.hasNext()) {
    Location<PsiMethod> methodLocation = locationIterator.next();
    if (JUnitUtil.isTestMethod(methodLocation)) {
      return methodLocation.getPsiElement();
    }
  }
  return null;
}
 
Example 3
Source File: JavaBinaryContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private static PsiClass getMainClass(ConfigurationContext context) {
  Location location = context.getLocation();
  if (location == null) {
    return null;
  }
  location = JavaExecutionUtil.stepIntoSingleClass(location);
  if (location == null) {
    return null;
  }
  PsiElement element = location.getPsiElement();
  if (!element.isPhysical()) {
    return null;
  }
  return ApplicationConfigurationType.getMainClass(element);
}
 
Example 4
Source File: PyBinaryContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
@Override
public BinaryRunContext getRunContext(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  PsiElement element = location.getPsiElement();
  PsiFile file = element.getContainingFile();
  if (!(file instanceof PyFile)) {
    return null;
  }
  TargetInfo binaryTarget = getTargetLabel(file);
  if (binaryTarget == null) {
    return null;
  }
  return BinaryRunContext.create(file, binaryTarget);
}
 
Example 5
Source File: ScalaBinaryContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
static ScObject getMainObject(ConfigurationContext context) {
  Location location = context.getLocation();
  if (location == null) {
    return null;
  }
  location = JavaExecutionUtil.stepIntoSingleClass(context.getLocation());
  if (location == null) {
    return null;
  }
  PsiElement element = location.getPsiElement();
  if (!(element.getContainingFile() instanceof ScalaFile)) {
    return null;
  }
  if (!element.isPhysical()) {
    return null;
  }
  return getMainObjectFromElement(element);
}
 
Example 6
Source File: ScalaTestContextProvider.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Nullable
private static ScClass getTestClass(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  location = JavaExecutionUtil.stepIntoSingleClass(location);
  if (location == null) {
    return null;
  }
  if (!SmRunnerUtils.getSelectedSmRunnerTreeElements(context).isEmpty()) {
    // handled by a different producer
    return null;
  }
  return getTestClass(location);
}
 
Example 7
Source File: XQueryRunConfigurationProducer.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context) {
    if (!(configuration instanceof XQueryRunConfiguration)) return false;
    XQueryRunConfiguration appConfiguration = (XQueryRunConfiguration) configuration;
    Location location = context.getLocation();
    PsiFile psiFile = location.getPsiElement().getContainingFile();
    if (isUnsupportedFile(psiFile) || ((XQueryFile) psiFile).isLibraryModule()) return false;
    return isForTheSameFile(psiFile, appConfiguration) && isForTheSameModule(location, appConfiguration)
            && appConfiguration.getDataSourceName() != null;
}
 
Example 8
Source File: XQueryRunConfigurationProducer.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean setupConfigurationFromContext(RunConfiguration configuration, ConfigurationContext context, Ref sourceElement) {
    if (!(configuration instanceof XQueryRunConfiguration)) return false;
    XQueryRunConfiguration appConfiguration = (XQueryRunConfiguration) configuration;
    Location location = context.getLocation();
    PsiElement psiElement = location.getPsiElement();
    PsiFile psiFile = psiElement.getContainingFile();
    if (isUnsupportedFile(psiFile)) return false;
    XQueryFile containingFile = (XQueryFile) psiFile;
    if (containingFile.isLibraryModule()) return false;
    final VirtualFile vFile = location.getVirtualFile();
    if (vFile == null) return false;
    prepareSettings(appConfiguration, context, vFile, containingFile);
    return true;
}
 
Example 9
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 10
Source File: GaugeExecutionProducer.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();
    PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(context.getDataContext());
    if (location == null || location.getVirtualFile() == null || element == null) return false;
    if (!isInSpecScope(context.getPsiLocation())) return false;
    String specsToExecute = ((GaugeRunConfiguration) configuration).getSpecsToExecute();
    return specsToExecute != null && (specsToExecute.equals(location.getVirtualFile().getPath()));
}
 
Example 11
Source File: BashRunConfigProducer.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isConfigurationFromContext(BashRunConfiguration configuration, ConfigurationContext context) {
    Location location = context.getLocation();
    if (location == null) {
        return false;
    }

    //fixme file checks needs to check the properties

    VirtualFile file = location.getVirtualFile();
    return file != null && FileUtil.pathsEqual(file.getPath(), configuration.getScriptName());
}
 
Example 12
Source File: KotlinTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  PsiElement element = location.getPsiElement();
  KtNamedFunction testMethod = PsiUtils.getParentOfType(element, KtNamedFunction.class, false);
  KtClass testClass = PsiUtils.getParentOfType(element, KtClass.class, false);
  if (testClass == null) {
    return null;
  }
  // TODO: detect test size from kotlin source code
  ListenableFuture<TargetInfo> target =
      TestTargetHeuristic.targetFutureForPsiElement(testClass, /* testSize= */ null);
  if (target == null) {
    return null;
  }
  String filter = getTestFilter(testClass, testMethod);
  String description = testClass.getName();
  if (testMethod != null) {
    description += "." + testMethod.getName();
  }
  PsiElement contextElement = testMethod != null ? testMethod : testClass;
  return TestContext.builder(contextElement, ExecutorType.DEBUG_SUPPORTED_TYPES)
      .setTarget(target)
      .setTestFilter(filter)
      .setDescription(description)
      .build();
}
 
Example 13
Source File: KotlinBinaryContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public BinaryRunContext getRunContext(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  TargetIdeInfo target = getTargetIdeInfo(context);
  if (target == null) {
    return null;
  }
  return BinaryRunContext.create(location.getPsiElement(), target.toTargetInfo());
}
 
Example 14
Source File: PyTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
/**
 * The single selected {@link PsiElement}. Returns null if we're in a SM runner tree UI context
 * (handled by a different producer).
 */
@Nullable
private static PsiElement selectedPsiElement(ConfigurationContext context) {
  List<Location<?>> selectedTestUiElements =
      SmRunnerUtils.getSelectedSmRunnerTreeElements(context);
  if (!selectedTestUiElements.isEmpty()) {
    return null;
  }
  Location<?> location = context.getLocation();
  return location != null ? location.getPsiElement() : null;
}
 
Example 15
Source File: CppTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
/** The single selected {@link PsiElement}. Returns null if multiple elements are selected. */
@Nullable
private static PsiElement selectedPsiElement(ConfigurationContext context) {
  PsiElement[] psi = LangDataKeys.PSI_ELEMENT_ARRAY.getData(context.getDataContext());
  if (psi != null && psi.length > 1) {
    return null; // multiple elements selected.
  }
  Location<?> location = context.getLocation();
  return location != null ? location.getPsiElement() : null;
}
 
Example 16
Source File: JavaTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiClass getSelectedTestClass(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  location = JavaExecutionUtil.stepIntoSingleClass(location);
  if (location == null) {
    return null;
  }
  if (JUnitConfigurationUtil.isMultipleElementsSelected(context)) {
    return null;
  }
  return ProducerUtils.getTestClass(location);
}
 
Example 17
Source File: AndroidTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public RunConfigurationContext getTestContext(ConfigurationContext context) {
  if (JUnitConfigurationUtil.isMultipleElementsSelected(context)) {
    return null;
  }
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  PsiMethod method = findTestMethod(location);
  PsiClass testClass =
      method != null ? method.getContainingClass() : JUnitUtil.getTestClass(location);
  return testClass != null ? AndroidTestContext.fromClassAndMethod(testClass, method) : null;
}
 
Example 18
Source File: KotlinBinaryContextProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Nullable
private static TargetIdeInfo getTargetIdeInfo(ConfigurationContext context) {
  Location<?> location = context.getLocation();
  if (location == null) {
    return null;
  }
  VirtualFile virtualFile = location.getVirtualFile();
  if (virtualFile == null) {
    return null;
  }
  KtDeclarationContainer entryPointContainer =
      KotlinRunConfigurationProducer.Companion.getEntryPointContainer(location.getPsiElement());
  if (entryPointContainer == null) {
    return null;
  }
  String startClassFqName =
      KotlinRunConfigurationProducer.Companion.getStartClassFqName(entryPointContainer);
  if (startClassFqName == null) {
    return null;
  }
  Collection<TargetIdeInfo> kotlinBinaryTargets =
      findKotlinBinaryTargets(context.getProject(), VfsUtil.virtualToIoFile(virtualFile));

  // first look for a matching main_class
  TargetIdeInfo match =
      kotlinBinaryTargets.stream()
          .filter(
              target ->
                  target.getJavaIdeInfo() != null
                      && startClassFqName.equals(
                          target.getJavaIdeInfo().getJavaBinaryMainClass()))
          .findFirst()
          .orElse(null);
  if (match != null) {
    return match;
  }

  match =
      kotlinBinaryTargets.stream()
          .filter(
              target ->
                  startClassFqName.equals(target.getKey().getLabel().targetName().toString()))
          .findFirst()
          .orElse(null);
  if (match != null) {
    return match;
  }

  return Iterables.getFirst(kotlinBinaryTargets, null);
}
 
Example 19
Source File: BashRunConfigProducer.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean setupConfigurationFromContext(BashRunConfiguration configuration, ConfigurationContext context, Ref<PsiElement> sourceElement) {
    Location location = context.getLocation();
    if (location == null) {
        return false;
    }

    PsiElement psiElement = location.getPsiElement();
    if (!psiElement.isValid()) {
        return false;
    }

    PsiFile psiFile = psiElement.getContainingFile();
    if (!(psiFile instanceof BashFile)) {
        return false;
    }
    sourceElement.set(psiFile);

    VirtualFile file = location.getVirtualFile();
    if (file == null) {
        return false;
    }

    configuration.setName(file.getPresentableName());
    configuration.setScriptName(VfsUtilCore.virtualToIoFile(file).getAbsolutePath());

    if (file.getParent() != null) {
        configuration.setWorkingDirectory(VfsUtilCore.virtualToIoFile(file.getParent()).getAbsolutePath());
    }

    Module module = context.getModule();
    if (module != null) {
        configuration.setModule(module);
    }

    // check the location given by the shebang line
    // do this only if the project interpreter isn't used because we don't want to add the options
    // because it would mess up the execution when the project interpreter was used.
    // options might be added by a shebang like "/usr/bin/env bash".
    // also, we don't want to override the defaults of the template run configuration
    if (!configuration.isUseProjectInterpreter() && configuration.getInterpreterPath().isEmpty()) {
        BashFile bashFile = (BashFile) psiFile;
        BashShebang shebang = bashFile.findShebang();
        if (shebang != null) {
            String shebandShell = shebang.shellCommand(false);

            if ((BashInterpreterDetection.instance().isSuitable(shebandShell))) {
                configuration.setInterpreterPath(shebandShell);
                configuration.setInterpreterOptions(shebang.shellCommandParams());
            }
        }
    }

    return true;
}
 
Example 20
Source File: WeaveConfigurationProducer.java    From mule-intellij-plugins with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean setupConfigurationFromContext(WeaveConfiguration weaveConfiguration, ConfigurationContext configurationContext, Ref<PsiElement> ref)
{
    final Location location = configurationContext.getLocation();
    if (location != null)
    {
        final PsiFile containingFile = location.getPsiElement().getContainingFile();
        if(containingFile != null)
        {
            final boolean weaveFile = containingFile.getFileType() == WeaveFileType.getInstance();
            if (weaveFile)
            {
                final List<WeaveInput> weaveInputs = new ArrayList<>();
                WeaveDocument document = WeavePsiUtils.getDocument(location.getPsiElement());
                if (document != null)
                {
                    final WeaveHeader header = document.getHeader();
                    if (header != null)
                    {
                        final List<WeaveDirective> directiveList = header.getDirectiveList();
                        for (WeaveDirective weaveDirective : directiveList)
                        {
                            if (weaveDirective instanceof WeaveInputDirective)
                            {
                                final WeaveIdentifier identifier = ((WeaveInputDirective) weaveDirective).getIdentifier();
                                if (identifier != null)
                                {
                                    weaveInputs.add(new WeaveInput(identifier.getName(), ""));
                                }
                            }
                        }
                    }
                }
                weaveConfiguration.setWeaveInputs(weaveInputs);
                weaveConfiguration.setWeaveFile(containingFile.getVirtualFile().getCanonicalPath());
                weaveConfiguration.setModule(configurationContext.getModule());
                weaveConfiguration.setName(StringUtils.capitalize(containingFile.getVirtualFile().getName()));
                return true;
            }
        }
    }
    return false;
}