Java Code Examples for com.intellij.psi.util.PsiUtilCore#toPsiElementArray()

The following examples show how to use com.intellij.psi.util.PsiUtilCore#toPsiElementArray() . 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: RTMergerTreeStructureProvider.java    From react-templates-plugin with MIT License 6 votes vote down vote up
private static PsiElement[] collectFormPsiElements(Collection<AbstractTreeNode> selected) {
    Set<PsiElement> result = new HashSet<PsiElement>();
    for (AbstractTreeNode node : selected) {
        if (node.getValue() instanceof RTFile) {
            RTFile form = (RTFile) node.getValue();
            result.add(form.getRtjsFile());
            if (form.getController() != null) {
                result.add(form.getController());
            }
            ContainerUtil.addAll(result, form.getRtFile());
        } else if (node.getValue() instanceof PsiElement) {
            result.add((PsiElement) node.getValue());
        }
    }
    return PsiUtilCore.toPsiElementArray(result);
}
 
Example 2
Source File: PackageViewPane.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteElement(@Nonnull DataContext dataContext) {
  List<PsiDirectory> allElements = Arrays.asList(getSelectedDirectories());
  List<PsiElement> validElements = new ArrayList<PsiElement>();
  for (PsiElement psiElement : allElements) {
    if (psiElement != null && psiElement.isValid()) validElements.add(psiElement);
  }
  final PsiElement[] elements = PsiUtilCore.toPsiElementArray(validElements);

  LocalHistoryAction a = LocalHistory.getInstance().startAction(IdeBundle.message("progress.deleting"));
  try {
    DeleteHandler.deletePsiElement(elements, myProject);
  }
  finally {
    a.finish();
  }
}
 
Example 3
Source File: Trees.java    From antlr4-intellij-adaptor with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/** Get all non-WS, non-Comment children of t */
@NotNull
public static PsiElement[] getChildren(PsiElement t) {
	if ( t==null ) return PsiElement.EMPTY_ARRAY;

	PsiElement psiChild = t.getFirstChild();
	if (psiChild == null) return PsiElement.EMPTY_ARRAY;

	List<PsiElement> result = new ArrayList<>();
	while (psiChild != null) {
		if ( !(psiChild instanceof PsiComment) &&
			 !(psiChild instanceof PsiWhiteSpace) )
		{
			result.add(psiChild);
		}
		psiChild = psiChild.getNextSibling();
	}
	return PsiUtilCore.toPsiElementArray(result);
}
 
Example 4
Source File: CSharpFindUsageHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
@RequiredReadAction
public PsiElement[] getPrimaryElements()
{
	PsiElement psiElement = getPsiElement();
	if(OverrideUtil.isAllowForOverride(psiElement))
	{
		Collection<DotNetVirtualImplementOwner> members = OverrideUtil.collectOverridingMembers((DotNetVirtualImplementOwner) psiElement);
		if(!members.isEmpty())
		{
			MessageDialogBuilder.YesNo builder = MessageDialogBuilder.yesNo("Find Usage", "Search for base target ir current target?");
			builder = builder.yesText("Base Target");
			builder = builder.noText("This Target");

			if(builder.show() == Messages.OK)
			{
				return PsiUtilCore.toPsiElementArray(members);
			}
		}
	}
	return super.getPrimaryElements();
}
 
Example 5
Source File: PsiDirectoryImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public PsiElement[] getChildren() {
  checkValid();

  VirtualFile[] files = myFile.getChildren();
  final ArrayList<PsiElement> children = new ArrayList<PsiElement>(files.length);
  processChildren(new PsiElementProcessor<PsiFileSystemItem>() {
    @Override
    public boolean execute(@Nonnull final PsiFileSystemItem element) {
      children.add(element);
      return true;
    }
  });

  return PsiUtilCore.toPsiElementArray(children);
}
 
Example 6
Source File: FavoritesTreeViewPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteElement(@Nonnull DataContext dataContext) {
  List<PsiElement> allElements = Arrays.asList(getElementsToDelete());
  List<PsiElement> validElements = new ArrayList<>();
  for (PsiElement psiElement : allElements) {
    if (psiElement != null && psiElement.isValid()) validElements.add(psiElement);
  }
  final PsiElement[] elements = PsiUtilCore.toPsiElementArray(validElements);

  LocalHistoryAction a = LocalHistory.getInstance().startAction(IdeBundle.message("progress.deleting"));
  try {
    DeleteHandler.deletePsiElement(elements, myProject);
  }
  finally {
    a.finish();
  }
}
 
Example 7
Source File: ASTDelegatePsiElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
@RequiredReadAction
public PsiElement[] getChildren() {
  PsiElement psiChild = getFirstChild();
  if (psiChild == null) return PsiElement.EMPTY_ARRAY;

  List<PsiElement> result = new ArrayList<PsiElement>();
  while (psiChild != null) {
    if (psiChild.getNode() instanceof CompositeElement) {
      result.add(psiChild);
    }
    psiChild = psiChild.getNextSibling();
  }
  return PsiUtilCore.toPsiElementArray(result);
}
 
