Search in sources :

Example 1 with DocumentBasedFormattingModel

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

the class TemplateLanguageFormattingModelBuilder method createModel.

@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);
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DocumentBasedFormattingModel

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

the class CodeFormatterFacade method processText.

public void processText(PsiFile file, final FormatTextRanges ranges, boolean doPostponedFormatting) {
    final Project project = file.getProject();
    Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    final List<FormatTextRange> textRanges = ranges.getRanges();
    if (document instanceof DocumentWindow) {
        file = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        final DocumentWindow documentWindow = (DocumentWindow) document;
        for (FormatTextRange range : textRanges) {
            range.setTextRange(documentWindow.injectedToHost(range.getTextRange()));
        }
        document = documentWindow.getDelegate();
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    final Language contextLanguage = file.getLanguage();
    if (builder != null) {
        if (file.getTextLength() > 0) {
            LOG.assertTrue(document != null);
            try {
                final FileViewProvider viewProvider = file.getViewProvider();
                final PsiElement startElement = viewProvider.findElementAt(textRanges.get(0).getTextRange().getStartOffset(), contextLanguage);
                final PsiElement endElement = viewProvider.findElementAt(textRanges.get(textRanges.size() - 1).getTextRange().getEndOffset() - 1, contextLanguage);
                final PsiElement commonParent = startElement != null && endElement != null ? PsiTreeUtil.findCommonParent(startElement, endElement) : null;
                ASTNode node = null;
                if (commonParent != null) {
                    node = commonParent.getNode();
                }
                if (node == null) {
                    node = file.getNode();
                }
                for (FormatTextRange range : ranges.getRanges()) {
                    TextRange rangeToUse = preprocess(node, range.getTextRange());
                    range.setTextRange(rangeToUse);
                }
                if (doPostponedFormatting) {
                    RangeMarker[] markers = new RangeMarker[textRanges.size()];
                    int i = 0;
                    for (FormatTextRange range : textRanges) {
                        TextRange textRange = range.getTextRange();
                        int start = textRange.getStartOffset();
                        int end = textRange.getEndOffset();
                        if (start >= 0 && end > start && end <= document.getTextLength()) {
                            markers[i] = document.createRangeMarker(textRange);
                            markers[i].setGreedyToLeft(true);
                            markers[i].setGreedyToRight(true);
                            i++;
                        }
                    }
                    final PostprocessReformattingAspect component = file.getProject().getComponent(PostprocessReformattingAspect.class);
                    FormattingProgressTask.FORMATTING_CANCELLED_FLAG.set(false);
                    component.doPostponedFormatting(file.getViewProvider());
                    i = 0;
                    for (FormatTextRange range : textRanges) {
                        RangeMarker marker = markers[i];
                        if (marker != null) {
                            range.setTextRange(TextRange.create(marker));
                            marker.dispose();
                        }
                        i++;
                    }
                }
                if (FormattingProgressTask.FORMATTING_CANCELLED_FLAG.get()) {
                    return;
                }
                final FormattingModel originalModel = CoreFormatterUtil.buildModel(builder, file, mySettings, FormattingMode.REFORMAT);
                final FormattingModel model = new DocumentBasedFormattingModel(originalModel, document, project, mySettings, file.getFileType(), file);
                FormatterEx formatter = FormatterEx.getInstanceEx();
                if (CodeStyleManager.getInstance(project).isSequentialProcessingAllowed()) {
                    formatter.setProgressTask(new FormattingProgressTask(project, file, document));
                }
                CommonCodeStyleSettings.IndentOptions indentOptions = mySettings.getIndentOptionsByFile(file, textRanges.size() == 1 ? textRanges.get(0).getTextRange() : null);
                formatter.format(model, mySettings, indentOptions, ranges, myReformatContext);
                for (FormatTextRange range : textRanges) {
                    TextRange textRange = range.getTextRange();
                    wrapLongLinesIfNecessary(file, document, textRange.getStartOffset(), textRange.getEndOffset());
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) TextRange(com.intellij.openapi.util.TextRange) DocumentWindow(com.intellij.injected.editor.DocumentWindow) DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) Project(com.intellij.openapi.project.Project) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) Language(com.intellij.lang.Language) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with DocumentBasedFormattingModel

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

the class GherkinFormattingModelBuilder method createModel.

@NotNull
public FormattingModel createModel(PsiElement element, CodeStyleSettings settings) {
    final PsiFile file = element.getContainingFile();
    final FileElement fileElement = TreeUtil.getFileElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(element));
    final GherkinBlock rootBlock = new GherkinBlock(fileElement);
    //FormattingModelDumper.dumpFormattingModel(rootBlock, 0, System.out);
    return new DocumentBasedFormattingModel(rootBlock, file.getProject(), settings, file.getFileType(), file);
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) FileElement(com.intellij.psi.impl.source.tree.FileElement) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with DocumentBasedFormattingModel

use of com.intellij.psi.formatter.DocumentBasedFormattingModel in project idea-handlebars by dmarcotte.

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) SyntheticBlock(com.intellij.psi.formatter.xml.SyntheticBlock) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with DocumentBasedFormattingModel

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

the class SimpleTemplateLanguageFormattingModelBuilder method createModel.

@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);
}
Also used : DocumentBasedFormattingModel(com.intellij.psi.formatter.DocumentBasedFormattingModel) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) PsiFile(com.intellij.psi.PsiFile) List(java.util.List) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) 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