Move 'else' Branch in 'when' Quick-Fix: Convert to Kotlin & refactor
This commit is contained in:
@@ -134,8 +134,6 @@ migrate.class.object.to.companion.in.whole.project.family=Replace 'class' Keywor
|
|||||||
migrate.lambda.syntax=Migrate lambda syntax
|
migrate.lambda.syntax=Migrate lambda syntax
|
||||||
migrate.lambda.syntax.family=Migrate lambda syntax
|
migrate.lambda.syntax.family=Migrate lambda syntax
|
||||||
remove.val.var.from.parameter=Remove ''{0}'' from parameter
|
remove.val.var.from.parameter=Remove ''{0}'' from parameter
|
||||||
move.when.else.branch.to.the.end.action=Move else branch to the end
|
|
||||||
move.when.else.branch.to.the.end.family.name=Move else branch to the end
|
|
||||||
change.to.property.name.family.name=Change to property name
|
change.to.property.name.family.name=Change to property name
|
||||||
change.to.property.name.action=Change ''{0}'' to ''{1}''
|
change.to.property.name.action=Change ''{0}'' to ''{1}''
|
||||||
create.from.usage.family=Create from usage
|
create.from.usage.family=Create from usage
|
||||||
|
|||||||
@@ -222,6 +222,8 @@ fun createSpacingBuilder(settings: CodeStyleSettings): KotlinSpacingBuilder {
|
|||||||
between(VALUE_ARGUMENT_LIST, LAMBDA_ARGUMENT).spaces(1)
|
between(VALUE_ARGUMENT_LIST, LAMBDA_ARGUMENT).spaces(1)
|
||||||
betweenInside(REFERENCE_EXPRESSION, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1)
|
betweenInside(REFERENCE_EXPRESSION, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1)
|
||||||
betweenInside(TYPE_ARGUMENT_LIST, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1)
|
betweenInside(TYPE_ARGUMENT_LIST, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1)
|
||||||
|
|
||||||
|
between(WHEN_ENTRY, WHEN_ENTRY).lineBreakInCode()
|
||||||
}
|
}
|
||||||
custom {
|
custom {
|
||||||
|
|
||||||
|
|||||||
@@ -14,77 +14,46 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.jetbrains.kotlin.idea.quickfix;
|
package org.jetbrains.kotlin.idea.quickfix
|
||||||
|
|
||||||
import com.intellij.codeInsight.CodeInsightUtilCore;
|
import com.intellij.codeInsight.CodeInsightUtilCore
|
||||||
import com.intellij.openapi.editor.Editor;
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElement;
|
import com.intellij.psi.PsiFile
|
||||||
import com.intellij.psi.PsiFile;
|
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import com.intellij.util.IncorrectOperationException;
|
import org.jetbrains.kotlin.psi.KtPsiUtil
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.kotlin.psi.KtWhenEntry
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.kotlin.psi.KtWhenExpression
|
||||||
import org.jetbrains.kotlin.diagnostics.Diagnostic;
|
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle;
|
|
||||||
import org.jetbrains.kotlin.psi.*;
|
|
||||||
|
|
||||||
public class MoveWhenElseBranchFix extends KotlinQuickFixAction<KtWhenExpression> {
|
class MoveWhenElseBranchFix(element: KtWhenExpression) : KotlinQuickFixAction<KtWhenExpression>(element) {
|
||||||
public MoveWhenElseBranchFix(@NotNull KtWhenExpression element) {
|
override fun getFamilyName() = "Move else branch to the end"
|
||||||
super(element);
|
|
||||||
|
override fun getText() = familyName
|
||||||
|
|
||||||
|
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
||||||
|
return super.isAvailable(project, editor, file) && KtPsiUtil.checkWhenExpressionHasSingleElse(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||||
@Override
|
val entries = element.entries
|
||||||
public String getText() {
|
val lastEntry = entries.lastOrNull() ?: return
|
||||||
return KotlinBundle.message("move.when.else.branch.to.the.end.action");
|
val elseEntry = entries.singleOrNull { it.isElse } ?: return
|
||||||
|
|
||||||
|
val cursorOffset = editor!!.caretModel.offset - elseEntry.textOffset
|
||||||
|
|
||||||
|
val insertedBranch = element.addAfter(elseEntry, lastEntry) as KtWhenEntry
|
||||||
|
elseEntry.delete()
|
||||||
|
val insertedWhenEntry = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(insertedBranch)
|
||||||
|
|
||||||
|
editor.caretModel.moveToOffset(insertedWhenEntry.textOffset + cursorOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
@Override
|
override fun createAction(diagnostic: Diagnostic): MoveWhenElseBranchFix? {
|
||||||
public String getFamilyName() {
|
val whenExpression = diagnostic.psiElement.getNonStrictParentOfType<KtWhenExpression>() ?: return null
|
||||||
return KotlinBundle.message("move.when.else.branch.to.the.end.family.name");
|
return MoveWhenElseBranchFix(whenExpression)
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiFile file) {
|
|
||||||
if (!super.isAvailable(project, editor, file)) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return KtPsiUtil.checkWhenExpressionHasSingleElse(getElement());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void invoke(@NotNull Project project, Editor editor, @NotNull KtFile file) throws IncorrectOperationException {
|
|
||||||
KtWhenEntry elseEntry = null;
|
|
||||||
KtWhenEntry lastEntry = null;
|
|
||||||
for (KtWhenEntry entry : getElement().getEntries()) {
|
|
||||||
if (entry.isElse()) {
|
|
||||||
elseEntry = entry;
|
|
||||||
}
|
|
||||||
lastEntry = entry;
|
|
||||||
}
|
|
||||||
assert (elseEntry != null) : "isAvailable should check whether there is only one else branch";
|
|
||||||
int cursorOffset = editor.getCaretModel().getOffset() - elseEntry.getTextOffset();
|
|
||||||
|
|
||||||
PsiElement insertedBranch = getElement().addAfter(elseEntry, lastEntry);
|
|
||||||
getElement().addAfter(KtPsiFactoryKt.KtPsiFactory(file).createNewLine(), lastEntry);
|
|
||||||
getElement().deleteChildRange(elseEntry, elseEntry);
|
|
||||||
KtWhenEntry insertedWhenEntry = (KtWhenEntry) CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(insertedBranch);
|
|
||||||
|
|
||||||
editor.getCaretModel().moveToOffset(insertedWhenEntry.getTextOffset() + cursorOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static KotlinSingleIntentionActionFactory createFactory() {
|
|
||||||
return new KotlinSingleIntentionActionFactory() {
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public KotlinQuickFixAction createAction(@NotNull Diagnostic diagnostic) {
|
|
||||||
PsiElement element = diagnostic.getPsiElement();
|
|
||||||
KtWhenExpression whenExpression = PsiTreeUtil.getParentOfType(element, KtWhenExpression.class, false);
|
|
||||||
if (whenExpression == null) return null;
|
|
||||||
return new MoveWhenElseBranchFix(whenExpression);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ class QuickFixRegistrar : QuickFixContributor {
|
|||||||
|
|
||||||
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
AMBIGUOUS_ANONYMOUS_TYPE_INFERRED.registerActions(SpecifyTypeExplicitlyFix())
|
||||||
|
|
||||||
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix.createFactory())
|
ELSE_MISPLACED_IN_WHEN.registerFactory(MoveWhenElseBranchFix)
|
||||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
|
NO_ELSE_IN_WHEN.registerFactory(AddWhenElseBranchFix)
|
||||||
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
NO_ELSE_IN_WHEN.registerFactory(AddWhenRemainingBranchesFix)
|
||||||
BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)
|
BREAK_OR_CONTINUE_IN_WHEN.registerFactory(AddLoopLabelFix)
|
||||||
|
|||||||
Reference in New Issue
Block a user