Add "Wrap extends/implements list" option

This commit is contained in:
Dmitry Jemerov
2017-06-22 17:27:59 +02:00
parent 722cc521a9
commit be15ee3c03
6 changed files with 36 additions and 7 deletions
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtSuperTypeListEntry
import java.util.*
private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS)
@@ -272,7 +273,7 @@ abstract class KotlinCommonBlock(
private fun buildSubBlock(child: ASTNode, alignmentStrategy: CommonAlignmentStrategy, wrappingStrategy: WrappingStrategy): Block {
val childWrap = wrappingStrategy.getWrap(child.elementType)
val childWrap = wrappingStrategy.getWrap(child)
// Skip one sub-level for operators, so type of block node is an element type of operator
if (child.elementType === KtNodeTypes.OPERATION_REFERENCE) {
@@ -334,6 +335,14 @@ abstract class KotlinCommonBlock(
return getWrappingStrategyForItemList(commonSettings.METHOD_PARAMETERS_WRAP, KtNodeTypes.VALUE_PARAMETER)
}
}
elementType === KtNodeTypes.SUPER_TYPE_LIST -> {
val wrap = Wrap.createWrap(commonSettings.EXTENDS_LIST_WRAP, false)
return object : WrappingStrategy {
override fun getWrap(childElement: ASTNode): Wrap? =
if (childElement.psi is KtSuperTypeListEntry) wrap else null
}
}
}
return WrappingStrategy.NoWrapping
@@ -496,8 +505,8 @@ private fun getPrevWithoutWhitespaceAndComments(pNode: ASTNode?): ASTNode? {
private fun getWrappingStrategyForItemList(wrapType: Int, itemType: IElementType): WrappingStrategy {
val itemWrap = Wrap.createWrap(wrapType, false)
return object : WrappingStrategy {
override fun getWrap(childElementType: IElementType): Wrap? {
return if (childElementType === itemType) itemWrap else null
override fun getWrap(childElement: ASTNode): Wrap? {
return if (childElement.elementType === itemType) itemWrap else null
}
}
}
@@ -17,12 +17,12 @@
package org.jetbrains.kotlin.idea.formatter
import com.intellij.formatting.Wrap
import com.intellij.psi.tree.IElementType
import com.intellij.lang.ASTNode
interface WrappingStrategy {
fun getWrap(childElementType: IElementType): Wrap?
fun getWrap(childElement: ASTNode): Wrap?
object NoWrapping: WrappingStrategy {
override fun getWrap(childElementType: IElementType): Wrap? = null
override fun getWrap(childElement: ASTNode): Wrap? = null
}
}
@@ -30,7 +30,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
override fun getCodeSample(settingsType: LanguageCodeStyleSettingsProvider.SettingsType): String = when (settingsType) {
LanguageCodeStyleSettingsProvider.SettingsType.WRAPPING_AND_BRACES_SETTINGS ->
"""
public class ThisIsASampleClass {
public class ThisIsASampleClass : Comparable<*>, Appendable {
val test =
12
@@ -235,6 +235,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
"FINALLY_ON_NEW_LINE",
"CALL_PARAMETERS_WRAP",
"METHOD_PARAMETERS_WRAP",
"EXTENDS_LIST_WRAP",
"METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE",
"METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE",
"CALL_PARAMETERS_LPAREN_ON_NEXT_LINE",
+7
View File
@@ -0,0 +1,7 @@
class Foo : Comparable<*>,
Appendable {
}
// SET_INT: EXTENDS_LIST_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_EXTENDS_LIST
+6
View File
@@ -0,0 +1,6 @@
class Foo : Comparable<*>, Appendable {
}
// SET_INT: EXTENDS_LIST_WRAP = 2
// SET_TRUE: ALIGN_MULTILINE_EXTENDS_LIST
@@ -308,6 +308,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTest(fileName);
}
@TestMetadata("ExtendsListWrap.after.kt")
public void testExtendsListWrap() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ExtendsListWrap.after.kt");
doTest(fileName);
}
@TestMetadata("ForLineBreak.after.kt")
public void testForLineBreak() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ForLineBreak.after.kt");