mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
[kotlin] Shorten expression if it is too long
#KTIJ-28873 Fixed GitOrigin-RevId: 9ccdd7436aecc315323edb82e053d716ec8f58f3
This commit is contained in:
committed by
intellij-monorepo-bot
parent
2eb0a45214
commit
80a81f44ff
+21
-9
@@ -110,12 +110,22 @@ fun KtElement.renderTrimmed(): String {
|
||||
|
||||
override fun visitCallExpression(expression: KtCallExpression) {
|
||||
expression.calleeExpression?.accept(this)
|
||||
expression.typeArgumentList?.accept(this)
|
||||
expression.valueArgumentList?.accept(this)
|
||||
repeat(expression.lambdaArguments.size) { builder.append("{...}") }
|
||||
repeat(expression.lambdaArguments.size) { builder.append("{$ellipsis}") }
|
||||
}
|
||||
|
||||
override fun visitValueArgumentList(list: KtValueArgumentList) {
|
||||
builder.append(if (list.arguments.isEmpty()) "()" else "(...)")
|
||||
val arguments = list.arguments
|
||||
builder.append("(")
|
||||
if (arguments.isNotEmpty()) {
|
||||
if (arguments.size <= 3 && arguments.all { it.getArgumentExpression() is KtConstantExpression }) {
|
||||
arguments.joinTo(builder) { it.text }
|
||||
} else {
|
||||
builder.append(ellipsis)
|
||||
}
|
||||
}
|
||||
builder.append(")")
|
||||
}
|
||||
|
||||
override fun visitQualifiedExpression(expression: KtQualifiedExpression) {
|
||||
@@ -171,7 +181,7 @@ fun KtElement.renderTrimmed(): String {
|
||||
if (expression.parent is KtFunctionLiteral) {
|
||||
super.visitBlockExpression(expression)
|
||||
} else {
|
||||
builder.append("{...}")
|
||||
builder.append("{$ellipsis}")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,7 +206,7 @@ fun KtElement.renderTrimmed(): String {
|
||||
it.accept(this)
|
||||
builder.append(')')
|
||||
}
|
||||
builder.append(" {...}")
|
||||
builder.append(" {$ellipsis}")
|
||||
}
|
||||
|
||||
override fun visitForExpression(expression: KtForExpression) {
|
||||
@@ -233,7 +243,7 @@ fun KtElement.renderTrimmed(): String {
|
||||
}
|
||||
|
||||
override fun visitTryExpression(expression: KtTryExpression) {
|
||||
builder.append("try {...}")
|
||||
builder.append("try {$ellipsis}")
|
||||
}
|
||||
|
||||
// Declarations
|
||||
@@ -277,7 +287,7 @@ fun KtElement.renderTrimmed(): String {
|
||||
|
||||
classOrObject.name?.let { builder.append(" $it") }
|
||||
classOrObject.getSuperTypeList()?.accept(this)
|
||||
classOrObject.body?.let { builder.append(" {...}") }
|
||||
classOrObject.body?.let { builder.append(" {$ellipsis}") }
|
||||
}
|
||||
|
||||
override fun visitSuperTypeList(list: KtSuperTypeList) {
|
||||
@@ -315,11 +325,13 @@ fun KtElement.renderTrimmed(): String {
|
||||
return Renderer().render(this)
|
||||
}
|
||||
|
||||
internal const val ellipsis = "${Typography.ellipsis}"
|
||||
|
||||
@NlsSafe
|
||||
fun getExpressionShortText(element: KtElement): String {
|
||||
val text = element.renderTrimmed().trimStart()
|
||||
fun getExpressionShortText(element: PsiElement): String {
|
||||
val text = ((element as? KtElement)?.renderTrimmed() ?: element.text).trimStart()
|
||||
val firstNewLinePos = text.indexOf('\n')
|
||||
var trimmedText = text.substring(0, if (firstNewLinePos != -1) firstNewLinePos else min(100, text.length))
|
||||
if (trimmedText.length != text.length) trimmedText += " ..."
|
||||
if (trimmedText.length != text.length) trimmedText += " $ellipsis"
|
||||
return trimmedText
|
||||
}
|
||||
+3
-2
@@ -8,10 +8,11 @@ import org.jetbrains.kotlin.analysis.api.renderer.types.impl.KtTypeRendererForSo
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtErrorType
|
||||
import org.jetbrains.kotlin.analysis.api.types.KtType
|
||||
import org.jetbrains.kotlin.idea.base.analysis.api.utils.shortenReferences
|
||||
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.base.psi.replaced
|
||||
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.*
|
||||
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.*
|
||||
import org.jetbrains.kotlin.idea.codeinsight.utils.getExpressionShortText
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
@@ -24,7 +25,7 @@ object CastExpressionFixFactories {
|
||||
|
||||
private val applicator: KotlinModCommandApplicator<PsiElement, Input> = modCommandApplicator {
|
||||
familyName(KotlinBundle.lazyMessage("fix.cast.expression.family"))
|
||||
actionName { psi, input -> KotlinBundle.message("fix.cast.expression.text", psi.text, input.typePresentation) }
|
||||
actionName { psi, input -> KotlinBundle.message("fix.cast.expression.text", getExpressionShortText(psi), input.typePresentation) }
|
||||
applyTo { psi, input, context, updater ->
|
||||
val expressionToInsert = KtPsiFactory(context.project).createExpressionByPattern("$0 as $1", psi, input.typeSourceCode)
|
||||
val newExpression = psi.replaced(expressionToInsert)
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
These declarations cannot be transformed:
|
||||
actual val a: Some
|
||||
constructor(a: Some, b: Int){...}
|
||||
constructor(a: Some, b: Int){…}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// "Add missing actual members" "true"
|
||||
// SHOULD_FAIL_WITH: Some types are not accessible:,class M {...} -> Type <Unknown> is not accessible from target module
|
||||
// SHOULD_FAIL_WITH: Some types are not accessible:,class M {…} -> Type <Unknown> is not accessible from target module
|
||||
// DISABLE-ERRORS
|
||||
// IGNORE_K2
|
||||
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
// "Add missing actual members" "true"
|
||||
// SHOULD_FAIL_WITH: Some types are not accessible:,class M {...} -> Type <Unknown> is not accessible from target module
|
||||
// SHOULD_FAIL_WITH: Some types are not accessible:,class M {…} -> Type <Unknown> is not accessible from target module
|
||||
// DISABLE-ERRORS
|
||||
// IGNORE_K2
|
||||
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
These declarations cannot be transformed:
|
||||
fun foo(arg: Int){...}
|
||||
fun foo(arg: Int){…}
|
||||
actual lateinit var some: Boolean
|
||||
|
||||
@@ -6,5 +6,5 @@ fun main() {
|
||||
fun foo(a: Int): Int = 5
|
||||
|
||||
/*
|
||||
foo(...)
|
||||
foo(…)
|
||||
*/
|
||||
@@ -7,5 +7,5 @@ fun foo(a: Int): Int = 5
|
||||
fun boo(a: Int): Int = 5
|
||||
|
||||
/*
|
||||
boo(...)
|
||||
boo(…)
|
||||
*/
|
||||
@@ -7,6 +7,6 @@ fun foo(a: Int): Int = 5
|
||||
fun boo(a: Int): Int = 5
|
||||
|
||||
/*
|
||||
foo(...)
|
||||
boo(...)
|
||||
foo(…)
|
||||
boo(…)
|
||||
*/
|
||||
@@ -11,7 +11,7 @@ fun foo(t: T) {
|
||||
/*
|
||||
1
|
||||
{ 1 }
|
||||
t.foo{...}
|
||||
t.foo{...}.foo{...}
|
||||
t.foo{...}.foo{...}.foo(...)
|
||||
t.foo{…}
|
||||
t.foo{…}.foo{…}
|
||||
t.foo{…}.foo{…}.foo(…)
|
||||
*/
|
||||
@@ -12,7 +12,7 @@ fun foo(t: T) {
|
||||
|
||||
/*
|
||||
2
|
||||
t.foo(...)
|
||||
t.foo(...).foo()
|
||||
t.foo(...).foo().foo(...)
|
||||
t.foo(…)
|
||||
t.foo(…).foo()
|
||||
t.foo(…).foo().foo(4)
|
||||
*/
|
||||
@@ -11,6 +11,6 @@ fun foo() {
|
||||
}
|
||||
|
||||
/*
|
||||
object : T {...}
|
||||
object : T {...}.foo(...)
|
||||
object : T {…}
|
||||
object : T {…}.foo(…)
|
||||
*/
|
||||
@@ -4,5 +4,5 @@ fun main(args: Array<String>) {
|
||||
/*
|
||||
1.0
|
||||
1.0 + 1
|
||||
Math.pow(...)
|
||||
Math.pow(…)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user