Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-52 fix groovy folding arg list #53

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -39,20 +37,17 @@ 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))
}
}

private fun GrListOrMap.hasNoChildren() = children.isEmpty()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class SampleClass extends Specification <fold text='{...}'>{

println([])
println([ ])

println([], [])
println([ ], [ ])
}</fold>

def 'empty map should fold'() <fold text='{...}'>{
Expand All @@ -23,6 +26,9 @@ class SampleClass extends Specification <fold text='{...}'>{

println([:])
println([ : ])

println([:], [:])
println([ : ], [ : ])
}</fold>

def 'non-empty list should fold'() <fold text='{...}'>{
Expand All @@ -37,6 +43,15 @@ class SampleClass extends Specification <fold text='{...}'>{
1,
2,
]</fold>)

println([1, 2], [3, 4])
println(<fold text='[...]'>[
1,
2,
]</fold>, <fold text='[...]'>[
1,
2,
]</fold>)
}</fold>

def 'non-empty map should fold'() <fold text='{...}'>{
Expand All @@ -51,6 +66,15 @@ class SampleClass extends Specification <fold text='{...}'>{
foo: 1,
bar: 2,
]</fold>)

println([foo: 1, bar: 2], [baz: 3, qux: 4])
println(<fold text='[...:...]'>[
foo: 1,
bar: 2,
]</fold>, <fold text='[...:...]'>[
foo: 1,
bar: 2,
]</fold>)
}</fold>

def 'nexted list/map should not fold'() <fold text='{...}'>{
Expand Down
Loading