Apply "unnamed boolean literal" to idea & J2K + other style fixes
This commit is contained in:
+11
-3
@@ -85,7 +85,7 @@ object SourceNavigationHelper {
|
||||
private fun Collection<GlobalSearchScope>.union(): List<GlobalSearchScope> =
|
||||
if (this.isNotEmpty()) listOf(GlobalSearchScope.union(this.toTypedArray())) else emptyList()
|
||||
|
||||
private fun haveRenamesInImports(files: Collection<KtFile>) = files.any { it.importDirectives.any { it.aliasName != null } }
|
||||
private fun haveRenamesInImports(files: Collection<KtFile>) = files.any { file -> file.importDirectives.any { it.aliasName != null } }
|
||||
|
||||
private fun findSpecialProperty(memberName: Name, containingClass: KtClass): KtNamedDeclaration? {
|
||||
// property constructor parameters
|
||||
@@ -191,7 +191,7 @@ object SourceNavigationHelper {
|
||||
): T? {
|
||||
val classFqName = entity.fqName ?: return null
|
||||
return targetScopes(entity, navigationKind).firstNotNullResult { scope ->
|
||||
index.get(classFqName.asString(), entity.project, scope).sortedBy { it.isExpectDeclaration() }.firstOrNull()
|
||||
index.get(classFqName.asString(), entity.project, scope).minBy { it.isExpectDeclaration() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,7 +273,15 @@ object SourceNavigationHelper {
|
||||
SourceNavigationHelper.NavigationKind.SOURCES_TO_CLASS_FILES -> {
|
||||
val file = from.containingFile
|
||||
if (file is KtFile && file.isCompiled) return from
|
||||
if (!ProjectRootsUtil.isInContent(from, false, true, false, true, false)) return from
|
||||
if (!ProjectRootsUtil.isInContent(
|
||||
from,
|
||||
includeProjectSource = false,
|
||||
includeLibrarySource = true,
|
||||
includeLibraryClasses = false,
|
||||
includeScriptDependencies = true,
|
||||
includeScriptsOutsideSourceRoots = false
|
||||
)
|
||||
) return from
|
||||
if (KtPsiUtil.isLocal(from)) return from
|
||||
}
|
||||
}
|
||||
|
||||
+81
-22
@@ -85,50 +85,109 @@ class KotlinSourceFilterScope private constructor(
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun sourcesAndLibraries(delegate: GlobalSearchScope, project: Project) = create(delegate, true, true, true, true, true, project)
|
||||
fun sourcesAndLibraries(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = true,
|
||||
includeLibrarySourceFiles = true,
|
||||
includeClassFiles = true,
|
||||
includeScriptDependencies = true,
|
||||
includeScriptsOutsideSourceRoots = true,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun sourceAndClassFiles(delegate: GlobalSearchScope, project: Project) = create(delegate, true, false, true, true, true, project)
|
||||
fun sourceAndClassFiles(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = true,
|
||||
includeLibrarySourceFiles = false,
|
||||
includeClassFiles = true,
|
||||
includeScriptDependencies = true,
|
||||
includeScriptsOutsideSourceRoots = true,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun projectSourceAndClassFiles(delegate: GlobalSearchScope, project: Project) = create(delegate, true, false, true, false, true, project)
|
||||
fun projectSourceAndClassFiles(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = true,
|
||||
includeLibrarySourceFiles = false,
|
||||
includeClassFiles = true,
|
||||
includeScriptDependencies = false,
|
||||
includeScriptsOutsideSourceRoots = true,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun projectSources(delegate: GlobalSearchScope, project: Project) = create(delegate, true, false, false, false, true, project)
|
||||
fun projectSources(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = true,
|
||||
includeLibrarySourceFiles = false,
|
||||
includeClassFiles = false,
|
||||
includeScriptDependencies = false,
|
||||
includeScriptsOutsideSourceRoots = true,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun librarySources(delegate: GlobalSearchScope, project: Project) = create(delegate, false, true, false, true, false, project)
|
||||
fun librarySources(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = false,
|
||||
includeLibrarySourceFiles = true,
|
||||
includeClassFiles = false,
|
||||
includeScriptDependencies = true,
|
||||
includeScriptsOutsideSourceRoots = false,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun libraryClassFiles(delegate: GlobalSearchScope, project: Project) = create(delegate, false, false, true, true, false, project)
|
||||
fun libraryClassFiles(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = false,
|
||||
includeLibrarySourceFiles = false,
|
||||
includeClassFiles = true,
|
||||
includeScriptDependencies = true,
|
||||
includeScriptsOutsideSourceRoots = false,
|
||||
project = project
|
||||
)
|
||||
|
||||
@JvmStatic
|
||||
fun projectAndLibrariesSources(delegate: GlobalSearchScope, project: Project) = create(delegate, true, true, false, false, true, project)
|
||||
fun projectAndLibrariesSources(delegate: GlobalSearchScope, project: Project) = create(
|
||||
delegate,
|
||||
includeProjectSourceFiles = true,
|
||||
includeLibrarySourceFiles = true,
|
||||
includeClassFiles = false,
|
||||
includeScriptDependencies = false,
|
||||
includeScriptsOutsideSourceRoots = true,
|
||||
project = project
|
||||
)
|
||||
|
||||
private fun create(
|
||||
delegate: GlobalSearchScope,
|
||||
includeProjectSourceFiles: Boolean,
|
||||
includeLibrarySourceFiles: Boolean,
|
||||
includeClassFiles: Boolean,
|
||||
includeScriptDependencies: Boolean,
|
||||
includeScriptsOutsideSourceRoots: Boolean,
|
||||
project: Project
|
||||
delegate: GlobalSearchScope,
|
||||
includeProjectSourceFiles: Boolean,
|
||||
includeLibrarySourceFiles: Boolean,
|
||||
includeClassFiles: Boolean,
|
||||
includeScriptDependencies: Boolean,
|
||||
includeScriptsOutsideSourceRoots: Boolean,
|
||||
project: Project
|
||||
): GlobalSearchScope {
|
||||
if (delegate === GlobalSearchScope.EMPTY_SCOPE) return delegate
|
||||
|
||||
if (delegate is KotlinSourceFilterScope) {
|
||||
return KotlinSourceFilterScope(
|
||||
delegate.myBaseScope,
|
||||
includeProjectSourceFiles = delegate.includeProjectSourceFiles && includeProjectSourceFiles,
|
||||
includeLibrarySourceFiles = delegate.includeLibrarySourceFiles && includeLibrarySourceFiles,
|
||||
includeClassFiles = delegate.includeClassFiles && includeClassFiles,
|
||||
includeScriptDependencies = delegate.includeScriptDependencies && includeScriptDependencies,
|
||||
includeScriptsOutsideSourceRoots = delegate.includeScriptsOutsideSourceRoots && includeScriptsOutsideSourceRoots,
|
||||
project = project
|
||||
delegate.myBaseScope,
|
||||
includeProjectSourceFiles = delegate.includeProjectSourceFiles && includeProjectSourceFiles,
|
||||
includeLibrarySourceFiles = delegate.includeLibrarySourceFiles && includeLibrarySourceFiles,
|
||||
includeClassFiles = delegate.includeClassFiles && includeClassFiles,
|
||||
includeScriptDependencies = delegate.includeScriptDependencies && includeScriptDependencies,
|
||||
includeScriptsOutsideSourceRoots = delegate.includeScriptsOutsideSourceRoots && includeScriptsOutsideSourceRoots,
|
||||
project = project
|
||||
)
|
||||
}
|
||||
|
||||
return KotlinSourceFilterScope(delegate, includeProjectSourceFiles, includeLibrarySourceFiles, includeClassFiles, includeScriptDependencies, includeScriptsOutsideSourceRoots, project)
|
||||
return KotlinSourceFilterScope(
|
||||
delegate, includeProjectSourceFiles, includeLibrarySourceFiles, includeClassFiles,
|
||||
includeScriptDependencies, includeScriptsOutsideSourceRoots, project
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
-12
@@ -87,8 +87,7 @@ class GroovyBuildScriptManipulator(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
val applyPluginDirective = getGroovyApplyPluginDirective(kotlinPluginName)
|
||||
if (!containsDirective(scriptFile.text, applyPluginDirective)) {
|
||||
val apply = GroovyPsiElementFactory.getInstance(scriptFile.project).createExpressionFromText(applyPluginDirective)
|
||||
@@ -246,11 +245,11 @@ class GroovyBuildScriptManipulator(
|
||||
.getBlockOrCreate("resolutionStrategy")
|
||||
.getBlockOrCreate("eachPlugin")
|
||||
.addLastStatementInBlockIfNeeded(
|
||||
"""
|
||||
if (requested.id.id == "$pluginId") {
|
||||
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${'$'}{requested.version}")
|
||||
}
|
||||
""".trimIndent()
|
||||
"""
|
||||
if (requested.id.id == "$pluginId") {
|
||||
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${'$'}{requested.version}")
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -415,9 +414,9 @@ class GroovyBuildScriptManipulator(
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val VERSION_TEMPLATE = "\$VERSION$"
|
||||
private const val VERSION_TEMPLATE = "\$VERSION$"
|
||||
private val VERSION = String.format("ext.kotlin_version = '%s'", VERSION_TEMPLATE)
|
||||
private val GRADLE_PLUGIN_ID = "kotlin-gradle-plugin"
|
||||
private const val GRADLE_PLUGIN_ID = "kotlin-gradle-plugin"
|
||||
private val CLASSPATH = "classpath \"$KOTLIN_GROUP_ID:$GRADLE_PLUGIN_ID:\$kotlin_version\""
|
||||
|
||||
private fun PsiElement.getBlockByName(name: String): GrClosableBlock? {
|
||||
@@ -454,13 +453,13 @@ class GroovyBuildScriptManipulator(
|
||||
}
|
||||
|
||||
fun GrClosableBlock.addLastExpressionInBlockIfNeeded(expressionText: String): Boolean =
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, false, false)
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, isStatement = false, isFirst = false)
|
||||
|
||||
fun GrClosableBlock.addLastStatementInBlockIfNeeded(expressionText: String): Boolean =
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, true, false)
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, isStatement = true, isFirst = false)
|
||||
|
||||
private fun GrClosableBlock.addFirstExpressionInBlockIfNeeded(expressionText: String): Boolean =
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, false, true)
|
||||
addExpressionOrStatementInBlockIfNeeded(expressionText, isStatement = false, isFirst = true)
|
||||
|
||||
private fun GrClosableBlock.addExpressionOrStatementInBlockIfNeeded(text: String, isStatement: Boolean, isFirst: Boolean): Boolean {
|
||||
if (statements.any { StringUtil.equalsIgnoreWhitespaces(it.text, text) }) return false
|
||||
|
||||
+10
-3
@@ -56,7 +56,9 @@ class CodeFragmentCompiler(private val executionContext: ExecutionContext) {
|
||||
return runReadAction { doCompile(codeFragment, bindingContext, moduleDescriptor) }
|
||||
}
|
||||
|
||||
private fun doCompile(codeFragment: KtCodeFragment, bindingContext: BindingContext, moduleDescriptor: ModuleDescriptor): CompilationResult {
|
||||
private fun doCompile(
|
||||
codeFragment: KtCodeFragment, bindingContext: BindingContext, moduleDescriptor: ModuleDescriptor
|
||||
): CompilationResult {
|
||||
require(codeFragment is KtBlockCodeFragment || codeFragment is KtExpressionCodeFragment) {
|
||||
"Unsupported code fragment type: $codeFragment"
|
||||
}
|
||||
@@ -111,7 +113,7 @@ class CodeFragmentCompiler(private val executionContext: ExecutionContext) {
|
||||
}
|
||||
|
||||
val ownerClassName = typeMapper.mapOwner(parameter.targetDescriptor).internalName
|
||||
val lastDollarIndex = ownerClassName.lastIndexOf('$').takeIf { it >= 0} ?: continue
|
||||
val lastDollarIndex = ownerClassName.lastIndexOf('$').takeIf { it >= 0 } ?: continue
|
||||
result[parameter.dumb] = ownerClassName.drop(lastDollarIndex)
|
||||
}
|
||||
|
||||
@@ -176,7 +178,12 @@ class CodeFragmentCompiler(private val executionContext: ExecutionContext) {
|
||||
val parameters = parameterInfo.parameters.mapIndexed { index, parameter ->
|
||||
ValueParameterDescriptorImpl(
|
||||
methodDescriptor, null, index, Annotations.EMPTY, Name.identifier("p$index"),
|
||||
parameter.targetType, false, false, false, null, SourceElement.NO_SOURCE
|
||||
parameter.targetType,
|
||||
declaresDefaultValue = false,
|
||||
isCrossinline = false,
|
||||
isNoinline = false,
|
||||
varargElementType = null,
|
||||
source = SourceElement.NO_SOURCE
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+27
-18
@@ -37,11 +37,11 @@ import kotlin.reflect.KClass
|
||||
// TODO: need to manage resources here, i.e. call replCompiler.dispose when engine is collected
|
||||
|
||||
class KotlinJsr223JvmScriptEngine4Idea(
|
||||
factory: ScriptEngineFactory,
|
||||
templateClasspath: List<File>,
|
||||
templateClassName: String,
|
||||
private val getScriptArgs: (ScriptContext, Array<out KClass<out Any>>?) -> ScriptArgsWithTypes?,
|
||||
private val scriptArgsTypes: Array<out KClass<out Any>>?
|
||||
factory: ScriptEngineFactory,
|
||||
templateClasspath: List<File>,
|
||||
templateClassName: String,
|
||||
private val getScriptArgs: (ScriptContext, Array<out KClass<out Any>>?) -> ScriptArgsWithTypes?,
|
||||
private val scriptArgsTypes: Array<out KClass<out Any>>?
|
||||
) : KotlinJsr223JvmScriptEngineBase(factory) {
|
||||
|
||||
private val daemon by lazy {
|
||||
@@ -53,28 +53,37 @@ class KotlinJsr223JvmScriptEngine4Idea(
|
||||
|
||||
val daemonReportMessages = arrayListOf<DaemonReportMessage>()
|
||||
|
||||
KotlinCompilerClient.connectToCompileService(compilerId, daemonJVMOptions, daemonOptions, DaemonReportingTargets(null, daemonReportMessages), true, true)
|
||||
?: throw ScriptException("Unable to connect to repl server:" + daemonReportMessages.joinToString("\n ", prefix = "\n ") { "${it.category.name} ${it.message}" })
|
||||
KotlinCompilerClient.connectToCompileService(
|
||||
compilerId, daemonJVMOptions, daemonOptions,
|
||||
DaemonReportingTargets(null, daemonReportMessages),
|
||||
autostart = true, checkId = true
|
||||
) ?: throw ScriptException(
|
||||
"Unable to connect to repl server:" + daemonReportMessages.joinToString("\n ", prefix = "\n ") {
|
||||
"${it.category.name} ${it.message}"
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private val messageCollector = MyMessageCollector()
|
||||
|
||||
override val replCompiler: ReplCompiler by lazy {
|
||||
daemon.let {
|
||||
KotlinRemoteReplCompilerClient(it,
|
||||
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
|
||||
CompileService.TargetPlatform.JVM,
|
||||
emptyArray(),
|
||||
messageCollector,
|
||||
templateClasspath,
|
||||
templateClassName)
|
||||
}
|
||||
KotlinRemoteReplCompilerClient(
|
||||
daemon,
|
||||
makeAutodeletingFlagFile("idea-jsr223-repl-session"),
|
||||
CompileService.TargetPlatform.JVM,
|
||||
emptyArray(),
|
||||
messageCollector,
|
||||
templateClasspath,
|
||||
templateClassName
|
||||
)
|
||||
}
|
||||
|
||||
override fun overrideScriptArgs(context: ScriptContext): ScriptArgsWithTypes? =
|
||||
getScriptArgs(getContext(), scriptArgsTypes)
|
||||
getScriptArgs(getContext(), scriptArgsTypes)
|
||||
|
||||
private val localEvaluator: ReplFullEvaluator by lazy { GenericReplCompilingEvaluator(replCompiler, templateClasspath, Thread.currentThread().contextClassLoader) }
|
||||
private val localEvaluator: ReplFullEvaluator by lazy {
|
||||
GenericReplCompilingEvaluator(replCompiler, templateClasspath, Thread.currentThread().contextClassLoader)
|
||||
}
|
||||
|
||||
override val replEvaluator: ReplFullEvaluator get() = localEvaluator
|
||||
|
||||
|
||||
@@ -113,7 +113,7 @@ fun provideTypeHint(element: KtCallableDeclaration, offset: Int): List<InlayInfo
|
||||
}
|
||||
append(getInlayHintsTypeRenderer(element.analyze(), element).renderType(type))
|
||||
}
|
||||
listOf(InlayInfo(declString, offset, false, true, true))
|
||||
listOf(InlayInfo(declString, offset, isShowOnlyIfExistedBefore = false, isFilterByBlacklist = true, relatesToPrecedingText = true))
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ private fun getActualTargetList(annotated: PsiTarget): AnnotationChecker.Compani
|
||||
else -> T_MEMBER_FUNCTION
|
||||
}
|
||||
is PsiExpression -> T_EXPRESSION
|
||||
is PsiField -> T_MEMBER_PROPERTY(true, false)
|
||||
is PsiField -> T_MEMBER_PROPERTY(backingField = true, delegate = false)
|
||||
is PsiLocalVariable -> T_LOCAL_VARIABLE
|
||||
is PsiParameter -> T_VALUE_PARAMETER_WITHOUT_VAL
|
||||
else -> EMPTY
|
||||
|
||||
+1
-1
@@ -130,7 +130,7 @@ abstract class TypeInfo(val variance: Variance) {
|
||||
return Collections.singletonList(callableBuilder.currentFileModule.builtIns.anyType)
|
||||
}
|
||||
val scope = getScopeForTypeApproximation(callableBuilder.config, callableBuilder.placement)
|
||||
val approximations = getResolvableApproximations(scope, false, true)
|
||||
val approximations = getResolvableApproximations(scope, checkTypeParameters = false, allowIntersections = true)
|
||||
return when (variance) {
|
||||
Variance.IN_VARIANCE -> approximations.toList()
|
||||
else -> listOf(approximations.firstOrNull() ?: this)
|
||||
|
||||
@@ -125,8 +125,8 @@ internal class ChangeMethodParameters(
|
||||
ValueParameterDescriptorImpl(
|
||||
this, null, index, Annotations.EMPTY,
|
||||
Name.identifier(parameter.name),
|
||||
parameter.ktType, false,
|
||||
false, false, null, SourceElement.NO_SOURCE
|
||||
parameter.ktType, declaresDefaultValue = false,
|
||||
isCrossinline = false, isNoinline = false, varargElementType = null, source = SourceElement.NO_SOURCE
|
||||
)
|
||||
},
|
||||
functionDescriptor.returnType,
|
||||
|
||||
+6
-1
@@ -210,7 +210,12 @@ class KotlinChangeSignatureUsageProcessor : ChangeSignatureUsageProcessor {
|
||||
val result = LinkedHashSet<PsiReference>()
|
||||
|
||||
val searchScope = functionPsi.useScope
|
||||
val options = KotlinReferencesSearchOptions(true, false, false, false)
|
||||
val options = KotlinReferencesSearchOptions(
|
||||
acceptCallableOverrides = true,
|
||||
acceptOverloads = false,
|
||||
acceptExtensionsOfDeclarationClass = false,
|
||||
acceptCompanionObjectMembers = false
|
||||
)
|
||||
val parameters = KotlinReferencesSearchParameters(functionPsi, searchScope, false, null, options)
|
||||
result.addAll(ReferencesSearch.search(parameters).findAll())
|
||||
if (functionPsi is KtProperty || functionPsi is KtParameter) {
|
||||
|
||||
+10
-11
@@ -37,14 +37,14 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
|
||||
class KotlinAwareDelegatingMoveDestination(
|
||||
private val delegate: MoveDestination,
|
||||
private val targetPackage: PsiPackage?,
|
||||
private val targetDirectory: PsiDirectory?
|
||||
private val delegate: MoveDestination,
|
||||
private val targetPackage: PsiPackage?,
|
||||
private val targetDirectory: PsiDirectory?
|
||||
) : MoveDestination by delegate {
|
||||
override fun analyzeModuleConflicts(
|
||||
elements: MutableCollection<PsiElement>,
|
||||
conflicts: MultiMap<PsiElement, String>,
|
||||
usages: Array<out UsageInfo>
|
||||
elements: MutableCollection<PsiElement>,
|
||||
conflicts: MultiMap<PsiElement, String>,
|
||||
usages: Array<out UsageInfo>
|
||||
) {
|
||||
delegate.analyzeModuleConflicts(elements, conflicts, usages)
|
||||
|
||||
@@ -71,7 +71,7 @@ class KotlinAwareDelegatingMoveDestination(
|
||||
super.visitElement(element)
|
||||
}
|
||||
}
|
||||
filesToProcess.flatMap {it.declarations}.forEach { it.accept(extraElementCollector) }
|
||||
filesToProcess.flatMap { it.declarations }.forEach { it.accept(extraElementCollector) }
|
||||
|
||||
val progressIndicator = ProgressManager.getInstance().progressIndicator!!
|
||||
progressIndicator.pushState()
|
||||
@@ -80,13 +80,12 @@ class KotlinAwareDelegatingMoveDestination(
|
||||
try {
|
||||
progressIndicator.text = "Looking for Usages"
|
||||
for ((index, element) in extraElementsForReferenceSearch.withIndex()) {
|
||||
progressIndicator.fraction = (index + 1)/extraElementsForReferenceSearch.size.toDouble()
|
||||
progressIndicator.fraction = (index + 1) / extraElementsForReferenceSearch.size.toDouble()
|
||||
ReferencesSearch.search(element, projectScope).mapNotNullTo(extraUsages) { ref ->
|
||||
createMoveUsageInfoIfPossible(ref, element, true, false)
|
||||
createMoveUsageInfoIfPossible(ref, element, addImportToOriginalFile = true, isInternal = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
progressIndicator.popState()
|
||||
}
|
||||
|
||||
|
||||
+31
-34
@@ -81,8 +81,7 @@ interface Mover : (KtNamedDeclaration, KtElement) -> KtNamedDeclaration {
|
||||
val container = originalElement.containingClassOrObject
|
||||
if (container is KtObjectDeclaration && container.isCompanion() && container.declarations.singleOrNull() == originalElement) {
|
||||
container.deleteSingle()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
originalElement.deleteSingle()
|
||||
}
|
||||
}
|
||||
@@ -142,11 +141,12 @@ private object ElementHashingStrategy : TObjectHashingStrategy<PsiElement> {
|
||||
}
|
||||
|
||||
class MoveKotlinDeclarationsProcessor(
|
||||
val descriptor: MoveDeclarationsDescriptor,
|
||||
val mover: Mover = Mover.Default) : BaseRefactoringProcessor(descriptor.project) {
|
||||
val descriptor: MoveDeclarationsDescriptor,
|
||||
val mover: Mover = Mover.Default
|
||||
) : BaseRefactoringProcessor(descriptor.project) {
|
||||
companion object {
|
||||
private val REFACTORING_NAME = "Move declarations"
|
||||
val REFACTORING_ID = "move.kotlin.declarations"
|
||||
private const val REFACTORING_NAME = "Move declarations"
|
||||
const val REFACTORING_ID = "move.kotlin.declarations"
|
||||
}
|
||||
|
||||
val project get() = descriptor.project
|
||||
@@ -155,8 +155,8 @@ class MoveKotlinDeclarationsProcessor(
|
||||
private val moveEntireFile = descriptor.moveSource is MoveSource.File
|
||||
private val elementsToMove = descriptor.moveSource.elementsToMove.filter { e -> e.parent != descriptor.moveTarget.getTargetPsiIfExists(e) }
|
||||
private val kotlinToLightElementsBySourceFile = elementsToMove
|
||||
.groupBy { it.containingKtFile }
|
||||
.mapValues { it.value.keysToMap { it.toLightElements().ifEmpty { listOf(it) } } }
|
||||
.groupBy { it.containingKtFile }
|
||||
.mapValues { it.value.keysToMap { it.toLightElements().ifEmpty { listOf(it) } } }
|
||||
private val conflicts = MultiMap<PsiElement, String>()
|
||||
|
||||
override fun getRefactoringId() = REFACTORING_ID
|
||||
@@ -207,23 +207,23 @@ class MoveKotlinDeclarationsProcessor(
|
||||
|
||||
val foundReferences = HashSet<PsiReference>()
|
||||
val results = ReferencesSearch
|
||||
.search(lightElement, searchScope)
|
||||
.mapNotNullTo(ArrayList()) { ref ->
|
||||
if (foundReferences.add(ref) && elementsToMove.none { it.isAncestor(ref.element)}) {
|
||||
createMoveUsageInfoIfPossible(ref, lightElement, true, false)
|
||||
}
|
||||
else null
|
||||
.search(lightElement, searchScope)
|
||||
.mapNotNullTo(ArrayList()) { ref ->
|
||||
if (foundReferences.add(ref) && elementsToMove.none { it.isAncestor(ref.element)}) {
|
||||
createMoveUsageInfoIfPossible(ref, lightElement, addImportToOriginalFile = true, isInternal = false)
|
||||
}
|
||||
else null
|
||||
}
|
||||
|
||||
val name = lightElement.getKotlinFqName()?.quoteIfNeeded()?.asString()
|
||||
if (name != null) {
|
||||
TextOccurrencesUtil.findNonCodeUsages(
|
||||
lightElement,
|
||||
name,
|
||||
descriptor.searchInCommentsAndStrings,
|
||||
descriptor.searchInNonCode,
|
||||
FqName(newFqName).quoteIfNeeded().asString(),
|
||||
results
|
||||
lightElement,
|
||||
name,
|
||||
descriptor.searchInCommentsAndStrings,
|
||||
descriptor.searchInNonCode,
|
||||
FqName(newFqName).quoteIfNeeded().asString(),
|
||||
results
|
||||
)
|
||||
}
|
||||
|
||||
@@ -237,11 +237,11 @@ class MoveKotlinDeclarationsProcessor(
|
||||
|
||||
val usages = ArrayList<UsageInfo>()
|
||||
val conflictChecker = MoveConflictChecker(
|
||||
project,
|
||||
elementsToMove,
|
||||
descriptor.moveTarget,
|
||||
elementsToMove.first(),
|
||||
allElementsToMove = descriptor.allElementsToMove
|
||||
project,
|
||||
elementsToMove,
|
||||
descriptor.moveTarget,
|
||||
elementsToMove.first(),
|
||||
allElementsToMove = descriptor.allElementsToMove
|
||||
)
|
||||
for ((sourceFile, kotlinToLightElements) in kotlinToLightElementsBySourceFile) {
|
||||
val internalUsages = LinkedHashSet<UsageInfo>()
|
||||
@@ -249,12 +249,11 @@ class MoveKotlinDeclarationsProcessor(
|
||||
|
||||
if (moveEntireFile) {
|
||||
val changeInfo = ContainerChangeInfo(
|
||||
ContainerInfo.Package(sourceFile.packageFqName),
|
||||
descriptor.moveTarget.targetContainerFqName?.let { ContainerInfo.Package(it) } ?: ContainerInfo.UnknownPackage
|
||||
ContainerInfo.Package(sourceFile.packageFqName),
|
||||
descriptor.moveTarget.targetContainerFqName?.let { ContainerInfo.Package(it) } ?: ContainerInfo.UnknownPackage
|
||||
)
|
||||
internalUsages += sourceFile.getInternalReferencesToUpdateOnPackageNameChange(changeInfo)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
kotlinToLightElements.keys.forEach {
|
||||
val packageNameInfo = descriptor.delegate.getContainerChangeInfo(it, descriptor.moveTarget)
|
||||
internalUsages += it.getInternalReferencesToUpdateOnPackageNameChange(packageNameInfo)
|
||||
@@ -284,7 +283,7 @@ class MoveKotlinDeclarationsProcessor(
|
||||
internal fun doPerformRefactoring(usages: List<UsageInfo>) {
|
||||
fun moveDeclaration(declaration: KtNamedDeclaration, moveTarget: KotlinMoveTarget): KtNamedDeclaration {
|
||||
val targetContainer = moveTarget.getOrCreateTargetPsi(declaration)
|
||||
?: throw AssertionError("Couldn't create Kotlin file for: ${declaration::class.java}: ${declaration.text}")
|
||||
?: throw AssertionError("Couldn't create Kotlin file for: ${declaration::class.java}: ${declaration.text}")
|
||||
descriptor.delegate.preprocessDeclaration(descriptor, declaration)
|
||||
if (moveEntireFile) return declaration
|
||||
return mover(declaration, targetContainer).apply {
|
||||
@@ -341,12 +340,10 @@ class MoveKotlinDeclarationsProcessor(
|
||||
usagesToProcess += newInternalUsages
|
||||
|
||||
nonCodeUsages = postProcessMoveUsages(usagesToProcess, oldToNewElementsMapping).toTypedArray()
|
||||
}
|
||||
catch (e: IncorrectOperationException) {
|
||||
} catch (e: IncorrectOperationException) {
|
||||
nonCodeUsages = null
|
||||
RefactoringUIUtil.processIncorrectOperation(myProject, e)
|
||||
}
|
||||
finally {
|
||||
} finally {
|
||||
cleanUpInternalUsages(newInternalUsages + oldInternalUsages)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
fun invokeMoveFilesOrDirectoriesRefactoring(
|
||||
moveDialog: KotlinAwareMoveFilesOrDirectoriesDialog?,
|
||||
project: Project,
|
||||
elements: Array<out PsiElement>,
|
||||
initialTargetDirectory: PsiDirectory?,
|
||||
moveCallback: MoveCallback?
|
||||
moveDialog: KotlinAwareMoveFilesOrDirectoriesDialog?,
|
||||
project: Project,
|
||||
elements: Array<out PsiElement>,
|
||||
initialTargetDirectory: PsiDirectory?,
|
||||
moveCallback: MoveCallback?
|
||||
) {
|
||||
fun closeDialog() {
|
||||
moveDialog?.close(DialogWrapper.CANCEL_EXIT_CODE)
|
||||
@@ -55,19 +55,19 @@ fun invokeMoveFilesOrDirectoriesRefactoring(
|
||||
try {
|
||||
val choice = if (elements.size > 1 || elements[0] is PsiDirectory) intArrayOf(-1) else null
|
||||
val elementsToMove = elements
|
||||
.filterNot {
|
||||
it is PsiFile
|
||||
&& runWriteAction { CopyFilesOrDirectoriesHandler.checkFileExist(selectedDir, choice, it, it.name, "Move") }
|
||||
.filterNot {
|
||||
it is PsiFile
|
||||
&& runWriteAction { CopyFilesOrDirectoriesHandler.checkFileExist(selectedDir, choice, it, it.name, "Move") }
|
||||
}
|
||||
.sortedWith( // process Kotlin files first so that light classes are updated before changing references in Java files
|
||||
java.util.Comparator { o1, o2 ->
|
||||
when {
|
||||
o1 is KtElement && o2 !is KtElement -> -1
|
||||
o1 !is KtElement && o2 is KtElement -> 1
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
.sortedWith( // process Kotlin files first so that light classes are updated before changing references in Java files
|
||||
java.util.Comparator { o1, o2 ->
|
||||
when {
|
||||
o1 is KtElement && o2 !is KtElement -> -1
|
||||
o1 !is KtElement && o2 is KtElement -> 1
|
||||
else -> 0
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
elementsToMove.forEach {
|
||||
MoveFilesOrDirectoriesUtil.checkMove(it, selectedDir)
|
||||
@@ -79,22 +79,20 @@ fun invokeMoveFilesOrDirectoriesRefactoring(
|
||||
if (elementsToMove.isNotEmpty()) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val processor = KotlinAwareMoveFilesOrDirectoriesProcessor(
|
||||
project,
|
||||
elementsToMove as List<KtFile>,
|
||||
selectedDir,
|
||||
moveDialog?.searchReferences ?: true,
|
||||
false,
|
||||
false,
|
||||
moveCallback,
|
||||
Runnable(::closeDialog)
|
||||
project,
|
||||
elementsToMove as List<KtFile>,
|
||||
selectedDir,
|
||||
searchReferences = moveDialog?.searchReferences ?: true,
|
||||
searchInComments = false,
|
||||
searchInNonJavaFiles = false,
|
||||
moveCallback = moveCallback,
|
||||
prepareSuccessfulCallback = Runnable(::closeDialog)
|
||||
)
|
||||
processor.run()
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
closeDialog()
|
||||
}
|
||||
}
|
||||
catch (e: IncorrectOperationException) {
|
||||
} catch (e: IncorrectOperationException) {
|
||||
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.message, "refactoring.moveFile", project)
|
||||
}
|
||||
}
|
||||
@@ -102,12 +100,12 @@ fun invokeMoveFilesOrDirectoriesRefactoring(
|
||||
|
||||
// Mostly copied from MoveFilesOrDirectoriesUtil.doMove()
|
||||
fun moveFilesOrDirectories(
|
||||
project: Project,
|
||||
elements: Array<PsiElement>,
|
||||
targetElement: PsiElement?,
|
||||
moveCallback: MoveCallback? = null
|
||||
project: Project,
|
||||
elements: Array<PsiElement>,
|
||||
targetElement: PsiElement?,
|
||||
moveCallback: MoveCallback? = null
|
||||
) {
|
||||
elements.forEach { if (it !is PsiFile && it !is PsiDirectory) throw IllegalArgumentException("unexpected element type: " + it) }
|
||||
elements.forEach { if (it !is PsiFile && it !is PsiDirectory) throw IllegalArgumentException("unexpected element type: $it") }
|
||||
|
||||
val targetDirectory = MoveFilesOrDirectoriesUtil.resolveToDirectory(project, targetElement)
|
||||
if (targetElement != null && targetDirectory == null) return
|
||||
|
||||
@@ -185,7 +185,7 @@ fun KtElement.processInternalReferencesToUpdateOnPackageNameChange(
|
||||
val declarationNotNull = declaration ?: return null
|
||||
|
||||
if (isExtension || containerFqName != null || isImported) return {
|
||||
createMoveUsageInfoIfPossible(it.mainReference, declarationNotNull, false, true)
|
||||
createMoveUsageInfoIfPossible(it.mainReference, declarationNotNull, addImportToOriginalFile = false, isInternal = true)
|
||||
}
|
||||
|
||||
return null
|
||||
|
||||
@@ -33,19 +33,20 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
|
||||
fun wrapOrSkip(s: String, inCode: Boolean) = if (inCode) "<code>$s</code>" else s
|
||||
|
||||
fun formatClassDescriptor(classDescriptor: DeclarationDescriptor) = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.render(classDescriptor)
|
||||
fun formatClassDescriptor(classDescriptor: DeclarationDescriptor) =
|
||||
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_NO_ANNOTATIONS.render(classDescriptor)
|
||||
|
||||
fun formatPsiClass(
|
||||
psiClass: PsiClass,
|
||||
markAsJava: Boolean,
|
||||
inCode: Boolean
|
||||
psiClass: PsiClass,
|
||||
markAsJava: Boolean,
|
||||
inCode: Boolean
|
||||
): String {
|
||||
var description: String
|
||||
|
||||
val kind = if (psiClass.isInterface) "interface " else "class "
|
||||
description = kind + PsiFormatUtil.formatClass(
|
||||
psiClass,
|
||||
PsiFormatUtilBase.SHOW_CONTAINING_CLASS or PsiFormatUtilBase.SHOW_NAME or PsiFormatUtilBase.SHOW_PARAMETERS or PsiFormatUtilBase.SHOW_TYPE
|
||||
psiClass,
|
||||
PsiFormatUtilBase.SHOW_CONTAINING_CLASS or PsiFormatUtilBase.SHOW_NAME or PsiFormatUtilBase.SHOW_PARAMETERS or PsiFormatUtilBase.SHOW_TYPE
|
||||
)
|
||||
description = wrapOrSkip(description, inCode)
|
||||
|
||||
@@ -56,8 +57,7 @@ fun formatClass(classDescriptor: DeclarationDescriptor, inCode: Boolean): String
|
||||
val element = DescriptorToSourceUtils.descriptorToDeclaration(classDescriptor)
|
||||
return if (element is PsiClass) {
|
||||
formatPsiClass(element, false, inCode)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
wrapOrSkip(formatClassDescriptor(classDescriptor), inCode)
|
||||
}
|
||||
}
|
||||
@@ -66,8 +66,7 @@ fun formatFunction(functionDescriptor: DeclarationDescriptor, inCode: Boolean):
|
||||
val element = DescriptorToSourceUtils.descriptorToDeclaration(functionDescriptor)
|
||||
return if (element is PsiMethod) {
|
||||
formatPsiMethod(element, false, inCode)
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
wrapOrSkip(formatFunctionDescriptor(functionDescriptor), inCode)
|
||||
}
|
||||
}
|
||||
@@ -75,9 +74,9 @@ fun formatFunction(functionDescriptor: DeclarationDescriptor, inCode: Boolean):
|
||||
private fun formatFunctionDescriptor(functionDescriptor: DeclarationDescriptor) = DescriptorRenderer.COMPACT.render(functionDescriptor)
|
||||
|
||||
fun formatPsiMethod(
|
||||
psiMethod: PsiMethod,
|
||||
showContainingClass: Boolean,
|
||||
inCode: Boolean
|
||||
psiMethod: PsiMethod,
|
||||
showContainingClass: Boolean,
|
||||
inCode: Boolean
|
||||
): String {
|
||||
var options = PsiFormatUtilBase.SHOW_NAME or PsiFormatUtilBase.SHOW_PARAMETERS or PsiFormatUtilBase.SHOW_TYPE
|
||||
if (showContainingClass) {
|
||||
@@ -95,9 +94,8 @@ fun formatJavaOrLightMethod(method: PsiMethod): String {
|
||||
val originalDeclaration = method.unwrapped
|
||||
return if (originalDeclaration is KtDeclaration) {
|
||||
formatFunctionDescriptor(originalDeclaration.unsafeResolveToDescriptor())
|
||||
}
|
||||
else {
|
||||
formatPsiMethod(method, false, false)
|
||||
} else {
|
||||
formatPsiMethod(method, showContainingClass = false, inCode = false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user