Search in sources :

Example 1 with PostprocessReformattingAspect

use of com.intellij.psi.impl.source.PostprocessReformattingAspect in project intellij-community by JetBrains.

the class PsiDocumentManagerImpl method doPostponedOperationsAndUnblockDocument.

@Override
public void doPostponedOperationsAndUnblockDocument(@NotNull Document doc) {
    if (doc instanceof DocumentWindow)
        doc = ((DocumentWindow) doc).getDelegate();
    final PostprocessReformattingAspect component = myProject.getComponent(PostprocessReformattingAspect.class);
    final FileViewProvider viewProvider = getCachedViewProvider(doc);
    if (viewProvider != null && component != null)
        component.doPostponedFormatting(viewProvider);
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) PostprocessReformattingAspect(com.intellij.psi.impl.source.PostprocessReformattingAspect) FileViewProvider(com.intellij.psi.FileViewProvider)

Example 2 with PostprocessReformattingAspect

use of com.intellij.psi.impl.source.PostprocessReformattingAspect 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)

Aggregations

DocumentWindow (com.intellij.injected.editor.DocumentWindow)2 PostprocessReformattingAspect (com.intellij.psi.impl.source.PostprocessReformattingAspect)2 ASTNode (com.intellij.lang.ASTNode)1 Language (com.intellij.lang.Language)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 DocumentBasedFormattingModel (com.intellij.psi.formatter.DocumentBasedFormattingModel)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1