Option to use continuation indent in parameter lists

This commit is contained in:
Dmitry Jemerov
2017-06-12 17:20:55 +02:00
parent a4916a3c00
commit f4c75e61ad
9 changed files with 123 additions and 14 deletions
@@ -40,6 +40,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public int NAME_COUNT_TO_USE_STAR_IMPORT = ApplicationManager.getApplication().isUnitTestMode() ? Integer.MAX_VALUE : 5;
public int NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS = ApplicationManager.getApplication().isUnitTestMode() ? Integer.MAX_VALUE : 3;
public boolean IMPORT_NESTED_CLASSES = false;
public boolean CONTINUATION_INDENT_IN_PARAMETER_LISTS = true;
public KotlinCodeStyleSettings(CodeStyleSettings container) {
super("JetCodeStyleSettings", container);
@@ -43,6 +43,9 @@ private val CODE_BLOCKS = TokenSet.create(KtNodeTypes.BLOCK, KtNodeTypes.CLASS_B
private val ALIGN_FOR_BINARY_OPERATIONS = TokenSet.create(MUL, DIV, PERC, PLUS, MINUS, ELVIS, LT, GT, LTEQ, GTEQ, ANDAND, OROR)
val CodeStyleSettings.kotlinSettings
get() = getCustomSettings(KotlinCodeStyleSettings::class.java)
abstract class KotlinCommonBlock(
private val node: ASTNode,
private val settings: CodeStyleSettings,
@@ -123,7 +126,7 @@ abstract class KotlinCommonBlock(
}
for (strategy in INDENT_RULES) {
val indent = strategy.getIndent(child)
val indent = strategy.getIndent(child, settings)
if (indent != null) {
return indent
}
@@ -178,7 +181,11 @@ abstract class KotlinCommonBlock(
ChildAttributes(block.indent, block.alignment)
}
else {
ChildAttributes(Indent.getContinuationIndent(), null)
val indent = if (type == KtNodeTypes.VALUE_PARAMETER_LIST && !settings.kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS)
Indent.getNormalIndent()
else
Indent.getContinuationIndent()
ChildAttributes(indent, null)
}
}
@@ -207,7 +214,7 @@ abstract class KotlinCommonBlock(
private fun getChildrenAlignmentStrategy(): CommonAlignmentStrategy {
val jetCommonSettings = settings.getCommonSettings(KotlinLanguage.INSTANCE)
val jetSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
val kotlinSettings = settings.kotlinSettings
val parentType = node.elementType
return when {
parentType === KtNodeTypes.VALUE_PARAMETER_LIST ->
@@ -221,7 +228,7 @@ abstract class KotlinCommonBlock(
jetCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR)
parentType === KtNodeTypes.WHEN ->
getAlignmentForCaseBranch(jetSettings.ALIGN_IN_COLUMNS_CASE_BRANCH)
getAlignmentForCaseBranch(kotlinSettings.ALIGN_IN_COLUMNS_CASE_BRANCH)
parentType === KtNodeTypes.WHEN_ENTRY ->
alignmentStrategy
@@ -397,7 +404,17 @@ private val INDENT_RULES = arrayOf<NodeIndentStrategy>(
strategy("Block in when entry")
.within(KtNodeTypes.WHEN_ENTRY)
.notForType(KtNodeTypes.BLOCK, KtNodeTypes.WHEN_CONDITION_EXPRESSION, KtNodeTypes.WHEN_CONDITION_IN_RANGE, KtNodeTypes.WHEN_CONDITION_IS_PATTERN, ELSE_KEYWORD, ARROW)
.set(Indent.getNormalIndent()))
.set(Indent.getNormalIndent()),
strategy("Parameter list")
.within(KtNodeTypes.VALUE_PARAMETER_LIST)
.forElement { it.elementType == KtNodeTypes.VALUE_PARAMETER && it.psi.prevSibling != null }
.set { settings ->
if (settings.kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS)
Indent.getContinuationIndent()
else
Indent.getNormalIndent()
})
private fun getOperationType(node: ASTNode): IElementType? = node.findChildByType(KtNodeTypes.OPERATION_REFERENCE)?.firstChildNode?.elementType
@@ -18,23 +18,24 @@ package org.jetbrains.kotlin.idea.formatter
import com.intellij.formatting.Indent
import com.intellij.lang.ASTNode
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import java.util.*
abstract class NodeIndentStrategy {
abstract fun getIndent(node: ASTNode): Indent?
abstract fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent?
class ConstIndentStrategy(private val indent: Indent) : NodeIndentStrategy() {
override fun getIndent(node: ASTNode): Indent? {
override fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent? {
return indent
}
}
class PositionStrategy(private val debugInfo: String?) : NodeIndentStrategy() {
private var defaultIndent = Indent.getNoneIndent()
private var indentCallback: (CodeStyleSettings) -> Indent = { Indent.getNoneIndent() }
private val within = ArrayList<IElementType>()
private val notIn = ArrayList<IElementType>()
@@ -46,7 +47,12 @@ abstract class NodeIndentStrategy {
}
fun set(indent: Indent): PositionStrategy {
defaultIndent = indent
indentCallback = { indent }
return this
}
fun set(indentCallback: (CodeStyleSettings) -> Indent): PositionStrategy {
this.indentCallback = indentCallback
return this
}
@@ -92,7 +98,7 @@ abstract class NodeIndentStrategy {
return this
}
override fun getIndent(node: ASTNode): Indent? {
override fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent? {
if (!forElement.isEmpty()) {
if (!forElement.contains(node.elementType)) {
return null
@@ -121,7 +127,7 @@ abstract class NodeIndentStrategy {
}
}
return defaultIndent
return indentCallback(settings)
}
private fun fillTypes(resultCollection: MutableList<IElementType>, singleType: IElementType, otherTypes: Array<out IElementType>) {