Moved and renamed classes
This commit is contained in:
@@ -324,22 +324,3 @@ public object KotlinNameSuggester {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
+27
-8
@@ -14,12 +14,11 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.refactoring
|
||||
package org.jetbrains.kotlin.idea.core
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
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.psi.*
|
||||
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.scopes.JetScope
|
||||
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 anchor: PsiElement?,
|
||||
private val target: NameValidatorImpl.Target
|
||||
private val target: NewDeclarationNameValidator.Target
|
||||
) : (String) -> Boolean {
|
||||
public enum class Target {
|
||||
FUNCTIONS_AND_CLASSES,
|
||||
PROPERTIES
|
||||
VARIABLES,
|
||||
FUNCTIONS_AND_CLASSES
|
||||
}
|
||||
|
||||
override fun invoke(name: String): Boolean {
|
||||
@@ -57,7 +76,7 @@ public class NameValidatorImpl(
|
||||
|
||||
private fun JetScope.hasConflict(name: Name): Boolean {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -66,7 +85,7 @@ public class NameValidatorImpl(
|
||||
if (getNameAsName() != name) return false
|
||||
if (this is JetCallableDeclaration && getReceiverTypeReference() != null) return false
|
||||
return when(target) {
|
||||
Target.PROPERTIES -> this is JetVariableDeclaration
|
||||
Target.VARIABLES -> this is JetVariableDeclaration
|
||||
Target.FUNCTIONS_AND_CLASSES -> this is JetNamedFunction || this is JetClassOrObject
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -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.imports.importableFqNameSafe
|
||||
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.introduce.extractionEngine.AnalysisResult.ErrorMessage
|
||||
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>()!!,
|
||||
originalElements.firstOrNull(),
|
||||
NameValidatorImpl.Target.PROPERTIES
|
||||
NewDeclarationNameValidator.Target.VARIABLES
|
||||
)
|
||||
|
||||
for ((descriptorToExtract, parameter) in extractedDescriptorToParameter) {
|
||||
@@ -909,10 +909,10 @@ private fun ExtractionData.suggestFunctionNames(returnType: JetType): List<Strin
|
||||
val functionNames = LinkedHashSet<String>()
|
||||
|
||||
val validator =
|
||||
NameValidatorImpl(
|
||||
NewDeclarationNameValidator(
|
||||
targetSibling.getParent(),
|
||||
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()) {
|
||||
functionNames.addAll(KotlinNameSuggester.suggestNamesByType(returnType, validator))
|
||||
|
||||
+3
-3
@@ -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.replaced
|
||||
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.Initializer
|
||||
import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.OutputValue.Jump
|
||||
@@ -278,7 +278,7 @@ private fun makeCall(
|
||||
controlFlow.outputValueBoxer.getUnboxingExpressions(callText)
|
||||
}
|
||||
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()
|
||||
block.addBefore(psiFactory.createDeclaration("val $resultVal = $callText"), anchorInBlock)
|
||||
block.addBefore(newLine, anchorInBlock)
|
||||
@@ -512,7 +512,7 @@ fun ExtractionGeneratorConfiguration.generateDeclaration(
|
||||
|
||||
val (defaultExpression, expressionToUnifyWith) =
|
||||
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 newDecl = body.addBefore(psiFactory.createDeclaration("val $resultVal = ${lastExpression!!.getText()}"), lastExpression) as JetProperty
|
||||
body.addBefore(psiFactory.createNewLine(), lastExpression)
|
||||
|
||||
+2
-2
@@ -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.KotlinNameSuggester
|
||||
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.changeSignature.*
|
||||
import org.jetbrains.kotlin.idea.refactoring.introduce.KotlinIntroduceHandlerBase
|
||||
@@ -234,7 +234,7 @@ public open class KotlinIntroduceParameterHandler(
|
||||
is JetClass -> targetParent.getBody()
|
||||
else -> null
|
||||
} ?: 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 parametersUsages = findInternalUsagesOfParametersAndReceiver(targetParent, functionDescriptor)
|
||||
|
||||
+3
-3
@@ -44,7 +44,7 @@ import org.jetbrains.kotlin.idea.core.CorePackage;
|
||||
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester;
|
||||
import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention;
|
||||
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.JetRefactoringUtil;
|
||||
import org.jetbrains.kotlin.idea.refactoring.introduce.IntroducePackage;
|
||||
@@ -193,10 +193,10 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
|
||||
|
||||
PsiElement commonParent = PsiTreeUtil.findCommonParent(allReplaces);
|
||||
PsiElement commonContainer = getContainer(commonParent);
|
||||
NameValidatorImpl validator = new NameValidatorImpl(
|
||||
NewDeclarationNameValidator validator = new NewDeclarationNameValidator(
|
||||
commonContainer,
|
||||
calculateAnchor(commonParent, commonContainer, allReplaces),
|
||||
NameValidatorImpl.Target.PROPERTIES
|
||||
NewDeclarationNameValidator.Target.VARIABLES
|
||||
);
|
||||
final Collection<String> suggestedNames = KotlinNameSuggester.INSTANCE$.suggestNamesByExpressionAndType(
|
||||
expression, ResolvePackage.analyze(expression, BodyResolveMode.PARTIAL), validator, "value");
|
||||
|
||||
Reference in New Issue
Block a user