Moved and renamed classes

This commit is contained in:
Valentin Kipyatkov
2015-06-26 21:29:48 +03:00
parent de2378f909
commit abb729b2ed
6 changed files with 40 additions and 40 deletions
@@ -324,22 +324,3 @@ public object KotlinNameSuggester {
return lexer.getTokenType() == null return lexer.getTokenType() == null
} }
} }
public class CollectingNameValidator @jvmOverloads constructor(
existingNames: Collection<String> = Collections.emptySet(),
private val filter: (String) -> Boolean = { true }
): (String) -> Boolean {
private val existingNames = HashSet(existingNames)
override fun invoke(name: String): Boolean {
if (name !in existingNames && filter(name)) {
existingNames.add(name)
return true
}
return false
}
public fun addName(name: String) {
existingNames.add(name)
}
}
@@ -14,12 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.idea.refactoring package org.jetbrains.kotlin.idea.core
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.allChildren import org.jetbrains.kotlin.psi.psiUtil.allChildren
@@ -30,15 +29,35 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.JetScope import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.util.*
public class NameValidatorImpl( public class CollectingNameValidator @jvmOverloads constructor(
existingNames: Collection<String> = Collections.emptySet(),
private val filter: (String) -> Boolean = { true }
): (String) -> Boolean {
private val existingNames = HashSet(existingNames)
override fun invoke(name: String): Boolean {
if (name !in existingNames && filter(name)) {
existingNames.add(name)
return true
}
return false
}
public fun addName(name: String) {
existingNames.add(name)
}
}
public class NewDeclarationNameValidator(
private val container: PsiElement, private val container: PsiElement,
private val anchor: PsiElement?, private val anchor: PsiElement?,
private val target: NameValidatorImpl.Target private val target: NewDeclarationNameValidator.Target
) : (String) -> Boolean { ) : (String) -> Boolean {
public enum class Target { public enum class Target {
FUNCTIONS_AND_CLASSES, VARIABLES,
PROPERTIES FUNCTIONS_AND_CLASSES
} }
override fun invoke(name: String): Boolean { override fun invoke(name: String): Boolean {
@@ -57,7 +76,7 @@ public class NameValidatorImpl(
private fun JetScope.hasConflict(name: Name): Boolean { private fun JetScope.hasConflict(name: Name): Boolean {
return when(target) { return when(target) {
Target.PROPERTIES -> getProperties(name).any { !it.isExtension } || getLocalVariable(name) != null Target.VARIABLES -> getProperties(name).any { !it.isExtension } || getLocalVariable(name) != null
Target.FUNCTIONS_AND_CLASSES -> getFunctions(name).any { !it.isExtension } || getClassifier(name) != null Target.FUNCTIONS_AND_CLASSES -> getFunctions(name).any { !it.isExtension } || getClassifier(name) != null
} }
} }
@@ -66,7 +85,7 @@ public class NameValidatorImpl(
if (getNameAsName() != name) return false if (getNameAsName() != name) return false
if (this is JetCallableDeclaration && getReceiverTypeReference() != null) return false if (this is JetCallableDeclaration && getReceiverTypeReference() != null) return false
return when(target) { return when(target) {
Target.PROPERTIES -> this is JetVariableDeclaration Target.VARIABLES -> this is JetVariableDeclaration
Target.FUNCTIONS_AND_CLASSES -> this is JetNamedFunction || this is JetClassOrObject Target.FUNCTIONS_AND_CLASSES -> this is JetNamedFunction || this is JetClassOrObject
} }
} }
@@ -50,7 +50,7 @@ import org.jetbrains.kotlin.idea.core.refactoring.createTempCopy
import org.jetbrains.kotlin.idea.core.refactoring.getContextForContainingDeclarationBody import org.jetbrains.kotlin.idea.core.refactoring.getContextForContainingDeclarationBody
import org.jetbrains.kotlin.idea.imports.importableFqNameSafe import org.jetbrains.kotlin.idea.imports.importableFqNameSafe
import org.jetbrains.kotlin.idea.kdoc.getResolutionScope import org.jetbrains.kotlin.idea.kdoc.getResolutionScope
import org.jetbrains.kotlin.idea.refactoring.NameValidatorImpl import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.ErrorMessage import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.ErrorMessage
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.Status import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.AnalysisResult.Status
@@ -743,10 +743,10 @@ private fun ExtractionData.inferParametersInfo(
} }
} }
val varNameValidator = NameValidatorImpl( val varNameValidator = NewDeclarationNameValidator(
commonParent.getNonStrictParentOfType<JetExpression>()!!, commonParent.getNonStrictParentOfType<JetExpression>()!!,
originalElements.firstOrNull(), originalElements.firstOrNull(),
NameValidatorImpl.Target.PROPERTIES NewDeclarationNameValidator.Target.VARIABLES
) )
for ((descriptorToExtract, parameter) in extractedDescriptorToParameter) { for ((descriptorToExtract, parameter) in extractedDescriptorToParameter) {
@@ -909,10 +909,10 @@ private fun ExtractionData.suggestFunctionNames(returnType: JetType): List<Strin
val functionNames = LinkedHashSet<String>() val functionNames = LinkedHashSet<String>()
val validator = val validator =
NameValidatorImpl( NewDeclarationNameValidator(
targetSibling.getParent(), targetSibling.getParent(),
if (targetSibling is JetClassInitializer) targetSibling.getParent() else targetSibling, if (targetSibling is JetClassInitializer) targetSibling.getParent() else targetSibling,
if (options.extractAsProperty) NameValidatorImpl.Target.PROPERTIES else NameValidatorImpl.Target.FUNCTIONS_AND_CLASSES if (options.extractAsProperty) NewDeclarationNameValidator.Target.VARIABLES else NewDeclarationNameValidator.Target.FUNCTIONS_AND_CLASSES
) )
if (!returnType.isDefault()) { if (!returnType.isDefault()) {
functionNames.addAll(KotlinNameSuggester.suggestNamesByType(returnType, validator)) functionNames.addAll(KotlinNameSuggester.suggestNamesByType(returnType, validator))
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.refactoring.isMultiLine import org.jetbrains.kotlin.idea.core.refactoring.isMultiLine
import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.intentions.ConvertToExpressionBodyIntention import org.jetbrains.kotlin.idea.intentions.ConvertToExpressionBodyIntention
import org.jetbrains.kotlin.idea.refactoring.NameValidatorImpl import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.ExpressionValue import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.ExpressionValue
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.Initializer import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.Initializer
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.Jump import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.Jump
@@ -278,7 +278,7 @@ private fun makeCall(
controlFlow.outputValueBoxer.getUnboxingExpressions(callText) controlFlow.outputValueBoxer.getUnboxingExpressions(callText)
} }
else { else {
val varNameValidator = NameValidatorImpl(block, anchorInBlock, NameValidatorImpl.Target.PROPERTIES) val varNameValidator = NewDeclarationNameValidator(block, anchorInBlock, NewDeclarationNameValidator.Target.VARIABLES)
val resultVal = KotlinNameSuggester.suggestNamesByType(extractableDescriptor.returnType, varNameValidator, null).first() val resultVal = KotlinNameSuggester.suggestNamesByType(extractableDescriptor.returnType, varNameValidator, null).first()
block.addBefore(psiFactory.createDeclaration("val $resultVal = $callText"), anchorInBlock) block.addBefore(psiFactory.createDeclaration("val $resultVal = $callText"), anchorInBlock)
block.addBefore(newLine, anchorInBlock) block.addBefore(newLine, anchorInBlock)
@@ -512,7 +512,7 @@ fun ExtractionGeneratorConfiguration.generateDeclaration(
val (defaultExpression, expressionToUnifyWith) = val (defaultExpression, expressionToUnifyWith) =
if (!generatorOptions.inTempFile && defaultValue != null && descriptor.controlFlow.outputValueBoxer.boxingRequired && lastExpression!!.isMultiLine()) { if (!generatorOptions.inTempFile && defaultValue != null && descriptor.controlFlow.outputValueBoxer.boxingRequired && lastExpression!!.isMultiLine()) {
val varNameValidator = NameValidatorImpl(body, lastExpression, NameValidatorImpl.Target.PROPERTIES) val varNameValidator = NewDeclarationNameValidator(body, lastExpression, NewDeclarationNameValidator.Target.VARIABLES)
val resultVal = KotlinNameSuggester.suggestNamesByType(defaultValue.valueType, varNameValidator, null).first() val resultVal = KotlinNameSuggester.suggestNamesByType(defaultValue.valueType, varNameValidator, null).first()
val newDecl = body.addBefore(psiFactory.createDeclaration("val $resultVal = ${lastExpression!!.getText()}"), lastExpression) as JetProperty val newDecl = body.addBefore(psiFactory.createDeclaration("val $resultVal = ${lastExpression!!.getText()}"), lastExpression) as JetProperty
body.addBefore(psiFactory.createNewLine(), lastExpression) body.addBefore(psiFactory.createNewLine(), lastExpression)
@@ -41,7 +41,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.core.getResolutionScope import org.jetbrains.kotlin.idea.core.getResolutionScope
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.refactoring.runRefactoringWithPostprocessing import org.jetbrains.kotlin.idea.core.refactoring.runRefactoringWithPostprocessing
import org.jetbrains.kotlin.idea.refactoring.NameValidatorImpl import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle
import org.jetbrains.kotlin.idea.refactoring.changeSignature.* import org.jetbrains.kotlin.idea.refactoring.changeSignature.*
import org.jetbrains.kotlin.idea.refactoring.introduce.KotlinIntroduceHandlerBase import org.jetbrains.kotlin.idea.refactoring.introduce.KotlinIntroduceHandlerBase
@@ -234,7 +234,7 @@ public open class KotlinIntroduceParameterHandler(
is JetClass -> targetParent.getBody() is JetClass -> targetParent.getBody()
else -> null else -> null
} ?: throw AssertionError("Body element is not found: ${targetParent.getElementTextWithContext()}") } ?: throw AssertionError("Body element is not found: ${targetParent.getElementTextWithContext()}")
val nameValidator = NameValidatorImpl(body, null, NameValidatorImpl.Target.PROPERTIES) val nameValidator = NewDeclarationNameValidator(body, null, NewDeclarationNameValidator.Target.VARIABLES)
val suggestedNames = KotlinNameSuggester.suggestNamesByType(replacementType, nameValidator, "p") val suggestedNames = KotlinNameSuggester.suggestNamesByType(replacementType, nameValidator, "p")
val parametersUsages = findInternalUsagesOfParametersAndReceiver(targetParent, functionDescriptor) val parametersUsages = findInternalUsagesOfParametersAndReceiver(targetParent, functionDescriptor)
@@ -44,7 +44,7 @@ import org.jetbrains.kotlin.idea.core.CorePackage;
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester; import org.jetbrains.kotlin.idea.core.KotlinNameSuggester;
import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention; import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention;
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention; import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention;
import org.jetbrains.kotlin.idea.refactoring.NameValidatorImpl; import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator;
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle; import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle;
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringUtil; import org.jetbrains.kotlin.idea.refactoring.JetRefactoringUtil;
import org.jetbrains.kotlin.idea.refactoring.introduce.IntroducePackage; import org.jetbrains.kotlin.idea.refactoring.introduce.IntroducePackage;
@@ -193,10 +193,10 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
PsiElement commonParent = PsiTreeUtil.findCommonParent(allReplaces); PsiElement commonParent = PsiTreeUtil.findCommonParent(allReplaces);
PsiElement commonContainer = getContainer(commonParent); PsiElement commonContainer = getContainer(commonParent);
NameValidatorImpl validator = new NameValidatorImpl( NewDeclarationNameValidator validator = new NewDeclarationNameValidator(
commonContainer, commonContainer,
calculateAnchor(commonParent, commonContainer, allReplaces), calculateAnchor(commonParent, commonContainer, allReplaces),
NameValidatorImpl.Target.PROPERTIES NewDeclarationNameValidator.Target.VARIABLES
); );
final Collection<String> suggestedNames = KotlinNameSuggester.INSTANCE$.suggestNamesByExpressionAndType( final Collection<String> suggestedNames = KotlinNameSuggester.INSTANCE$.suggestNamesByExpressionAndType(
expression, ResolvePackage.analyze(expression, BodyResolveMode.PARTIAL), validator, "value"); expression, ResolvePackage.analyze(expression, BodyResolveMode.PARTIAL), validator, "value");