From e82f4c00c8bc46ff0b730890ada88ba868b9b2ed Mon Sep 17 00:00:00 2001 From: akefirad Date: Mon, 20 Jan 2025 12:52:31 +0100 Subject: [PATCH] GH-52 fix groovy folding arg list --- .../groom/groovy/GroovyFoldingBuilder.kt | 13 +++------- .../groovy/GroovyFoldingBuilderTest.groovy | 25 +++---------------- .../ExpectedTestData.groovy | 24 ++++++++++++++++++ 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/main/kotlin/com/akefirad/groom/groovy/GroovyFoldingBuilder.kt b/src/main/kotlin/com/akefirad/groom/groovy/GroovyFoldingBuilder.kt index 6ae9a21..483eeed 100644 --- a/src/main/kotlin/com/akefirad/groom/groovy/GroovyFoldingBuilder.kt +++ b/src/main/kotlin/com/akefirad/groom/groovy/GroovyFoldingBuilder.kt @@ -8,8 +8,6 @@ import com.intellij.lang.folding.FoldingDescriptor import com.intellij.openapi.editor.Document import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement -import com.intellij.psi.tree.TokenSet -import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes import org.jetbrains.plugins.groovy.lang.psi.GroovyFile import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap @@ -39,7 +37,7 @@ class GroovyFoldingBuilder : CustomFoldingBuilder() { val start = child.startOffset val end = child.endOffset val range = TextRange(start, end) - d.add(FoldingDescriptor(e.node, range)) + d.add(FoldingDescriptor(child.node, range)) } } @@ -47,12 +45,9 @@ class GroovyFoldingBuilder : CustomFoldingBuilder() { private fun GrListOrMap.isSingleLine() = !text.contains("\n") // TODO: find a better way to do these! private fun GrListOrMap.hasLeftOpen() = !text.trim().endsWith("]") // TODO: find a better way to do these! - public override fun getLanguagePlaceholderText(node: ASTNode, range: TextRange): String { - val selected = node.getChildren(TokenSet.create(GroovyElementTypes.LIST_OR_MAP)) - assert(selected.size == 1) { "Unexpected number of children: ${selected.size}" } - val psi = selected.single().psi - check(psi is GrListOrMap) { "Unexpected type: ${psi.javaClass}" } - return if (psi.isMap) "[...:...]" else "[...]" + public override fun getLanguagePlaceholderText(node: ASTNode, range: TextRange) = node.psi.let { + check(it is GrListOrMap) { "Unexpected type: ${it.javaClass}" } + if (it.isMap) "[...:...]" else "[...]" } override fun isRegionCollapsedByDefault(node: ASTNode) = false diff --git a/src/test/groovy/com/akefirad/groom/groovy/GroovyFoldingBuilderTest.groovy b/src/test/groovy/com/akefirad/groom/groovy/GroovyFoldingBuilderTest.groovy index 0815b47..da75bf1 100644 --- a/src/test/groovy/com/akefirad/groom/groovy/GroovyFoldingBuilderTest.groovy +++ b/src/test/groovy/com/akefirad/groom/groovy/GroovyFoldingBuilderTest.groovy @@ -9,12 +9,10 @@ import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixture4TestC import org.junit.Test import static com.akefirad.oss.easymock.EasyMock.mock -import static org.easymock.EasyMock.anyObject import static org.easymock.EasyMock.expect import static org.easymock.EasyMock.replay import static org.easymock.EasyMock.verify - class GroovyFoldingBuilderTest extends LightPlatformCodeInsightFixture4TestCase { @Override @@ -46,34 +44,17 @@ class GroovyFoldingBuilderTest extends LightPlatformCodeInsightFixture4TestCase } @Test - void 'getLanguagePlaceholderText should fail when node has no list or map'() { + void 'getLanguagePlaceholderText should fail when node is not GrListOrMap'() { // given: def subject = new GroovyFoldingBuilder() def node = mock(ASTNode) - expect(node.getChildren(anyObject())).andReturn(new ASTNode[0]) + expect(node.getPsi()).andReturn(mock(PsiElement)) replay(node) // expect: - assertThrows(AssertionError) { subject.getLanguagePlaceholderText(node, new TextRange(0, 0)) } + assertThrows(IllegalStateException) { subject.getLanguagePlaceholderText(node, TextRange.EMPTY_RANGE) } // and: verify(node) } - - @Test - void 'getLanguagePlaceholderText should fail when node has non-list-map child'() { - // given: - def subject = new GroovyFoldingBuilder() - def node = mock(ASTNode) - def child = mock(ASTNode) - expect(child.psi).andReturn(mock(PsiElement)) - expect(node.getChildren(anyObject())).andReturn(new ASTNode[]{child}) - replay(node, child) - - // expect: - assertThrows(IllegalStateException) { subject.getLanguagePlaceholderText(node, new TextRange(0, 0)) } - - // and: - verify(node, child) - } } diff --git a/src/test/resources/test-data/GroovyFoldingBuilderTest/ExpectedTestData.groovy b/src/test/resources/test-data/GroovyFoldingBuilderTest/ExpectedTestData.groovy index a24d289..4cf471d 100644 --- a/src/test/resources/test-data/GroovyFoldingBuilderTest/ExpectedTestData.groovy +++ b/src/test/resources/test-data/GroovyFoldingBuilderTest/ExpectedTestData.groovy @@ -12,6 +12,9 @@ class SampleClass extends Specification { println([]) println([ ]) + + println([], []) + println([ ], [ ]) } def 'empty map should fold'() { @@ -23,6 +26,9 @@ class SampleClass extends Specification { println([:]) println([ : ]) + + println([:], [:]) + println([ : ], [ : ]) } def 'non-empty list should fold'() { @@ -37,6 +43,15 @@ class SampleClass extends Specification { 1, 2, ]) + + println([1, 2], [3, 4]) + println([ + 1, + 2, + ], [ + 1, + 2, + ]) } def 'non-empty map should fold'() { @@ -51,6 +66,15 @@ class SampleClass extends Specification { foo: 1, bar: 2, ]) + + println([foo: 1, bar: 2], [baz: 3, qux: 4]) + println([ + foo: 1, + bar: 2, + ], [ + foo: 1, + bar: 2, + ]) } def 'nexted list/map should not fold'() {