com.intellij.lang.documentation.DocumentationProvider Java Examples

The following examples show how to use com.intellij.lang.documentation.DocumentationProvider. 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: BaseDocumentationTest.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
public String verify() {
    Editor editor = myFixture.getEditor();
    PsiFile file = myFixture.getFile();

    PsiElement originalElement = file.findElementAt(editor.getCaretModel().getOffset());
    PsiElement element = DocumentationManager.getInstance(getProject()).findTargetElement(editor, file);

    assertThat(originalElement).isNotNull();
    assertThat(element).isNotNull();

    DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(originalElement);

    return documentationProvider.generateDoc(element, originalElement);
}
 
Example #2
Source File: LanguageDocumentation.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public DocumentationProvider forLanguage(@Nonnull final Language l) {
  final List<DocumentationProvider> providers = allForLanguage(l);
  if (providers.size() < 2) {
    return super.forLanguage(l);
  }
  return CompositeDocumentationProvider.wrapProviders(providers);
}
 
Example #3
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setData(@Nullable PsiElement element, @Nonnull String text, @Nullable String effectiveExternalUrl, @Nullable String ref, @Nullable DocumentationProvider provider) {
  pushHistory();
  myExternalUrl = effectiveExternalUrl;
  myProvider = provider;

  SmartPsiElementPointer<PsiElement> pointer = null;
  if (element != null && element.isValid()) {
    pointer = SmartPointerManager.getInstance(element.getProject()).createSmartPsiElementPointer(element);
  }
  setDataInternal(pointer, text, new Rectangle(0, 0), ref);
}
 
Example #4
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
static boolean shouldShowExternalDocumentationLink(DocumentationProvider provider, PsiElement element, PsiElement originalElement) {
  if (provider instanceof CompositeDocumentationProvider) {
    List<DocumentationProvider> providers = ((CompositeDocumentationProvider)provider).getProviders();
    for (DocumentationProvider p : providers) {
      if (p instanceof ExternalDocumentationHandler) {
        return ((ExternalDocumentationHandler)p).canHandleExternal(element, originalElement);
      }
    }
  }
  else if (provider instanceof ExternalDocumentationHandler) {
    return ((ExternalDocumentationHandler)provider).canHandleExternal(element, originalElement);
  }
  return true;
}
 
Example #5
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
Context(SmartPsiElementPointer<PsiElement> element, String text, String externalUrl, DocumentationProvider provider, Rectangle viewRect, int highlightedLink) {
  this.element = element;
  this.text = text;
  this.externalUrl = externalUrl;
  this.provider = provider;
  this.viewRect = viewRect;
  this.highlightedLink = highlightedLink;
}
 
Example #6
Source File: QuickDocUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Contract("_, _, _, null -> null")
public static String inferLinkFromFullDocumentation(@Nonnull DocumentationProvider provider, PsiElement element, PsiElement originalElement, @Nullable String navigationInfo) {
  if (navigationInfo != null) {
    String fqn = element instanceof PsiQualifiedNamedElement ? ((PsiQualifiedNamedElement)element).getQualifiedName() : null;
    String fullText = provider.generateDoc(element, originalElement);
    return HintUtil.prepareHintText(DocPreviewUtil.buildPreview(navigationInfo, fqn, fullText), HintUtil.getInformationHint());
  }
  return null;
}
 
Example #7
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static DocInfo generateInfo(PsiElement element, PsiElement atPointer, boolean fallbackToBasicInfo) {
  final DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(element, atPointer);
  String result = documentationProvider.getQuickNavigateInfo(element, atPointer);
  if (result == null && fallbackToBasicInfo) {
    result = doGenerateInfo(element);
  }
  return result == null ? DocInfo.EMPTY : new DocInfo(result, documentationProvider);
}
 
