Java Code Examples for com.intellij.execution.Location#getPsiElement()

The following examples show how to use com.intellij.execution.Location#getPsiElement() . 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: 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 2
Source File: TrackCoverageAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void selectSubCoverage() {
  final CoverageDataManager coverageDataManager = CoverageDataManager.getInstance(myProperties.getProject());
  final CoverageSuitesBundle currentSuite = coverageDataManager.getCurrentSuitesBundle();
  if (currentSuite != null) {
    final AbstractTestProxy test = myModel.getTreeView().getSelectedTest();
    List<String> testMethods = new ArrayList<String>();
    if (test != null && !test.isInProgress()) {
      final List<? extends AbstractTestProxy> list = test.getAllTests();
      for (AbstractTestProxy proxy : list) {
        final Location location = proxy.getLocation(myProperties.getProject(), myProperties.getScope());
        if (location != null) {
          final PsiElement element = location.getPsiElement();
          final String name = currentSuite.getCoverageEngine().getTestMethodName(element, proxy);
          if (name != null) {
            testMethods.add(name);
          }
        }
      }
    }
    coverageDataManager.selectSubCoverage(currentSuite, testMethods);
  }
}
 
Example 3
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 4
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 5
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 6
Source File: ProducerUtils.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Same as {@link JUnitUtil#getTestClass}, but handles classes outside the project. */
@Nullable
public static PsiClass getTestClass(final Location<?> location) {
  for (Iterator<Location<PsiClass>> iterator = location.getAncestors(PsiClass.class, false);
      iterator.hasNext(); ) {
    final Location<PsiClass> classLocation = iterator.next();
    if (isTestClass(classLocation.getPsiElement())) {
      return classLocation.getPsiElement();
    }
  }
  PsiElement element = location.getPsiElement();
  if (element instanceof PsiClassOwner) {
    PsiClass[] classes = ((PsiClassOwner) element).getClasses();
    if (classes.length == 1 && isTestClass(classes[0])) {
      return classes[0];
    }
  }
  return null;
}
 
Example 7
Source File: BlazeJUnitTestFilterFlags.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static String testFilterForLocation(Location<?> location) {
  PsiElement psi = location.getPsiElement();
  assert (psi instanceof PsiMethod);
  String methodName = ((PsiMethod) psi).getName();
  if (location instanceof PsiMemberParameterizedLocation) {
    return methodName
        + StringUtil.escapeToRegexp(
            ((PsiMemberParameterizedLocation) location).getParamSetName());
  }
  return methodName;
}
 
Example 8
Source File: BlazeJavaTestEventsHandler.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void appendTest(Map<PsiClass, Collection<Location<?>>> map, Location<?> location) {
  PsiElement psi = location.getPsiElement();
  if (psi instanceof PsiClass) {
    map.computeIfAbsent((PsiClass) psi, k -> new HashSet<>());
    return;
  }
  if (!(psi instanceof PsiMethod)) {
    return;
  }
  PsiClass psiClass = ((PsiMethod) psi).getContainingClass();
  if (psiClass == null) {
    return;
  }
  map.computeIfAbsent(psiClass, k -> new HashSet<>()).add(location);
}
 
Example 9
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 10
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 11
Source File: ScalaTestContextProvider.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static ScClass getTestClass(Location<?> location) {
  PsiElement element = location.getPsiElement();
  ScClass testClass;
  if (element instanceof ScClass) {
    testClass = (ScClass) element;
  } else {
    testClass = PsiTreeUtil.getParentOfType(element, ScClass.class);
  }
  if (testClass != null && isTestClass(testClass)) {
    return testClass;
  }
  return null;
}
 
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: 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 14
Source File: FileUrlLocationTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void doTest(int expectedOffset, String filePath, int lineNum, int columnNumber) {
  SMTestProxy testProxy = new SMTestProxy("myTest", false, "file://" + filePath + ":" + lineNum + (columnNumber > 0 ? (":" + columnNumber) : ""));
  testProxy.setLocator(FileUrlProvider.INSTANCE);

  Location location = testProxy.getLocation(getProject(), GlobalSearchScope.allScope(getProject()));
  assertNotNull(location);
  PsiElement element = location.getPsiElement();
  assertNotNull(element);
  assertEquals(expectedOffset, element.getTextOffset());
}
 
Example 15
Source File: TestsUIUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@SuppressWarnings("unchecked")
public static <T> T getData(final AbstractTestProxy testProxy, final Key<T> dataId, final TestFrameworkRunningModel model) {
  final TestConsoleProperties properties = model.getProperties();
  final Project project = properties.getProject();
  if (testProxy == null) return null;
  if (AbstractTestProxy.DATA_KEY == dataId) return (T)testProxy;
  if (CommonDataKeys.NAVIGATABLE == dataId) return (T)getOpenFileDescriptor(testProxy, model);
  if (CommonDataKeys.PSI_ELEMENT == dataId) {
    final Location location = testProxy.getLocation(project, properties.getScope());
    if (location != null) {
      final PsiElement element = location.getPsiElement();
      return element.isValid() ? (T)element : null;
    }
    else {
      return null;
    }
  }
  if (Location.DATA_KEY == dataId) return (T)testProxy.getLocation(project, properties.getScope());
  if (RunConfiguration.DATA_KEY == dataId) {
    final RunProfile configuration = properties.getConfiguration();
    if (configuration instanceof RunConfiguration) {
      return (T)configuration;
    }
  }
  return null;
}
 
Example 16
Source File: BlazeAndroidTestEventsHandler.java    From intellij with Apache License 2.0 5 votes vote down vote up
private static void appendTest(Map<PsiClass, Collection<Location<?>>> map, Location<?> location) {
  PsiElement psi = location.getPsiElement();
  if (psi instanceof PsiClass) {
    map.computeIfAbsent((PsiClass) psi, k -> new HashSet<>());
    return;
  }
  if (!(psi instanceof PsiMethod)) {
    return;
  }
  PsiClass psiClass = ((PsiMethod) psi).getContainingClass();
  if (psiClass == null) {
    return;
  }
  map.computeIfAbsent(psiClass, k -> new HashSet<>()).add(location);
}
 
Example 17
Source File: AndroidTestContextProvider.java    From intellij with Apache License 2.0 4 votes vote down vote up
@Nullable
private static PsiMethod findTestMethod(Location<?> location) {
  Location<PsiMethod> methodLocation = ProducerUtils.getMethodLocation(location);
  return methodLocation != null ? methodLocation.getPsiElement() : null;
}
 
Example 18
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;
}