@Override 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()); } } }
@Override public String generateDoc(PsiElement element, PsiElement originalElement) { final XmlTag tag = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, false); String result = generateDocForHtml(element, false, tag, originalElement); DocumentationProvider styleProvider = getStyleProvider(); if (result == null && styleProvider !=null) { result = styleProvider.generateDoc(element, originalElement); } if (result == null && ourScriptProvider !=null) { result = ourScriptProvider.generateDoc(element, originalElement); } if (result == null && element instanceof XmlAttributeValue) { result = generateDocForHtml(element.getParent(), false, tag, originalElement); } return result; }
@Override public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) { PsiElement result = createNavigationElementHTML(psiManager, object.toString(),element); DocumentationProvider styleProvider = getStyleProvider(); if (result== null && styleProvider !=null) { result = styleProvider.getDocumentationElementForLookupItem(psiManager, object, element); } if (result== null && ourScriptProvider !=null) { result = ourScriptProvider.getDocumentationElementForLookupItem(psiManager, object, element); } if (result == null && object instanceof String && element != null) { result = XmlDocumentationProvider.findDeclWithName((String)object, element); } return result; }
public void testExtensionPointDocumentation() { myFixture.configureByFiles("extensionPointDocumentation.xml", "bar/MyExtensionPoint.java"); final PsiElement docElement = DocumentationManager.getInstance(getProject()).findTargetElement(myFixture.getEditor(), myFixture.getFile()); DocumentationProvider provider = DocumentationManager.getProviderFromElement(docElement); String epDefinition = "[" + myModule.getName() + "] foo<br/>" + "<b>bar</b> (extensionPointDocumentation.xml)<br/>" + "<a href=\"psi_element://bar.MyExtensionPoint\"><code>MyExtensionPoint</code></a>"; assertEquals(epDefinition, provider.getQuickNavigateInfo(docElement, null)); assertEquals("<em>EP Definition</em><br/>" + epDefinition + "<br/><br/>" + "<em>EP Implementation</em>" + "<html><head> <style type=\"text/css\"> #error { background-color: #eeeeee; margin-bottom: 10px; } p { margin: 5px 0; } </style></head>" + "<body><small><b>bar</b></small><PRE>public interface <b>MyExtensionPoint</b></PRE>\n" + " MyExtensionPoint JavaDoc.</body></html>", provider.generateDoc(docElement, null)); }
@Override public List<String> getUrlFor(PsiElement element, PsiElement originalElement) { String possibleCssName = findPossibleCssName(element); if(possibleCssName != null) { try { final DocumentationProvider documentationProvider = getCssProvider(element.getProject()); final Method method = documentationProvider.getClass().getMethod("getUrlFor", new Class[]{String.class}); final Object o = method.invoke(null, new Object[]{possibleCssName}); if(o instanceof String) { return Collections.singletonList((String) o); } } catch(Exception e) { } } return null; }
@Override public DocumentationProvider forLanguage(@NotNull final Language l) { final List<DocumentationProvider> providers = allForLanguage(l); if (providers.size() < 2) { return super.forLanguage(l); } return CompositeDocumentationProvider.wrapProviders(providers); }
@Nullable private static String doGenerateInfo(@NotNull PsiElement element, @NotNull PsiElement atPointer, @NotNull DocumentationProvider documentationProvider) { 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 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; }
@Override public void update(AnActionEvent event) { Presentation presentation = event.getPresentation(); DataContext dataContext = event.getDataContext(); Editor editor = CommonDataKeys.EDITOR.getData(dataContext); PsiElement element = getElement(dataContext, editor); final PsiElement originalElement = getOriginalElement(CommonDataKeys.PSI_FILE.getData(dataContext), editor); DocumentationManager.storeOriginalElement(CommonDataKeys.PROJECT.getData(dataContext), 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); } }
private static void doTest(String text) throws Exception { configureFromFileText("test.html", text); PsiElement originalElement = getFile().findElementAt(myEditor.getCaretModel().getOffset()); PsiElement element = DocumentationManager.getInstance(getProject()).findTargetElement(getEditor(), getFile()); DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(originalElement); assertNotNull("inline help is null", documentationProvider.generateDoc(element, originalElement)); assertNotNull("external help is null", documentationProvider.getUrlFor(element, originalElement)); }
@Override public List<String> getUrlFor(PsiElement element, PsiElement originalElement) { String result = getUrlForHtml(element, PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, false)); DocumentationProvider styleProvider = getStyleProvider(); if (result == null && styleProvider != null) { return styleProvider.getUrlFor(element, originalElement); } return result != null ? Collections.singletonList(result) : null; }
@Override public PsiElement getDocumentationElementForLink(PsiManager psiManager, String link, PsiElement context) { PsiElement result = createNavigationElementHTML(psiManager, link, context); DocumentationProvider styleProvider = getStyleProvider(); if (result== null && styleProvider !=null) { result = styleProvider.getDocumentationElementForLink(psiManager, link, context); } if (result== null && ourScriptProvider != null && !DumbService.isDumb(psiManager.getProject())) { result = ourScriptProvider.getDocumentationElementForLink(psiManager, link,context); } return result; }
@Nullable private DocumentationProvider getStyleProvider() { if (myStyleProvider == null) { Language cssLanguage = Language.findLanguageByID("CSS"); if (cssLanguage != null) { myStyleProvider = LanguageDocumentation.INSTANCE.forLanguage(cssLanguage); } } return myStyleProvider; }
protected void doTestExternalDoc(String expectedPart) { final PsiElement originalElement = myFixture.getFile().findElementAt( myFixture.getEditor().getCaretModel().getOffset()); final PsiElement docTargetElement = DocumentationManager.getInstance(getProject()). findTargetElement(myFixture.getEditor(), myFixture.getFile(), originalElement); final DocumentationProvider provider = DocumentationManager.getProviderFromElement(docTargetElement); final List<String> urls = provider.getUrlFor(docTargetElement, originalElement); final String doc = ((ExternalDocumentationProvider)provider).fetchExternalDocumentation(myFixture.getProject(), docTargetElement, urls); assertNotNull(doc); assertTrue("Can't find " + expectedPart + " in " + doc, doc.contains(expectedPart)); }
protected void assertDocumentation(String expectedText) throws IOException { PsiElement originalElement = getElementAtCaret(myProjectPom); PsiElement targetElement = DocumentationManager.getInstance(myProject) .findTargetElement(getEditor(), getTestPsiFile(), originalElement); DocumentationProvider provider = DocumentationManager.getProviderFromElement(targetElement); assertEquals(expectedText, provider.generateDoc(targetElement, originalElement)); // should work for lookup as well as for tags PsiElement lookupElement = provider.getDocumentationElementForLookupItem(PsiManager.getInstance(myProject), originalElement.getText(), originalElement); assertSame(targetElement, lookupElement); }
@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 = docProvider.generateDocumentationContentStub(comment); 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; }
@Override public void update(AnActionEvent event) { Presentation presentation = event.getPresentation(); DataContext dataContext = event.getDataContext(); Editor editor = PlatformDataKeys.EDITOR.getData(dataContext); final PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext); final PsiElement originalElement = getOriginalElement(LangDataKeys.PSI_FILE.getData(dataContext), editor); DocumentationManager.storeOriginalElement(PlatformDataKeys.PROJECT.getData(dataContext), 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 (event.getPlace().equals(ActionPlaces.MAIN_MENU)) { presentation.setVisible(true); } else { presentation.setVisible(enabled); } } else{ presentation.setEnabled(enabled); presentation.setVisible(true); } }
private void doTest(String text) throws Exception { configureByText(StdFileTypes.HTML, text); PsiElement originalElement = getFile().findElementAt(myEditor.getCaretModel().getOffset()); PsiElement element = DocumentationManager.getInstance(getProject()).findTargetElement(getEditor(), getFile()); DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(originalElement); assertNotNull("inline help is null", documentationProvider.generateDoc(element, originalElement)); assertNotNull("external help is null", documentationProvider.getUrlFor(element, originalElement)); }
@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); }
@Nullable private static String doGenerateInfo(@Nonnull PsiElement element, @Nonnull PsiElement atPointer, @Nonnull DocumentationProvider documentationProvider) { 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; }
@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); } }
@NotNull private static DocInfo generateInfo(PsiElement element, PsiElement atPointer) { final DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(element, atPointer); String result = doGenerateInfo(element, atPointer, documentationProvider); return result == null ? DocInfo.EMPTY : new DocInfo(result, documentationProvider, element); }
DocInfo(@Nullable String text, @Nullable DocumentationProvider provider, @Nullable PsiElement documentationAnchor) { this.text = text; docProvider = provider; this.documentationAnchor = documentationAnchor; }
QuickDocHyperlinkListener(@NotNull DocumentationProvider provider, @NotNull PsiElement context) { myProvider = provider; myContext = context; }
@Override public void actionPerformed(AnActionEvent e) { DataContext dataContext = e.getDataContext(); Project project = CommonDataKeys.PROJECT.getData(dataContext); if (project == null) { return; } Editor editor = CommonDataKeys.EDITOR.getData(dataContext); PsiElement element = getElement(dataContext, editor); if (element == null) { Messages.showMessageDialog( project, IdeBundle.message("message.please.select.element.for.javadoc"), IdeBundle.message("title.no.element.selected"), Messages.getErrorIcon() ); return; } PsiFile context = CommonDataKeys.PSI_FILE.getData(dataContext); PsiElement originalElement = getOriginalElement(context, editor); DocumentationManager.storeOriginalElement(project, originalElement, element); final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element); if (provider instanceof ExternalDocumentationHandler && ((ExternalDocumentationHandler)provider).handleExternal(element, originalElement)) { return; } final List<String> urls = provider.getUrlFor(element, originalElement); if (urls != null && !urls.isEmpty()) { showExternalJavadoc(urls, PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext)); } else if (provider instanceof ExternalDocumentationProvider) { final ExternalDocumentationProvider externalDocumentationProvider = (ExternalDocumentationProvider)provider; if (externalDocumentationProvider.canPromptToConfigureDocumentation(element)) { externalDocumentationProvider.promptToConfigureDocumentation(element); } } }
public static void registerScriptDocumentationProvider(final DocumentationProvider provider) { ourScriptProvider = provider; }
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)); }
@Override public void actionPerformed(AnActionEvent e) { DataContext dataContext = e.getDataContext(); Project project = PlatformDataKeys.PROJECT.getData(dataContext); if (project == null) { return; } PsiElement element = LangDataKeys.PSI_ELEMENT.getData(dataContext); if (element == null) { Messages.showMessageDialog( project, IdeBundle.message("message.please.select.element.for.javadoc"), IdeBundle.message("title.no.element.selected"), Messages.getErrorIcon() ); return; } PsiFile context = LangDataKeys.PSI_FILE.getData(dataContext); Editor editor = PlatformDataKeys.EDITOR.getData(dataContext); PsiElement originalElement = getOriginalElement(context, editor); DocumentationManager.storeOriginalElement(project, originalElement, element); final DocumentationProvider provider = DocumentationManager.getProviderFromElement(element); if (provider instanceof ExternalDocumentationHandler && ((ExternalDocumentationHandler)provider).handleExternal(element, originalElement)) { return; } final List<String> urls = provider.getUrlFor(element, originalElement); if (urls != null && !urls.isEmpty()) { showExternalJavadoc(urls); } else if (provider instanceof ExternalDocumentationProvider) { final ExternalDocumentationProvider externalDocumentationProvider = (ExternalDocumentationProvider)provider; if (externalDocumentationProvider.canPromptToConfigureDocumentation(element)) { externalDocumentationProvider.promptToConfigureDocumentation(element); } } }
public static void registerStyleDocumentationProvider(DocumentationProvider documentationProvider) { ourStyleProvider = documentationProvider; }
@Nonnull private static DocInfo generateInfo(PsiElement element, PsiElement atPointer) { final DocumentationProvider documentationProvider = DocumentationManager.getProviderFromElement(element, atPointer); String result = doGenerateInfo(element, atPointer, documentationProvider); return result == null ? DocInfo.EMPTY : new DocInfo(result, documentationProvider, element); }