@Nullable private Navigatable getNavigatableInTemplateLanguageFile() { PsiElement element = (PsiElement)myTreeElement.getValue(); if (element == null) return null; int offset = element.getTextRange().getStartOffset(); final Language dataLanguage = ((TemplateLanguageFileViewProvider)myMainFile.getViewProvider()).getTemplateDataLanguage(); final PsiFile dataFile = myMainFile.getViewProvider().getPsi(dataLanguage); if (dataFile == null) return null; PsiElement tlElement = dataFile.findElementAt(offset); while(true) { if (tlElement == null || tlElement.getTextRange().getStartOffset() != offset) break; if (tlElement instanceof Navigatable) { return (Navigatable)tlElement; } tlElement = tlElement.getParent(); } return null; }
@Nullable public static Commenter getCommenter(final PsiFile file, final Editor editor, final Language lineStartLanguage, final Language lineEndLanguage) { final FileViewProvider viewProvider = file.getViewProvider(); for (MultipleLangCommentProvider provider : MultipleLangCommentProvider.EP_NAME.getExtensions()) { if (provider.canProcess(file, viewProvider)) { return provider.getLineCommenter(file, editor, lineStartLanguage, lineEndLanguage); } } final Language fileLanguage = file.getLanguage(); Language lang = lineStartLanguage == null || LanguageCommenters.INSTANCE.forLanguage(lineStartLanguage) == null || fileLanguage.getBaseLanguage() == lineStartLanguage // file language is a more specific dialect of the line language ? fileLanguage : lineStartLanguage; if (viewProvider instanceof TemplateLanguageFileViewProvider && lang == ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage()) { lang = viewProvider.getBaseLanguage(); } return LanguageCommenters.INSTANCE.forLanguage(lang); }
protected Block getRootBlock(PsiElement element, FileViewProvider viewProvider, CodeStyleSettings settings) { ASTNode node = element.getNode(); if (node == null) { return createDummyBlock(node); } if (viewProvider instanceof TemplateLanguageFileViewProvider) { final Language dataLanguage = ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage(); final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forLanguage(dataLanguage); if (builder instanceof DelegatingFormattingModelBuilder && ((DelegatingFormattingModelBuilder)builder).dontFormatMyModel()) { return createDummyBlock(node); } if (builder != null) { final FormattingModel model = builder.createModel(viewProvider.getPsi(dataLanguage), settings); List<DataLanguageBlockWrapper> childWrappers = buildChildWrappers(model.getRootBlock()); if (childWrappers.size() == 1) { childWrappers = buildChildWrappers(childWrappers.get(0).getOriginal()); } return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), null, filterBlocksByRange(childWrappers, node.getTextRange()), settings); } } return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), null, Collections.<DataLanguageBlockWrapper>emptyList(), settings); }
/** * Fetches template data language if file has {@link TemplateLanguageFileViewProvider} * * @param psiElement element to get lang for * @param expectedProvider only fetch language if provider has certain type. Pass null for any type. * @return template data language */ @Nullable public static Language getTemplateDataLanguage(@Nullable final PsiElement psiElement, @Nullable final Class<? extends TemplateLanguageFileViewProvider> expectedProvider) { if (psiElement == null) { return null; } final FileViewProvider provider = psiElement.getContainingFile().getViewProvider(); if (provider instanceof TemplateLanguageFileViewProvider) { if (expectedProvider == null || expectedProvider.isInstance(provider)) { return (((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage()); } } return psiElement.getLanguage(); }
protected boolean buildInjectedPsiBlocks(List<Block> result, final ASTNode child, Wrap wrap, Alignment alignment, Indent indent) { if (myInjectedBlockBuilder.addInjectedBlocks(result, child, wrap, alignment, indent)) { return true; } PsiFile containingFile = child.getPsi().getContainingFile(); FileViewProvider fileViewProvider = containingFile.getViewProvider(); if (fileViewProvider instanceof TemplateLanguageFileViewProvider) { Language templateLanguage = ((TemplateLanguageFileViewProvider)fileViewProvider).getTemplateDataLanguage(); PsiElement at = fileViewProvider.findElementAt(child.getStartOffset(), templateLanguage); if (at instanceof XmlToken) { at = at.getParent(); } // TODO: several comments if (at instanceof PsiComment && at.getTextRange().equals(child.getTextRange()) && at.getNode() != child) { return buildInjectedPsiBlocks(result, at.getNode(), wrap, alignment, indent); } } return false; }
@Override public void visitContinueStatement(GrContinueStatement continueStatement) { super.visitContinueStatement(continueStatement); if (continueStatement.getContainingFile().getViewProvider() instanceof TemplateLanguageFileViewProvider) { return; } final GrStatement continuedStatement = continueStatement.findTargetStatement(); if (continuedStatement == null) { return; } if (!(continuedStatement instanceof GrLoopStatement)) return; final GrStatement body = ((GrLoopStatement)continuedStatement).getBody(); if (body == null) return; if (body instanceof GrBlockStatement && ControlFlowUtils.blockCompletesWithStatement((GrBlockStatement)body, continueStatement) || !(body instanceof GrBlockStatement) && ControlFlowUtils.statementCompletesWithStatement(body, continueStatement)){ registerStatementError(continueStatement); } }
private void updateAfterPsiChange() { if (myProject.isDisposed()) return; if (myBaseStructureViewDescriptor != null && ((StructureViewComponent)myBaseStructureViewDescriptor.structureView).getTree() == null) return; ApplicationManager.getApplication().runReadAction(new Runnable(){ @Override public void run() { if (!myVirtualFile.isValid()) return; final TemplateLanguageFileViewProvider provider = getViewProvider(); if (provider == null) return; StructureViewWrapper structureViewWrapper = StructureViewFactoryEx.getInstanceEx(myProject).getStructureViewWrapper(); if (structureViewWrapper == null) return; Language baseLanguage = provider.getTemplateDataLanguage(); if (baseLanguage == myTemplateDataLanguage && (myBaseStructureViewDescriptor == null || isPsiValid(myBaseStructureViewDescriptor))) { updateBaseLanguageView(); } else { myTemplateDataLanguage = baseLanguage; ((StructureViewWrapperImpl)structureViewWrapper).rebuild(); } } }); }
@Nullable private StructureViewComposite.StructureViewDescriptor createBaseLanguageStructureView(final FileEditor fileEditor, final Language language) { if (!myVirtualFile.isValid()) return null; final TemplateLanguageFileViewProvider viewProvider = getViewProvider(); if (viewProvider == null) return null; final PsiFile dataFile = viewProvider.getPsi(language); if (dataFile == null || !isAcceptableBaseLanguageFile(dataFile)) return null; final PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(language); if (factory == null) return null; final StructureViewBuilder builder = factory.getStructureViewBuilder(dataFile); if (builder == null) return null; StructureView structureView = builder.createStructureView(fileEditor, myProject); return new StructureViewComposite.StructureViewDescriptor(IdeBundle.message("tab.structureview.baselanguage.view", language.getDisplayName()), structureView, findFileType(language).getIcon()); }
private void updateTemplateDataFileView() { final TemplateLanguageFileViewProvider provider = getViewProvider(); final Language newDataLanguage = provider == null ? null : provider.getTemplateDataLanguage(); if (myBaseStructureViewDescriptor != null) { if (myTemplateDataLanguage == newDataLanguage) return; Disposer.dispose(myBaseStructureViewDescriptor.structureView); } if (newDataLanguage != null) { myBaseStructureViewDescriptor = createBaseLanguageStructureView(myFileEditor, newDataLanguage); if (myStructureViewComposite != null) { myStructureViewComposite.setStructureView(myBaseLanguageViewDescriptorIndex, myBaseStructureViewDescriptor); } } }
@Nullable private static XmlTag getRootTag(PsiFile psifile) { XmlFile xmlFile = null; if (psifile instanceof XmlFile) { xmlFile = (XmlFile)psifile; } else if (psifile.getViewProvider() instanceof TemplateLanguageFileViewProvider) { TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider)psifile.getViewProvider(); if (viewProvider.getPsi(viewProvider.getTemplateDataLanguage()) instanceof XmlFile) { xmlFile = (XmlFile)viewProvider.getPsi(viewProvider.getTemplateDataLanguage()); } } if (xmlFile != null) { return xmlFile.getDocument().getRootTag(); } else { return null; } }
@Override public void update(final PsiFile element, PsiFile originalElement) throws IncorrectOperationException { if (element.getViewProvider() instanceof TemplateLanguageFileViewProvider) return; PsiDirectory dir = element.getContainingDirectory(); if (dir == null) return; PsiPackage aPackage = JavaDirectoryService.getInstance().getPackage(dir); if (aPackage == null) return; String packageName = aPackage.getQualifiedName(); ((PsiClassOwner)element).setPackageName(packageName); }
@Override public Object getSelectorInFile() { if (myPsiFile.getViewProvider() instanceof TemplateLanguageFileViewProvider) { return super.getSelectorInFile(); } Editor editor = myEditor.getEditor(); int offset = TargetElementUtil.adjustOffset(myPsiFile, editor.getDocument(), editor.getCaretModel().getOffset()); PsiElement element = myPsiFile.findElementAt(offset); return element != null ? element : super.getSelectorInFile(); }
public static boolean isHtmlTagContainingFile(PsiElement element) { if (element == null) { return false; } final PsiFile containingFile = element.getContainingFile(); if (containingFile != null) { final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false); if (tag instanceof HtmlTag) { return true; } final XmlDocument document = PsiTreeUtil.getParentOfType(element, XmlDocument.class, false); if (document instanceof HtmlDocumentImpl) { return true; } final FileViewProvider provider = containingFile.getViewProvider(); Language language; if (provider instanceof TemplateLanguageFileViewProvider) { language = ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage(); } else { language = provider.getBaseLanguage(); } return language == XHTMLLanguage.INSTANCE; } return false; }
static boolean fileContainsXmlLanguage(PsiFile editedFile) { if (editedFile.getLanguage() instanceof XMLLanguage) { return true; } if (HtmlUtil.supportsXmlTypedHandlers(editedFile)) { return true; } final FileViewProvider provider = editedFile.getViewProvider(); if (provider.getBaseLanguage() instanceof XMLLanguage) { return true; } return provider instanceof TemplateLanguageFileViewProvider && ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage() instanceof XMLLanguage; }
public static void autoPopupXmlLookup(final Project project, final Editor editor){ AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, new Condition<PsiFile>() { @Override public boolean value(PsiFile file) { int offset = editor.getCaretModel().getOffset(); PsiElement lastElement = InjectedLanguageUtil.findElementAtNoCommit(file, offset - 1); if (lastElement instanceof PsiFile) { //the very end of an injected file lastElement = file.findElementAt(offset - 1); } if (lastElement == null || !lastElement.isValid()) return false; if (doCompleteIfNeeded(offset, file, lastElement)) { return true; } FileViewProvider fileViewProvider = file.getViewProvider(); Language templateDataLanguage; final PsiElement parent = lastElement.getParent(); if (fileViewProvider instanceof TemplateLanguageFileViewProvider && (templateDataLanguage = ((TemplateLanguageFileViewProvider)fileViewProvider).getTemplateDataLanguage()) != parent.getLanguage()) { lastElement = fileViewProvider.findElementAt(offset - 1, templateDataLanguage); if (lastElement == null || !lastElement.isValid()) return false; return doCompleteIfNeeded(offset, file, lastElement); } return false; } }); }
private static void moveToNextPoint(Editor editor, PsiFile file, int offset, int inc) { final Document doc = editor.getDocument(); final TemplateLanguageFileViewProvider provider = ObjectUtils.tryCast(file.getViewProvider(), TemplateLanguageFileViewProvider.class); final Language additionalLanguage = provider != null ? provider.getTemplateDataLanguage() : null; PsiDocumentManager.getInstance(file.getProject()).commitDocument(doc); for (int i = offset + inc; i < doc.getTextLength() && i >= 0; i += inc) { PsiElement current = InjectedLanguageUtil.findElementAtNoCommit(file, i); if (checkAndMove(editor, doc, i, current)) return; if (additionalLanguage != null) { current = provider.findElementAt(i, additionalLanguage); if (checkAndMove(editor, doc, i, current)) return; } } }
static String generateHtmlAdditionalDocTemplate(@NotNull PsiElement element) { StringBuilder buf = new StringBuilder(); final PsiFile containingFile = element.getContainingFile(); if (containingFile != null) { final XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class, false); boolean append; if (tag instanceof HtmlTag) { append = true; } else { final FileViewProvider provider = containingFile.getViewProvider(); Language language; if (provider instanceof TemplateLanguageFileViewProvider) { language = ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage(); } else { language = provider.getBaseLanguage(); } append = language == XHTMLLanguage.INSTANCE; } if (tag != null) { EntityDescriptor descriptor = HtmlDescriptorsTable.getTagDescriptor(tag.getName()); if (descriptor != null && append) { buf.append("<br>"); buf.append(XmlBundle.message("html.quickdoc.additional.template", descriptor.getHelpRef(), BASE_SITEPOINT_URL + tag.getName())); } } } return buf.toString(); }
private static boolean canHaveAdequateFix(PsiElement element) { final PsiFile containingFile = element.getContainingFile(); if (containingFile.getLanguage() == HTMLLanguage.INSTANCE || containingFile.getLanguage() == XHTMLLanguage.INSTANCE || containingFile.getViewProvider() instanceof TemplateLanguageFileViewProvider ) { return false; } return true; }
protected void processXmlFile(VirtualFile t) { myIndicator.setText(findBundleMessage("find.searching.for.string.in.file.progress", myExpression.expression, t.getPresentableUrl())); final PsiFile psiFile = myManager.findFile(t); if (psiFile instanceof XmlFile) { final XmlFile t1 = (XmlFile)psiFile; final XmlDocument document; FileViewProvider fileViewProvider = t1.getViewProvider(); if (fileViewProvider instanceof TemplateLanguageFileViewProvider) { final PsiFile root = fileViewProvider.getPsi(((TemplateLanguageFileViewProvider)fileViewProvider).getTemplateDataLanguage()); if (root instanceof XmlFile) { document = ((XmlFile)root).getDocument(); } else { document = null; } } else { document = t1.getDocument(); } if (document != null) { process(document); } } myIndicator.setFraction(++myFileCount / (double)myTotalFileCount); }
@Override public Object getSelectorInFile() { if (myPsiFile.getViewProvider() instanceof TemplateLanguageFileViewProvider) { return super.getSelectorInFile(); } final int offset = myEditor.getEditor().getCaretModel().getOffset(); if (offset >= 0 && offset < myPsiFile.getTextLength()) { return myPsiFile.findElementAt(offset); } return super.getSelectorInFile(); }
private static boolean fileContainsXmlLanguage(PsiFile editedFile) { if (editedFile.getLanguage() instanceof XMLLanguage) { return true; } final FileViewProvider provider = editedFile.getViewProvider(); if (provider.getBaseLanguage() instanceof XMLLanguage) { return true; } return provider instanceof TemplateLanguageFileViewProvider && ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage() instanceof XMLLanguage; }
public void visitContinueStatement(GrContinueStatement continueStatement) { super.visitContinueStatement(continueStatement); if (continueStatement.getContainingFile().getViewProvider() instanceof TemplateLanguageFileViewProvider) { return; } final GrStatement continuedStatement = continueStatement.findTargetStatement(); if (continuedStatement == null) { return; } if (!(continuedStatement instanceof GrLoopStatement)) { return; } final GrCondition body = ((GrLoopStatement) continuedStatement).getBody(); if (body == null) { return; } if (body instanceof GrBlockStatement) { if (ControlFlowUtils.blockCompletesWithStatement((GrBlockStatement) body, continueStatement)) { registerStatementError(continueStatement); } } else if (body instanceof GrStatement) { if (ControlFlowUtils.statementCompletesWithStatement((GrStatement) body, continueStatement)) { registerStatementError(continueStatement); } } }