Skip to content

Commit

Permalink
Merge pull request #64 from rock3r/trailing-comma
Browse files Browse the repository at this point in the history
Add "trailing comma" setting
  • Loading branch information
suusan2go authored Aug 15, 2022
2 parents 7753d29 + bb6b84f commit 57f486e
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 47 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ intellij {

repositories {
mavenCentral()
jcenter()
}

group 'com.github.suusan2go.kotlin-fill-class'
version '1.0.12'
version '1.0.13'

sourceCompatibility = 11

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.descriptors.CallableDescriptor
Expand Down Expand Up @@ -36,7 +37,8 @@ import javax.swing.JComponent

class FillClassInspection(
@JvmField var withoutDefaultValues: Boolean = false,
@JvmField var withoutDefaultArguments: Boolean = false
@JvmField var withoutDefaultArguments: Boolean = false,
@JvmField var withTrailingComma: Boolean = false
) : AbstractKotlinInspection() {
override fun buildVisitor(
holder: ProblemsHolder,
Expand All @@ -48,7 +50,8 @@ class FillClassInspection(
val fix = FillClassFix(
description = description,
withoutDefaultValues = withoutDefaultValues,
withoutDefaultArguments = withoutDefaultArguments
withoutDefaultArguments = withoutDefaultArguments,
withTrailingComma = withTrailingComma
)
holder.registerProblem(element, description, fix)
})
Expand All @@ -57,6 +60,7 @@ class FillClassInspection(
val panel = MultipleCheckboxOptionsPanel(this)
panel.addCheckbox("Fill arguments without default values", "withoutDefaultValues")
panel.addCheckbox("Do not fill default arguments", "withoutDefaultArguments")
panel.addCheckbox("Append trailing comma", "withTrailingComma")
return panel
}
}
Expand All @@ -71,7 +75,8 @@ private fun KtValueArgumentList.descriptor(): CallableDescriptor? {
class FillClassFix(
private val description: String,
private val withoutDefaultValues: Boolean,
private val withoutDefaultArguments: Boolean
private val withoutDefaultArguments: Boolean,
private val withTrailingComma: Boolean
) : LocalQuickFix {
override fun getName() = description

Expand All @@ -96,6 +101,9 @@ class FillClassFix(
if (argumentExpression is KtQualifiedExpression || argumentExpression is KtLambdaExpression) {
ShortenReferences.DEFAULT.process(argumentExpression)
}
if (argumentExpression != null && withTrailingComma && index == parameters.lastIndex) {
argumentExpression.addCommaAfter(factory)
}
}
}

Expand Down Expand Up @@ -166,4 +174,9 @@ class FillClassFix(
}
append("}")
}

private fun PsiElement.addCommaAfter(factory: KtPsiFactory) {
val comma = factory.createComma()
parent.addAfter(comma, this)
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
]]></description>

<change-notes><![CDATA[
1.0.12 - Add support for 221* / 2022.1 version of IDEA
1.0.13- Add trailing comma support
]]>
</change-notes>

Expand Down
Loading

0 comments on commit 57f486e

Please sign in to comment.