Support for implicit package prefixes

Now, if all Kotlin files directly under a source root have the same
package statement, it's used as an implicit package prefix for that
source root. It's honored by the package name/directory name mismatch
inspection and by new file creation actions;
support in refactorings is TODO.

 #KT-17306 Fixed
This commit is contained in:
Dmitry Jemerov
2018-03-06 14:01:06 +01:00
parent 753319a89d
commit 81adbcb7e6
8 changed files with 281 additions and 97 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2015 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.
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.core
@@ -20,6 +9,8 @@ import com.intellij.psi.JavaDirectoryService
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiPackage
import org.jetbrains.kotlin.idea.caches.PerModulePackageCacheService
import org.jetbrains.kotlin.idea.util.sourceRoot
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
@@ -30,4 +21,16 @@ fun PsiFile.getFqNameByDirectory(): FqName {
return qualifiedNameByDirectory?.let(::FqName) ?: FqName.ROOT
}
fun KtFile.packageMatchesDirectory(): Boolean = packageFqName == getFqNameByDirectory()
fun PsiDirectory.getFqNameWithImplicitPrefix(): FqName {
val packageFqName = getPackage()?.qualifiedName?.let(::FqName) ?: FqName.ROOT
sourceRoot?.let {
val implicitPrefix = PerModulePackageCacheService.getInstance(project).getImplicitPackagePrefix(it)
return FqName.fromSegments((implicitPrefix.pathSegments() + packageFqName.pathSegments()).map { it.asString() })
}
return packageFqName
}
fun KtFile.packageMatchesDirectory(): Boolean = packageFqName == getFqNameByDirectory()
fun KtFile.packageMatchesDirectoryOrImplicit() =
packageFqName == getFqNameByDirectory() || packageFqName == parent?.getFqNameWithImplicitPrefix()