Reformat: KotlinInlineValHandler

This commit is contained in:
Mikhail Glukhikh
2018-05-08 15:39:05 +03:00
parent 5246c54dad
commit de85235bd7
@@ -47,7 +47,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHandler() { class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHandler() {
constructor(): this(withPrompt = true) constructor() : this(withPrompt = true)
override fun isEnabledForLanguage(l: Language) = l == KotlinLanguage.INSTANCE override fun isEnabledForLanguage(l: Language) = l == KotlinLanguage.INSTANCE
@@ -92,8 +92,7 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
readReplacement = buildCodeToInline(declaration, descriptor.type, isTypeExplicit, initialization.value, false, editor) ?: return readReplacement = buildCodeToInline(declaration, descriptor.type, isTypeExplicit, initialization.value, false, editor) ?: return
writeReplacement = null writeReplacement = null
assignmentToDelete = initialization.assignment assignmentToDelete = initialization.assignment
} } else {
else {
readReplacement = getter?.let { readReplacement = getter?.let {
buildCodeToInline(getter, descriptor.type, isTypeExplicit, getter.bodyExpression!!, getter.hasBlockBody(), editor) ?: return buildCodeToInline(getter, descriptor.type, isTypeExplicit, getter.bodyExpression!!, getter.hasBlockBody(), editor) ?: return
} }
@@ -111,8 +110,7 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
project.checkConflictsInteractively(conflictsCopy) { project.checkConflictsInteractively(conflictsCopy) {
performRefactoring(declaration, readReplacement, writeReplacement, assignmentToDelete, editor, hasHighlightings) performRefactoring(declaration, readReplacement, writeReplacement, assignmentToDelete, editor, hasHighlightings)
} }
} } else {
else {
performRefactoring(declaration, readReplacement, writeReplacement, assignmentToDelete, editor, hasHighlightings) performRefactoring(declaration, readReplacement, writeReplacement, assignmentToDelete, editor, hasHighlightings)
} }
} }
@@ -145,10 +143,10 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
private data class Initialization(val value: KtExpression, val assignment: KtBinaryExpression?) private data class Initialization(val value: KtExpression, val assignment: KtBinaryExpression?)
private fun extractInitialization( private fun extractInitialization(
declaration: KtProperty, declaration: KtProperty,
referenceExpressions: Collection<KtExpression>, referenceExpressions: Collection<KtExpression>,
project: Project, project: Project,
editor: Editor? editor: Editor?
): Initialization? { ): Initialization? {
val writeUsages = referenceExpressions.filter { it.readWriteAccess(useResolveForReadWrite = true) != ReferenceAccess.READ } val writeUsages = referenceExpressions.filter { it.readWriteAccess(useResolveForReadWrite = true) != ReferenceAccess.READ }
@@ -159,11 +157,10 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
return null return null
} }
return Initialization(initializerInDeclaration, assignment = null) return Initialization(initializerInDeclaration, assignment = null)
} } else {
else {
val assignment = writeUsages.singleOrNull() val assignment = writeUsages.singleOrNull()
?.getAssignmentByLHS() ?.getAssignmentByLHS()
?.takeIf { it.operationToken == KtTokens.EQ } ?.takeIf { it.operationToken == KtTokens.EQ }
val initializer = assignment?.right val initializer = assignment?.right
if (initializer == null) { if (initializer == null) {
reportAmbiguousAssignment(project, editor, declaration.name!!, writeUsages) reportAmbiguousAssignment(project, editor, declaration.name!!, writeUsages)
@@ -174,12 +171,12 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
} }
private fun performRefactoring( private fun performRefactoring(
declaration: KtProperty, declaration: KtProperty,
readReplacement: CodeToInline?, readReplacement: CodeToInline?,
writeReplacement: CodeToInline?, writeReplacement: CodeToInline?,
assignmentToDelete: KtBinaryExpression?, assignmentToDelete: KtBinaryExpression?,
editor: Editor?, editor: Editor?,
hasHighlightings: Boolean hasHighlightings: Boolean
) { ) {
val replacementStrategy = PropertyUsageReplacementStrategy(readReplacement, writeReplacement) val replacementStrategy = PropertyUsageReplacementStrategy(readReplacement, writeReplacement)
val reference = editor?.findSimpleNameReference() val reference = editor?.findSimpleNameReference()
@@ -192,8 +189,7 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
val statusBar = WindowManager.getInstance().getStatusBar(declaration.project) val statusBar = WindowManager.getInstance().getStatusBar(declaration.project)
statusBar?.info = RefactoringBundle.message("press.escape.to.remove.the.highlighting") statusBar?.info = RefactoringBundle.message("press.escape.to.remove.the.highlighting")
} }
} } else {
else {
dialog.doAction() dialog.doAction()
} }
} }
@@ -205,7 +201,13 @@ class KotlinInlineValHandler(private val withPrompt: Boolean) : InlineActionHand
} }
private fun showErrorHint(project: Project, editor: Editor?, message: String) { private fun showErrorHint(project: Project, editor: Editor?, message: String) {
CommonRefactoringUtil.showErrorHint(project, editor, message, RefactoringBundle.message("inline.variable.title"), HelpID.INLINE_VARIABLE) CommonRefactoringUtil.showErrorHint(
project,
editor,
message,
RefactoringBundle.message("inline.variable.title"),
HelpID.INLINE_VARIABLE
)
} }
} }