Formatter: force keyword to be on exactly next line if the corresponding option is set

This commit is contained in:
Pavel V. Talanov
2014-01-20 14:40:17 +04:00
parent 53d503e242
commit 4ef7086e5c
10 changed files with 61 additions and 3 deletions
@@ -67,11 +67,12 @@ class KotlinSpacingBuilder(val codeStyleSettings: CodeStyleSettings) {
return this
}
fun lineBreakIfLineBreakInParent(numSpacesOtherwise: Int) {
fun lineBreakIfLineBreakInParent(numSpacesOtherwise: Int, allowBlankLines: Boolean = true) {
newRule {
p, l, r ->
Spacing.createDependentLFSpacing(numSpacesOtherwise, numSpacesOtherwise, p.getTextRange(),
codeStyleSettings.KEEP_LINE_BREAKS, codeStyleSettings.KEEP_BLANK_LINES_IN_CODE)
codeStyleSettings.KEEP_LINE_BREAKS,
if (allowBlankLines) codeStyleSettings.KEEP_BLANK_LINES_IN_CODE else 0)
}
}
@@ -191,7 +192,7 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
fun CustomSpacingBuilder.ruleForKeywordOnNewLine(shouldBeOnNewLine: Boolean, keyword: IElementType, parent: IElementType) {
if (shouldBeOnNewLine) {
inPosition(parent = parent, right = keyword)
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1)
.lineBreakIfLineBreakInParent(numSpacesOtherwise = 1, allowBlankLines = false)
}
else {
inPosition(parent = parent, right = keyword).customRule {
@@ -8,6 +8,11 @@ fun f() {
} catch (e: E) {
} finally {
}
try {
} catch (e: E) {
} finally {
}
}
// SET_TRUE: CATCH_ON_NEW_LINE
@@ -12,6 +12,13 @@ fun f() {
}
finally {
}
try {
}
catch (e: E) {
}
finally {
}
}
// SET_TRUE: CATCH_ON_NEW_LINE
@@ -10,6 +10,15 @@ fun f() {
} catch (e: E) {
} finally {
}
try {
}
catch (e: E) {
}
finally {
}
}
// SET_TRUE: CATCH_ON_NEW_LINE
@@ -5,6 +5,11 @@ fun f() {
}
if (true) {
} else if (true) {
} else {
}
if (true) {
} else if (true) {
} else {
@@ -7,6 +7,13 @@ fun f() {
}
if (true) {
}
else if (true) {
}
else {
}
if (true) {
}
else if (true) {
+9
View File
@@ -12,6 +12,15 @@ fun f() {
} else {
}
if (true) {
}
else if (true) {
}
else {
}
if (a) 3 else 4
if (a) 5
@@ -7,6 +7,10 @@ fun f() {
} while (true)
do {
} while (true)
do a += 1 while (true)
do a += 1
@@ -9,6 +9,11 @@ fun f() {
}
while (true)
do {
}
while (true)
do a += 1 while (true)
do a += 1
@@ -8,6 +8,12 @@ fun f() {
}
while (true)
do {
}
while (true)
do a += 1 while (true)
do a += 1