IDEA-90860 Reformat Code breaks Copyright/Header

1. Block.getSpacing() contract is refined;
2. Java formatter tests don't strip line feeds and trailing spaces now;
3. Test data is corrected;
This commit is contained in:
Denis.Zhdanov
2012-09-01 18:43:06 +04:00
parent a0023648bd
commit 3d15472599
59 changed files with 65 additions and 125 deletions
@@ -297,7 +297,7 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
@Nullable
@Override
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return JavaSpacePropertyProcessor.getSpacing(getTreeNode(child2), mySettings);
}
@@ -56,7 +56,7 @@ public class CommentWithInjectionBlock extends AbstractJavaBlock {
}
@Override
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return null;
}
@@ -70,6 +70,12 @@ public class JavaSpacePropertyProcessor extends JavaElementVisitor {
init(child);
mySettings = settings;
if (myChild1 == null) {
// Given node corresponds to the first document block.
createSpaceInCode(false);
return;
}
final PsiElement myChild1Psi1 = myChild1.getPsi();
final PsiElement myChild1Psi2 = myChild2.getPsi();
if (myChild1Psi1 == null || myChild1Psi1.getLanguage() != JavaLanguage.INSTANCE ||
@@ -80,7 +80,7 @@ public class LeafBlock implements ASTBlock{
}
@Override
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return null;
}
@@ -72,7 +72,7 @@ public class ReadonlyWhitespaceBlock implements Block {
@Override
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return null;
}
@@ -88,7 +88,7 @@ public class SyntheticCodeBlock implements Block, JavaBlock{
}
@Override
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return JavaSpacePropertyProcessor.getSpacing(AbstractJavaBlock.getTreeNode(child2), mySettings);
}
@@ -1,4 +1,3 @@
class Foo {
void foo() {
foo(
@@ -1,4 +1,3 @@
public class A {
private void foo() {
Runnable a = new Runnable() {
@@ -1,4 +1,3 @@
class Foo {
void foo() {
someVariable = x =
@@ -1,4 +1,3 @@
class Foo {
void foo() {
for (int i = 0;
@@ -1,4 +1,3 @@
class Foo {
public void foo() {
int someVariable = (y +
@@ -1,4 +1,3 @@
class A {
A IMPL = new A() {
void f(boolean b) {
@@ -1,4 +1,3 @@
public class A {
void f() {
new My() {
@@ -1,4 +1,3 @@
class Foo {
void foo() {
assert false;
@@ -1,4 +1,3 @@
class Foo {
void foo() {
if (a) {
@@ -1,4 +1,3 @@
class Foo {
public void a() {
if (b) {
@@ -1,4 +1,3 @@
class Foo {
public void foo() {
if (a) {
@@ -1,4 +1,3 @@
class A {
void foo() {
continue;
@@ -1,4 +1,3 @@
class C {
{
for (String s : collection) {
@@ -1,4 +1,3 @@
class Foo {
void foo() {
if (a) {
@@ -1,4 +1,3 @@
class Foo {
void foo() {
if (a) {
@@ -1,4 +1,3 @@
class Foo {
void foo() {
if (a)
@@ -1,4 +1,3 @@
class Foo {
public void foo() {
bar_call(new Runnable() {
@@ -1,4 +1,3 @@
public class Test {
void anotherMethod(String s);
@@ -1,4 +1,3 @@
class Foo {
void foo() {
ClassBar bar =
@@ -1,4 +1,3 @@
class C {
public static final C1 ourC =
new C2(new C3(),
@@ -1,4 +1,3 @@
class Foo {
void foo() {
try
@@ -1,4 +1,3 @@
class TestFormat {
public void test(boolean isA, boolean isB, boolean isC) {
if (isA)
@@ -1,4 +1,3 @@
class Foo {
void foo() {
if (AAA == BBB ||
@@ -1,4 +1,3 @@
public class Y implements X {
public void doSomething() // this does something
{
@@ -1,4 +1,3 @@
class Foo {
@@ -1,4 +1,3 @@
class A {
{
y = x+++6;
@@ -1,4 +1,3 @@
class Foo {
void foo() {
switch (a) {
@@ -1,4 +1,3 @@
class Foo {
public void foo() {
LOG.bar();
@@ -1,4 +1,3 @@
class Foo {
void foo() {
int someVariable = a ?
@@ -1,4 +1,3 @@
class Foo {
void foo() {
int someVariable = a ?
@@ -1,4 +1,3 @@
class Foo {
public void foo() {
assertEquals(map.get(new File("C:\\PerforceTest1\\created2.txt")), );
@@ -20,7 +20,6 @@ import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
@@ -161,9 +160,9 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
fail("Don't expect the document to be null");
return;
}
assertEquals(prepareText(textAfter), prepareText(document.getText()));
assertEquals(textAfter, document.getText());
manager.commitDocument(document);
assertEquals(prepareText(textAfter), prepareText(file.getText()));
assertEquals(textAfter, file.getText());
}
@@ -183,31 +182,6 @@ public abstract class AbstractJavaFormatterTest extends LightIdeaTestCase {
);
}
private static String prepareText(String actual) {
if (actual.startsWith("\n")) {
actual = actual.substring(1);
}
if (actual.startsWith("\n")) {
actual = actual.substring(1);
}
// Strip trailing spaces
final Document doc = EditorFactory.getInstance().createDocument(actual);
CommandProcessor.getInstance().executeCommand(getProject(), new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
((DocumentImpl)doc).stripTrailingSpaces();
}
});
}
}, "formatting", null);
return doc.getText();
}
private static String loadFile(String name) throws Exception {
String fullName = BASE_PATH + File.separatorChar + name;
String text = FileUtil.loadFile(new File(fullName));
@@ -103,12 +103,12 @@ public class JavaFormatterBracesTest extends AbstractJavaFormatterTest {
"void foo() {\n" +
"}",
"@Override\n" +
"public int hashCode() \n" +
"public int hashCode()\n" +
"{\n" +
"}\n" +
"\n" +
"@Deprecated\n" +
"void foo() \n" +
"void foo()\n" +
"{\n" +
"}"
);
@@ -147,7 +147,7 @@ public class JavaFormatterBracesTest extends AbstractJavaFormatterTest {
getSettings().CLASS_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
String text =
"class Test \n" +
"class Test\n" +
"{\n" +
" void foo() {\n" +
" bind(new TypeLiteral<MyType>() {}).toProvider(MyProvider.class);\n" +
@@ -321,14 +321,14 @@ public class JavaFormatterIndentationTest extends AbstractJavaFormatterTest {
"} \n" +
"); ",
"foo(\"long string as the first argument\", new Runnable() {\n" +
" public void run() { \n" +
" } \n" +
" }, \n" +
" new Runnable() { \n" +
" public void run() { \n" +
" } \n" +
" } \n" +
"); "
" public void run() {\n" +
" }\n" +
" },\n" +
" new Runnable() {\n" +
" public void run() {\n" +
" }\n" +
" }\n" +
");"
);
doMethodTest(
@@ -343,7 +343,7 @@ public class JavaFormatterIndentationTest extends AbstractJavaFormatterTest {
" @Override\n" +
" public void run() {\n" +
" }\n" +
"}); "
"});"
);
doMethodTest(
@@ -504,7 +504,7 @@ public class JavaFormatterIndentationTest extends AbstractJavaFormatterTest {
);
}
public void testMultilineComment() {
public void testMultilineCommentAtFileStart() {
// IDEA-90860
String text =
"\n" +
@@ -242,8 +242,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_BINARY_OPERATION = true;
settings.ALIGN_MULTILINE_ASSIGNMENT = true;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" + ccc + eee + ddd;\n" +
@@ -252,8 +251,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_BINARY_OPERATION = true;
settings.ALIGN_MULTILINE_ASSIGNMENT = false;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" + ccc + eee + ddd;\n" +
@@ -263,8 +261,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_BINARY_OPERATION = false;
settings.ALIGN_MULTILINE_ASSIGNMENT = true;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" + ccc + eee + ddd;\n" +
@@ -274,8 +271,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_ASSIGNMENT = false;
settings.ALIGN_MULTILINE_BINARY_OPERATION = false;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" + ccc + eee + ddd;\n" +
@@ -285,8 +281,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_ASSIGNMENT = false;
settings.ALIGN_MULTILINE_BINARY_OPERATION = false;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" + ccc + eee + ddd;\n" +
@@ -297,7 +292,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.ALIGN_MULTILINE_BINARY_OPERATION = true;
doTextTest("class Foo {\n" + " void foo () {\n" + " xxx = aaa + bbb \n" + " - ccc + eee + ddd;\n" + " }\n" + "}",
"\nclass Foo {\n" +
"class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" - ccc + eee + ddd;\n" +
@@ -305,7 +300,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
"}");
doTextTest("class Foo {\n" + " void foo () {\n" + " xxx = aaa + bbb \n" + " * ccc + eee + ddd;\n" + " }\n" + "}",
"\nclass Foo {\n" +
"class Foo {\n" +
" void foo() {\n" +
" xxx = aaa + bbb\n" +
" * ccc + eee + ddd;\n" +
@@ -347,7 +342,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
doTextTest("class Foo {\n" + " void foo () {\n" + "String s = \"abc\" +\n" + "\"def\";" + " }\n" + "}",
"\nclass Foo {\n" +
"class Foo {\n" +
" void foo() {\n" +
" String s = \"abc\" +\n" +
" \"def\";\n" +
@@ -379,8 +374,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.BRACE_STYLE = CommonCodeStyleSettings.END_OF_LINE;
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.END_OF_LINE;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo() {\n" +
" if (a) {\n" +
" int i = 0;\n" +
@@ -390,8 +384,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo()\n" +
" {\n" +
" if (a)\n" +
@@ -404,8 +397,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED;
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo()\n" +
" {\n" +
" if (a)\n" +
@@ -417,8 +409,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED;
settings.BRACE_STYLE = CommonCodeStyleSettings.END_OF_LINE;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo()\n" +
" {\n" +
" if (a) {\n" +
@@ -430,8 +421,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
settings.METHOD_BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED2;
settings.BRACE_STYLE = CommonCodeStyleSettings.NEXT_LINE_SHIFTED2;
doTextTest(text, "\n" +
"class Foo {\n" +
doTextTest(text, "class Foo {\n" +
" void foo()\n" +
" {\n" +
" if (a)\n" +
@@ -538,7 +528,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
public void testTwoJavaDocs() throws Exception {
doTextTest("/**\n" + " * \n" + " */\n" + " class Test {\n" + " /**\n" + " */\n" + " public void foo();\n" + "}",
"/**\n" + " * \n" + " */\n" + "class Test {\n" + " /**\n" + " */\n" + " public void foo();\n" + "}");
"/**\n" + " *\n" + " */\n" + "class Test {\n" + " /**\n" + " */\n" + " public void foo();\n" + "}");
}
public void testJavaDocLinksWithParameterNames() throws Exception {
@@ -595,7 +585,7 @@ public class JavaFormatterTest extends AbstractJavaFormatterTest {
public void testDocComment() throws Exception {
doTextTest("public class TestClass {\n" + "/**\n" + "* \n" + "*/\n" + " public void f1() {\n" + " }\n" + "}",
"public class TestClass {\n" + " /**\n" + " * \n" + " */\n" + " public void f1() {\n" + " }\n" + "}");
"public class TestClass {\n" + " /**\n" + " *\n" + " */\n" + " public void f1() {\n" + " }\n" + "}");
}
public void testDocComment2() throws Exception {
@@ -179,12 +179,12 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
" public String s = \"this is a string that is long enough to be wrapped\"\n" +
"}",
"class TestClass {\n" +
" // Single line comment that is long enough\n" +
" // Single line comment that is long enough \n" +
" // to exceed right margin\n" +
" /* Multi line comment that is long enough\n" +
" /* Multi line comment that is long enough \n" +
" to exceed right margin*/\n" +
" /**\n" +
" * Javadoc comment that is long enough to\n" +
" * Javadoc comment that is long enough to \n" +
" * exceed right margin\n" +
" */\n" +
" public String s = \"this is a string that is\" +\n" +
@@ -205,7 +205,7 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
"\t \t //This is a comment\n" +
"}",
"class TestClass {\n" +
"\t//This is a\n" +
"\t//This is a \n" +
"\t// comment\n" +
"}"
);
@@ -231,7 +231,7 @@ public class JavaFormatterWrapTest extends AbstractJavaFormatterTest {
doTextTest(
initial,
"class TestClass {\n" +
" //This is a\n" +
" //This is a \n" +
" // comment\n" +
" //This is another comment\n" +
"}"
@@ -33,7 +33,7 @@ public class JavadocFormatterTest extends AbstractJavaFormatterTest {
"class Foo {\n" +
"}",
"/**\n" +
" * Here is one-line java-doc\n" +
" * Here is one-line java-doc \n" +
" * comment\n" +
" */\n" +
"class Foo {\n" +
@@ -83,7 +83,8 @@ public interface Block {
* Returns a spacing object indicating what spaces and/or line breaks are added between two
* specified children of this block.
*
* @param child1 the first child for which spacing is requested.
* @param child1 the first child for which spacing is requested;
* <code>null</code> if given <code>'child2'</code> block is the first document block
* @param child2 the second child for which spacing is requested.
* @return the spacing instance, or null if no special spacing is required. If null is returned,
* the formatter does not insert or delete spaces between the child blocks, but may insert
@@ -92,7 +93,7 @@ public interface Block {
* @see Spacing#getReadOnlySpacing()
*/
@Nullable
Spacing getSpacing(Block child1, Block child2);
Spacing getSpacing(@Nullable Block child1, @NotNull Block child2);
/**
* Returns the alignment and indent attributes which are applied to a new block inserted at
@@ -215,7 +215,7 @@ class InitialInfoBuilder {
final int subBlocksCount = subBlocks.size();
int childBlockIndex = state.getIndexOfChildBlockToProcess();
final Block block = subBlocks.get(childBlockIndex);
if (state.previousBlock != null) {
if (state.previousBlock != null || (myCurrentWhiteSpace != null && myCurrentWhiteSpace.isIsFirstWhiteSpace())) {
myCurrentSpaceProperty = (SpacingImpl)state.parentBlock.getSpacing(state.previousBlock, block);
}
@@ -387,7 +387,6 @@ class WhiteSpace {
setLineFeeds(spaceProperty.getPrefLineFeeds());
}
}
} else if (isFirst()) {
setLineFeeds(0);
mySpaces = 0;
@@ -61,7 +61,7 @@ public class DataLanguageBlockFragmentWrapper implements Block {
}
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return Spacing.getReadOnlySpacing();
}
@@ -118,7 +118,7 @@ public class DataLanguageBlockWrapper implements ASTBlock, BlockEx, BlockWithPar
}
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if (child1 instanceof DataLanguageBlockWrapper && child2 instanceof DataLanguageBlockWrapper) {
return myOriginal.getSpacing(((DataLanguageBlockWrapper)child1).myOriginal, ((DataLanguageBlockWrapper)child2).myOriginal);
}
@@ -101,7 +101,7 @@ public abstract class TemplateLanguageBlock extends AbstractBlock implements Blo
}
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if (child1 instanceof DataLanguageBlockWrapper && child2 instanceof DataLanguageBlockWrapper) {
return ((DataLanguageBlockWrapper)child1).getRightHandSpacing((DataLanguageBlockWrapper)child2);
}
@@ -78,7 +78,7 @@ public abstract class TemplateLanguageFormattingModelBuilder implements Delegati
return Collections.emptyList();
}
public Spacing getSpacing(final Block child1, final Block child2) {
public Spacing getSpacing(final Block child1, @NotNull final Block child2) {
return Spacing.getReadOnlySpacing();
}
@@ -118,7 +118,7 @@ public final class InjectedLanguageBlockWrapper implements Block {
}
@Override
@Nullable public Spacing getSpacing(final Block child1, final Block child2) {
@Nullable public Spacing getSpacing(final Block child1, @NotNull final Block child2) {
int shift = 0;
Block child1ToUse = child1;
Block child2ToUse = child2;
@@ -57,7 +57,7 @@ public class SimpleTemplateLanguageFormattingModelBuilder implements FormattingM
}
@Override
public Spacing getSpacing(final Block child1, final Block child2) {
public Spacing getSpacing(final Block child1, @NotNull final Block child2) {
return Spacing.getReadOnlySpacing();
}
@@ -151,7 +151,7 @@ public class GroovyBlock implements Block, GroovyElementTypes, ASTBlock {
* @return
*/
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if ((child1 instanceof GroovyBlock) && (child2 instanceof GroovyBlock)) {
if (((GroovyBlock)child1).getNode() == ((GroovyBlock)child2).getNode()) {
return Spacing.getReadOnlySpacing();
@@ -67,7 +67,7 @@ public class AnotherLanguageBlockWrapper extends AbstractXmlBlock{
}
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return myInjectedBlock.getSpacing(child1, child2);
}
@@ -19,6 +19,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.formatting.Block;
import com.intellij.formatting.Spacing;
import com.intellij.psi.formatter.common.AbstractBlock;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -30,7 +31,7 @@ public class ReadOnlyBlock extends AbstractBlock {
super(node, null, null);
}
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
return null;
}
@@ -46,7 +46,7 @@ public class SyntheticBlock extends AbstractSyntheticBlock implements Block, Rea
return mySubBlocks;
}
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if (child1 instanceof ReadOnlyBlock || child2 instanceof ReadOnlyBlock) {
return Spacing.getReadOnlySpacing();
}
@@ -223,7 +223,7 @@ public class XmlBlock extends AbstractXmlBlock {
}
}
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if (!(child1 instanceof AbstractBlock) || !(child2 instanceof AbstractBlock)) {
return null;
}
@@ -203,7 +203,7 @@ public class XmlTagBlock extends AbstractXmlBlock{
return createSyntheticBlock(localResult, null);
}
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
if (isPreserveSpace()) return Spacing.getReadOnlySpacing();
if(child1 instanceof AbstractSyntheticBlock && child2 instanceof AbstractSyntheticBlock) {
return getSpacing((AbstractSyntheticBlock)child1, (AbstractSyntheticBlock)child2);
@@ -89,7 +89,7 @@ class RncBlock implements Block {
}
@Nullable
public Spacing getSpacing(Block child1, Block child2) {
public Spacing getSpacing(Block child1, @NotNull Block child2) {
final ASTNode lnode = ((RncBlock)child1).myNode;
final PsiElement lpsi = lnode.getPsi();
final PsiElement rpsi = ((RncBlock)child2).myNode.getPsi();