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;
}
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;
}
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()));
}
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);
}
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;
}
Aggregations