Search in sources :

Example 21 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class TestLocationDataRule method getData.

@Nullable
@Override
public Object getData(DataProvider dataProvider) {
    final Project project = CommonDataKeys.PROJECT.getData(dataProvider);
    final VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(dataProvider);
    if (project != null && file != null) {
        final List<Location> locations = collectRelativeLocations(project, file);
        return locations.size() == 1 ? locations.get(0) : null;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class TestLocationDataRule method collectRelativeLocations.

@NotNull
protected static List<Location> collectRelativeLocations(Project project, VirtualFile file) {
    if (DumbService.isDumb(project))
        return Collections.emptyList();
    final List<Location> locations = new ArrayList<>();
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    if (fileIndex.isInContent(file) && !fileIndex.isInSource(file) && !fileIndex.isInLibraryClasses(file)) {
        final VirtualFile parent = file.getParent();
        final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
        if (contentRoot != null && parent != null) {
            final String relativePath = VfsUtilCore.getRelativePath(parent, contentRoot, '/');
            if (relativePath != null) {
                final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(project);
                final List<String> words = StringUtil.getWordsIn(relativePath);
                // put longer strings first
                Collections.sort(words, (o1, o2) -> o2.length() - o1.length());
                final GlobalSearchScope testScope = GlobalSearchScopesCore.projectTestScope(project);
                Set<PsiFile> resultFiles = null;
                for (String word : words) {
                    if (word.length() < 5) {
                        continue;
                    }
                    final Set<PsiFile> files = new THashSet<>();
                    searchHelper.processAllFilesWithWordInLiterals(word, testScope, new CommonProcessors.CollectProcessor<>(files));
                    if (resultFiles == null) {
                        resultFiles = files;
                    } else {
                        resultFiles.retainAll(files);
                    }
                    if (resultFiles.isEmpty())
                        break;
                }
                if (resultFiles != null) {
                    for (Iterator<PsiFile> iterator = resultFiles.iterator(); iterator.hasNext(); ) {
                        if (!VfsUtilCore.isAncestor(contentRoot, iterator.next().getVirtualFile(), true)) {
                            iterator.remove();
                        }
                    }
                    final String fileName = file.getName();
                    final String nameWithoutExtension = file.getNameWithoutExtension();
                    for (PsiFile resultFile : resultFiles) {
                        if (resultFile instanceof PsiClassOwner) {
                            final PsiClass[] classes = ((PsiClassOwner) resultFile).getClasses();
                            if (classes.length > 0) {
                                ContainerUtil.addIfNotNull(locations, getLocation(project, fileName, nameWithoutExtension, classes[0]));
                            }
                        }
                    }
                }
            }
        }
    }
    return locations;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashSet(gnu.trove.THashSet) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) CommonProcessors(com.intellij.util.CommonProcessors) PsiMemberParameterizedLocation(com.intellij.execution.junit2.PsiMemberParameterizedLocation) PsiLocation(com.intellij.execution.PsiLocation) MethodLocation(com.intellij.execution.junit2.info.MethodLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class TestCaseAsRelatedFileProvider method getItems.

@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
    final Editor editor = context.getData(CommonDataKeys.EDITOR);
    final Project project = context.getData(CommonDataKeys.PROJECT);
    final VirtualFile file = context.getData(CommonDataKeys.VIRTUAL_FILE);
    if (editor == null || file == null || project == null) {
        return Collections.emptyList();
    }
    final List<Location> locations = TestLocationDataRule.collectRelativeLocations(project, file);
    if (locations.isEmpty()) {
        return Collections.emptyList();
    }
    return ContainerUtil.map(locations, location -> new GotoRelatedItem(location.getPsiElement()));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Editor(com.intellij.openapi.editor.Editor) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class TaskExecutionView method getData.

@Nullable
@Override
public Object getData(@NonNls String dataId) {
    final TreeTableTree tree = myTreeTable.getTree();
    if (Location.DATA_KEYS.is(dataId)) {
        TreePath[] paths = tree.getSelectionModel().getSelectionPaths();
        if (paths != null && paths.length > 1) {
            final List<Location<?>> locations = new ArrayList<>(paths.length);
            for (TreePath path : paths) {
                if (tree.isPathSelected(path.getParentPath()))
                    continue;
                ExecutionInfo executionInfo = getSelectedExecution(path);
                if (executionInfo != null) {
                    final Location<?> location = (Location<?>) GradleRunnerUtil.getData(myProject, Location.DATA_KEY.getName(), executionInfo);
                    if (location != null) {
                        locations.add(location);
                    }
                }
            }
            return locations.isEmpty() ? null : locations.toArray(new Location[locations.size()]);
        }
    }
    if (Location.DATA_KEY.is(dataId)) {
        TreePath[] paths = tree.getSelectionModel().getSelectionPaths();
        if (paths != null && paths.length > 1) {
            final List<ExecutionInfo> executionInfos = ContainerUtil.newArrayListWithCapacity(paths.length);
            for (TreePath path : paths) {
                if (tree.isPathSelected(path.getParentPath()))
                    continue;
                ExecutionInfo executionInfo = getSelectedExecution(path);
                ContainerUtil.addIfNotNull(executionInfos, executionInfo);
            }
            return executionInfos.isEmpty() ? null : GradleRunnerUtil.getTaskLocation(myProject, executionInfos.toArray(new ExecutionInfo[executionInfos.size()]));
        }
    }
    final TreePath selectionPath = tree.getSelectionPath();
    if (selectionPath == null)
        return null;
    ExecutionInfo executionInfo = getSelectedExecution(selectionPath);
    if (executionInfo == null)
        return null;
    return GradleRunnerUtil.getData(myProject, dataId, executionInfo);
}
Also used : TreePath(javax.swing.tree.TreePath) TreeTableTree(com.intellij.ui.treeStructure.treetable.TreeTableTree) ArrayList(java.util.ArrayList) Location(com.intellij.execution.Location) Nullable(org.jetbrains.annotations.Nullable)

Example 25 with Location

use of com.intellij.execution.Location in project intellij-community by JetBrains.

the class GradleUrlProvider method getLocation.

@NotNull
@Override
public List<Location> getLocation(@NotNull String protocol, @NotNull String path, @NotNull Project project, @NotNull GlobalSearchScope scope) {
    if (!PROTOCOL_ID.equals(protocol))
        return Collections.emptyList();
    final String className = extractFullClassName(path);
    if (className == null)
        return Collections.emptyList();
    final PsiClass testClass = JavaPsiFacade.getInstance(project).findClass(className, GlobalSearchScope.allScope(project));
    if (testClass == null)
        return Collections.emptyList();
    final String methodName = extractMethodName(path);
    if (methodName == null) {
        return Collections.<Location>singletonList(new PsiLocation<>(project, testClass));
    }
    final PsiMethod[] methods = testClass.findMethodsByName(methodName, true);
    final List<Location> list = new ArrayList<>(methods.length);
    for (PsiMethod method : methods) {
        list.add(new PsiLocation<>(project, method));
    }
    return list;
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) ArrayList(java.util.ArrayList) PsiLocation(com.intellij.execution.PsiLocation) Location(com.intellij.execution.Location) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Location (com.intellij.execution.Location)62 PsiElement (com.intellij.psi.PsiElement)20 PsiClass (com.intellij.psi.PsiClass)19 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiMemberParameterizedLocation (com.intellij.execution.junit2.PsiMemberParameterizedLocation)17 PsiLocation (com.intellij.execution.PsiLocation)16 MethodLocation (com.intellij.execution.junit2.info.MethodLocation)16 Project (com.intellij.openapi.project.Project)15 Module (com.intellij.openapi.module.Module)13 PsiMethod (com.intellij.psi.PsiMethod)11 PsiFile (com.intellij.psi.PsiFile)8 Nullable (org.jetbrains.annotations.Nullable)8 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)7 SMTestProxy (com.intellij.execution.testframework.sm.runner.SMTestProxy)6 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 PatternConfigurationProducer (com.intellij.execution.junit.PatternConfigurationProducer)5 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)3 AbstractTestProxy (com.intellij.execution.testframework.AbstractTestProxy)3 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)3