Example #8
Source File: EnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiComment createJavaDocStub(final CodeInsightSettings settings, final PsiComment comment, final Project project) {
  if (settings.JAVADOC_STUB_ON_ENTER) {
    final DocumentationProvider langDocumentationProvider = LanguageDocumentation.INSTANCE.forLanguage(comment.getParent().getLanguage());

    @Nullable final CodeDocumentationProvider docProvider;
    if (langDocumentationProvider instanceof CompositeDocumentationProvider) {
      docProvider = ((CompositeDocumentationProvider)langDocumentationProvider).getFirstCodeDocumentationProvider();
    }
    else {
      docProvider = langDocumentationProvider instanceof CodeDocumentationProvider ? (CodeDocumentationProvider)langDocumentationProvider : null;
    }

    if (docProvider != null) {
      if (docProvider.findExistingDocComment(comment) != comment) return comment;
      String docStub;

      DumbService.getInstance(project).setAlternativeResolveEnabled(true);
      try {
        docStub = docProvider.generateDocumentationContentStub(comment);
      }
      finally {
        DumbService.getInstance(project).setAlternativeResolveEnabled(false);
      }

      if (docStub != null && docStub.length() != 0) {
        myOffset = CharArrayUtil.shiftForwardUntil(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR);
        myOffset = CharArrayUtil.shiftForward(myDocument.getCharsSequence(), myOffset, LINE_SEPARATOR);
        myDocument.insertString(myOffset, docStub);
      }
    }

    PsiDocumentManager.getInstance(project).commitAllDocuments();
    return PsiTreeUtil.getNonStrictParentOfType(myFile.findElementAt(myOffset), PsiComment.class);
  }
  return comment;
}
 
Example #9
Source File: ExternalJavaDocAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void update(AnActionEvent event) {
  Presentation presentation = event.getPresentation();
  DataContext dataContext = event.getDataContext();
  Editor editor = dataContext.getData(CommonDataKeys.EDITOR);
  PsiElement element = getElement(dataContext, editor);
  final PsiElement originalElement = getOriginalElement(dataContext.getData(CommonDataKeys.PSI_FILE), editor);
  DocumentationManager.storeOriginalElement(dataContext.getData(CommonDataKeys.PROJECT), originalElement, element);
  final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element);
  boolean enabled;
  if (provider instanceof ExternalDocumentationProvider) {
    final ExternalDocumentationProvider edProvider = (ExternalDocumentationProvider)provider;
    enabled = edProvider.hasDocumentationFor(element, originalElement) || edProvider.canPromptToConfigureDocumentation(element);
  }
  else {
    final List<String> urls = provider.getUrlFor(element, originalElement);
    enabled = urls != null && !urls.isEmpty();
  }
  if (editor != null) {
    presentation.setEnabled(enabled);
    if (ActionPlaces.isMainMenuOrActionSearch(event.getPlace())) {
      presentation.setVisible(true);
    }
    else {
      presentation.setVisible(enabled);
    }
  }
  else{
    presentation.setEnabled(enabled);
    presentation.setVisible(true);
  }
}
 
Example #10
Source File: DocumentationTest.java    From intellij-swagger with MIT License 4 votes vote down vote up
protected void testQuickDocumentation(final String fileName, final String expectedDocumentation) {
  final PsiFile psiFile = myFixture.configureByFile(filesPath + fileName);

  final PsiElement originalElement =
      psiFile.findElementAt(myFixture.getEditor().getCaretModel().getOffset()).getParent();

  final PsiElement targetElement = originalElement.getReferences()[0].resolve();

  final DocumentationProvider documentationProvider =
      DocumentationManager.getProviderFromElement(targetElement);

  final String quickNavigateInfo =
      documentationProvider.getQuickNavigateInfo(targetElement, originalElement);

  assertEquals(expectedDocumentation, quickNavigateInfo);
}
 
Example #11
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setText(@Nonnull String text, @Nullable PsiElement element, @Nullable DocumentationProvider provider) {
  setData(element, text, null, null, provider);
}
 
Example #12
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
DocInfo(@Nullable String text, @Nullable DocumentationProvider provider) {
  this.text = text;
  docProvider = provider;
}
 
Example #13
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
QuickDocHyperlinkListener(@Nonnull DocumentationProvider provider, @Nonnull PsiElement context) {
  myProvider = provider;
  myContext = context;
}
 
Example #14
Source File: FixDocCommentAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Generates comment if it's not exist or try to fix if exists
 *
 * @param element target element for which a comment should be generated
 * @param project current project
 * @param editor  target editor
 */
