Java Code Examples for com.intellij.usageView.UsageInfo#getElement()

The following examples show how to use com.intellij.usageView.UsageInfo#getElement() . 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: ChangeSignatureProcessorBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected List<UsageInfo> filterUsages(List<UsageInfo> infos) {
  Map<PsiElement, MoveRenameUsageInfo> moveRenameInfos = new HashMap<PsiElement, MoveRenameUsageInfo>();
  Set<PsiElement> usedElements = new HashSet<PsiElement>();

  List<UsageInfo> result = new ArrayList<UsageInfo>(infos.size() / 2);
  for (UsageInfo info : infos) {
    LOG.assertTrue(info != null, getClass());
    PsiElement element = info.getElement();
    if (info instanceof MoveRenameUsageInfo) {
      if (usedElements.contains(element)) continue;
      moveRenameInfos.put(element, (MoveRenameUsageInfo)info);
    }
    else {
      moveRenameInfos.remove(element);
      usedElements.add(element);
      if (!(info instanceof PossiblyIncorrectUsage) || ((PossiblyIncorrectUsage)info).isCorrect()) {
        result.add(info);
      }
    }
  }
  result.addAll(moveRenameInfos.values());
  return result;
}
 
Example 2
Source File: UsagePreviewPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private String cannotPreviewMessage(@Nullable List<? extends UsageInfo> infos) {
  if (infos == null || infos.isEmpty()) {
    return UsageViewBundle.message("select.the.usage.to.preview", myPresentation.getUsagesWord());
  }
  PsiFile psiFile = null;
  for (UsageInfo info : infos) {
    PsiElement element = info.getElement();
    if (element == null) continue;
    PsiFile file = element.getContainingFile();
    if (psiFile == null) {
      psiFile = file;
    }
    else {
      if (psiFile != file) {
        return UsageViewBundle.message("several.occurrences.selected");
      }
    }
  }
  return null;
}
 
Example 3
Source File: PullUpProcessor.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@Override
protected void performRefactoring(UsageInfo[] usages) {
  moveMembersToBase();
  moveFieldInitializations();
  for (UsageInfo usage : usages) {
    PsiElement element = usage.getElement();
    if (element == null) continue;

    PullUpHelper<MemberInfo> processor = getProcessor(element);
    processor.updateUsage(element);
  }
  ApplicationManager.getApplication().invokeLater(new Runnable() {
    @Override
    public void run() {
      processMethodsDuplicates();
    }
  }, ModalityState.NON_MODAL, myProject.getDisposed());
}
 
Example 4
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test if it can find usage from a Spring Service bean method to it's Camel routes bean method
 */
public void testFindUsageFromSpringServiceMethodToBeanDSL() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaSpringServiceBeanTestData.java", "CompleteJavaSpringServiceBeanRouteTestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(\"myServiceBean\", \"anotherBeanMethod\")", referenceElement.getParent().getText());
}
 
Example 5
Source File: GenericInlineHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void inlineReference(final UsageInfo usage,
                                   final PsiElement element,
                                   final Map<Language, InlineHandler.Inliner> inliners) {
  PsiElement usageElement = usage.getElement();
  if (usageElement == null) return;
  final Language language = usageElement.getLanguage();
  final InlineHandler.Inliner inliner = inliners.get(language);
  if (inliner != null) {
    inliner.inlineUsage(usage, element);
  }
}
 
Example 6
Source File: UsageProjectTreeNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean contains(@Nonnull VirtualFile file) {
  final UsageInfo info = getValue();
  if (info == null) return false;
  final PsiElement element = info.getElement();
  return element != null && file.equals(element.getContainingFile().getVirtualFile());
}
 
Example 7
Source File: UsageInfoToUsageConverter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static Usage convert(@Nonnull PsiElement[] primaryElements, @Nonnull UsageInfo usageInfo) {
  PsiElement usageElement = usageInfo.getElement();
  for(ReadWriteAccessDetector detector: ReadWriteAccessDetector.EP_NAME.getExtensionList()) {
    if (isReadWriteAccessibleElements(primaryElements, detector)) {
      final ReadWriteAccessDetector.Access rwAccess = detector.getExpressionAccess(usageElement);
      return new ReadWriteAccessUsageInfo2UsageAdapter(usageInfo,
                                                       rwAccess != ReadWriteAccessDetector.Access.Write,
                                                       rwAccess != ReadWriteAccessDetector.Access.Read);
    }
  }
  return new UsageInfo2UsageAdapter(usageInfo);
}
 