Example 8
Source File: GotoTestOrCodeHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
protected GotoData getSourceAndTargetElements(final Editor editor, final PsiFile file) {
  PsiElement selectedElement = getSelectedElement(editor, file);
  PsiElement sourceElement = TestFinderHelper.findSourceElement(selectedElement);
  if (sourceElement == null) return null;

  List<AdditionalAction> actions = new SmartList<>();

  Collection<PsiElement> candidates;
  if (TestFinderHelper.isTest(selectedElement)) {
    candidates = TestFinderHelper.findClassesForTest(selectedElement);
  }
  else {
    candidates = TestFinderHelper.findTestsForClass(selectedElement);
    final TestCreator creator = LanguageTestCreators.INSTANCE.forLanguage(file.getLanguage());
    if (creator != null && creator.isAvailable(file.getProject(), editor, file)) {
      actions.add(new AdditionalAction() {
        @Nonnull
        @Override
        public String getText() {
          return "Create New Test...";
        }

        @Override
        public Icon getIcon() {
          return AllIcons.Actions.IntentionBulb;
        }

        @Override
        public void execute() {
          creator.createTest(file.getProject(), editor, file);
        }
      });
    }
  }

  return new GotoData(sourceElement, PsiUtilCore.toPsiElementArray(candidates), actions);
}
 
Example 9
Source File: HighlightUsagesHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void highlightOtherOccurrences(final List<PsiElement> otherOccurrences, Editor editor, boolean clearHighlights) {
  EditorColorsManager manager = EditorColorsManager.getInstance();
  TextAttributes attributes = manager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);

  PsiElement[] elements = PsiUtilCore.toPsiElementArray(otherOccurrences);
  doHighlightElements(editor, elements, attributes, clearHighlights);
}
 
Example 10
Source File: ImplementationViewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiElement[] collectNonBinaryElements() {
  List<PsiElement> result = new ArrayList<PsiElement>();
  for (PsiElement element : myElements) {
    if (!(element instanceof PsiBinaryFile)) {
      result.add(element);
    }
  }
  return PsiUtilCore.toPsiElementArray(result);
}
 
Example 11
Source File: ShowImplementationsAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static PsiElement[] filterElements(@Nonnull final PsiElement[] targetElements) {
  final Set<PsiElement> unique = new LinkedHashSet<>(Arrays.asList(targetElements));
  for (final PsiElement elt : targetElements) {
    ApplicationManager.getApplication().runReadAction(() -> {
      final PsiFile containingFile = elt.getContainingFile();
      LOG.assertTrue(containingFile != null, elt);
      PsiFile psiFile = containingFile.getOriginalFile();
      if (psiFile.getVirtualFile() == null) unique.remove(elt);
    });
  }
  // special case for Python (PY-237)
  // if the definition is the tree parent of the target element, filter out the target element
  for (int i = 1; i < targetElements.length; i++) {
    final PsiElement targetElement = targetElements[i];
    if (ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
      @Override
      public Boolean compute() {
        return PsiTreeUtil.isAncestor(targetElement, targetElements[0], true);
      }
    })) {
      unique.remove(targetElements[0]);
      break;
    }
  }
  return PsiUtilCore.toPsiElementArray(unique);
}
 
Example 12
Source File: StructureViewComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public Object getData(@Nonnull Key dataId) {
  if (CommonDataKeys.PSI_ELEMENT == dataId) {
    PsiElement element = getSelectedValues().filter(PsiElement.class).single();
    return element != null && element.isValid() ? element : null;
  }
  if (LangDataKeys.PSI_ELEMENT_ARRAY == dataId) {
    return PsiUtilCore.toPsiElementArray(getSelectedValues().filter(PsiElement.class).toList());
  }
  if (PlatformDataKeys.FILE_EDITOR == dataId) {
    return myFileEditor;
  }
  if (PlatformDataKeys.CUT_PROVIDER == dataId) {
    return myCopyPasteDelegator.getCutProvider();
  }
  if (PlatformDataKeys.COPY_PROVIDER == dataId) {
    return myCopyPasteDelegator.getCopyProvider();
  }
  if (PlatformDataKeys.PASTE_PROVIDER == dataId) {
    return myCopyPasteDelegator.getPasteProvider();
  }
  if (CommonDataKeys.NAVIGATABLE == dataId) {
    List<Object> list = JBIterable.of(getTree().getSelectionPaths()).map(TreePath::getLastPathComponent).map(StructureViewComponent::unwrapNavigatable).toList();
    Object[] selectedElements = list.isEmpty() ? null : ArrayUtil.toObjectArray(list);
    if (selectedElements == null || selectedElements.length == 0) return null;
    if (selectedElements[0] instanceof Navigatable) {
      return selectedElements[0];
    }
  }
  if (PlatformDataKeys.HELP_ID == dataId) {
    return getHelpID();
  }
  if (CommonDataKeys.PROJECT == dataId) {
    return myProject;
  }
  return super.getData(dataId);
}
 
