Java Code Examples for com.intellij.psi.PsiElement#getNavigationElement()

The following examples show how to use com.intellij.psi.PsiElement#getNavigationElement() . 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: IdeHelper.java    From yiistorm with MIT License 6 votes vote down vote up
public static void navigateToPsiElement(PsiElement psiElement) {
    Project project = psiElement.getProject();
    PsiElement navElement = psiElement.getNavigationElement();
    navElement = TargetElementUtilBase.getInstance().getGotoDeclarationTarget(psiElement, navElement);
    if (navElement instanceof Navigatable) {
        if (((Navigatable) navElement).canNavigate()) {
            ((Navigatable) navElement).navigate(true);
        }
    } else if (navElement != null) {
        int navOffset = navElement.getTextOffset();
        VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navElement);
        if (virtualFile != null) {
            new OpenFileDescriptor(project, virtualFile, navOffset).navigate(true);
        }
    }
}
 
Example 2
Source File: CommonHelper.java    From yiistorm with MIT License 6 votes vote down vote up
public static PsiElement getFileByClass(String actionClass, Project project) {
    if (actionClass != null) {
        List<PsiElement> elements = PsiPhpHelper.getPsiElementsFromClassName(actionClass, project);
        if (elements.size() > 0) {
            PsiElement element = elements.get(0);
            PsiElement navElement = element.getNavigationElement();
            navElement = TargetElementUtilBase.getInstance().getGotoDeclarationTarget(element, navElement);
            if (navElement instanceof Navigatable) {
                if (((Navigatable) navElement).canNavigate()) {
                    ((Navigatable) navElement).navigate(true);
                }
            } else if (navElement != null) {
                int navOffset = navElement.getTextOffset();
                VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navElement);
                if (virtualFile != null) {
                    new OpenFileDescriptor(project, virtualFile, navOffset).navigate(true);
                }
            }
        }
    }
    return null;
}
 
Example 3
Source File: ARRelationReferenceProvider.java    From yiistorm with MIT License 6 votes vote down vote up
/**
 * Search file by Class name
 *
 * @param className Name of class
 * @return VirtualFile
 */
protected static VirtualFile getClassFile(String className) {
    String namespacedName = CommonHelper.prepareClassName(className);
    String cleanName = CommonHelper.getCleanClassName(className);
    List<PsiElement> elements = PsiPhpHelper.getPsiElementsFromClassName(cleanName, YiiPsiReferenceProvider.project);
    if (elements.size() > 0) {
        for (PsiElement element : elements) {
            String elementName = "";
            PsiElement namespaceElement = ExtendedPsiPhpHelper.getNamespaceElement(element);
            if (namespaceElement != null) {
                elementName = ExtendedPsiPhpHelper.getNamespaceFullName(namespaceElement) + "\\";
            }
            elementName += PsiPhpHelper.getClassIdentifierName(element);
            PsiElement navElement = element.getNavigationElement();
            if (namespacedName.equals(elementName)) {
                navElement = TargetElementUtilBase.getInstance().getGotoDeclarationTarget(element, navElement);
                if (navElement != null) {
                    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navElement);
                    return virtualFile;
                }
            }
        }
    }
    return null;
}
 
Example 4
Source File: EditSourceUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static Navigatable getDescriptor(final PsiElement element) {
  if (!canNavigate(element)) {
    return null;
  }
  if (element instanceof PomTargetPsiElement) {
    return ((PomTargetPsiElement)element).getTarget();
  }
  final PsiElement navigationElement = element.getNavigationElement();
  if (navigationElement instanceof PomTargetPsiElement) {
    return ((PomTargetPsiElement)navigationElement).getTarget();
  }
  final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
  final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(navigationElement);
  if (virtualFile == null || !virtualFile.isValid()) {
    return null;
  }
  OpenFileDescriptor desc = new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset);
  desc.setUseCurrentWindow(FileEditorManager.USE_CURRENT_WINDOW.isIn(navigationElement));
  return desc;
}
 
