From 3a5fb23841fa6789d36d7107e064aae282d9858e Mon Sep 17 00:00:00 2001 From: "Denis.Zhdanov" Date: Thu, 29 Mar 2012 19:02:17 +0400 Subject: [PATCH] IDEA-83278 "Wrapping and Braces -> Keep when reformatting -> Multiple expressions in one line" prevents refactorings/intentions from producing correctly formatted code --- .../java/JavaSpacePropertyProcessor.java | 9 ++++++++- .../intellij/formatting/FormatterImpl.java | 8 +++----- .../intellij/psi/formatter/FormatterUtil.java | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java index f27234ef9a5c..8d04c48535b9 100644 --- a/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java +++ b/java/java-impl/src/com/intellij/psi/formatter/java/JavaSpacePropertyProcessor.java @@ -767,7 +767,14 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor { int minSpaces = 0; int minLineFeeds = 1; PsiElement psi = myChild1.getPsi(); - if (mySettings.KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE || psi != null && PsiTreeUtil.hasErrorElements(psi)) { + + // We want to avoid situations like below: + // 1. Call 'introduce variable' refactoring for the code like 'System.out.println(1);'; + // 2. When KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE is on, the output looks like 'int i = 1; System.out.println(i);'; + // That's why we process the option only during the explicit reformat (directly invoked by an user). + if ((mySettings.KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE && FormatterUtil.isFormatterCalledExplicitly()) + || psi != null && PsiTreeUtil.hasErrorElements(psi)) + { minSpaces = 1; minLineFeeds = 0; } diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java index 660eedc034a7..0b47d69e55ef 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package com.intellij.formatting; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.components.ApplicationComponent; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.fileTypes.FileType; @@ -34,7 +33,6 @@ import com.intellij.psi.formatter.PsiBasedFormattingModel; import com.intellij.util.IncorrectOperationException; import com.intellij.util.SequentialTask; import com.intellij.util.text.CharArrayUtil; -import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -98,7 +96,7 @@ public class FormatterImpl extends FormatterEx @Override public void setProgressTask(@NotNull FormattingProgressTask progressIndicator) { - if (!FormatterUtil.FORMATTER_ACTION_NAMES.contains(CommandProcessor.getInstance().getCurrentCommandName())) { + if (!FormatterUtil.isFormatterCalledExplicitly()) { return; } myProgressTask.set(progressIndicator); @@ -694,7 +692,7 @@ public class FormatterImpl extends FormatterEx final boolean keepLineBreaksFlag, final int keepLineBreaks, final boolean keepFirstColumn, int prefLineFeeds) { - synchronized(this) { + synchronized(ourSharedSpacing) { ourSharedSpacing.init(minSpaces, maxSpaces, minLineFeeds, readOnly, safe, keepLineBreaksFlag, keepLineBreaks, keepFirstColumn, prefLineFeeds); SpacingImpl spacing = ourSharedProperties.get(ourSharedSpacing); diff --git a/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtil.java b/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtil.java index c40a94360506..12b46a4f83e9 100644 --- a/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtil.java +++ b/platform/lang-impl/src/com/intellij/psi/formatter/FormatterUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ import com.intellij.codeInsight.actions.ReformatAndOptimizeImportsProcessor; import com.intellij.codeInsight.actions.ReformatCodeProcessor; import com.intellij.lang.ASTFactory; import com.intellij.lang.ASTNode; +import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.TokenType; @@ -292,7 +293,11 @@ public class FormatterUtil { private static ASTNode findPreviousWhiteSpace(final ASTNode leafElement, final IElementType whiteSpaceTokenType) { final int offset = leafElement.getTextRange().getStartOffset() - 1; if (offset < 0) return null; - final PsiElement found = SourceTreeToPsiMap.treeElementToPsi(leafElement).getContainingFile().findElementAt(offset); + final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(leafElement); + if (psiElement == null) { + return null; + } + final PsiElement found = psiElement.getContainingFile().findElementAt(offset); if (found == null) return null; final ASTNode treeElement = found.getNode(); if (treeElement != null && treeElement.getElementType() == whiteSpaceTokenType) return treeElement; @@ -357,6 +362,9 @@ public class FormatterUtil { public static void replaceLastWhiteSpace(final ASTNode astNode, final String whiteSpace, final TextRange textRange) { ASTNode lastWS = TreeUtil.findLastLeaf(astNode); + if (lastWS == null) { + return; + } if (lastWS.getElementType() != TokenType.WHITE_SPACE) { lastWS = null; } @@ -381,4 +389,11 @@ public class FormatterUtil { treeParent.replaceChild(lastWS, whiteSpaceElement); } } + + /** + * @return true explicitly called 'reformat' is in progress at the moment; false otherwise + */ + public static boolean isFormatterCalledExplicitly() { + return FORMATTER_ACTION_NAMES.contains(CommandProcessor.getInstance().getCurrentCommandName()); + } }