Search in sources :

Example 6 with DocumentBasedFormattingModel

use of com.intellij.psi.formatter.DocumentBasedFormattingModel in project intellij-plugins by JetBrains.

the class DartFormattingModelBuilder method createModel.

@NotNull
@Override
public FormattingModel createModel(@NotNull PsiElement element, @NotNull CodeStyleSettings settings, @NotNull FormattingMode mode) {
    // element can be DartFile, DartEmbeddedContent, DartExpressionCodeFragment
    final PsiFile psiFile = element.getContainingFile();
    final ASTNode rootNode = psiFile instanceof DartFile ? psiFile.getNode() : element.getNode();
    final DartBlockContext context = new DartBlockContext(settings, mode);
    final DartBlock rootBlock = new DartBlock(rootNode, null, null, settings, context);
    return new DocumentBasedFormattingModel(rootBlock, element.getProject(), settings, psiFile.getFileType(), psiFile);
}
Also used : DartFile(com.jetbrains.lang.dart.psi.DartFile) DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with DocumentBasedFormattingModel

use of com.intellij.psi.formatter.DocumentBasedFormattingModel in project intellij-plugins by JetBrains.

the class HbFormattingModelBuilder method createModel.

/**
   * 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 HbTokenTypes.OUTER_ELEMENT_TYPE token we tell them to ignore,
   * causing an stack-overflowing loop of polite format-delegation.
   */
@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    if (!HbConfig.isFormattingEnabled()) {
        // to the templated language, which lets the users manage that separately)
        return new SimpleTemplateLanguageFormattingModelBuilder().createModel(element, settings);
    }
    final PsiFile file = element.getContainingFile();
    Block rootBlock;
    ASTNode node = element.getNode();
    if (node.getElementType() == HbTokenTypes.OUTER_ELEMENT_TYPE) {
        // 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);
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) SimpleTemplateLanguageFormattingModelBuilder(com.intellij.psi.templateLanguages.SimpleTemplateLanguageFormattingModelBuilder) ASTNode(com.intellij.lang.ASTNode) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) SyntheticBlock(com.intellij.psi.formatter.xml.SyntheticBlock) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DocumentBasedFormattingModel (com.intellij.psi.formatter.DocumentBasedFormattingModel)7 PsiFile (com.intellij.psi.PsiFile)6 NotNull (org.jetbrains.annotations.NotNull)6 ASTNode (com.intellij.lang.ASTNode)4 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)3 Language (com.intellij.lang.Language)2 SyntheticBlock (com.intellij.psi.formatter.xml.SyntheticBlock)2 SimpleTemplateLanguageFormattingModelBuilder (com.intellij.psi.templateLanguages.SimpleTemplateLanguageFormattingModelBuilder)2 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 PostprocessReformattingAspect (com.intellij.psi.impl.source.PostprocessReformattingAspect)1 FileElement (com.intellij.psi.impl.source.tree.FileElement)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 DartFile (com.jetbrains.lang.dart.psi.DartFile)1 List (java.util.List)1