Use property access syntax: fix problem text and highlight range

#KT-30910 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-04-13 00:01:18 +09:00
committed by Dmitry Gridin
parent bf5b559923
commit 864f99782e
9 changed files with 76 additions and 1 deletions
@@ -83,6 +83,8 @@ abstract class IntentionBasedInspection<TElement : PsiElement> private construct
open fun additionalFixes(element: TElement): List<LocalQuickFix>? = null
open fun inspectionTarget(element: TElement): PsiElement? = null
open fun inspectionProblemText(element: TElement): String? = null
private fun PsiElement.toRange(baseElement: PsiElement): TextRange {
val start = getStartOffsetIn(baseElement)
@@ -126,7 +128,7 @@ abstract class IntentionBasedInspection<TElement : PsiElement> private construct
if (!allFixes.isEmpty()) {
holder.registerProblemWithoutOfflineInformation(
targetElement,
problemText ?: allFixes.first().name,
inspectionProblemText(element) ?: problemText ?: allFixes.first().name,
isOnTheFly,
problemHighlightType(targetElement),
range,
@@ -79,6 +79,19 @@ class UsePropertyAccessSyntaxInspection : IntentionBasedInspection<KtCallExpress
val list = NotPropertyListPanel(fqNameList)
return LabeledComponent.create(list, "Excluded methods")
}
override fun inspectionTarget(element: KtCallExpression): PsiElement? {
return element.calleeExpression
}
override fun inspectionProblemText(element: KtCallExpression): String? {
val accessor = when (element.valueArguments.size) {
0 -> "getter"
1 -> "setter"
else -> null
}
return "Use of $accessor method instead of property access syntax"
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.UsePropertyAccessSyntaxInspection
@@ -0,0 +1,7 @@
// PROBLEM: none
// WITH_RUNTIME
import java.io.File
fun foo(file: File) {
file.getAbsolutePath(<caret>)
}
@@ -0,0 +1,7 @@
// PROBLEM: "Use of getter method instead of property access syntax"
// WITH_RUNTIME
import java.io.File
fun foo(file: File) {
file.getAbsolutePath<caret>()
}
@@ -0,0 +1,7 @@
// PROBLEM: "Use of getter method instead of property access syntax"
// WITH_RUNTIME
import java.io.File
fun foo(file: File) {
file.absolutePath
}
@@ -0,0 +1,5 @@
// PROBLEM: "Use of setter method instead of property access syntax"
// WITH_RUNTIME
fun foo(thread: Thread) {
thread.setName<caret>("name")
}
@@ -0,0 +1,5 @@
// PROBLEM: "Use of setter method instead of property access syntax"
// WITH_RUNTIME
fun foo(thread: Thread) {
thread.name = "name"
}
@@ -9816,6 +9816,34 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/usePropertyAccessSyntax")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class UsePropertyAccessSyntax extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInUsePropertyAccessSyntax() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/usePropertyAccessSyntax"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("caretOnValueArgumentList.kt")
public void testCaretOnValueArgumentList() throws Exception {
runTest("idea/testData/inspectionsLocal/usePropertyAccessSyntax/caretOnValueArgumentList.kt");
}
@TestMetadata("get.kt")
public void testGet() throws Exception {
runTest("idea/testData/inspectionsLocal/usePropertyAccessSyntax/get.kt");
}
@TestMetadata("set.kt")
public void testSet() throws Exception {
runTest("idea/testData/inspectionsLocal/usePropertyAccessSyntax/set.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/whenWithOnlyElse")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)