Search in sources :

Example 1 with FormattingModel

use of com.intellij.formatting.FormattingModel 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 FormattingModel

use of com.intellij.formatting.FormattingModel 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 3 with FormattingModel

use of com.intellij.formatting.FormattingModel 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 4 with FormattingModel

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

the class JavaFormatterPerformanceTest method testPerformance.

public void testPerformance() throws Exception {
    final String name = getTestName(true) + ".java";
    final String text = loadFile(name);
    final PsiFile file = LightPlatformTestCase.createFile(name, text);
    transformAllChildren(SourceTreeToPsiMap.psiElementToTree(file));
    final CodeStyleSettings settings = new CodeStyleSettings();
    PlatformTestUtil.startPerformanceTest("java formatting", 1000, () -> {
        final FormattingModel model = LanguageFormatting.INSTANCE.forContext(file).createModel(file, settings);
        ((FormatterImpl) FormatterEx.getInstanceEx()).formatWithoutModifications(model.getDocumentModel(), model.getRootBlock(), settings, settings.getIndentOptions(StdFileTypes.JAVA), file.getTextRange());
    }).cpuBound().useLegacyScaling().assertTiming();
}
Also used : FormattingModel(com.intellij.formatting.FormattingModel) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) PsiFile(com.intellij.psi.PsiFile)

Example 5 with FormattingModel

use of com.intellij.formatting.FormattingModel 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

FormattingModel (com.intellij.formatting.FormattingModel)6 FormattingModelBuilder (com.intellij.formatting.FormattingModelBuilder)5 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)4 Block (com.intellij.formatting.Block)3 Document (com.intellij.openapi.editor.Document)3 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 PsiFile (com.intellij.psi.PsiFile)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 NewLineBlocksIterator (com.intellij.psi.formatter.common.NewLineBlocksIterator)1