@Override
    public void actionPerformed(AnActionEvent e) {
      if (myElement != null) {
        final PsiElement element = myElement.getElement();
        final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element);
        final PsiElement originalElement = DocumentationManager.getOriginalElement(element);
        boolean processed = false;
        if (provider instanceof CompositeDocumentationProvider) {
          for (final DocumentationProvider documentationProvider :
              ((CompositeDocumentationProvider) provider).getProviders()) {
            if (documentationProvider instanceof ExternalDocumentationHandler
                && ((ExternalDocumentationHandler) documentationProvider)
                    .handleExternal(element, originalElement)) {
              processed = true;
              break;
            }
          }
        }

        if (!processed) {
          final List<String> urls = provider.getUrlFor(element, originalElement);
          assert urls != null : provider;
          assert !urls.isEmpty() : provider;
          ExternalJavaDocAction.showExternalJavadoc(urls);
        }
      }
    }
  @Nullable
  private static String generateInfo(PsiElement element, PsiElement atPointer) {
    final DocumentationProvider documentationProvider =
        DocumentationManager.getProviderFromElement(element, atPointer);

    String info = documentationProvider.getQuickNavigateInfo(element, atPointer);
    if (info != null) {
      return info;
    }

    if (element instanceof PsiFile) {
      final VirtualFile virtualFile = ((PsiFile) element).getVirtualFile();
      if (virtualFile != null) {
        return virtualFile.getPresentableUrl();
      }
    }

    info = getQuickNavigateInfo(element);
    if (info != null) {
      return info;
    }

    if (element instanceof NavigationItem) {
      final ItemPresentation presentation = ((NavigationItem) element).getPresentation();
      if (presentation != null) {
        return presentation.getPresentableText();
      }
    }

    return null;
  }
 @Nullable
 public String generateDocForCompletion(Object completionVariant) {
   PsiElement lookupItem =
       documentationProvider.getDocumentationElementForLookupItem(
           myPsiManager, completionVariant, originalElement);
   assert lookupItem != null;
   return documentationProvider.generateDoc(lookupItem, originalElement);
 }
  public void testDimenUnitsCompletion1() throws Exception {
    final VirtualFile file = copyFileToProject(getTestName(true) + ".xml");
    myFixture.configureFromExistingVirtualFile(file);
    myFixture.complete(CompletionType.BASIC);

    final List<String> lookupElementStrings = myFixture.getLookupElementStrings();
    assertNotNull(lookupElementStrings);
    UsefulTestCase.assertSameElements(
        lookupElementStrings, "3dp", "3px", "3sp", "3pt", "3mm", "3in");

    final PsiElement originalElement =
        myFixture.getFile().findElementAt(myFixture.getEditor().getCaretModel().getOffset());
    assertNotNull(originalElement);

    final LookupEx lookup = myFixture.getLookup();
    LookupElement dpElement = null;
    LookupElement pxElement = null;

    for (LookupElement element : lookup.getItems()) {
      if (element.getLookupString().endsWith("dp")) {
        dpElement = element;
      } else if (element.getLookupString().endsWith("px")) {
        pxElement = element;
      }
    }
    assertNotNull(dpElement);
    assertNotNull(pxElement);
    DocumentationProvider provider;
    PsiElement docTargetElement;

    lookup.setCurrentItem(dpElement);
    docTargetElement =
        DocumentationManager.getInstance(getProject())
            .findTargetElement(myFixture.getEditor(), myFixture.getFile(), originalElement);
    assertNotNull(docTargetElement);
    provider = DocumentationManager.getProviderFromElement(docTargetElement);
    assertEquals(
        "<html><body><b>Density-independent Pixels</b> - an abstract unit that is based on the physical "
            + "density of the screen.</body></html>",
        provider.generateDoc(docTargetElement, originalElement));

    lookup.setCurrentItem(pxElement);
    docTargetElement =
        DocumentationManager.getInstance(getProject())
            .findTargetElement(myFixture.getEditor(), myFixture.getFile(), originalElement);
    assertNotNull(docTargetElement);
    provider = DocumentationManager.getProviderFromElement(docTargetElement);
    assertEquals(
        "<html><body><b>Pixels</b> - corresponds to actual pixels on the screen. Not recommended.</body></html>",
        provider.generateDoc(docTargetElement, originalElement));
  }
Exemple #5
0
    @Override
    public void hyperlinkUpdate(@NotNull HyperlinkEvent e) {
      if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
        return;
      }

      String description = e.getDescription();
      if (StringUtil.isEmpty(description)
          || !description.startsWith(DocumentationManager.PSI_ELEMENT_PROTOCOL)) {
        return;
      }

      String elementName =
          e.getDescription().substring(DocumentationManager.PSI_ELEMENT_PROTOCOL.length());

      final PsiElement targetElement =
          myProvider.getDocumentationElementForLink(
              PsiManager.getInstance(myProject), elementName, myContext);
      if (targetElement != null) {
        LightweightHint hint = myHint;
        if (hint != null) {
          hint.hide(true);
        }
        myDocumentationManager.showJavaDocInfo(targetElement, myContext, true, null);
      }
    }
 public void update(AnActionEvent e) {
   final Presentation presentation = e.getPresentation();
   presentation.setEnabled(false);
   if (myElement != null) {
     final PsiElement element = myElement.getElement();
     final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element);
     final PsiElement originalElement = DocumentationManager.getOriginalElement(element);
     if (provider instanceof ExternalDocumentationProvider) {
       presentation.setEnabled(
           element != null
               && ((ExternalDocumentationProvider) provider)
                   .hasDocumentationFor(element, originalElement));
     } else {
       final List<String> urls = provider.getUrlFor(element, originalElement);
       presentation.setEnabled(element != null && urls != null && !urls.isEmpty());
     }
   }
 }
 @Nullable
 String getQuickNavigateInfo() {
   return documentationProvider.getQuickNavigateInfo(element, originalElement);
 }
 @Nullable
 String generateDoc() {
   return documentationProvider.generateDoc(element, originalElement);
 }