Example 13
Source File: OfflineProblemDescriptorNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static PsiElement[] getElementsIntersectingRange(PsiFile file, final int startOffset, final int endOffset) {
  final FileViewProvider viewProvider = file.getViewProvider();
  final Set<PsiElement> result = new LinkedHashSet<PsiElement>();
  for (Language language : viewProvider.getLanguages()) {
    final PsiFile psiRoot = viewProvider.getPsi(language);
    if (HighlightingLevelManager.getInstance(file.getProject()).shouldInspect(psiRoot)) {
      result.addAll(CollectHighlightsUtil.getElementsInRange(psiRoot, startOffset, endOffset, true));
    }
  }
  return PsiUtilCore.toPsiElementArray(result);
}
 
Example 14
Source File: PsiSearchHelperImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiElement[] findCommentsContainingIdentifier(@Nonnull String identifier, @Nonnull SearchScope searchScope) {
  final List<PsiElement> result = Collections.synchronizedList(new ArrayList<>());
  Processor<PsiElement> processor = Processors.cancelableCollectProcessor(result);
  processCommentsContainingIdentifier(identifier, searchScope, processor);
  return PsiUtilCore.toPsiElementArray(result);
}
 
Example 15
Source File: LocalSearchScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
public SearchScope union(LocalSearchScope scope2) {
  if (equals(scope2)) return this;
  PsiElement[] elements1 = getScope();
  PsiElement[] elements2 = scope2.getScope();
  boolean[] united = new boolean[elements2.length];
  List<PsiElement> result = new ArrayList<PsiElement>();
  loop1:
  for (final PsiElement element1 : elements1) {
    for (int j = 0; j < elements2.length; j++) {
      final PsiElement element2 = elements2[j];
      final PsiElement unionElement = scopeElementsUnion(element1, element2);
      if (unionElement != null && unionElement.getContainingFile() != null) {
        result.add(unionElement);
        united[j] = true;
        break loop1;
      }
    }
    result.add(element1);
  }
  for (int i = 0; i < united.length; i++) {
    final boolean b = united[i];
    if (!b) {
      result.add(elements2[i]);
    }
  }
  return new LocalSearchScope(PsiUtilCore.toPsiElementArray(result));
}
 
Example 16
Source File: LocalSearchScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static LocalSearchScope intersection(LocalSearchScope scope1, LocalSearchScope scope2) {
  List<PsiElement> result = new ArrayList<PsiElement>();
  final PsiElement[] elements1 = scope1.myScope;
  final PsiElement[] elements2 = scope2.myScope;
  for (final PsiElement element1 : elements1) {
    for (final PsiElement element2 : elements2) {
      final PsiElement element = intersectScopeElements(element1, element2);
      if (element != null) {
        result.add(element);
      }
    }
  }
  return new LocalSearchScope(PsiUtilCore.toPsiElementArray(result), null, scope1.myIgnoreInjectedPsi || scope2.myIgnoreInjectedPsi);
}
 
Example 17
Source File: CommanderPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiElement[] getSelectedElements() {
  if (myBuilder == null) return PsiElement.EMPTY_ARRAY;
  final int[] indices = myList.getSelectedIndices();

  final ArrayList<PsiElement> elements = new ArrayList<>();
  for (int index : indices) {
    final PsiElement element = getSelectedElement(index);
    if (element != null) {
      elements.add(element);
    }
  }

  return PsiUtilCore.toPsiElementArray(elements);
}
 
Example 18
Source File: HierarchyBrowserBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected PsiElement[] getSelectedElements() {
  HierarchyNodeDescriptor[] descriptors = getSelectedDescriptors();
  ArrayList<PsiElement> elements = new ArrayList<PsiElement>();
  for (HierarchyNodeDescriptor descriptor : descriptors) {
    PsiElement element = getElementFromDescriptor(descriptor);
    if (element != null) elements.add(element);
  }
  return PsiUtilCore.toPsiElementArray(elements);
}
 
Example 19
Source File: PsiElementProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public PsiElement[] toArray() {
  return PsiUtilCore.toPsiElementArray(myCollection);
}
 
Example 20
Source File: BaseRefactoringProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean ensureFilesWritable(@Nonnull Project project, @Nonnull Collection<? extends PsiElement> elements) {
  PsiElement[] psiElements = PsiUtilCore.toPsiElementArray(elements);
  return CommonRefactoringUtil.checkReadOnlyStatus(project, psiElements);
}