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>) {
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2017 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.kotlin.idea.formatter
import com.intellij.application.options.IndentOptionsEditor
import com.intellij.application.options.SmartIndentOptionsEditor
import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
import javax.swing.JCheckBox
class KotlinIndentOptionsEditor : SmartIndentOptionsEditor() {
private val useContinuationIndentInParameterList = JCheckBox("Use continuation indent in parameter lists")
override fun addComponents() {
super.addComponents()
add(useContinuationIndentInParameterList)
}
override fun isModified(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions): Boolean {
var isModified = super.isModified(settings, options)
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
isModified = isModified || IndentOptionsEditor.isFieldModified(useContinuationIndentInParameterList,
kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS)
return isModified
}
override fun apply(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions) {
super.apply(settings, options)
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS = useContinuationIndentInParameterList.isSelected
}
override fun reset(settings: CodeStyleSettings, options: CommonCodeStyleSettings.IndentOptions) {
super.reset(settings, options)
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)
useContinuationIndentInParameterList.isSelected = kotlinSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS
}
override fun setEnabled(enabled: Boolean) {
super.setEnabled(enabled)
useContinuationIndentInParameterList.isEnabled = enabled
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.kotlin.idea.formatter
import com.intellij.application.options.IndentOptionsEditor
import com.intellij.application.options.SmartIndentOptionsEditor
import com.intellij.psi.codeStyle.CodeStyleSettingsCustomizable
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider
@@ -122,7 +121,11 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
return 0
}
fun multilineMethod(
foo: String,
bar: String
) {
}
}
class AnotherClass<T : Any> : Some()
""".trimIndent()
@@ -229,7 +232,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
}
}
override fun getIndentOptionsEditor(): IndentOptionsEditor = SmartIndentOptionsEditor()
override fun getIndentOptionsEditor(): IndentOptionsEditor = KotlinIndentOptionsEditor()
override fun getDefaultCommonSettings(): CommonCodeStyleSettings =
CommonCodeStyleSettings(language).apply {
@@ -0,0 +1,6 @@
fun foo(
x: Int
) {
}
// SET_TRUE: CONTINUATION_INDENT_IN_PARAMETER_LISTS
@@ -0,0 +1,6 @@
fun foo(
x: Int
) {
}
// SET_TRUE: CONTINUATION_INDENT_IN_PARAMETER_LISTS
@@ -0,0 +1,6 @@
fun foo(
x: Int
) {
}
// SET_TRUE: CONTINUATION_INDENT_IN_PARAMETER_LISTS
@@ -164,6 +164,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
doTest(fileName);
}
@TestMetadata("ContinuationIndentInParameterLists.after.kt")
public void testContinuationIndentInParameterLists() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ContinuationIndentInParameterLists.after.kt");
doTest(fileName);
}
@TestMetadata("CurlyBraceStringInterpolation.after.kt")
public void testCurlyBraceStringInterpolation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/CurlyBraceStringInterpolation.after.kt");