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();
}
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);
}
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;
}
}
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);
}
}
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);
}
Aggregations