Refactoring: move method to rules
This commit is contained in:
@@ -24,6 +24,9 @@ import com.intellij.psi.tree.IElementType
|
||||
import org.jetbrains.jet.plugin.JetLanguage
|
||||
import com.intellij.psi.formatter.common.AbstractBlock
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.formatting.DependentSpacingRule.Trigger
|
||||
import com.intellij.formatting.DependentSpacingRule.Anchor
|
||||
import com.intellij.openapi.util.TextRange
|
||||
|
||||
class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
|
||||
class SpacingNodeBlock(node: ASTNode): AbstractBlock(node, null, null) {
|
||||
@@ -77,6 +80,18 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
|
||||
}
|
||||
}
|
||||
|
||||
fun emptyLinesIfLineBreakInLeft(emptyLines: Int) {
|
||||
newRule {(parent: ASTBlock, left: ASTBlock, right: ASTBlock) ->
|
||||
val dependentSpacingRule = DependentSpacingRule(Trigger.HAS_LINE_FEEDS).registerData(Anchor.MIN_LINE_FEEDS, emptyLines + 1)
|
||||
LineFeedDependantSpacing(
|
||||
0, 0,
|
||||
minimumLineFeeds = 1,
|
||||
keepLineBreaks = codeStyleSettings.KEEP_LINE_BREAKS,
|
||||
keepBlankLines = codeStyleSettings.KEEP_BLANK_LINES_IN_DECLARATIONS,
|
||||
dependency = left.getTextRange(), rule = dependentSpacingRule)
|
||||
}
|
||||
}
|
||||
|
||||
fun spacing(spacing: Spacing) {
|
||||
newRule { (parent, left, right) -> spacing }
|
||||
}
|
||||
@@ -117,6 +132,20 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
|
||||
builder.init()
|
||||
builders.add(builder)
|
||||
}
|
||||
|
||||
private class LineFeedDependantSpacing(
|
||||
minSpaces: Int,
|
||||
maxSpaces: Int,
|
||||
val minimumLineFeeds: Int,
|
||||
keepLineBreaks: Boolean,
|
||||
keepBlankLines: Int,
|
||||
dependency: TextRange,
|
||||
rule: DependentSpacingRule) : DependantSpacingImpl(minSpaces, maxSpaces, dependency, keepLineBreaks, keepBlankLines, rule) {
|
||||
override fun getMinLineFeeds(): Int {
|
||||
val superMin = super.getMinLineFeeds()
|
||||
return if (superMin == 0) minimumLineFeeds else superMin
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun rules(codeStyleSettings: CodeStyleSettings, init: KotlinSpacingBuilder.() -> Unit): KotlinSpacingBuilder {
|
||||
|
||||
@@ -51,26 +51,13 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
|
||||
return rules(settings) {
|
||||
custom {
|
||||
val emptyLineIfLeftMultiline = { (parent: ASTBlock, left: ASTBlock, right: ASTBlock) ->
|
||||
val multilineLF = 2
|
||||
val singleLineLF = 1
|
||||
|
||||
val dependentSpacingRule = DependentSpacingRule(Trigger.HAS_LINE_FEEDS).registerData(Anchor.MIN_LINE_FEEDS, multilineLF)
|
||||
LineFeedDependantSpacing(
|
||||
0, 0,
|
||||
minimumLineFeeds = singleLineLF,
|
||||
keepLineBreaks = settings.KEEP_LINE_BREAKS,
|
||||
keepBlankLines = settings.KEEP_BLANK_LINES_IN_DECLARATIONS,
|
||||
dependency = left.getTextRange(), rule = dependentSpacingRule)
|
||||
}
|
||||
|
||||
inPosition(left = CLASS, right = CLASS).customRule(emptyLineIfLeftMultiline)
|
||||
inPosition(left = FUN, right = FUN).customRule(emptyLineIfLeftMultiline)
|
||||
inPosition(left = PROPERTY, right = FUN).customRule(emptyLineIfLeftMultiline)
|
||||
inPosition(left = FUN, right = PROPERTY).customRule(emptyLineIfLeftMultiline)
|
||||
inPosition(left = CLASS, right = CLASS).emptyLinesIfLineBreakInLeft(1)
|
||||
inPosition(left = FUN, right = FUN).emptyLinesIfLineBreakInLeft(1)
|
||||
inPosition(left = PROPERTY, right = FUN).emptyLinesIfLineBreakInLeft(1)
|
||||
inPosition(left = FUN, right = PROPERTY).emptyLinesIfLineBreakInLeft(1)
|
||||
|
||||
// Case left for alternative constructors
|
||||
inPosition(left = FUN, right = CLASS).customRule(emptyLineIfLeftMultiline)
|
||||
inPosition(left = FUN, right = CLASS).emptyLinesIfLineBreakInLeft(1)
|
||||
}
|
||||
|
||||
simple {
|
||||
@@ -89,7 +76,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
before(FUN).lineBreakInCode()
|
||||
before(PROPERTY).lineBreakInCode()
|
||||
|
||||
|
||||
// =============== Spacing ================
|
||||
betweenInside(LBRACE, RBRACE, CLASS_BODY).spaces(0)
|
||||
|
||||
@@ -302,18 +288,4 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class LineFeedDependantSpacing(
|
||||
minSpaces: Int,
|
||||
maxSpaces: Int,
|
||||
val minimumLineFeeds: Int,
|
||||
keepLineBreaks: Boolean,
|
||||
keepBlankLines: Int,
|
||||
dependency: TextRange,
|
||||
rule: DependentSpacingRule) : DependantSpacingImpl(minSpaces, maxSpaces, dependency, keepLineBreaks, keepBlankLines, rule) {
|
||||
override fun getMinLineFeeds(): Int {
|
||||
val superMin = super.getMinLineFeeds()
|
||||
return if (superMin == 0) minimumLineFeeds else superMin
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user