/** * We have to override {@link com.intellij.formatting.templateLanguages.TemplateLanguageFormattingModelBuilder#createModel} * since after we delegate to some templated languages, those languages (xml/html for sure, potentially others) * delegate right back to us to format the DustTypes.OUTER_TYPE token we tell them to ignore, * causing an stack-overflowing loop. */ @NotNull public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { final PsiFile file = element.getContainingFile(); Block rootBlock; ASTNode node = element.getNode(); if (node.getElementType() == DustFileViewProvider.OUTER_TYPE) { // If we're looking at a DustTypes.HTML element, then we've been invoked by our templated // language. Make a dummy block to allow that formatter to continue return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings); } else { rootBlock = getRootBlock(file, file.getViewProvider(), settings); } return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file); }
@NotNull public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { final PsiFile file = element.getContainingFile(); if (element.getNode().getElementType() == SoyTypes.OTHER) { return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings); } else { return new DocumentBasedFormattingModel( getRootBlock(file, file.getViewProvider(), settings), element.getProject(), settings, file.getFileType(), file); } }
@NotNull public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { final PsiFile file = element.getContainingFile(); Block rootBlock; ASTNode node = element.getNode(); if (node.getElementType() == RYTHM) { return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings); } else { rootBlock = getRootBlock(file, file.getViewProvider(), settings); } return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file); }
@Override @NotNull public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) { if (element instanceof PsiFile) { final FileViewProvider viewProvider = ((PsiFile)element).getViewProvider(); if (viewProvider instanceof TemplateLanguageFileViewProvider) { final Language language = ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage(); FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forLanguage(language); if (builder != null) { return builder.createModel(viewProvider.getPsi(language), settings); } } } final PsiFile file = element.getContainingFile(); return new DocumentBasedFormattingModel(new AbstractBlock(element.getNode(), Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment()) { @Override protected List<Block> buildChildren() { return Collections.emptyList(); } @Override public Spacing getSpacing(final Block child1, @NotNull final Block child2) { return Spacing.getReadOnlySpacing(); } @Override public boolean isLeaf() { return true; } }, element.getProject(), settings, file.getFileType(), file); }
@Override @NotNull public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { final PsiFile file = element.getContainingFile(); Block rootBlock = getRootBlock(file, file.getViewProvider(), settings); return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file); }
@NotNull @Override public FormattingModel createModel(@NotNull final PsiElement element, @NotNull final CodeStyleSettings settings) { final PsiFile psiFile = element.getContainingFile(); final ASTNode rootNode = psiFile instanceof SquirrelFile ? psiFile.getNode() : element.getNode(); final SquirrelBlock rootBlock = new SquirrelBlock(rootNode, null, null, settings); return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, psiFile.getFileType(), psiFile); }
@Override @Nonnull public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) { if (element instanceof PsiFile) { final FileViewProvider viewProvider = ((PsiFile)element).getViewProvider(); if (viewProvider instanceof TemplateLanguageFileViewProvider) { final Language language = ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage(); FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forLanguage(language); if (builder != null) { return builder.createModel(viewProvider.getPsi(language), settings); } } } final PsiFile file = element.getContainingFile(); return new DocumentBasedFormattingModel(new AbstractBlock(element.getNode(), Wrap.createWrap(WrapType.NONE, false), Alignment.createAlignment()) { @Override protected List<Block> buildChildren() { return Collections.emptyList(); } @Override public Spacing getSpacing(final Block child1, @Nonnull final Block child2) { return Spacing.getReadOnlySpacing(); } @Override public boolean isLeaf() { return true; } }, element.getProject(), settings, file.getFileType(), file); }
@Override @Nonnull public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) { final PsiFile file = element.getContainingFile(); Block rootBlock = getRootBlock(file, file.getViewProvider(), settings); return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, file.getFileType(), file); }
@Override protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) { if (!myCanModifyAllWhiteSpaces) { if (leafElement.getElementType() == TokenType.WHITE_SPACE) return null; ASTNode prevNode = TreeUtil.prevLeaf(leafElement); if (prevNode != null) { IElementType type = prevNode.getElementType(); if(type == TokenType.WHITE_SPACE) { final String text = prevNode.getText(); @NonNls final String cdataStartMarker = "<![CDATA["; final int cdataPos = text.indexOf(cdataStartMarker); if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) { whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, text, cdataPos); if (whiteSpace == null) return null; } prevNode = TreeUtil.prevLeaf(prevNode); type = prevNode != null ? prevNode.getElementType():null; } @NonNls final String cdataEndMarker = "]]>"; if(type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) { final ASTNode at = findElementAt(prevNode.getStartOffset()); if (at != null && at.getPsi() instanceof PsiWhiteSpace) { final String s = at.getText(); final int cdataEndPos = s.indexOf(cdataEndMarker); whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos); leafElement = at; } else { whiteSpace = null; } if (whiteSpace == null) return null; } } } FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange); return whiteSpace; }
@Override protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) { if (!myCanModifyAllWhiteSpaces) { if (leafElement.getElementType() == TokenType.WHITE_SPACE) return null; ASTNode prevNode = TreeUtil.prevLeaf(leafElement); if (prevNode != null) { IElementType type = prevNode.getElementType(); if(type == TokenType.WHITE_SPACE) { final String text = prevNode.getText(); final @NonNls String cdataStartMarker = "<![CDATA["; final int cdataPos = text.indexOf(cdataStartMarker); if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) { whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, text, cdataPos); if (whiteSpace == null) return null; } prevNode = TreeUtil.prevLeaf(prevNode); type = prevNode != null ? prevNode.getElementType():null; } final @NonNls String cdataEndMarker = "]]>"; if(type == XmlElementType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) { final ASTNode at = findElementAt(prevNode.getStartOffset()); if (at != null && at.getPsi() instanceof PsiWhiteSpace) { final String s = at.getText(); final int cdataEndPos = s.indexOf(cdataEndMarker); whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos); leafElement = at; } else { whiteSpace = null; } if (whiteSpace == null) return null; } } } FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange); return whiteSpace; }
@Override protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) { if(!myCanModifyAllWhiteSpaces) { if(leafElement.getElementType() == TokenType.WHITE_SPACE) { return null; } ASTNode prevNode = TreeUtil.prevLeaf(leafElement); if(prevNode != null) { IElementType type = prevNode.getElementType(); if(type == TokenType.WHITE_SPACE) { final String text = prevNode.getText(); @NonNls final String cdataStartMarker = "<![CDATA["; final int cdataPos = text.indexOf(cdataStartMarker); if(cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) { whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, text, cdataPos); if(whiteSpace == null) { return null; } } prevNode = TreeUtil.prevLeaf(prevNode); type = prevNode != null ? prevNode.getElementType() : null; } @NonNls final String cdataEndMarker = "]]>"; if(type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) { final ASTNode at = findElementAt(prevNode.getStartOffset()); if(at != null && at.getPsi() instanceof PsiWhiteSpace) { final String s = at.getText(); final int cdataEndPos = s.indexOf(cdataEndMarker); whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos); leafElement = at; } else { whiteSpace = null; } if(whiteSpace == null) { return null; } } } } FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange); return whiteSpace; }