diff --git a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java index d7df9391208..29e64ee3a6c 100644 --- a/generators/org/jetbrains/jet/generators/tests/GenerateTests.java +++ b/generators/org/jetbrains/jet/generators/tests/GenerateTests.java @@ -31,6 +31,7 @@ import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveDescriptorRenderer import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest; import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveTest; import org.jetbrains.jet.plugin.codeInsight.surroundWith.AbstractSurroundWithTest; +import org.jetbrains.jet.plugin.folding.AbstractKotlinFoldingTest; import org.jetbrains.jet.plugin.highlighter.AbstractDeprecatedHighlightingTest; import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixMultiFileTest; import org.jetbrains.jet.plugin.quickfix.AbstractQuickFixTest; @@ -231,6 +232,13 @@ public class GenerateTests { testModel("idea/testData/highlighter/deprecated") ); + generateTest( + "idea/tests/", + "KotlinFoldingTestGenerated", + AbstractKotlinFoldingTest.class, + testModel("idea/testData/folding") + ); + generateTest( "idea/tests/", "SurroundWithTestGenerated", diff --git a/idea/src/org/jetbrains/jet/plugin/JetFoldingBuilder.java b/idea/src/org/jetbrains/jet/plugin/JetFoldingBuilder.java index ca6630f2527..c31ee547d3a 100644 --- a/idea/src/org/jetbrains/jet/plugin/JetFoldingBuilder.java +++ b/idea/src/org/jetbrains/jet/plugin/JetFoldingBuilder.java @@ -26,6 +26,8 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.tree.IElementType; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetNodeTypes; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.psi.JetImportDirective; import org.jetbrains.jet.lexer.JetTokens; import java.util.ArrayList; @@ -36,7 +38,22 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware { @NotNull @Override public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) { + if (!(root instanceof JetFile)) { + return FoldingDescriptor.EMPTY; + } List descriptors = new ArrayList(); + JetFile file = (JetFile) root; + + List importList = file.getImportDirectives(); + if (importList != null && !importList.isEmpty()) { + JetImportDirective firstImport = importList.get(0); + PsiElement importKeyword = firstImport.getFirstChild(); + int startOffset = importKeyword.getTextRange().getEndOffset() + 1; + int endOffset = importList.get(importList.size() - 1).getTextRange().getEndOffset(); + TextRange range = new TextRange(startOffset, endOffset); + descriptors.add(new FoldingDescriptor(firstImport, range)); + } + appendDescriptors(root.getNode(), document, descriptors); return descriptors.toArray(new FoldingDescriptor[descriptors.size()]); } @@ -77,6 +94,9 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware { && next.getElementType() == JetTokens.IDE_TEMPLATE_END) { return astNode.getText(); } + if (astNode.getPsi() instanceof JetImportDirective) { + return "..."; + } return "{...}"; } @@ -87,6 +107,9 @@ public class JetFoldingBuilder extends FoldingBuilderEx implements DumbAware { @Override public boolean isCollapsedByDefault(@NotNull ASTNode astNode) { + if (astNode.getPsi() instanceof JetImportDirective) { + return true; + } return false; } } diff --git a/idea/testData/folding/class.kt b/idea/testData/folding/class.kt new file mode 100644 index 00000000000..68fe9540a2d --- /dev/null +++ b/idea/testData/folding/class.kt @@ -0,0 +1,4 @@ +class A { + val a = 1 + val b = 1 +} \ No newline at end of file diff --git a/idea/testData/folding/function.kt b/idea/testData/folding/function.kt new file mode 100644 index 00000000000..ed954219268 --- /dev/null +++ b/idea/testData/folding/function.kt @@ -0,0 +1,4 @@ +fun foo() { + val a = 1 + val b = 1 +} \ No newline at end of file diff --git a/idea/testData/folding/imports.kt b/idea/testData/folding/imports.kt new file mode 100644 index 00000000000..22c39d9ee1e --- /dev/null +++ b/idea/testData/folding/imports.kt @@ -0,0 +1,6 @@ +import kotlin.* +import kotlin.* +import kotlin.* +import kotlin.* + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/folding/object.kt b/idea/testData/folding/object.kt new file mode 100644 index 00000000000..3fc1068be2a --- /dev/null +++ b/idea/testData/folding/object.kt @@ -0,0 +1,4 @@ +object A { + val a = 1 + val b = 1 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/jet/plugin/folding/AbstractKotlinFoldingTest.java b/idea/tests/org/jetbrains/jet/plugin/folding/AbstractKotlinFoldingTest.java new file mode 100644 index 00000000000..6fb9a60988a --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/folding/AbstractKotlinFoldingTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.folding; + +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.JetLightProjectDescriptor; + +public abstract class AbstractKotlinFoldingTest extends LightCodeInsightFixtureTestCase { + + protected void doTest(@NotNull String path) { + myFixture.testFolding(path); + } + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JetLightProjectDescriptor.INSTANCE; + } + + +} diff --git a/idea/tests/org/jetbrains/jet/plugin/folding/KotlinFoldingTestGenerated.java b/idea/tests/org/jetbrains/jet/plugin/folding/KotlinFoldingTestGenerated.java new file mode 100644 index 00000000000..88a4a3bc4ac --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/folding/KotlinFoldingTestGenerated.java @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.plugin.folding; + +import junit.framework.Assert; +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.File; +import java.util.regex.Pattern; +import org.jetbrains.jet.JetTestUtils; +import org.jetbrains.jet.test.InnerTestClasses; +import org.jetbrains.jet.test.TestMetadata; + +import org.jetbrains.jet.plugin.folding.AbstractKotlinFoldingTest; + +/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("idea/testData/folding") +public class KotlinFoldingTestGenerated extends AbstractKotlinFoldingTest { + public void testAllFilesPresentInFolding() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("idea/testData/folding"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("class.kt") + public void testClass() throws Exception { + doTest("idea/testData/folding/class.kt"); + } + + @TestMetadata("function.kt") + public void testFunction() throws Exception { + doTest("idea/testData/folding/function.kt"); + } + + @TestMetadata("imports.kt") + public void testImports() throws Exception { + doTest("idea/testData/folding/imports.kt"); + } + + @TestMetadata("object.kt") + public void testObject() throws Exception { + doTest("idea/testData/folding/object.kt"); + } + +}