From 1444919460f351f8fd71ee62e387a276484c32fc Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 19 Nov 2018 14:37:03 +0700 Subject: [PATCH] ReplaceWithTernaryOperatorFix: disable in batch mode; check document (IDEA-CR-40010) --- .../dataFlow/DataFlowInspectionBase.java | 6 +++--- .../ReplaceWithTernaryOperatorFix.java | 14 ++++++++++---- .../dataFlow/DataFlowInspection.java | 8 +++++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java index db457dcedd87..58532139130f 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java @@ -227,7 +227,7 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool return Collections.emptyList(); } - protected List createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef) { + protected List createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef, boolean onTheFly) { return Collections.emptyList(); } @@ -482,7 +482,7 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool }); NullabilityProblemKind.callMethodRefNPE.ifMyProblem(problem, methodRef -> holder.registerProblem(methodRef, InspectionsBundle.message("dataflow.message.npe.methodref.invocation"), - createMethodReferenceNPEFixes(methodRef).toArray(LocalQuickFix.EMPTY_ARRAY))); + createMethodReferenceNPEFixes(methodRef, holder.isOnTheFly()).toArray(LocalQuickFix.EMPTY_ARRAY))); NullabilityProblemKind.callNPE.ifMyProblem(problem, call -> reportCallMayProduceNpe(holder, problem.getMessage(expressions), call)); NullabilityProblemKind.passingNullableToNotNullParameter.ifMyProblem(problem, expr -> reportNullableArgument(holder, expr, expressions)); NullabilityProblemKind.arrayAccessNPE.ifMyProblem(problem, expression -> { @@ -881,7 +881,7 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool if (anchor.getParent() instanceof PsiMethodReferenceExpression) { PsiMethodReferenceExpression methodRef = (PsiMethodReferenceExpression)anchor.getParent(); holder.registerProblem(methodRef, InspectionsBundle.message("dataflow.message.passing.nullable.argument.methodref"), - createMethodReferenceNPEFixes(methodRef).toArray(LocalQuickFix.EMPTY_ARRAY)); + createMethodReferenceNPEFixes(methodRef, holder.isOnTheFly()).toArray(LocalQuickFix.EMPTY_ARRAY)); } else { PsiExpression expression = PsiUtil.skipParenthesizedExprDown((PsiExpression)anchor); diff --git a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java index 1345f75dcd76..72bd11ae2ba9 100644 --- a/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java +++ b/java/java-impl/src/com/intellij/codeInspection/ReplaceWithTernaryOperatorFix.java @@ -18,6 +18,8 @@ package com.intellij.codeInspection; import com.intellij.codeInsight.template.TemplateBuilder; import com.intellij.codeInsight.template.TemplateBuilderFactory; import com.intellij.codeInsight.template.impl.ConstantNode; +import com.intellij.lang.injection.InjectedLanguageManager; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.fileEditor.FileEditorManager; import com.intellij.openapi.project.Project; @@ -83,10 +85,14 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix { Project project = file.getProject(); Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor(); if (editor != null) { - PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument()); - TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(elseExpression); - builder.replaceElement(elseExpression, new ConstantNode(elseExpression.getText())); - builder.run(editor, true); + Document document = editor.getDocument(); + PsiFile topLevelFile = InjectedLanguageManager.getInstance(project).getTopLevelFile(file); + if (topLevelFile != null && document == topLevelFile.getViewProvider().getDocument()) { + PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(document); + TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(elseExpression); + builder.replaceElement(elseExpression, new ConstantNode(elseExpression.getText())); + builder.run(editor, true); + } } } } diff --git a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java index 253c2c0d80a2..1f7767b08124 100644 --- a/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java +++ b/java/java-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspection.java @@ -109,10 +109,12 @@ public class DataFlowInspection extends DataFlowInspectionBase { @NotNull @Override - protected List createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef) { + protected List createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef, boolean onTheFly) { List fixes = new ArrayList<>(); ContainerUtil.addIfNotNull(fixes, StreamFilterNotNullFix.makeFix(methodRef)); - fixes.add(new ReplaceWithTernaryOperatorFix.ReplaceMethodRefWithTernaryOperatorFix()); + if (onTheFly) { + fixes.add(new ReplaceWithTernaryOperatorFix.ReplaceMethodRefWithTernaryOperatorFix()); + } return fixes; } @@ -153,7 +155,7 @@ public class DataFlowInspection extends DataFlowInspectionBase { fixes.add(new SurroundWithIfFix(qualifier)); } - if (ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) { + if (onTheFly && ReplaceWithTernaryOperatorFix.isAvailable(qualifier, expression)) { fixes.add(new ReplaceWithTernaryOperatorFix(qualifier)); } }