From 6a72cebe880c922e895785d241a6176d88f60fbd Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Fri, 7 Oct 2016 13:09:30 +0300 Subject: [PATCH] Add indent before colon of super types list on new line (KT-13981, KT-5117) #KT-13981 Fixed #KT-5117 Fixed (cherry picked from commit dfea3af) --- .../idea/formatter/KotlinCommonBlock.kt | 14 ++- .../idea/editor/KotlinTypedHandler.java | 21 +++++ .../EmptyLineBetweenClasses.after.kt | 2 +- ...aceAroundExtendColonInObjects.after.inv.kt | 2 +- .../SpaceAroundExtendColonInObjects.after.kt | 2 +- .../formatter/SuperListIndent.after.kt | 21 +++++ idea/testData/formatter/SuperListIndent.kt | 21 +++++ .../formatter/FormatterTestGenerated.java | 6 ++ .../kotlin/idea/editor/TypedHandlerTest.kt | 89 ++++++++++++++++++- 9 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 idea/testData/formatter/SuperListIndent.after.kt create mode 100644 idea/testData/formatter/SuperListIndent.kt diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index e774b507731..ee45d05bc4c 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -24,13 +24,14 @@ import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.idea.KotlinLanguage +import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings +import org.jetbrains.kotlin.idea.formatter.NodeIndentStrategy.strategy import org.jetbrains.kotlin.kdoc.lexer.KDocTokens +import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes +import org.jetbrains.kotlin.lexer.KtTokens +import org.jetbrains.kotlin.lexer.KtTokens.* import org.jetbrains.kotlin.psi.KtDeclaration import java.util.* -import org.jetbrains.kotlin.idea.formatter.NodeIndentStrategy.strategy -import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings -import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes -import org.jetbrains.kotlin.lexer.KtTokens.* private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS) private val KDOC_COMMENT_INDENT = 1 @@ -344,6 +345,11 @@ private val INDENT_RULES = arrayOf( .`in`(KtNodeTypes.DOT_QUALIFIED_EXPRESSION, KtNodeTypes.SAFE_ACCESS_EXPRESSION) .set(Indent.getContinuationWithoutFirstIndent(false)), + strategy("Colon of delegation list") + .`in`(KtNodeTypes.CLASS, KtNodeTypes.OBJECT_DECLARATION) + .forType(KtTokens.COLON) + .set(Indent.getNormalIndent(false)), + strategy("Delegation list") .`in`(KtNodeTypes.SUPER_TYPE_LIST, KtNodeTypes.INITIALIZER_LIST) .set(Indent.getContinuationIndent(false)), diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java index a1f962cb7e4..88017450096 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java @@ -46,6 +46,7 @@ import com.intellij.util.text.CharArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.KtNodeTypes; import org.jetbrains.kotlin.lexer.KtTokens; +import org.jetbrains.kotlin.psi.KtClassOrObject; import org.jetbrains.kotlin.psi.KtFile; import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry; @@ -242,6 +243,12 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { } } + if (c == ':') { + if (autoIndentCase(editor, project, file)) { + return Result.STOP; + } + } + return Result.CONTINUE; } @@ -285,4 +292,18 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { } } } + + private static boolean autoIndentCase(Editor editor, Project project, PsiFile file) { + int offset = editor.getCaretModel().getOffset(); + PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); + PsiElement currElement = file.findElementAt(offset - 1); + if (currElement != null) { + PsiElement parent = currElement.getParent(); + if (parent != null && parent instanceof KtClassOrObject) { + CodeStyleManager.getInstance(project).adjustLineIndent(file, offset - 1); + return true; + } + } + return false; + } } diff --git a/idea/testData/formatter/EmptyLineBetweenClasses.after.kt b/idea/testData/formatter/EmptyLineBetweenClasses.after.kt index 64010ce04af..f2f618cbd23 100644 --- a/idea/testData/formatter/EmptyLineBetweenClasses.after.kt +++ b/idea/testData/formatter/EmptyLineBetweenClasses.after.kt @@ -9,7 +9,7 @@ class D { } class E -: Some + : Some class F diff --git a/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.inv.kt b/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.inv.kt index b5b9ce65ab0..9ae256f4391 100644 --- a/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.inv.kt +++ b/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.inv.kt @@ -8,7 +8,7 @@ object Some2: A object Some3 -: + : A diff --git a/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.kt b/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.kt index 2232d93e6b5..7ab9a36cb41 100644 --- a/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.kt +++ b/idea/testData/formatter/SpaceAroundExtendColonInObjects.after.kt @@ -8,7 +8,7 @@ object Some2 :A object Some3 -: + : A diff --git a/idea/testData/formatter/SuperListIndent.after.kt b/idea/testData/formatter/SuperListIndent.after.kt new file mode 100644 index 00000000000..46026ee7798 --- /dev/null +++ b/idea/testData/formatter/SuperListIndent.after.kt @@ -0,0 +1,21 @@ +interface Foo + +class Bar + : Foo { +} + +class Ob + : Foo + +class C { + companion object + : Foo { + } +} + +class Unfinished1 + : + +class Unfinished2 + : { +} \ No newline at end of file diff --git a/idea/testData/formatter/SuperListIndent.kt b/idea/testData/formatter/SuperListIndent.kt new file mode 100644 index 00000000000..588ad283d9b --- /dev/null +++ b/idea/testData/formatter/SuperListIndent.kt @@ -0,0 +1,21 @@ +interface Foo + +class Bar +: Foo { +} + +class Ob +: Foo + +class C { + companion object + : Foo { + } +} + +class Unfinished1 +: + +class Unfinished2 +: { +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 4e0d507980b..8309983c98f 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -607,6 +607,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("SuperListIndent.after.kt") + public void testSuperListIndent() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/SuperListIndent.after.kt"); + doTest(fileName); + } + @TestMetadata("TryCatchLineBreak.after.kt") public void testTryCatchLineBreak() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/TryCatchLineBreak.after.kt"); diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt index 2552c627e0a..58005ca5d48 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt @@ -422,6 +422,91 @@ class TypedHandlerTest : LightCodeInsightTestCase() { doLtGtTestNoAutoClose("fun some() { val a = 12; a < }") } + fun testColonOfSuperTypeList() { + doCharTypeTest( + ':', + """ + |open class A + |class B + | + """, + """ + |open class A + |class B + | : + """) + } + + fun testColonOfSuperTypeListInObject() { + doCharTypeTest( + ':', + """ + |interface A + |object B + | + """, + """ + |interface A + |object B + | : + """) + } + + fun testColonOfSuperTypeListInCompanionObject() { + doCharTypeTest( + ':', + """ + |interface A + |class B { + | companion object + | + |} + """, + """ + |interface A + |class B { + | companion object + | : + |} + """) + } + + fun testColonOfSuperTypeListBeforeBody() { + doCharTypeTest( + ':', + """ + |open class A + |class B + | { + |} + """, + """ + |open class A + |class B + | : { + |} + """) + } + + fun testColonOfSuperTypeListNotNullIndent() { + doCharTypeTest( + ':', + """ + |fun test() { + | open class A + | class B + | + |} + """, + """ + |fun test() { + | open class A + | class B + | : + |} + """) + } + fun testMoveThroughGT() { LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List>>") EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>') @@ -430,9 +515,9 @@ class TypedHandlerTest : LightCodeInsightTestCase() { } private fun doCharTypeTest(ch: Char, beforeText: String, afterText: String) { - LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", beforeText) + LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", beforeText.trimMargin()) EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), ch) - checkResultByText(afterText) + checkResultByText(afterText.trimMargin()) } private fun doLtGtTestNoAutoClose(initText: String) {