From 66795db042289f5d3db129d5988e09911ae9957a Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 19 Jun 2019 21:22:53 +0700 Subject: [PATCH] PackageDirectoryMismatchInspection: fix visibility in editor #KT-31816 Fixed --- .../changePackage/PackageDirectoryMismatchInspection.kt | 7 ++++++- .../mismatchedProjectAndDirectoryRoot/expected.xml | 2 +- .../moveToDefaultDirectory/after/test.kt | 2 ++ .../moveToDefaultDirectory/before/source/test.kt | 2 ++ .../after/test.kt | 1 + .../before/source/test.kt | 5 +++-- 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/changePackage/PackageDirectoryMismatchInspection.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/changePackage/PackageDirectoryMismatchInspection.kt index 98b444e101d..5868f612327 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/move/changePackage/PackageDirectoryMismatchInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/move/changePackage/PackageDirectoryMismatchInspection.kt @@ -12,6 +12,7 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.openapi.project.Project import com.intellij.openapi.roots.JavaProjectRootsUtil import com.intellij.openapi.ui.Messages +import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiManager import com.intellij.refactoring.PackageWrapper import com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination @@ -29,6 +30,7 @@ import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtPackageDirective import org.jetbrains.kotlin.psi.packageDirectiveVisitor +import org.jetbrains.kotlin.psi.psiUtil.startOffset class PackageDirectoryMismatchInspection : AbstractKotlinInspection() { override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = packageDirectiveVisitor(fun(directive: KtPackageDirective) { @@ -51,9 +53,12 @@ class PackageDirectoryMismatchInspection : AbstractKotlinInspection() { fixes += ChangePackageFix("'${fqNameWithImplicitPrefix.asString()}'", fqNameWithImplicitPrefix) } + val textRange = if (directive.textLength != 0) directive.textRange else file.declarations.firstOrNull()?.let { + TextRange.from(it.startOffset, 1) + } holder.registerProblem( file, - directive.textRange, + textRange, "Package directive doesn't match file location", *fixes.toTypedArray() ) diff --git a/idea/testData/multiFileInspections/mismatchedProjectAndDirectoryRoot/expected.xml b/idea/testData/multiFileInspections/mismatchedProjectAndDirectoryRoot/expected.xml index 0ffbf4c15d9..66e2729d4e3 100644 --- a/idea/testData/multiFileInspections/mismatchedProjectAndDirectoryRoot/expected.xml +++ b/idea/testData/multiFileInspections/mismatchedProjectAndDirectoryRoot/expected.xml @@ -1,7 +1,7 @@ rootPackageMatched.kt - 1 + 2 testMismatchedProjectAndDirectoryRoot_MismatchedProjectAndDirectoryRoot Package name does not match containing directory diff --git a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/after/test.kt b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/after/test.kt index fc74dec1f81..6f8d6734d78 100644 --- a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/after/test.kt +++ b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/after/test.kt @@ -1,3 +1,5 @@ +// FIX: Move file to source root + package class Foo diff --git a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/before/source/test.kt b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/before/source/test.kt index 061c23d4f1e..6797d8b8f1e 100644 --- a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/before/source/test.kt +++ b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/before/source/test.kt @@ -1,3 +1,5 @@ +// FIX: Move file to source root + package class Foo diff --git a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/after/test.kt b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/after/test.kt index 3409dddcd69..2a8a7ef85e7 100644 --- a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/after/test.kt +++ b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/after/test.kt @@ -1,3 +1,4 @@ +// FIX: Move file to source root class Foo diff --git a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/before/source/test.kt b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/before/source/test.kt index 9ebdad4a93c..a78a998dd03 100644 --- a/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/before/source/test.kt +++ b/idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/before/source/test.kt @@ -1,5 +1,6 @@ - -class Foo +// FIX: Move file to source root + +class Foo fun foo() {