Example 5
Source File: BlazeGoImportResolverTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
private ResolvedSubject assertThatResolveCaret(int index) {
  Editor editor = testFixture.getEditor();
  Caret caret = editor.getCaretModel().getAllCarets().get(index);
  PsiElement resolved =
      GotoDeclarationAction.findTargetElement(getProject(), editor, caret.getOffset());
  if (resolved instanceof PomTargetPsiElement) {
    PomTarget target = ((PomTargetPsiElement) resolved).getTarget();
    if (target instanceof GoPackage) {
      resolved = ((GoPackage) target).getNavigableElement();
    }
  } else if (resolved != null) {
    resolved = resolved.getNavigationElement();
  }
  return new ResolvedSubject(resolved);
}
 
Example 6
Source File: CSharpLikeMethodDeclarationImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public static boolean isEquivalentTo(@Nonnull PsiElement o1, @Nullable PsiElement o2)
{
	if(o2 == null)
	{
		return false;
	}
	PsiElement originalElement1 = o1.getOriginalElement();
	PsiElement originalElement2 = o2.getOriginalElement();

	if(o1.getUserData(CSharpResolveUtil.EXTENSION_METHOD_WRAPPER) == originalElement2)
	{
		return true;
	}

	if(originalElement1 == originalElement2)
	{
		return true;
	}

	if(o1 instanceof CSharpConstructorDeclaration && o2 instanceof CSharpTypeDeclaration)
	{
		// default constructor builder
		PsiElement navigationElement = o1.getNavigationElement();
		if(navigationElement == o2)
		{
			return true;
		}
	}

	return false;
}
 
Example 7
Source File: CSharpGotoDeclarationHandler.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@RequiredReadAction
public PsiElement getGotoDeclarationTarget(PsiElement psiElement, Editor editor)
{
	IElementType elementType = PsiUtilCore.getElementType(psiElement);
	if(elementType == CSharpSoftTokens.VAR_KEYWORD)
	{
		PsiElement maybeType = psiElement.getParent();
		if(maybeType instanceof CSharpNativeType)
		{
			PsiElement maybeVar = maybeType.getParent();
			if(maybeVar instanceof DotNetVariable)
			{
				DotNetTypeRef ref = ((DotNetVariable) maybeVar).toTypeRef(true);
				return ref.resolve().getElement();
			}
		}
	}
	else if(elementType == CSharpTokens.DARROW)
	{
		if(psiElement.getParent() instanceof CSharpLambdaExpressionImpl)
		{
			CSharpLambdaResolveResult typeRef = CSharpLambdaExpressionImplUtil.resolveLeftLambdaTypeRef(psiElement.getParent());

			if(typeRef != null)
			{
				PsiElement element = typeRef.getElement();
				return element != null ? element.getNavigationElement() : null;
			}
		}
	}
	return null;
}
 
Example 8
Source File: GotoClassAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void handleSubMemberNavigation(ChooseByNamePopup popup, Object element) {
  if (element instanceof PsiElement && ((PsiElement)element).isValid()) {
    PsiElement psiElement = getElement(((PsiElement)element), popup);
    psiElement = psiElement.getNavigationElement();
    VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);

    if (file != null && popup.getLinePosition() != -1) {
      OpenFileDescriptor descriptor = new OpenFileDescriptor(psiElement.getProject(), file, popup.getLinePosition(), popup.getColumnPosition());
      Navigatable n = descriptor.setUseCurrentWindow(popup.isOpenInCurrentWindowRequested());
      if (n.canNavigate()) {
        n.navigate(true);
        return;
      }
    }

    if (file != null && popup.getMemberPattern() != null) {
      NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
      Navigatable member = findMember(popup.getMemberPattern(), popup.getTrimmedText(), psiElement, file);
      if (member != null) {
        member.navigate(true);
      }
    }

    NavigationUtil.activateFileWithPsiElement(psiElement, !popup.isOpenInCurrentWindowRequested());
  }
  else {
    EditSourceUtil.navigate(((NavigationItem)element), true, popup.isOpenInCurrentWindowRequested());
  }
}
 
