PackageDirectoryMismatchInspection should report when a package statement is missing
#KT-13549 Fixed
This commit is contained in:
+14
-17
@@ -6,10 +6,14 @@
|
|||||||
package org.jetbrains.kotlin.idea.refactoring.move.changePackage
|
package org.jetbrains.kotlin.idea.refactoring.move.changePackage
|
||||||
|
|
||||||
import com.intellij.CommonBundle
|
import com.intellij.CommonBundle
|
||||||
import com.intellij.codeInspection.*
|
import com.intellij.codeInspection.LocalQuickFix
|
||||||
|
import com.intellij.codeInspection.ProblemDescriptor
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType
|
||||||
|
import com.intellij.codeInspection.ProblemsHolder
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.roots.JavaProjectRootsUtil
|
import com.intellij.openapi.roots.JavaProjectRootsUtil
|
||||||
import com.intellij.openapi.ui.Messages
|
import com.intellij.openapi.ui.Messages
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiManager
|
import com.intellij.psi.PsiManager
|
||||||
import com.intellij.refactoring.PackageWrapper
|
import com.intellij.refactoring.PackageWrapper
|
||||||
import com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination
|
import com.intellij.refactoring.move.moveClassesOrPackages.AutocreatingSingleSourceRootMoveDestination
|
||||||
@@ -24,17 +28,12 @@ import org.jetbrains.kotlin.idea.refactoring.hasIdentifiersOnly
|
|||||||
import org.jetbrains.kotlin.idea.refactoring.isInjectedFragment
|
import org.jetbrains.kotlin.idea.refactoring.isInjectedFragment
|
||||||
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
import org.jetbrains.kotlin.idea.util.application.runWriteAction
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.psi.KtPackageDirective
|
import org.jetbrains.kotlin.psi.KtPackageDirective
|
||||||
import org.jetbrains.kotlin.psi.KtVisitorVoid
|
import org.jetbrains.kotlin.psi.packageDirectiveVisitor
|
||||||
|
|
||||||
class PackageDirectoryMismatchInspection : AbstractKotlinInspection() {
|
class PackageDirectoryMismatchInspection : AbstractKotlinInspection() {
|
||||||
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = packageDirectiveVisitor(fun(directive: KtPackageDirective) {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
|
|
||||||
object : KtVisitorVoid() {
|
|
||||||
override fun visitPackageDirective(directive: KtPackageDirective) {
|
|
||||||
super.visitPackageDirective(directive)
|
|
||||||
if (directive.text.isEmpty()) return
|
|
||||||
|
|
||||||
val file = directive.containingKtFile
|
val file = directive.containingKtFile
|
||||||
if (file.isInjectedFragment || file.packageMatchesDirectoryOrImplicit()) return
|
if (file.isInjectedFragment || file.packageMatchesDirectoryOrImplicit()) return
|
||||||
|
|
||||||
@@ -55,13 +54,12 @@ class PackageDirectoryMismatchInspection : AbstractKotlinInspection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
holder.registerProblem(
|
holder.registerProblem(
|
||||||
directive,
|
file,
|
||||||
|
directive.textRange,
|
||||||
"Package directive doesn't match file location",
|
"Package directive doesn't match file location",
|
||||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
|
|
||||||
*fixes.toTypedArray()
|
*fixes.toTypedArray()
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
private class MoveFileToPackageFix(val dirName: String) : LocalQuickFix {
|
private class MoveFileToPackageFix(val dirName: String) : LocalQuickFix {
|
||||||
override fun getFamilyName() = "Move file to package-matching directory"
|
override fun getFamilyName() = "Move file to package-matching directory"
|
||||||
@@ -71,8 +69,8 @@ class PackageDirectoryMismatchInspection : AbstractKotlinInspection() {
|
|||||||
override fun startInWriteAction() = false
|
override fun startInWriteAction() = false
|
||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
val directive = descriptor.psiElement as? KtPackageDirective ?: return
|
val file = descriptor.psiElement as? KtFile ?: return
|
||||||
val file = directive.containingKtFile
|
val directive = file.packageDirective ?: return
|
||||||
|
|
||||||
val sourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project)
|
val sourceRoots = JavaProjectRootsUtil.getSuitableDestinationSourceRoots(project)
|
||||||
val packageWrapper = PackageWrapper(PsiManager.getInstance(project), directive.qualifiedName)
|
val packageWrapper = PackageWrapper(PsiManager.getInstance(project), directive.qualifiedName)
|
||||||
@@ -107,8 +105,7 @@ class PackageDirectoryMismatchInspection : AbstractKotlinInspection() {
|
|||||||
override fun getName() = "Change file's package to $packageName"
|
override fun getName() = "Change file's package to $packageName"
|
||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
val directive = descriptor.psiElement as? KtPackageDirective ?: return
|
val file = descriptor.psiElement as? KtFile ?: return
|
||||||
val file = directive.containingKtFile
|
|
||||||
KotlinChangePackageRefactoring(file).run(packageFqName)
|
KotlinChangePackageRefactoring(file).run(packageFqName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
@@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
class Foo {
|
||||||
|
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
<problems>
|
||||||
|
<problem>
|
||||||
|
<file>rootPackageMatched.kt</file>
|
||||||
|
<line>1</line>
|
||||||
|
<module>testMismatchedProjectAndDirectoryRoot_MismatchedProjectAndDirectoryRoot</module>
|
||||||
|
<entry_point TYPE="file" FQNAME="rootPackageMatched.kt" />
|
||||||
|
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Package name does not match containing directory</problem_class>
|
||||||
|
<description>Package directive doesn't match file location</description>
|
||||||
|
</problem>
|
||||||
|
</problems>
|
||||||
+3
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.refactoring.move.changePackage.PackageDirectoryMismatchInspection"
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
class Foo
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<caret>
|
||||||
|
class Foo
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"mainFile": "source/test.kt",
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.refactoring.move.changePackage.PackageDirectoryMismatchInspection",
|
||||||
|
"fix": "Move file to source root"
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
package target
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
package target
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo()
|
||||||
|
foo()
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package usages
|
||||||
|
|
||||||
|
import target.foo
|
||||||
|
import target.Foo
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo()
|
||||||
|
foo()
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<caret>class Foo
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package target
|
||||||
|
|
||||||
|
import foo
|
||||||
|
import Foo
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo()
|
||||||
|
foo()
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package usages
|
||||||
|
|
||||||
|
import foo
|
||||||
|
import Foo
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo()
|
||||||
|
foo()
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"mainFile": "target/test.kt",
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.refactoring.move.changePackage.PackageDirectoryMismatchInspection",
|
||||||
|
"fix": "Change file's package to 'target'"
|
||||||
|
}
|
||||||
+5
@@ -54,6 +54,11 @@ public class MultiFileInspectionTestGenerated extends AbstractMultiFileInspectio
|
|||||||
runTest("idea/testData/multiFileInspections/mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test");
|
runTest("idea/testData/multiFileInspections/mismatchedProjectAndDirectory/mismatchedProjectAndDirectory.test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("mismatchedProjectAndDirectoryRoot/mismatchedProjectAndDirectoryRoot.test")
|
||||||
|
public void testMismatchedProjectAndDirectoryRoot_MismatchedProjectAndDirectoryRoot() throws Exception {
|
||||||
|
runTest("idea/testData/multiFileInspections/mismatchedProjectAndDirectoryRoot/mismatchedProjectAndDirectoryRoot.test");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("platformExtensionReceiverOfInline/platformExtensionReceiverOfInline.test")
|
@TestMetadata("platformExtensionReceiverOfInline/platformExtensionReceiverOfInline.test")
|
||||||
public void testPlatformExtensionReceiverOfInline_PlatformExtensionReceiverOfInline() throws Exception {
|
public void testPlatformExtensionReceiverOfInline_PlatformExtensionReceiverOfInline() throws Exception {
|
||||||
runTest("idea/testData/multiFileInspections/platformExtensionReceiverOfInline/platformExtensionReceiverOfInline.test");
|
runTest("idea/testData/multiFileInspections/platformExtensionReceiverOfInline/platformExtensionReceiverOfInline.test");
|
||||||
|
|||||||
Generated
+10
@@ -44,6 +44,11 @@ public class MultiFileLocalInspectionTestGenerated extends AbstractMultiFileLoca
|
|||||||
runTest("idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/moveToDefaultDirectory.test");
|
runTest("idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectory/moveToDefaultDirectory.test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/moveToDefaultDirectoryWithoutPackageKeyword.test")
|
||||||
|
public void testMoveFileToPackageMatchingDirectory_moveToDefaultDirectoryWithoutPackageKeyword_MoveToDefaultDirectoryWithoutPackageKeyword() throws Exception {
|
||||||
|
runTest("idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToDefaultDirectoryWithoutPackageKeyword/moveToDefaultDirectoryWithoutPackageKeyword.test");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("moveFileToPackageMatchingDirectory/moveToNonDefaultDirectory/moveToNonDefaultDirectory.test")
|
@TestMetadata("moveFileToPackageMatchingDirectory/moveToNonDefaultDirectory/moveToNonDefaultDirectory.test")
|
||||||
public void testMoveFileToPackageMatchingDirectory_moveToNonDefaultDirectory_MoveToNonDefaultDirectory() throws Exception {
|
public void testMoveFileToPackageMatchingDirectory_moveToNonDefaultDirectory_MoveToNonDefaultDirectory() throws Exception {
|
||||||
runTest("idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToNonDefaultDirectory/moveToNonDefaultDirectory.test");
|
runTest("idea/testData/multiFileLocalInspections/moveFileToPackageMatchingDirectory/moveToNonDefaultDirectory/moveToNonDefaultDirectory.test");
|
||||||
@@ -69,6 +74,11 @@ public class MultiFileLocalInspectionTestGenerated extends AbstractMultiFileLoca
|
|||||||
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/changeToNonDefaultPackage/changeToNonDefaultPackage.test");
|
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/changeToNonDefaultPackage/changeToNonDefaultPackage.test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("reconcilePackageWithDirectory/changeToNonDefaultPackageFromRoot/changeToNonDefaultPackageFromRoot.test")
|
||||||
|
public void testReconcilePackageWithDirectory_changeToNonDefaultPackageFromRoot_ChangeToNonDefaultPackageFromRoot() throws Exception {
|
||||||
|
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/changeToNonDefaultPackageFromRoot/changeToNonDefaultPackageFromRoot.test");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("reconcilePackageWithDirectory/innerClass/innerClass.test")
|
@TestMetadata("reconcilePackageWithDirectory/innerClass/innerClass.test")
|
||||||
public void testReconcilePackageWithDirectory_innerClass_InnerClass() throws Exception {
|
public void testReconcilePackageWithDirectory_innerClass_InnerClass() throws Exception {
|
||||||
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/innerClass/innerClass.test");
|
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/innerClass/innerClass.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user