Search in sources :

Example 1 with FormattingModelBuilder

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

the class IndentOptionsDetectorImpl method calcLineIndentInfo.

@Nullable
private List<LineIndentInfo> calcLineIndentInfo(@Nullable ProgressIndicator indicator) {
    if (myDocument == null || myDocument.getLineCount() < 3 || isFileBigToDetect()) {
        return null;
    }
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(myProject);
    FormattingModelBuilder modelBuilder = LanguageFormatting.INSTANCE.forContext(myFile);
    if (modelBuilder == null)
        return null;
    FormattingModel model = modelBuilder.createModel(myFile, settings);
    Block rootBlock = model.getRootBlock();
    return new FormatterBasedLineIndentInfoBuilder(myDocument, rootBlock, indicator).build();
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Block(com.intellij.formatting.Block) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with FormattingModelBuilder

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

the class ReformatCodeAction method update.

@Override
public void update(AnActionEvent event) {
    Presentation presentation = event.getPresentation();
    DataContext dataContext = event.getDataContext();
    Project project = CommonDataKeys.PROJECT.getData(dataContext);
    if (project == null) {
        presentation.setEnabled(false);
        return;
    }
    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    final VirtualFile[] files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(dataContext);
    if (editor != null) {
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null || file.getVirtualFile() == null) {
            presentation.setEnabled(false);
            return;
        }
        if (LanguageFormatting.INSTANCE.forContext(file) != null) {
            presentation.setEnabled(true);
            return;
        }
    } else if (files != null && containsAtLeastOneFile(files)) {
        boolean anyFormatters = false;
        for (VirtualFile virtualFile : files) {
            if (virtualFile.isDirectory()) {
                presentation.setEnabled(false);
                return;
            }
            final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
            if (psiFile == null) {
                presentation.setEnabled(false);
                return;
            }
            final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(psiFile);
            if (builder != null) {
                anyFormatters = true;
            }
        }
        if (!anyFormatters) {
            presentation.setEnabled(false);
            return;
        }
    } else if (files != null && files.length == 1) {
    // skip. Both directories and single files are supported.
    } else if (LangDataKeys.MODULE_CONTEXT.getData(dataContext) == null && PlatformDataKeys.PROJECT_CONTEXT.getData(dataContext) == null) {
        PsiElement element = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
        if (element == null) {
            presentation.setEnabled(false);
            return;
        }
        if (!(element instanceof PsiDirectory)) {
            PsiFile file = element.getContainingFile();
            if (file == null || LanguageFormatting.INSTANCE.forContext(file) == null) {
                presentation.setEnabled(false);
                return;
            }
        }
    }
    presentation.setEnabled(true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Editor(com.intellij.openapi.editor.Editor)

Example 3 with FormattingModelBuilder

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

the class PsiViewerDialog method buildBlocks.

@Nullable
private static Block buildBlocks(@NotNull PsiElement rootElement) {
    FormattingModelBuilder formattingModelBuilder = LanguageFormatting.INSTANCE.forContext(rootElement);
    CodeStyleSettings settings = CodeStyleSettingsManager.getSettings(rootElement.getProject());
    if (formattingModelBuilder != null) {
        FormattingModel formattingModel = formattingModelBuilder.createModel(rootElement, settings);
        return formattingModel.getRootBlock();
    } else {
        return null;
    }
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with FormattingModelBuilder

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

the class JavaAutoDetectIndentTest method doTestLineToIndentMapping.

private static void doTestLineToIndentMapping(@NotNull String text, int... spacesForLine) {
    configureFromFileText("x.java", text);
    Document document = PsiDocumentManager.getInstance(getProject()).getDocument(myFile);
    FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile);
    Assert.assertNotNull(document);
    Assert.assertNotNull(builder);
    FormattingModel model = builder.createModel(myFile, CodeStyleSettingsManager.getSettings(getProject()));
    Block block = model.getRootBlock();
    List<LineIndentInfo> list = new FormatterBasedLineIndentInfoBuilder(document, block, null).build();
    Assert.assertEquals(list.size(), spacesForLine.length);
    for (int i = 0; i < spacesForLine.length; i++) {
        int indentSize = list.get(i).getIndentSize();
        Assert.assertEquals("Mismatch on line " + i, spacesForLine[i], indentSize);
    }
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Block(com.intellij.formatting.Block) Document(com.intellij.openapi.editor.Document)

Example 5 with FormattingModelBuilder

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

the class AbstractIndentAutoDetectionTest method getMaxUsedIndentInfo.

@NotNull
private IndentUsageInfo getMaxUsedIndentInfo() {
    configureByFile(getFileNameWithExtension());
    Document document = getDocument(myFile);
    FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(myFile);
    Assert.assertNotNull(builder);
    FormattingModel model = builder.createModel(myFile, CodeStyleSettingsManager.getSettings(getProject()));
    List<LineIndentInfo> lines = new FormatterBasedLineIndentInfoBuilder(document, model.getRootBlock(), null).build();
    IndentUsageStatistics statistics = new IndentUsageStatisticsImpl(lines);
    return statistics.getKMostUsedIndentInfo(0);
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) FormattingModelBuilder(com.intellij.formatting.FormattingModelBuilder) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

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