Example 9
Source File: NavigationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean activatePsiElementIfOpen(@Nonnull PsiElement elt, boolean searchForOpen, boolean requestFocus) {
  if (!elt.isValid()) return false;
  elt = elt.getNavigationElement();
  final PsiFile file = elt.getContainingFile();
  if (file == null || !file.isValid()) return false;

  VirtualFile vFile = file.getVirtualFile();
  if (vFile == null) return false;

  if (!EditorHistoryManager.getInstance(elt.getProject()).hasBeenOpen(vFile)) return false;

  final FileEditorManager fem = FileEditorManager.getInstance(elt.getProject());
  if (!fem.isFileOpen(vFile)) {
    fem.openFile(vFile, requestFocus, searchForOpen);
  }

  final TextRange range = elt.getTextRange();
  if (range == null) return false;

  final FileEditor[] editors = fem.getEditors(vFile);
  for (FileEditor editor : editors) {
    if (editor instanceof TextEditor) {
      final Editor text = ((TextEditor)editor).getEditor();
      final int offset = text.getCaretModel().getOffset();

      if (range.containsOffset(offset)) {
        // select the file
        fem.openFile(vFile, requestFocus, searchForOpen);
        return true;
      }
    }
  }

  return false;
}
 
Example 10
Source File: ConstructorInjectToProvidesHandler.java    From dagger-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private boolean navigateToElement(PsiElement element) {
  PsiElement navigationElement = element.getNavigationElement();
  if (navigationElement != null && navigationElement instanceof Navigatable &&
          ((Navigatable) navigationElement).canNavigate()) {
    ((Navigatable) navigationElement).navigate(true);
    return true;
  }
  return false;
}
 
Example 11
Source File: PsiNavigateUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void navigate(@Nullable final PsiElement psiElement) {
  if (psiElement != null && psiElement.isValid()) {
    final PsiElement navigationElement = psiElement.getNavigationElement();
    final int offset = navigationElement instanceof PsiFile ? -1 : navigationElement.getTextOffset();
    final VirtualFile virtualFile = navigationElement.getContainingFile().getVirtualFile();
    if (virtualFile != null && virtualFile.isValid()) {
      new OpenFileDescriptor(navigationElement.getProject(), virtualFile, offset).navigate(true);
    }
  }
}
 
Example 12
Source File: LightElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setNavigationElement(@Nonnull PsiElement navigationElement) {
  PsiElement nnElement = navigationElement.getNavigationElement();
  if (nnElement != navigationElement && nnElement != null) {
    navigationElement = nnElement;
  }
  myNavigationElement = navigationElement;
}
 
Example 13
Source File: MarkdownDocumentationProvider.java    From markdown-doclet with GNU General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    boolean process = false;
    for ( Class supported: SUPPORTED_ELEMENT_TYPES ) {
        if ( supported.isInstance(element) ) {
            process = true;
            break;
        }
    }
    if ( !process ) {
        return null;
    }
    PsiFile file = null;
    if ( element instanceof PsiDirectory ) {
        // let's see whether we can map the directory to a package; if so, change the
        // element to the package and continue
        PsiPackage pkg = JavaDirectoryService.getInstance().getPackage((PsiDirectory)element);
        if ( pkg != null ) {
            element = pkg;
        }
        else {
            return null;
        }
    }
    if ( element instanceof PsiPackage ) {
        for ( PsiDirectory dir : ((PsiPackage)element).getDirectories() ) {
            PsiFile info = dir.findFile(PsiPackage.PACKAGE_INFO_FILE);
            if ( info != null ) {
                ASTNode node = info.getNode();
                if ( node != null ) {
                    ASTNode docCommentNode = node.findChildByType(JavaDocElementType.DOC_COMMENT);
                    if ( docCommentNode != null ) {
                        // the default implementation will now use this file
                        // we're going to take over below, if Markdown is enabled in
                        // the corresponding module
                        // see JavaDocInfoGenerator.generatePackageJavaDoc()
                        file = info;
                        break;
                    }
                }
            }
            if ( dir.findFile("package.html") != null ) {
                // leave that to the default
                return null;
            }
        }
    }
    else {
        if ( JavaLanguage.INSTANCE.equals(element.getLanguage()) ) {
            element = element.getNavigationElement();
            if ( element.getContainingFile() != null ) {
                file = element.getContainingFile();
            }
        }
    }
    if ( file != null ) {
        DocCommentProcessor processor = new DocCommentProcessor(file);
        if ( processor.isEnabled() ) {
            String docHtml;
            if ( element instanceof PsiMethod ) {
                docHtml = super.generateDoc(PsiProxy.forMethod((PsiMethod)element), originalElement);
            }
            else if ( element instanceof PsiParameter ) {
                docHtml = super.generateDoc(PsiProxy.forParameter((PsiParameter)element), originalElement);
            }
            else {
                MarkdownJavaDocInfoGenerator javaDocInfoGenerator = new MarkdownJavaDocInfoGenerator(element.getProject(), element, processor);
                List<String> docURLs = getExternalJavaDocUrl(element);
                String text = javaDocInfoGenerator.generateDocInfo(docURLs);
                Plugin.print("Intermediate HTML output", text);
                docHtml = JavaDocExternalFilter.filterInternalDocInfo(text);
            }
            docHtml = extendCss(docHtml);
            Plugin.print("Final HTML output", docHtml);
            return docHtml;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}
 
Example 14
Source File: AbstractGotoSEContributor.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected PsiElement preparePsi(PsiElement psiElement, int modifiers, String searchText) {
  return psiElement.getNavigationElement();
}
 
Example 15
Source File: IdempotenceChecker.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean seemsToBeResolveTarget(@Nonnull PsiElement psi) {
  if (psi.isPhysical()) return true;
  PsiElement nav = psi.getNavigationElement();
  return nav != null && nav.isPhysical();
}
 
Example 16
Source File: LombokRenameMethodProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
public PsiElement substituteElementToRename(@NotNull PsiElement elem, @Nullable Editor editor) {
  return elem.getNavigationElement();
}
 
Example 17
Source File: LombokRenameMethodProcessor.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public boolean canProcessElement(@NotNull PsiElement elem) {
  return (elem instanceof LombokLightMethodBuilder || elem instanceof LombokLightFieldBuilder)
    && !(elem.getNavigationElement() instanceof PsiAnnotation);
}
 
Example 18
Source File: LombokElementRenameHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file) {
  return super.isAvailable(element, editor, file) &&
    ((element instanceof LombokLightMethodBuilder || element instanceof LombokLightFieldBuilder)
      && !(element.getNavigationElement() instanceof PsiAnnotation));
}
 
