Formatter: left brace formatting for "when" expression and "when" entries
This commit is contained in:
@@ -183,10 +183,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
between(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL_EXPRESSION).spaces(1)
|
||||
beforeInside(ARROW, FUNCTION_LITERAL).spaceIf(jetSettings.SPACE_BEFORE_LAMBDA_ARROW)
|
||||
|
||||
//when
|
||||
aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW)
|
||||
beforeInside(LBRACE, WHEN).spacing(1, 1, 0, true, 0) //omit blank lines before '{' in 'when' statement
|
||||
|
||||
aroundInside(ARROW, FUNCTION_TYPE).spaceIf(jetSettings.SPACE_AROUND_FUNCTION_TYPE_ARROW)
|
||||
|
||||
betweenInside(REFERENCE_EXPRESSION, FUNCTION_LITERAL_EXPRESSION, CALL_EXPRESSION).spaces(1)
|
||||
@@ -197,8 +193,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
|
||||
fun spacingForLeftBrace(block: ASTNode?, blockType: IElementType = BLOCK): Spacing? {
|
||||
if (block != null && block.getElementType() == blockType) {
|
||||
val leftBrace = block.getFirstChildNode()
|
||||
if (leftBrace != null && leftBrace.getElementType() == LBRACE) {
|
||||
val leftBrace = block.findChildByType(LBRACE)
|
||||
if (leftBrace != null) {
|
||||
val previousLeaf = FormatterUtil.getPreviousNonWhitespaceLeaf(leftBrace)
|
||||
val isAfterEolComment = previousLeaf != null && (previousLeaf.getElementType() == EOL_COMMENT)
|
||||
val keepLineBreaks = jetSettings.LBRACE_ON_NEXT_LINE || isAfterEolComment
|
||||
@@ -234,6 +230,12 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
|
||||
inPosition(right = CLASS_BODY).customRule(leftBraceRule(blockType = CLASS_BODY))
|
||||
|
||||
inPosition(parent = WHEN_ENTRY, right = BLOCK).customRule(leftBraceRule())
|
||||
inPosition(parent = WHEN, right = LBRACE).customRule {
|
||||
parent, left, right ->
|
||||
spacingForLeftBrace(block = parent.getNode(), blockType = WHEN)
|
||||
}
|
||||
|
||||
val spacesInSimpleFunction = if (jetSettings.INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD) 1 else 0
|
||||
inPosition(parent = FUNCTION_LITERAL,
|
||||
left = LBRACE,
|
||||
@@ -273,6 +275,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
||||
beforeInside(RBRACE, CLASS_BODY).lineBreakInCode()
|
||||
beforeInside(RBRACE, BLOCK).lineBreakInCode()
|
||||
between(RPAR, BODY).spaces(1)
|
||||
|
||||
// if when entry has block, spacing after arrow should be set by lbrace rule
|
||||
aroundInside(ARROW, WHEN_ENTRY).spaceIf(jetSettings.SPACE_AROUND_WHEN_ARROW)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,44 +24,36 @@ fun some(x: Any) {
|
||||
}
|
||||
}
|
||||
when (x) {
|
||||
is Int ->
|
||||
{
|
||||
is Int -> {
|
||||
0
|
||||
}
|
||||
3 ->
|
||||
{
|
||||
3 -> {
|
||||
2
|
||||
}
|
||||
in 0..3 ->
|
||||
{
|
||||
in 0..3 -> {
|
||||
2
|
||||
}
|
||||
else ->
|
||||
{
|
||||
else -> {
|
||||
1
|
||||
}
|
||||
}
|
||||
when (x) {
|
||||
is
|
||||
Int
|
||||
->
|
||||
{
|
||||
-> {
|
||||
0
|
||||
}
|
||||
3
|
||||
->
|
||||
{
|
||||
-> {
|
||||
2
|
||||
}
|
||||
in
|
||||
0..3
|
||||
->
|
||||
{
|
||||
-> {
|
||||
2
|
||||
}
|
||||
else
|
||||
->
|
||||
{
|
||||
-> {
|
||||
1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
fun f() {
|
||||
when(c) {
|
||||
a -> {
|
||||
}
|
||||
b -> j
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
when(c) {
|
||||
a -> {
|
||||
}
|
||||
b -> j
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
when(c) {
|
||||
a -> {
|
||||
}
|
||||
b -> j
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
when(c) //eol comment
|
||||
{
|
||||
a -> //eol comment
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else -> //eol comment
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
@@ -0,0 +1,49 @@
|
||||
fun f()
|
||||
{
|
||||
when(c)
|
||||
{
|
||||
a ->
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else ->
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
when(c)
|
||||
{
|
||||
a ->
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else ->
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
when(c)
|
||||
{
|
||||
a ->
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else ->
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
when(c) //eol comment
|
||||
{
|
||||
a -> //eol comment
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else -> //eol comment
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
@@ -0,0 +1,49 @@
|
||||
fun f()
|
||||
{
|
||||
when(c) {
|
||||
a -> {
|
||||
}
|
||||
b -> j
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
||||
when(c)
|
||||
{
|
||||
a ->
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else ->
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
when(c)
|
||||
|
||||
{
|
||||
a ->
|
||||
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else ->
|
||||
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
when(c) //eol comment
|
||||
{
|
||||
a -> //eol comment
|
||||
{
|
||||
}
|
||||
b -> j
|
||||
else -> //eol comment
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// SET_TRUE: LBRACE_ON_NEXT_LINE
|
||||
@@ -1,6 +1,5 @@
|
||||
fun f(x: Any): Int {
|
||||
return when (x)
|
||||
{
|
||||
return when (x) {
|
||||
is Int -> 1
|
||||
else -> 0
|
||||
}
|
||||
|
||||
@@ -249,6 +249,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
|
||||
doTest("idea/testData/formatter/WhenEntryExpr.after.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenLineBreak.after.kt")
|
||||
public void testWhenLineBreak() throws Exception {
|
||||
doTest("idea/testData/formatter/WhenLineBreak.after.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenLinesBeforeLbrace.after.kt")
|
||||
public void testWhenLinesBeforeLbrace() throws Exception {
|
||||
doTest("idea/testData/formatter/WhenLinesBeforeLbrace.after.kt");
|
||||
@@ -452,6 +457,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
|
||||
doTestInverted("idea/testData/formatter/WhenArrow.after.inv.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhenLineBreak.after.inv.kt")
|
||||
public void testWhenLineBreak() throws Exception {
|
||||
doTestInverted("idea/testData/formatter/WhenLineBreak.after.inv.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("WhileLineBreak.after.inv.kt")
|
||||
public void testWhileLineBreak() throws Exception {
|
||||
doTestInverted("idea/testData/formatter/WhileLineBreak.after.inv.kt");
|
||||
|
||||
Reference in New Issue
Block a user