Example 8
Source File: PushDownProcessor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Override
protected void performRefactoring(UsageInfo[] usages) {
  try {
    encodeRefs();
    if (myCreateClassDlg != null) { //usages.length == 0
      final PsiClass psiClass =
        CreateSubclassAction.createSubclass(myClass, myCreateClassDlg.getTargetDirectory(), myCreateClassDlg.getClassName());
      if (psiClass != null) {
        pushDownToClass(psiClass);
      }
    }

    PsiReference reference;
    PsiElement resolve;

    for (UsageInfo usage : usages) {
      if (usage.getElement() instanceof PsiClass) {
        final PsiClass targetClass = (PsiClass)usage.getElement();
        pushDownToClass(targetClass);
      }
    }
    removeFromTargetClass();
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
 
Example 9
Source File: PushDownProcessor.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
protected RefactoringEventData getAfterData(UsageInfo[] usages) {
  final List<PsiElement> elements = new ArrayList<PsiElement>();
  for (UsageInfo usage : usages) {
    PsiElement element = usage.getElement();
    if (element instanceof PsiClass) {
      elements.add(element);
    }
  }
  RefactoringEventData data = new RefactoringEventData();
  data.addElements(elements);
  return data;
}
 
Example 10
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test if the find usage is working with camel DSL bean method call with parameters
 */
public void testFindUsageFromWithAmbiguousToBeanDSLWithParameters() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaBeanTest3Data.java", "CompleteJavaBeanRoute8TestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(beanTestData, \"myAmbiguousMethod(${body})\")", referenceElement.getParent().getText());
}
 
Example 11
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public void testFindUsageFromWithOverloadedMethodToBeanDSL() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaBeanTest2Data.java", "CompleteJavaBeanRoute7TestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(beanTestData, \"myOverLoadedBean\")", referenceElement.getParent().getText());
}
 
Example 12
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test if it can find usage from a Spring Repository bean method to it's Camel routes bean method
 */
public void testFindUsageFromSpringRepositoryMethodToBeanDSL() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaSpringRepositoryBeanTestData.java", "CompleteJavaSpringRepositoryBeanRouteTestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(\"myRepositoryBean\", \"anotherBeanMethod\")", referenceElement.getParent().getText());
}
 
Example 13
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Test if it can find usage from a Spring Component bean method to it's Camel routes bean method
 */
public void testFindUsageFromSpringComponentMethodToBeanDSL() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaSpringComponentBeanTestData.java", "CompleteJavaSpringComponentBeanRouteTestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(\"myComponentBean\", \"anotherBeanMethod\")", referenceElement.getParent().getText());
}
 
Example 14
Source File: CamelBeanMethodReferenceTest.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public void testFindUsageFromMethodToBeanDSL() {
    Collection<UsageInfo> usageInfos = myFixture.testFindUsages("CompleteJavaBeanTestData.java", "CompleteJavaBeanRoute1TestData.java");
    assertEquals(1, usageInfos.size());

    final UsageInfo usageInfo = usageInfos.iterator().next();
    final PsiElement referenceElement = usageInfo.getElement();
    assertThat(referenceElement, instanceOf(PsiLiteralExpression.class));
    assertEquals("(beanTestData, \"anotherBeanMethod\")", referenceElement.getParent().getText());
}
 
Example 15
Source File: CSharpChangeSignatureUsageProcessor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
@RequiredReadAction
public boolean processUsage(@Nonnull ChangeInfo changeInfo, @Nonnull UsageInfo usageInfo, boolean beforeMethodChange, @Nonnull UsageInfo[] usages)
{
	if(!(changeInfo instanceof CSharpChangeInfo))
	{
		return false;
	}
	PsiElement element = usageInfo.getElement();
	if(!(element instanceof DotNetReferenceExpression))
	{
		return false;
	}

	if(!beforeMethodChange)
	{
		return true;
	}

	if(changeInfo.isNameChanged())
	{
		((DotNetReferenceExpression) element).handleElementRename(changeInfo.getNewName());
	}

	if(((CSharpChangeInfo) changeInfo).isParametersChanged())
	{
		PsiElement parent = element.getParent();
		if(parent instanceof CSharpCallArgumentListOwner)
		{
			CSharpCallArgumentList parameterList = ((CSharpCallArgumentListOwner) parent).getParameterList();
			if(parameterList == null)
			{
				return true;
			}

			CSharpParameterInfo[] newParameters = ((CSharpChangeInfo) changeInfo).getNewParameters();

			DotNetExpression[] expressions = parameterList.getExpressions();
			String[] newArguments = new String[newParameters.length];

			for(CSharpParameterInfo newParameter : newParameters)
			{
				if(newParameter.getOldIndex() != -1)
				{
					newArguments[newParameter.getNewIndex()] = expressions[newParameter.getOldIndex()].getText();
				}
				else
				{
					newArguments[newParameter.getNewIndex()] = newParameter.getDefaultValue();
				}
			}

			StringBuilder builder = new StringBuilder("test(");
			builder.append(StringUtil.join(newArguments, ", "));
			builder.append(");");

			DotNetStatement statement = CSharpFileFactory.createStatement(usageInfo.getProject(), builder);
			CSharpCallArgumentListOwner call = PsiTreeUtil.getChildOfType(statement, CSharpCallArgumentListOwner.class);
			parameterList.replace(call.getParameterList());
		}
		return true;
	}
	return false;
}
 