public static void generateOrFixComment(@Nonnull final PsiElement element, @Nonnull final Project project, @Nonnull final Editor editor) {
  Language language = element.getLanguage();
  final CodeDocumentationProvider docProvider;
  final DocumentationProvider langDocumentationProvider = LanguageDocumentation.INSTANCE.forLanguage(language);
  if (langDocumentationProvider instanceof CompositeDocumentationProvider) {
    docProvider = ((CompositeDocumentationProvider)langDocumentationProvider).getFirstCodeDocumentationProvider();
  }
  else if (langDocumentationProvider instanceof CodeDocumentationProvider) {
    docProvider = (CodeDocumentationProvider)langDocumentationProvider;
  }
  else {
    docProvider = null;
  }
  if (docProvider == null) {
    return;
  }

  final Pair<PsiElement, PsiComment> pair = docProvider.parseContext(element);
  if (pair == null) {
    return;
  }

  Commenter c = LanguageCommenters.INSTANCE.forLanguage(language);
  if (!(c instanceof CodeDocumentationAwareCommenter)) {
    return;
  }
  final CodeDocumentationAwareCommenter commenter = (CodeDocumentationAwareCommenter)c;
  final Runnable task;
  if (pair.second == null || pair.second.getTextRange().isEmpty()) {
    task = () -> generateComment(pair.first, editor, docProvider, commenter, project);
  }
  else {
    final DocCommentFixer fixer = DocCommentFixer.EXTENSION.forLanguage(language);
    if (fixer == null) {
      return;
    }
    else {
      task = () -> fixer.fixComment(project, editor, pair.second);
    }
  }
  final Runnable command = () -> ApplicationManager.getApplication().runWriteAction(task);
  CommandProcessor.getInstance().executeCommand(project, command, "Fix documentation", null);

}
 
Example #15
Source File: ExternalJavaDocAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void showExternalJavadoc(PsiElement element, PsiElement originalElement, String docUrl, DataContext dataContext) {
  DocumentationProvider provider = DocumentationManager.getProviderFromElement(element);
  if (provider instanceof ExternalDocumentationHandler &&
      ((ExternalDocumentationHandler)provider).handleExternal(element, originalElement)) {
    return;
  }
  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final Component contextComponent = dataContext.getData(PlatformDataKeys.CONTEXT_COMPONENT);
  ApplicationManager.getApplication().executeOnPooledThread(() -> {
    List<String> urls;
    if (StringUtil.isEmptyOrSpaces(docUrl)) {
      ThrowableComputable<List<String>,RuntimeException> action = () -> provider.getUrlFor(element, originalElement);
      urls = AccessRule.read(action);
    }
    else {
      urls = Collections.singletonList(docUrl);
    }
    if (provider instanceof ExternalDocumentationProvider && urls != null && urls.size() > 1) {
      for (String url : urls) {
        List<String> thisUrlList = Collections.singletonList(url);
        String doc = ((ExternalDocumentationProvider)provider).fetchExternalDocumentation(project, element, thisUrlList);
        if (doc != null) {
          urls = thisUrlList;
          break;
        }
      }
    }
    final List<String> finalUrls = urls;
    ApplicationManager.getApplication().invokeLater(() -> {
      if (ContainerUtil.isEmpty(finalUrls)) {
        if (element != null && provider instanceof ExternalDocumentationProvider) {
          ExternalDocumentationProvider externalDocumentationProvider = (ExternalDocumentationProvider)provider;
          if (externalDocumentationProvider.canPromptToConfigureDocumentation(element)) {
            externalDocumentationProvider.promptToConfigureDocumentation(element);
          }
        }
      }
      else if (finalUrls.size() == 1) {
        BrowserUtil.browse(finalUrls.get(0));
      }
      else {
        JBPopupFactory.getInstance().createListPopup(new BaseListPopupStep<String>("Choose external documentation root",
                                                                                   ArrayUtil.toStringArray(finalUrls)) {
          @Override
          public PopupStep onChosen(final String selectedValue, final boolean finalChoice) {
            BrowserUtil.browse(selectedValue);
            return FINAL_CHOICE;
          }
        }).showInBestPositionFor(DataManager.getInstance().getDataContext(contextComponent));
      }
    }, ModalityState.NON_MODAL);
  });

}