TrailingCommaInspection: add dependency to settings
* Change severity to `INFO` * Mark useless comma as unused symbol * Add dependency on `Use trailing comma` #KT-39069 Fixed #KT-38568 Fixed
This commit is contained in:
@@ -2181,7 +2181,7 @@ inspection.redundant.inner.class.modifier.descriptor=Redundant 'inner' modifier
|
|||||||
inspection.redundant.inner.class.modifier.display.name=Redundant 'inner' modifier
|
inspection.redundant.inner.class.modifier.display.name=Redundant 'inner' modifier
|
||||||
fix.remove.annotation.text=Remove annotation
|
fix.remove.annotation.text=Remove annotation
|
||||||
inspection.trailing.comma.display.name=Trailing comma recommendations
|
inspection.trailing.comma.display.name=Trailing comma recommendations
|
||||||
inspection.trailing.comma.report.also.a.missing.comma=Report also a missing comma
|
inspection.trailing.comma.report.also.a.missing.comma=Report also a missing comma or a line break
|
||||||
inspection.trailing.comma.add.line.break=Add line break
|
inspection.trailing.comma.add.line.break=Add line break
|
||||||
inspection.trailing.comma.missing.line.break=Missing line break
|
inspection.trailing.comma.missing.line.break=Missing line break
|
||||||
inspection.trailing.comma.remove.trailing.comma=Remove trailing comma
|
inspection.trailing.comma.remove.trailing.comma=Remove trailing comma
|
||||||
|
|||||||
@@ -2739,7 +2739,7 @@
|
|||||||
groupPath="Kotlin"
|
groupPath="Kotlin"
|
||||||
groupName="Style issues"
|
groupName="Style issues"
|
||||||
enabledByDefault="true"
|
enabledByDefault="true"
|
||||||
level="WEAK WARNING"
|
level="INFO"
|
||||||
language="kotlin"
|
language="kotlin"
|
||||||
key="inspection.trailing.comma.display.name" bundle="messages.KotlinBundle"/>
|
key="inspection.trailing.comma.display.name" bundle="messages.KotlinBundle"/>
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.idea.util.isLineBreak
|
|||||||
import org.jetbrains.kotlin.psi.KtElement
|
import org.jetbrains.kotlin.psi.KtElement
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.*
|
import org.jetbrains.kotlin.psi.psiUtil.*
|
||||||
import javax.swing.JComponent
|
import javax.swing.JComponent
|
||||||
|
import kotlin.properties.Delegates
|
||||||
|
|
||||||
class TrailingCommaInspection(
|
class TrailingCommaInspection(
|
||||||
@JvmField
|
@JvmField
|
||||||
@@ -30,13 +31,16 @@ class TrailingCommaInspection(
|
|||||||
) : AbstractKotlinInspection() {
|
) : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : TrailingCommaVisitor() {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = object : TrailingCommaVisitor() {
|
||||||
override val recursively: Boolean = false
|
override val recursively: Boolean = false
|
||||||
|
var useTrailingComma by Delegates.notNull<Boolean>()
|
||||||
|
|
||||||
override fun process(commaOwner: KtElement) {
|
override fun process(commaOwner: KtElement) {
|
||||||
|
useTrailingComma = CodeStyle.getSettings(commaOwner.project).kotlinCustomSettings.ALLOW_TRAILING_COMMA
|
||||||
val action = TrailingCommaAction.create(commaOwner)
|
val action = TrailingCommaAction.create(commaOwner)
|
||||||
if (action != TrailingCommaAction.REMOVE) {
|
if (action != TrailingCommaAction.REMOVE) {
|
||||||
checkCommaPosition(commaOwner)
|
checkCommaPosition(commaOwner)
|
||||||
checkLineBreaks(commaOwner)
|
checkLineBreaks(commaOwner)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkTrailingComma(commaOwner, action)
|
checkTrailingComma(commaOwner, action)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,16 +48,8 @@ class TrailingCommaInspection(
|
|||||||
val first = TrailingCommaHelper.elementBeforeFirstElement(commaOwner)
|
val first = TrailingCommaHelper.elementBeforeFirstElement(commaOwner)
|
||||||
if (first?.nextLeaf(true)?.isLineBreak() == false) {
|
if (first?.nextLeaf(true)?.isLineBreak() == false) {
|
||||||
first.nextSibling?.let {
|
first.nextSibling?.let {
|
||||||
registerProblemForLineBreak(
|
registerProblemForLineBreak(commaOwner, it, ProblemHighlightType.INFORMATION)
|
||||||
commaOwner,
|
|
||||||
it,
|
|
||||||
if (ApplicationManager.getApplication().isUnitTestMode)
|
|
||||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
|
||||||
else
|
|
||||||
ProblemHighlightType.INFORMATION,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val last = TrailingCommaHelper.elementAfterLastElement(commaOwner)
|
val last = TrailingCommaHelper.elementAfterLastElement(commaOwner)
|
||||||
@@ -61,7 +57,7 @@ class TrailingCommaInspection(
|
|||||||
registerProblemForLineBreak(
|
registerProblemForLineBreak(
|
||||||
commaOwner,
|
commaOwner,
|
||||||
last,
|
last,
|
||||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
if (addCommaWarning) ProblemHighlightType.GENERIC_ERROR_OR_WARNING else ProblemHighlightType.INFORMATION,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -84,17 +80,16 @@ class TrailingCommaInspection(
|
|||||||
trailingCommaOrLastElement,
|
trailingCommaOrLastElement,
|
||||||
KotlinBundle.message("inspection.trailing.comma.missing.trailing.comma"),
|
KotlinBundle.message("inspection.trailing.comma.missing.trailing.comma"),
|
||||||
KotlinBundle.message("inspection.trailing.comma.add.trailing.comma"),
|
KotlinBundle.message("inspection.trailing.comma.add.trailing.comma"),
|
||||||
if (addCommaWarning || ApplicationManager.getApplication().isUnitTestMode)
|
if (addCommaWarning) ProblemHighlightType.GENERIC_ERROR_OR_WARNING else ProblemHighlightType.INFORMATION,
|
||||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
|
||||||
else
|
|
||||||
ProblemHighlightType.INFORMATION,
|
|
||||||
)
|
)
|
||||||
} else if (action == TrailingCommaAction.REMOVE) {
|
} else if (action == TrailingCommaAction.REMOVE) {
|
||||||
if (!trailingCommaOrLastElement.isComma) return
|
if (!trailingCommaOrLastElement.isComma) return
|
||||||
reportProblem(
|
reportProblem(
|
||||||
trailingCommaOrLastElement,
|
trailingCommaOrLastElement,
|
||||||
KotlinBundle.message("inspection.trailing.comma.useless.trailing.comma"),
|
KotlinBundle.message("inspection.trailing.comma.useless.trailing.comma"),
|
||||||
KotlinBundle.message("inspection.trailing.comma.remove.trailing.comma")
|
KotlinBundle.message("inspection.trailing.comma.remove.trailing.comma"),
|
||||||
|
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
||||||
|
checkTrailingCommaSettings = false,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,6 +99,7 @@ class TrailingCommaInspection(
|
|||||||
message: String,
|
message: String,
|
||||||
fixMessage: String,
|
fixMessage: String,
|
||||||
highlightType: ProblemHighlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
highlightType: ProblemHighlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
||||||
|
checkTrailingCommaSettings: Boolean = true,
|
||||||
) {
|
) {
|
||||||
val commaOwner = commaOrElement.parent as KtElement
|
val commaOwner = commaOrElement.parent as KtElement
|
||||||
// case for KtFunctionLiteral, where PsiWhiteSpace after KtTypeParameterList isn't included in this list
|
// case for KtFunctionLiteral, where PsiWhiteSpace after KtTypeParameterList isn't included in this list
|
||||||
@@ -111,7 +107,7 @@ class TrailingCommaInspection(
|
|||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
problemOwner,
|
problemOwner,
|
||||||
message,
|
message,
|
||||||
highlightType,
|
highlightType.applyCondition(!checkTrailingCommaSettings || useTrailingComma),
|
||||||
commaOrElement.textRangeOfCommaOrSymbolAfter.shiftLeft(problemOwner.startOffset),
|
commaOrElement.textRangeOfCommaOrSymbolAfter.shiftLeft(problemOwner.startOffset),
|
||||||
createQuickFix(fixMessage, commaOwner),
|
createQuickFix(fixMessage, commaOwner),
|
||||||
)
|
)
|
||||||
@@ -126,12 +122,18 @@ class TrailingCommaInspection(
|
|||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
problemElement,
|
problemElement,
|
||||||
KotlinBundle.message("inspection.trailing.comma.missing.line.break"),
|
KotlinBundle.message("inspection.trailing.comma.missing.line.break"),
|
||||||
highlightType,
|
highlightType.applyCondition(useTrailingComma),
|
||||||
TextRange.from(elementForTextRange.startOffset, 1).shiftLeft(problemElement.startOffset),
|
TextRange.from(elementForTextRange.startOffset, 1).shiftLeft(problemElement.startOffset),
|
||||||
createQuickFix(KotlinBundle.message("inspection.trailing.comma.add.line.break"), commaOwner),
|
createQuickFix(KotlinBundle.message("inspection.trailing.comma.add.line.break"), commaOwner),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ProblemHighlightType.applyCondition(condition: Boolean): ProblemHighlightType = when {
|
||||||
|
ApplicationManager.getApplication().isUnitTestMode -> ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
||||||
|
condition -> this
|
||||||
|
else -> ProblemHighlightType.INFORMATION
|
||||||
|
}
|
||||||
|
|
||||||
private fun createQuickFix(
|
private fun createQuickFix(
|
||||||
fixMessage: String,
|
fixMessage: String,
|
||||||
commaOwner: KtElement,
|
commaOwner: KtElement,
|
||||||
|
|||||||
Reference in New Issue
Block a user