-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically manage trailing commas when running with --google-style
Commas are added/removed according to the following rules: - Lambda param lists => remove - Single element lists => remove - Lists that fit on one line => remove - All other lists (multiline lists) => add
- Loading branch information
Showing
7 changed files
with
468 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
core/src/main/java/com/facebook/ktfmt/format/TrailingCommas.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.facebook.ktfmt.format | ||
|
||
import org.jetbrains.kotlin.com.intellij.psi.PsiComment | ||
import org.jetbrains.kotlin.com.intellij.psi.PsiElement | ||
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace | ||
import org.jetbrains.kotlin.psi.KtCollectionLiteralExpression | ||
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration | ||
import org.jetbrains.kotlin.psi.KtElement | ||
import org.jetbrains.kotlin.psi.KtFunctionLiteral | ||
import org.jetbrains.kotlin.psi.KtLambdaExpression | ||
import org.jetbrains.kotlin.psi.KtParameterList | ||
import org.jetbrains.kotlin.psi.KtTypeArgumentList | ||
import org.jetbrains.kotlin.psi.KtTypeParameterList | ||
import org.jetbrains.kotlin.psi.KtValueArgumentList | ||
import org.jetbrains.kotlin.psi.KtWhenEntry | ||
import org.jetbrains.kotlin.psi.psiUtil.allChildren | ||
|
||
/** Detects trailing commas or elements that should have trailing commas. */ | ||
object TrailingCommas { | ||
|
||
class Detector { | ||
private val trailingCommas = mutableListOf<PsiElement>() | ||
|
||
fun getTrailingCommaElements(): List<PsiElement> = trailingCommas | ||
|
||
/** returns **true** if this element was a traling comma, **false** otherwise. */ | ||
fun takeElement(element: PsiElement) { | ||
if (isTrailingComma(element)) { | ||
trailingCommas += element | ||
} | ||
} | ||
|
||
private fun isTrailingComma(element: PsiElement): Boolean { | ||
if (element.text != ",") { | ||
return false | ||
} | ||
|
||
return extractManagedList(element.parent)?.trailingComma == element | ||
} | ||
} | ||
|
||
class Suggestor { | ||
private val suggestionElements = mutableListOf<PsiElement>() | ||
|
||
fun getTrailingCommaSuggestions(): List<PsiElement> = suggestionElements | ||
|
||
fun takeElement(element: KtElement) { | ||
if (!element.text.contains("\n")) { | ||
return // Only suggest trailing commas where there is already a line break | ||
} | ||
|
||
when (element) { | ||
is KtWhenEntry -> return | ||
is KtParameterList -> { | ||
if (element.parent is KtFunctionLiteral && element.parent.parent is KtLambdaExpression) { | ||
return // Never add trailing commas to lambda param lists | ||
} | ||
} | ||
} | ||
|
||
val list = extractManagedList(element) ?: return | ||
if (list.items.size <= 1) { | ||
return // Never insert commas to single-element lists | ||
} | ||
if (list.trailingComma != null) { | ||
return // Never insert a comma if there already is one somehow | ||
} | ||
|
||
suggestionElements.add(list.items.last().leftLeafIgnoringCommentsAndWhitespace()) | ||
} | ||
} | ||
|
||
private class ManagedList(val items: List<KtElement>, val trailingComma: PsiElement?) | ||
|
||
private fun extractManagedList(element: PsiElement): ManagedList? { | ||
// TODO(nickreid): What about: | ||
// - function-type param lists | ||
// - destructuring declarations | ||
return when (element) { | ||
is KtValueArgumentList -> ManagedList(element.arguments, element.trailingComma) | ||
is KtParameterList -> ManagedList(element.parameters, element.trailingComma) | ||
is KtTypeArgumentList -> ManagedList(element.arguments, element.trailingComma) | ||
is KtTypeParameterList -> ManagedList(element.parameters, element.trailingComma) | ||
is KtCollectionLiteralExpression -> { | ||
ManagedList(element.innerExpressions, element.trailingComma) | ||
} | ||
is KtWhenEntry -> ManagedList(element.conditions.toList(), element.trailingComma) | ||
else -> null | ||
} | ||
} | ||
|
||
private fun PsiElement.leftLeafIgnoringCommentsAndWhitespace(): PsiElement { | ||
var child = this.lastChild | ||
while (child != null) { | ||
if (child is PsiWhiteSpace || child is PsiComment) { | ||
child = child.prevSibling | ||
} else { | ||
return child.leftLeafIgnoringCommentsAndWhitespace() | ||
} | ||
} | ||
return this | ||
} | ||
} |
Oops, something went wrong.