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)
This commit is contained in:
Nikolay Krasko
2016-10-07 13:09:30 +03:00
committed by Nikolay Krasko
parent 944b9b5b6c
commit 6a72cebe88
9 changed files with 169 additions and 9 deletions
@@ -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<NodeIndentStrategy>(
.`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)),
@@ -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;
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ class D {
}
class E
: Some
: Some
class F
@@ -8,7 +8,7 @@ object Some2: A
object Some3
:
:
A
@@ -8,7 +8,7 @@ object Some2 :A
object Some3
:
:
A
+21
View File
@@ -0,0 +1,21 @@
interface Foo
class Bar
: Foo {
}
class Ob
: Foo
class C {
companion object
: Foo {
}
}
class Unfinished1
:
class Unfinished2
: {
}
+21
View File
@@ -0,0 +1,21 @@
interface Foo
class Bar
: Foo {
}
class Ob
: Foo
class C {
companion object
: Foo {
}
}
class Unfinished1
:
class Unfinished2
: {
}
@@ -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");
@@ -422,6 +422,91 @@ class TypedHandlerTest : LightCodeInsightTestCase() {
doLtGtTestNoAutoClose("fun some() { val a = 12; a <<caret> }")
}
fun testColonOfSuperTypeList() {
doCharTypeTest(
':',
"""
|open class A
|class B
|<caret>
""",
"""
|open class A
|class B
| :<caret>
""")
}
fun testColonOfSuperTypeListInObject() {
doCharTypeTest(
':',
"""
|interface A
|object B
|<caret>
""",
"""
|interface A
|object B
| :<caret>
""")
}
fun testColonOfSuperTypeListInCompanionObject() {
doCharTypeTest(
':',
"""
|interface A
|class B {
| companion object
| <caret>
|}
""",
"""
|interface A
|class B {
| companion object
| :<caret>
|}
""")
}
fun testColonOfSuperTypeListBeforeBody() {
doCharTypeTest(
':',
"""
|open class A
|class B
|<caret> {
|}
""",
"""
|open class A
|class B
| :<caret> {
|}
""")
}
fun testColonOfSuperTypeListNotNullIndent() {
doCharTypeTest(
':',
"""
|fun test() {
| open class A
| class B
| <caret>
|}
""",
"""
|fun test() {
| open class A
| class B
| :<caret>
|}
""")
}
fun testMoveThroughGT() {
LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List<Set<Int<caret>>>")
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) {