Example 16
Source File: UsageContextCallHierarchyPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static PsiElement getElementToSliceOn(@Nonnull List<? extends UsageInfo> infos) {
  UsageInfo info = infos.get(0);
  return info.getElement();
}
 
Example 17
Source File: RenameUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void rename(UsageInfo info, String newName) throws IncorrectOperationException {
  if (info.getElement() == null) return;
  PsiReference ref = info.getReference();
  if (ref == null) return;
  ref.handleElementRename(newName);
}
 
Example 18
Source File: BaseRefactoringProcessor.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void doRefactoring(@Nonnull final Collection<UsageInfo> usageInfoSet) {
  for (Iterator<UsageInfo> iterator = usageInfoSet.iterator(); iterator.hasNext(); ) {
    UsageInfo usageInfo = iterator.next();
    final PsiElement element = usageInfo.getElement();
    if (element == null || !isToBeChanged(usageInfo)) {
      iterator.remove();
    }
  }

  String commandName = getCommandName();
  LocalHistoryAction action = LocalHistory.getInstance().startAction(commandName);

  final UsageInfo[] writableUsageInfos = usageInfoSet.toArray(UsageInfo.EMPTY_ARRAY);
  try {
    PsiDocumentManager.getInstance(myProject).commitAllDocuments();
    RefactoringListenerManagerImpl listenerManager = (RefactoringListenerManagerImpl)RefactoringListenerManager.getInstance(myProject);
    myTransaction = listenerManager.startTransaction();
    final Map<RefactoringHelper, Object> preparedData = new LinkedHashMap<>();
    final Runnable prepareHelpersRunnable = () -> {
      for (final RefactoringHelper helper : RefactoringHelper.EP_NAME.getExtensionList()) {
        Object operation = ReadAction.compute(() -> helper.prepareOperation(writableUsageInfos));
        preparedData.put(helper, operation);
      }
    };

    ProgressManager.getInstance().runProcessWithProgressSynchronously(prepareHelpersRunnable, "Prepare ...", false, myProject);

    Runnable performRefactoringRunnable = () -> {
      final String refactoringId = getRefactoringId();
      if (refactoringId != null) {
        RefactoringEventData data = getBeforeData();
        if (data != null) {
          data.addUsages(usageInfoSet);
        }
        myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(refactoringId, data);
      }

      try {
        if (refactoringId != null) {
          UndoableAction action1 = new UndoRefactoringAction(myProject, refactoringId);
          UndoManager.getInstance(myProject).undoableActionPerformed(action1);
        }

        performRefactoring(writableUsageInfos);
      }
      finally {
        if (refactoringId != null) {
          myProject.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));
        }
      }
    };
    ApplicationEx2 app = (ApplicationEx2)Application.get();
    if (Registry.is("run.refactorings.under.progress")) {
      app.runWriteActionWithNonCancellableProgressInDispatchThread(commandName, myProject, null, indicator -> performRefactoringRunnable.run());
    }
    else {
      app.runWriteAction(performRefactoringRunnable);
    }

    DumbService.getInstance(myProject).completeJustSubmittedTasks();

    for (Map.Entry<RefactoringHelper, Object> e : preparedData.entrySet()) {
      //noinspection unchecked
      e.getKey().performOperation(myProject, e.getValue());
    }
    myTransaction.commit();
    if (Registry.is("run.refactorings.under.progress")) {
      app.runWriteActionWithNonCancellableProgressInDispatchThread(commandName, myProject, null, indicator -> performPsiSpoilingRefactoring());
    }
    else {
      app.runWriteAction(this::performPsiSpoilingRefactoring);
    }
  }
  finally {
    action.finish();
  }

  int count = writableUsageInfos.length;
  if (count > 0) {
    StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.refactoring.result", count));
  }
  else {
    if (!isPreviewUsages(writableUsageInfos)) {
      StatusBarUtil.setStatusBarInfo(myProject, RefactoringBundle.message("statusBar.noUsages"));
    }
  }
}