Use own kotlin settings for spacing instead of default set
Mostly affected by settings: - KEEP_LINE_BREAKS, - KEEP_BLANK_LINES_IN_DECLARATIONS - KEEP_BLANK_LINES_IN_CODE Allow settings KEEP_LINE_BREAKS in UI.
This commit is contained in:
@@ -21,22 +21,21 @@ import com.intellij.formatting.DependentSpacingRule.Anchor
|
|||||||
import com.intellij.formatting.DependentSpacingRule.Trigger
|
import com.intellij.formatting.DependentSpacingRule.Trigger
|
||||||
import com.intellij.lang.ASTNode
|
import com.intellij.lang.ASTNode
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
import com.intellij.psi.codeStyle.CommonCodeStyleSettings
|
||||||
import com.intellij.psi.tree.IElementType
|
import com.intellij.psi.tree.IElementType
|
||||||
import com.intellij.psi.tree.TokenSet
|
import com.intellij.psi.tree.TokenSet
|
||||||
import org.jetbrains.kotlin.KtNodeTypes
|
import org.jetbrains.kotlin.KtNodeTypes
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage
|
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings, val spacingBuilderUtil: KotlinSpacingBuilderUtil) {
|
class KotlinSpacingBuilder(val commonCodeStyleSettings: CommonCodeStyleSettings, val spacingBuilderUtil: KotlinSpacingBuilderUtil) {
|
||||||
private val builders = ArrayList<Builder>()
|
private val builders = ArrayList<Builder>()
|
||||||
|
|
||||||
private interface Builder {
|
private interface Builder {
|
||||||
fun getSpacing(parent: ASTBlock, left: ASTBlock, right: ASTBlock): Spacing?
|
fun getSpacing(parent: ASTBlock, left: ASTBlock, right: ASTBlock): Spacing?
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class BasicSpacingBuilder() : SpacingBuilder(codeStyleSettings, KotlinLanguage.INSTANCE), Builder {
|
inner class BasicSpacingBuilder() : SpacingBuilder(commonCodeStyleSettings), Builder {
|
||||||
override fun getSpacing(parent: ASTBlock, left: ASTBlock, right: ASTBlock): Spacing? {
|
override fun getSpacing(parent: ASTBlock, left: ASTBlock, right: ASTBlock): Spacing? {
|
||||||
return super.getSpacing(parent, left, right)
|
return super.getSpacing(parent, left, right)
|
||||||
}
|
}
|
||||||
@@ -74,8 +73,8 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings, val spacing
|
|||||||
newRule {
|
newRule {
|
||||||
p, l, r ->
|
p, l, r ->
|
||||||
Spacing.createDependentLFSpacing(numSpacesOtherwise, numSpacesOtherwise, p.textRange,
|
Spacing.createDependentLFSpacing(numSpacesOtherwise, numSpacesOtherwise, p.textRange,
|
||||||
codeStyleSettings.KEEP_LINE_BREAKS,
|
commonCodeStyleSettings.KEEP_LINE_BREAKS,
|
||||||
if (allowBlankLines) codeStyleSettings.KEEP_BLANK_LINES_IN_CODE else 0)
|
if (allowBlankLines) commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE else 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,8 +84,8 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings, val spacing
|
|||||||
spacingBuilderUtil.createLineFeedDependentSpacing(numSpacesOtherwise,
|
spacingBuilderUtil.createLineFeedDependentSpacing(numSpacesOtherwise,
|
||||||
numSpacesOtherwise,
|
numSpacesOtherwise,
|
||||||
numberOfLineFeedsOtherwise,
|
numberOfLineFeedsOtherwise,
|
||||||
codeStyleSettings.KEEP_LINE_BREAKS,
|
commonCodeStyleSettings.KEEP_LINE_BREAKS,
|
||||||
codeStyleSettings.KEEP_BLANK_LINES_IN_DECLARATIONS,
|
commonCodeStyleSettings.KEEP_BLANK_LINES_IN_DECLARATIONS,
|
||||||
left.textRange,
|
left.textRange,
|
||||||
dependentSpacingRule)
|
dependentSpacingRule)
|
||||||
}
|
}
|
||||||
@@ -119,7 +118,7 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings, val spacing
|
|||||||
// TODO: it's a severe hack but I don't know how to implement it in other way
|
// TODO: it's a severe hack but I don't know how to implement it in other way
|
||||||
if (child1.node.elementType == KtTokens.EOL_COMMENT && spacing.toString().contains("minLineFeeds=0")) {
|
if (child1.node.elementType == KtTokens.EOL_COMMENT && spacing.toString().contains("minLineFeeds=0")) {
|
||||||
val isBeforeBlock = child2.node.elementType == KtNodeTypes.BLOCK || child2.node.firstChildNode?.elementType == KtNodeTypes.BLOCK
|
val isBeforeBlock = child2.node.elementType == KtNodeTypes.BLOCK || child2.node.firstChildNode?.elementType == KtNodeTypes.BLOCK
|
||||||
val keepBlankLines = if (isBeforeBlock) 0 else codeStyleSettings.KEEP_BLANK_LINES_IN_CODE
|
val keepBlankLines = if (isBeforeBlock) 0 else commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE
|
||||||
return createSpacing(0, minLineFeeds = 1, keepLineBreaks = true, keepBlankLines = keepBlankLines)
|
return createSpacing(0, minLineFeeds = 1, keepLineBreaks = true, keepBlankLines = keepBlankLines)
|
||||||
}
|
}
|
||||||
return spacing
|
return spacing
|
||||||
@@ -143,8 +142,8 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings, val spacing
|
|||||||
fun createSpacing(minSpaces: Int,
|
fun createSpacing(minSpaces: Int,
|
||||||
maxSpaces: Int = minSpaces,
|
maxSpaces: Int = minSpaces,
|
||||||
minLineFeeds: Int = 0,
|
minLineFeeds: Int = 0,
|
||||||
keepLineBreaks: Boolean = codeStyleSettings.KEEP_LINE_BREAKS,
|
keepLineBreaks: Boolean = commonCodeStyleSettings.KEEP_LINE_BREAKS,
|
||||||
keepBlankLines: Int = codeStyleSettings.KEEP_BLANK_LINES_IN_CODE): Spacing {
|
keepBlankLines: Int = commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE): Spacing {
|
||||||
return Spacing.createSpacing(minSpaces, maxSpaces, minLineFeeds, keepLineBreaks, keepBlankLines)
|
return Spacing.createSpacing(minSpaces, maxSpaces, minLineFeeds, keepLineBreaks, keepBlankLines)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -163,8 +162,8 @@ interface KotlinSpacingBuilderUtil {
|
|||||||
fun isWhitespaceOrEmpty(node: ASTNode?): Boolean
|
fun isWhitespaceOrEmpty(node: ASTNode?): Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
fun rules(codeStyleSettings: CodeStyleSettings, builderUtil: KotlinSpacingBuilderUtil, init: KotlinSpacingBuilder.() -> Unit): KotlinSpacingBuilder {
|
fun rules(commonCodeStyleSettings: CommonCodeStyleSettings, builderUtil: KotlinSpacingBuilderUtil, init: KotlinSpacingBuilder.() -> Unit): KotlinSpacingBuilder {
|
||||||
val builder = KotlinSpacingBuilder(codeStyleSettings, builderUtil)
|
val builder = KotlinSpacingBuilder(commonCodeStyleSettings, builderUtil)
|
||||||
builder.init()
|
builder.init()
|
||||||
return builder
|
return builder
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)!!
|
val kotlinSettings = settings.getCustomSettings(KotlinCodeStyleSettings::class.java)!!
|
||||||
val kotlinCommonSettings = settings.getCommonSettings(KotlinLanguage.INSTANCE)!!
|
val kotlinCommonSettings = settings.getCommonSettings(KotlinLanguage.INSTANCE)!!
|
||||||
|
|
||||||
return rules(settings, builderUtil) {
|
return rules(kotlinCommonSettings, builderUtil) {
|
||||||
val DECLARATIONS =
|
val DECLARATIONS =
|
||||||
TokenSet.create(PROPERTY, FUN, CLASS, OBJECT_DECLARATION, ENUM_ENTRY, SECONDARY_CONSTRUCTOR, CLASS_INITIALIZER)
|
TokenSet.create(PROPERTY, FUN, CLASS, OBJECT_DECLARATION, ENUM_ENTRY, SECONDARY_CONSTRUCTOR, CLASS_INITIALIZER)
|
||||||
|
|
||||||
@@ -159,8 +159,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
betweenInside(RETURN_KEYWORD, LABEL_QUALIFIER, RETURN).spaces(0)
|
betweenInside(RETURN_KEYWORD, LABEL_QUALIFIER, RETURN).spaces(0)
|
||||||
afterInside(RETURN_KEYWORD, RETURN).spaces(1)
|
afterInside(RETURN_KEYWORD, RETURN).spaces(1)
|
||||||
afterInside(LABEL_QUALIFIER, RETURN).spaces(1)
|
afterInside(LABEL_QUALIFIER, RETURN).spaces(1)
|
||||||
betweenInside(LABEL_QUALIFIER, EOL_COMMENT, LABELED_EXPRESSION).spacing(0, Int.MAX_VALUE, 0, true, codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
betweenInside(LABEL_QUALIFIER, EOL_COMMENT, LABELED_EXPRESSION).spacing(0, Int.MAX_VALUE, 0, true, commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
betweenInside(LABEL_QUALIFIER, BLOCK_COMMENT, LABELED_EXPRESSION).spacing(0, Int.MAX_VALUE, 0, true, codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
betweenInside(LABEL_QUALIFIER, BLOCK_COMMENT, LABELED_EXPRESSION).spacing(0, Int.MAX_VALUE, 0, true, commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
afterInside(LABEL_QUALIFIER, LABELED_EXPRESSION).spaces(1)
|
afterInside(LABEL_QUALIFIER, LABELED_EXPRESSION).spaces(1)
|
||||||
|
|
||||||
betweenInside(FUN_KEYWORD, VALUE_PARAMETER_LIST, FUN).spacing(0, 0, 0, false, 0)
|
betweenInside(FUN_KEYWORD, VALUE_PARAMETER_LIST, FUN).spacing(0, 0, 0, false, 0)
|
||||||
@@ -429,8 +429,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
|
|
||||||
val spaces = if (empty) 0 else spacesInSimpleFunction
|
val spaces = if (empty) 0 else spacesInSimpleFunction
|
||||||
Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRangeWithoutComments,
|
Spacing.createDependentLFSpacing(spaces, spaces, psiElement.textRangeWithoutComments,
|
||||||
codeStyleSettings.KEEP_LINE_BREAKS,
|
commonCodeStyleSettings.KEEP_LINE_BREAKS,
|
||||||
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
}
|
}
|
||||||
|
|
||||||
inPosition(parent = BLOCK, left = LBRACE).customRule { parent, left, right ->
|
inPosition(parent = BLOCK, left = LBRACE).customRule { parent, left, right ->
|
||||||
@@ -441,8 +441,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
|
|||||||
|
|
||||||
// Empty block is covered in above rule
|
// Empty block is covered in above rule
|
||||||
Spacing.createDependentLFSpacing(spacesInSimpleFunction, spacesInSimpleFunction, funNode.textRangeWithoutComments,
|
Spacing.createDependentLFSpacing(spacesInSimpleFunction, spacesInSimpleFunction, funNode.textRangeWithoutComments,
|
||||||
codeStyleSettings.KEEP_LINE_BREAKS,
|
commonCodeStyleSettings.KEEP_LINE_BREAKS,
|
||||||
codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-1
@@ -39,6 +39,9 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
|
|||||||
case WRAPPING_AND_BRACES_SETTINGS:
|
case WRAPPING_AND_BRACES_SETTINGS:
|
||||||
return
|
return
|
||||||
"public class ThisIsASampleClass {\n" +
|
"public class ThisIsASampleClass {\n" +
|
||||||
|
" val test = \n" +
|
||||||
|
" 12\n" +
|
||||||
|
"\n" +
|
||||||
" fun foo1(i1: Int, i2: Int, i3: Int) : Int {\n" +
|
" fun foo1(i1: Int, i2: Int, i3: Int) : Int {\n" +
|
||||||
" when (i1) {\n" +
|
" when (i1) {\n" +
|
||||||
" is Number -> 0\n" +
|
" is Number -> 0\n" +
|
||||||
@@ -64,7 +67,14 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
|
|||||||
" }" +
|
" }" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
" private val f = {(a: Int)->a*2}\n" +
|
" private val f = {(a: Int)->a*2}\n" +
|
||||||
"}";
|
"}\n" +
|
||||||
|
"\n" +
|
||||||
|
"enum class Enumeration {\n" +
|
||||||
|
" A,\n" +
|
||||||
|
" B\n" +
|
||||||
|
"}\n" +
|
||||||
|
"";
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
"open class Some {\n"+
|
"open class Some {\n"+
|
||||||
@@ -150,6 +160,7 @@ public class KotlinLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
|
|||||||
consumer.showStandardOptions(
|
consumer.showStandardOptions(
|
||||||
// "ALIGN_MULTILINE_CHAINED_METHODS",
|
// "ALIGN_MULTILINE_CHAINED_METHODS",
|
||||||
"KEEP_FIRST_COLUMN_COMMENT",
|
"KEEP_FIRST_COLUMN_COMMENT",
|
||||||
|
"KEEP_LINE_BREAKS",
|
||||||
"ALIGN_MULTILINE_EXTENDS_LIST",
|
"ALIGN_MULTILINE_EXTENDS_LIST",
|
||||||
"ALIGN_MULTILINE_PARAMETERS",
|
"ALIGN_MULTILINE_PARAMETERS",
|
||||||
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
|
"ALIGN_MULTILINE_PARAMETERS_IN_CALLS",
|
||||||
|
|||||||
Reference in New Issue
Block a user