Example 19
Source File: LombokElementRenameVetoHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public boolean isAvailableOnDataContext(DataContext dataContext) {
  final PsiElement element = PsiElementRenameHandler.getElement(dataContext);
  return element instanceof LombokLightClassBuilder ||
    ((element instanceof LombokLightMethodBuilder || element instanceof LombokLightFieldBuilder)
      && element.getNavigationElement() instanceof PsiAnnotation);
}
 
Example 20
Source File: UnityScriptToNativeElementTransformer.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
@Nullable
@Override
public PsiElement transform(@Nonnull PsiElement psiElement)
{
	if(psiElement instanceof UnityScriptDotNetTypeDeclaration)
	{
		CSharpLightTypeDeclarationBuilder builder = new CSharpLightTypeDeclarationBuilder(psiElement)
		{
			@Nonnull
			@Override
			public Language getLanguage()
			{
				return JavaScriptLanguage.INSTANCE;
			}
		};
		PsiElement navigationElement = psiElement.getNavigationElement();

		builder.withName(((UnityScriptDotNetTypeDeclaration) psiElement).getName());
		builder.setNavigationElement(navigationElement);
		builder.addModifier(DotNetModifier.PUBLIC);
		builder.putUserData(JS_MARKER, Boolean.TRUE);
		builder.addExtendType(new CSharpTypeRefByQName(psiElement, Unity3dTypes.UnityEngine.MonoBehaviour));

		if(navigationElement instanceof JSFile)
		{
			for(JSSourceElement jsSourceElement : ((JSFile) navigationElement).getStatements())
			{
				if(jsSourceElement instanceof JSFunction)
				{
					String funcName = jsSourceElement.getName();
					if(funcName == null)
					{
						continue;
					}
					CSharpLightMethodDeclarationBuilder methodDeclarationBuilder = new CSharpLightMethodDeclarationBuilder(psiElement.getProject());
					methodDeclarationBuilder.addModifier(DotNetModifier.PUBLIC);
					methodDeclarationBuilder.withReturnType(new CSharpTypeRefByQName(psiElement, DotNetTypes.System.Void));
					methodDeclarationBuilder.withName(funcName);
					methodDeclarationBuilder.setNavigationElement(jsSourceElement);
					builder.addMember(methodDeclarationBuilder);
				}
			}
		}
		return builder;
	}
	return null;
}