Search in sources :

Example 6 with FormattingModelBuilder

use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.

the class AbstractNewLineBlocksIteratorTest method newLineBlockIterator.

@NotNull
protected static Iterator<Block> newLineBlockIterator() {
    FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile);
    Assert.assertNotNull(builder);
    CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(getProject()).getCurrentSettings();
    FormattingModel model = builder.createModel(myFile, settings);
    Block root = model.getRootBlock();
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(myFile);
    Assert.assertNotNull(document);
    return new NewLineBlocksIterator(root, document);
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Block(com.intellij.formatting.Block) Document(com.intellij.openapi.editor.Document) NewLineBlocksIterator(com.intellij.psi.formatter.common.NewLineBlocksIterator) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with FormattingModelBuilder

use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.

the class CodeStyleManagerRunnable method perform.

public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
    if (file instanceof PsiCompiledFile) {
        file = ((PsiCompiledFile) file).getDecompiledPsiFile();
    }
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
    Document document = documentManager.getDocument(file);
    if (document instanceof DocumentWindow) {
        final DocumentWindow documentWindow = (DocumentWindow) document;
        final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
        if (!file.equals(topLevelFile)) {
            if (range != null) {
                range = documentWindow.injectedToHost(range);
            }
            if (offset != -1) {
                offset = documentWindow.injectedToHost(offset);
            }
            return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
        }
    }
    final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
    if (templateFile != null) {
        file = templateFile;
        document = documentManager.getDocument(templateFile);
    }
    PsiElement element = null;
    if (offset != -1) {
        element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
        if (element == null && offset != file.getTextLength()) {
            return defaultValue;
        }
        if (isInsidePlainComment(offset, element)) {
            return computeValueInsidePlainComment(file, offset, defaultValue);
        }
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
    if (builder != null && elementBuilder != null) {
        mySettings = CodeStyleSettingsManager.getSettings(myCodeStyleManager.getProject());
        mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
        myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);
        FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
        myCodeStyleManager.setCurrentFormattingMode(myMode);
        try {
            myModel = buildModel(builder, file, document);
            T result = doPerform(offset, range);
            if (result != null) {
                return result;
            }
        } finally {
            myCodeStyleManager.setCurrentFormattingMode(currentMode);
        }
    }
    return defaultValue;
}
Also used : DocumentWindow(com.intellij.injected.editor.DocumentWindow) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) FormattingMode(com.intellij.formatting.FormattingMode) Document(com.intellij.openapi.editor.Document)

Example 8 with FormattingModelBuilder

use of com.intellij.formatting.FormattingModelBuilder in project intellij-community by JetBrains.

the class CodeStyleManagerRunnable method getSignificantRange.

private static TextRange getSignificantRange(final PsiFile file, final int offset) {
    final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset));
    if (elementAtOffset == null) {
        int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\n\r\t ");
        return new TextRange(Math.max(significantRangeStart, 0), offset);
    }
    final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
    if (builder != null) {
        final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset);
        if (textRange != null) {
            return textRange;
        }
    }
    final TextRange elementRange = elementAtOffset.getTextRange();
    if (isWhiteSpace(elementAtOffset)) {
        return extendRangeAtStartOffset(file, elementRange);
    }
    return elementRange;
}
Also used : ASTNode(com.intellij.lang.ASTNode) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)8 FormattingModel (com.intellij.formatting.FormattingModel)5 Document (com.intellij.openapi.editor.Document)4 Block (com.intellij.formatting.Block)3 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)3 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 FormattingMode (com.intellij.formatting.FormattingMode)1 DocumentWindow (com.intellij.injected.editor.DocumentWindow)1 ASTNode (com.intellij.lang.ASTNode)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 NewLineBlocksIterator (com.intellij.psi.formatter.common.NewLineBlocksIterator)1