From 5ae568cc41059cf1d741fcdebf4b0e0dfc81aaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pap=20L=C5=91rinc?= Date: Sun, 30 Aug 2015 13:34:35 +0300 Subject: [PATCH 1/2] [groovy] format comments properly in Spock 'where' tables --- .../fixtures/JavaCodeInsightTestFixture.java | 3 +- .../blocks/GroovyBlockGenerator.java | 3 +- .../lang/formatter/FormatterTest.groovy | 7 ++- .../testdata/groovy/formatter/spockTable.test | 31 ------------- .../groovy/formatter/spockTableComments.test | 23 ---------- .../formatter/spockTableWithComments.test | 43 +++++++++++++++++++ .../spockTableWithStringComment.test | 11 +++-- 7 files changed, 54 insertions(+), 67 deletions(-) delete mode 100644 plugins/groovy/testdata/groovy/formatter/spockTable.test delete mode 100644 plugins/groovy/testdata/groovy/formatter/spockTableComments.test create mode 100644 plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test diff --git a/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestFixture.java b/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestFixture.java index e5d8c43a8c0a..be2411c7db63 100644 --- a/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestFixture.java +++ b/java/testFramework/src/com/intellij/testFramework/fixtures/JavaCodeInsightTestFixture.java @@ -18,6 +18,7 @@ package com.intellij.testFramework.fixtures; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiPackage; import com.intellij.psi.impl.JavaPsiFacadeEx; +import org.intellij.lang.annotations.Language; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -27,7 +28,7 @@ import org.jetbrains.annotations.NotNull; public interface JavaCodeInsightTestFixture extends CodeInsightTestFixture { JavaPsiFacadeEx getJavaFacade(); - PsiClass addClass(@NotNull @NonNls final String classText); + PsiClass addClass(@NotNull @NonNls @Language("JAVA") final String classText); @NotNull PsiClass findClass(@NotNull @NonNls String name); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java index 38040726ea90..eec076d41ead 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/blocks/GroovyBlockGenerator.java @@ -438,8 +438,7 @@ public class GroovyBlockGenerator { private boolean shouldSkip(boolean classLevel, PsiElement psi) { if (psi instanceof PsiComment) { - PsiElement prev = psi.getPrevSibling(); - if (prev != null && prev.getNode().getElementType() != GroovyTokenTypes.mNLS || classLevel && !fieldGroupEnded(psi)) { + if ((psi.getPrevSibling() != null) || (classLevel && !fieldGroupEnded(psi))) { return true; } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy index 4080c3029e6f..dfcdeebe1253 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy @@ -115,9 +115,8 @@ public class FormatterTest extends GroovyFormatterTestCase { public void testAnnotationOnSeparateLine() throws Throwable { doTest(); } public void testAlignMultipleVariables() throws Throwable { doTest(); } - //public void testSpockTable() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } - //public void testSpockTableComments() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } - //public void testSpockTableWithStringComment() throws Throwable {myFixture.addClass('package spock.lang; public class Specification{}'); doTest(); } + //public void testSpockTableWithStringComment() throws Throwable { doTest() } + public void testSpockTableWithComments() throws Throwable { doTest() } public void testElseIfs() throws Throwable { groovySettings.SPECIAL_ELSE_IF_TREATMENT = false; @@ -223,7 +222,7 @@ public class FormatterTest extends GroovyFormatterTestCase { doTest(); } - + public void testGeese1() {doGeeseTest();} public void testGeese2() {doGeeseTest();} public void testGeese3() {doGeeseTest();} diff --git a/plugins/groovy/testdata/groovy/formatter/spockTable.test b/plugins/groovy/testdata/groovy/formatter/spockTable.test deleted file mode 100644 index 1277d36f0fce..000000000000 --- a/plugins/groovy/testdata/groovy/formatter/spockTable.test +++ /dev/null @@ -1,31 +0,0 @@ -class HelloSpock extends spock.lang.Specification { - def "length of Spock's and his friends' names"() { - expect: - name.size() == length - - where: - name | length | foo - "Spock" | 5 - "Kirk" | 4 | xxx | yyy - "Scotty" | 6 |dddddddddd | fff - - //aaa - a | b | c - } -} ------ -class HelloSpock extends spock.lang.Specification { - def "length of Spock's and his friends' names"() { - expect: - name.size() == length - - where: - name | length | foo - "Spock" | 5 - "Kirk" | 4 | xxx | yyy - "Scotty" | 6 | dddddddddd | fff - - //aaa - a | b | c - } -} \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/formatter/spockTableComments.test b/plugins/groovy/testdata/groovy/formatter/spockTableComments.test deleted file mode 100644 index d6676ec27c77..000000000000 --- a/plugins/groovy/testdata/groovy/formatter/spockTableComments.test +++ /dev/null @@ -1,23 +0,0 @@ -class HelloSpock extends spock.lang.Specification { - def "length of Spock's and his friends' names"() { - expect: - name.size() == length - - where: - name | length - "Kirk" | 4 // a great guy - "Scotty" | 6 - } -} ------ -class HelloSpock extends spock.lang.Specification { - def "length of Spock's and his friends' names"() { - expect: - name.size() == length - - where: - name | length - "Kirk" | 4 // a great guy - "Scotty" | 6 - } -} \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test b/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test new file mode 100644 index 000000000000..f07fd85bb76e --- /dev/null +++ b/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test @@ -0,0 +1,43 @@ +class HelloSpock extends spock.lang.Specification { + def 'different length tables with comments are aligned properly'() { + expect: + name.size() == length + + where: + ordinal | value1 | value2 || value3 + 'first' | 111111111 | 1 || 1 + 'second' | 2 | 2 || 2 // comment after lines + 'third' | 3 | 3 || 3 + + 'fourth' | 4 | 4 || 4444444444 + //comment between lines + 'fifth' | 5 | 555555555 || 5 + + /* block comment and empty line between lines */ + 'sixth' | 6 | 6 || 6 + + unalignedVariableAfterTable=value1+value2 + } +} +----- +class HelloSpock extends spock.lang.Specification { + def 'different length tables with comments are aligned properly'() { + expect: + name.size() == length + + where: + ordinal | value1 | value2 || value3 + 'first' | 111111111 | 1 || 1 + 'second' | 2 | 2 || 2 // comment after lines + 'third' | 3 | 3 || 3 + + 'fourth' | 4 | 4 || 4444444444 + //comment between lines + 'fifth' | 5 | 555555555 || 5 + + /* block comment and empty line between lines */ + 'sixth' | 6 | 6 || 6 + + unalignedVariableAfterTable = value1 + value2 + } +} \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/formatter/spockTableWithStringComment.test b/plugins/groovy/testdata/groovy/formatter/spockTableWithStringComment.test index 2137a3594f23..679494ff8b95 100644 --- a/plugins/groovy/testdata/groovy/formatter/spockTableWithStringComment.test +++ b/plugins/groovy/testdata/groovy/formatter/spockTableWithStringComment.test @@ -13,12 +13,11 @@ class HelloSpock extends spock.lang.Specification { class HelloSpock extends spock.lang.Specification { def "length of Spock's and his friends' names"() { expect: - name.size() == length + name.size() == length - where: - "something" - name || length - "Kirk" || 4 - "Scotty" || 6 + where: "something" + name || length + "Kirk" || 4 + "Scotty" || 6 } } \ No newline at end of file From df996edf5c7ddc99a368284905653fdda71ab3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pap=20L=C5=91rinc?= Date: Mon, 31 Aug 2015 23:41:35 +0300 Subject: [PATCH 2/2] [groovy] fix spockTableWithComments --- .../testdata/groovy/formatter/spockTableWithComments.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test b/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test index f07fd85bb76e..0526e59c2e0a 100644 --- a/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test +++ b/plugins/groovy/testdata/groovy/formatter/spockTableWithComments.test @@ -1,7 +1,7 @@ class HelloSpock extends spock.lang.Specification { def 'different length tables with comments are aligned properly'() { expect: - name.size() == length + ordinal.size() > 0 where: ordinal | value1 | value2 || value3 @@ -10,7 +10,7 @@ class HelloSpock extends spock.lang.Specification { 'third' | 3 | 3 || 3 'fourth' | 4 | 4 || 4444444444 - //comment between lines + // comment between lines 'fifth' | 5 | 555555555 || 5 /* block comment and empty line between lines */ @@ -23,7 +23,7 @@ class HelloSpock extends spock.lang.Specification { class HelloSpock extends spock.lang.Specification { def 'different length tables with comments are aligned properly'() { expect: - name.size() == length + ordinal.size() > 0 where: ordinal | value1 | value2 || value3 @@ -32,7 +32,7 @@ class HelloSpock extends spock.lang.Specification { 'third' | 3 | 3 || 3 'fourth' | 4 | 4 || 4444444444 - //comment between lines + // comment between lines 'fifth' | 5 | 555555555 || 5 /* block comment and empty line between lines */