-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide quick fix for migration of single elements in named arguments
See more in KT-20171
- Loading branch information
1 parent
8ab7c26
commit 8a545f0
Showing
14 changed files
with
164 additions
and
5 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
60 changes: 60 additions & 0 deletions
60
.../org/jetbrains/kotlin/idea/quickfix/SurroundWithArrayOfWithSpreadOperatorInFunctionFix.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,60 @@ | ||
/* | ||
* Copyright 2010-2017 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.idea.quickfix | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns | ||
import org.jetbrains.kotlin.diagnostics.Diagnostic | ||
import org.jetbrains.kotlin.diagnostics.Errors | ||
import org.jetbrains.kotlin.name.Name | ||
import org.jetbrains.kotlin.psi.* | ||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType | ||
import org.jetbrains.kotlin.resolve.CollectionLiteralResolver.Companion.ARRAY_OF_FUNCTION | ||
import org.jetbrains.kotlin.resolve.CollectionLiteralResolver.Companion.PRIMITIVE_TYPE_TO_ARRAY | ||
|
||
class SurroundWithArrayOfWithSpreadOperatorInFunctionFix( | ||
val wrapper: Name, | ||
argument: KtExpression | ||
) : KotlinQuickFixAction<KtExpression>(argument) { | ||
override fun getText() = "Surround with *$wrapper(...)" | ||
|
||
override fun getFamilyName() = text | ||
|
||
override fun invoke(project: Project, editor: Editor?, file: KtFile) { | ||
val argument = element?.getParentOfType<KtValueArgument>(false) ?: return | ||
val argumentName = argument.getArgumentName()?.asName ?: return | ||
val argumentExpression = argument.getArgumentExpression() ?: return | ||
|
||
val factory = KtPsiFactory(argumentExpression) | ||
|
||
val surroundedWithArrayOf = factory.createExpressionByPattern("$wrapper($0)", argumentExpression) | ||
val newArgument = factory.createArgument(surroundedWithArrayOf, argumentName, isSpread = true) | ||
|
||
argument.replace(newArgument) | ||
} | ||
|
||
companion object : KotlinSingleIntentionActionFactory() { | ||
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtExpression>? { | ||
val actualDiagnostic = Errors.ASSIGNING_SINGLE_ELEMENT_TO_VARARG_IN_NAMED_FORM_FUNCTION.cast(diagnostic) | ||
val parameterType = actualDiagnostic.a | ||
|
||
val wrapper = PRIMITIVE_TYPE_TO_ARRAY[KotlinBuiltIns.getPrimitiveArrayElementType(parameterType)] ?: ARRAY_OF_FUNCTION | ||
return SurroundWithArrayOfWithSpreadOperatorInFunctionFix(wrapper, actualDiagnostic.psiElement) | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
.../testData/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceForVarargOfAny.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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun anyFoo(vararg a: Any) {} | ||
|
||
fun test() { | ||
anyFoo(a = intArr<caret>ayOf(1)) | ||
} |
8 changes: 8 additions & 0 deletions
8
...ata/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceForVarargOfAny.kt.after
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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun anyFoo(vararg a: Any) {} | ||
|
||
fun test() { | ||
anyFoo(a = *arrayOf(intArrayOf(1))) | ||
} |
8 changes: 8 additions & 0 deletions
8
.../quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceToArrayOfPrimitiveTypes.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,8 @@ | ||
// "Surround with *intArrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun foo(vararg s: Int) {} | ||
|
||
fun test() { | ||
foo(s = <caret>1) | ||
} |
8 changes: 8 additions & 0 deletions
8
...fix/surroundWithArrayOfForNamedArgumentsToVarargs/replaceToArrayOfPrimitiveTypes.kt.after
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,8 @@ | ||
// "Surround with *intArrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun foo(vararg s: Int) {} | ||
|
||
fun test() { | ||
foo(s = <caret>*intArrayOf(1)) | ||
} |
8 changes: 8 additions & 0 deletions
8
...ata/quickfix/surroundWithArrayOfForNamedArgumentsToVarargs/simpleNamedArgumentToVararg.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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun foo(vararg s: String) {} | ||
|
||
fun test() { | ||
foo(s = <caret>"value") | ||
} |
8 changes: 8 additions & 0 deletions
8
...ickfix/surroundWithArrayOfForNamedArgumentsToVarargs/simpleNamedArgumentToVararg.kt.after
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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
fun foo(vararg s: String) {} | ||
|
||
fun test() { | ||
foo(s = *arrayOf("value")) | ||
} |
8 changes: 8 additions & 0 deletions
8
...fix/surroundWithArrayOfForNamedArgumentsToVarargs/surroundWithSpreadForConstructorCall.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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
class Foo<T>(vararg val p: T) | ||
|
||
fun test() { | ||
Foo(p = 123<caret>) | ||
} |
8 changes: 8 additions & 0 deletions
8
...rroundWithArrayOfForNamedArgumentsToVarargs/surroundWithSpreadForConstructorCall.kt.after
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,8 @@ | ||
// "Surround with *arrayOf(...)" "true" | ||
// LANGUAGE_VERSION: 1.2 | ||
|
||
class Foo<T>(vararg val p: T) | ||
|
||
fun test() { | ||
Foo(p = *arrayOf(123)) | ||
} |
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