Java源码示例:com.intellij.psi.formatter.DocumentBasedFormattingModel
示例1
/**
* 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);
}
示例2
@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);
}
}
示例3
@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);
}
示例4
@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);
}