Introduce Variable: Do not replace assignment left-hand sides

#KT-14240 Fixed
This commit is contained in:
Alexey Sedunov
2016-10-12 11:42:45 +03:00
parent ec00b9f3ea
commit 5c2ad48375
11 changed files with 98 additions and 0 deletions
@@ -320,11 +320,18 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
return commonContainer.allChildren.lastOrNull { it.textRange.contains(startOffset) } ?: return null
}
private fun PsiElement.isAssignmentLHS(): Boolean {
return parents.any { KtPsiUtil.isAssignment(it) && (it as KtBinaryExpression).left == this }
}
private fun KtExpression.findOccurrences(occurrenceContainer: PsiElement): List<KtExpression> {
return toRange()
.match(occurrenceContainer, KotlinPsiUnifier.DEFAULT)
.mapNotNull {
val candidate = it.range.elements.first()
if (candidate.isAssignmentLHS()) return@mapNotNull null
when (candidate) {
is KtExpression -> candidate
is KtStringTemplateEntryWithExpression -> candidate.expression
@@ -694,6 +701,10 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
val expression = expressionToExtract?.let { KtPsiUtil.safeDeparenthesize(it) }
?: return showErrorHint(project, editor, KotlinRefactoringBundle.message("cannot.refactor.no.expression"))
if (expression.isAssignmentLHS()) {
return showErrorHint(project, editor, KotlinRefactoringBundle.message("cannot.refactor.no.expression"))
}
val physicalExpression = expression.substringContextOrThis
val resolutionFacade = physicalExpression.getResolutionFacade()