Java Code Examples for com.intellij.psi.util.PsiTreeUtil#findElementOfClassAtRange()

The following examples show how to use com.intellij.psi.util.PsiTreeUtil#findElementOfClassAtRange() . 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: CSharpExpressionSurroundDescriptor.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
private static DotNetExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset)
{
	PsiElement element1 = file.findElementAt(startOffset);
	PsiElement element2 = file.findElementAt(endOffset - 1);
	if(element1 instanceof PsiWhiteSpace)
	{
		startOffset = element1.getTextRange().getEndOffset();
	}
	if(element2 instanceof PsiWhiteSpace)
	{
		endOffset = element2.getTextRange().getStartOffset();
	}
	DotNetExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, DotNetExpression.class);
	if(expression == null || expression.getTextRange().getEndOffset() != endOffset)
	{
		return null;
	}
	return expression;
}
 
Example 2
Source File: CSharpRefactoringUtil.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
public static DotNetExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset)
{
	PsiElement element1 = file.findElementAt(startOffset);
	PsiElement element2 = file.findElementAt(endOffset - 1);
	if(element1 instanceof PsiWhiteSpace)
	{
		startOffset = element1.getTextRange().getEndOffset();
	}
	if(element2 instanceof PsiWhiteSpace)
	{
		endOffset = element2.getTextRange().getStartOffset();
	}
	DotNetExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, DotNetExpression.class);
	if(expression == null || expression.getTextRange().getEndOffset() != endOffset)
	{
		return null;
	}
	if(expression instanceof CSharpReferenceExpression && expression.getParent() instanceof CSharpMethodCallExpressionImpl)
	{
		return null;
	}
	return expression;
}
 
Example 3
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
protected PsiNamedElement getVariable() {
  // todo we can use more specific class, shouldn't we?
  //Class clazz = myElementToRename != null? myElementToRename.getClass() : PsiNameIdentifierOwner.class; 
  if (myElementToRename != null && myElementToRename.isValid()) {
    if (Comparing.strEqual(myOldName, myElementToRename.getName())) return myElementToRename;
    if (myRenameOffset != null) return PsiTreeUtil.findElementOfClassAtRange(
      myElementToRename.getContainingFile(), myRenameOffset.getStartOffset(), myRenameOffset.getEndOffset(), PsiNameIdentifierOwner.class);
  }

  if (myRenameOffset != null) {
    final PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
    if (psiFile != null) {
      return PsiTreeUtil.findElementOfClassAtRange(psiFile, myRenameOffset.getStartOffset(), myRenameOffset.getEndOffset(), PsiNameIdentifierOwner.class);
    }
  }
  return myElementToRename;
}
 
Example 4
Source File: CompletionUtilCoreImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T extends PsiElement> T getOriginalElement(@Nonnull T psi, PsiFile containingFile) {
  if (containingFile != null && containingFile != containingFile.getOriginalFile() && psi.getTextRange() != null) {
    TextRange range = psi.getTextRange();
    Integer start = range.getStartOffset();
    Integer end = range.getEndOffset();
    final Document document = containingFile.getViewProvider().getDocument();
    if (document != null) {
      Document hostDocument = document instanceof DocumentWindow ? ((DocumentWindow)document).getDelegate() : document;
      OffsetTranslator translator = hostDocument.getUserData(OffsetTranslator.RANGE_TRANSLATION);
      if (translator != null) {
        if (document instanceof DocumentWindow) {
          TextRange translated = ((DocumentWindow)document).injectedToHost(new TextRange(start, end));
          start = translated.getStartOffset();
          end = translated.getEndOffset();
        }

        start = translator.translateOffset(start);
        end = translator.translateOffset(end);
        if (start == null || end == null) {
          return null;
        }

        if (document instanceof DocumentWindow) {
          start = ((DocumentWindow)document).hostToInjected(start);
          end = ((DocumentWindow)document).hostToInjected(end);
        }
      }
    }
    //noinspection unchecked
    return (T)PsiTreeUtil.findElementOfClassAtRange(containingFile.getOriginalFile(), start, end, psi.getClass());
  }

  return psi;
}
 
Example 5
Source File: SelectionBasedPsiElementInternalAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected T getElementFromSelection(@Nonnull PsiFile file, @Nonnull SelectionModel selectionModel) {
  final int selectionStart = selectionModel.getSelectionStart();
  final int selectionEnd = selectionModel.getSelectionEnd();
  return PsiTreeUtil.findElementOfClassAtRange(file, selectionStart, selectionEnd, myClass);
}