@Override
  public void invoke(final @NotNull Project project, final Editor editor, final PsiFile file) {
    PsiInstanceOfExpression instanceOfExpression = getInstanceOfExpressionAtCaret(file, editor.getCaretModel().getOffset());
    assert instanceOfExpression.getContainingFile() == file : instanceOfExpression.getContainingFile() + "; file="+file;
    try {
      final PsiStatement statementInside = isNegated(instanceOfExpression) ? null : getExpressionStatementInside(editor, instanceOfExpression.getOperand());
      PsiDeclarationStatement decl = createLocalVariableDeclaration(instanceOfExpression, statementInside);
      if (decl == null) return;
      decl = (PsiDeclarationStatement)CodeStyleManager.getInstance(project).reformat(decl);
      decl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(decl);
      if (decl == null) return;

      PsiLocalVariable localVariable = (PsiLocalVariable)decl.getDeclaredElements()[0];
      PsiExpression initializer = Objects.requireNonNull(localVariable.getInitializer());
      List<String> names = new VariableNameGenerator(initializer, VariableKind.LOCAL_VARIABLE).byExpression(initializer)
        .byType(localVariable.getType()).generateAll(true);
      PsiIdentifier identifier = Objects.requireNonNull(localVariable.getNameIdentifier());

      TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
      builder.setEndVariableAfter(localVariable.getNameIdentifier());

      Template template = generateTemplate(project, names);
      Editor newEditor = CodeInsightUtil.positionCursor(project, file, identifier);
      if (newEditor == null) return;
      TextRange range = localVariable.getNameIdentifier().getTextRange();
      newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());

      CreateFromUsageBaseFix.startTemplate(newEditor, template, project, new TemplateEditingAdapter() {

        @Override
        public void beforeTemplateFinished(@NotNull TemplateState state, Template template) {
          final TextResult value = state.getVariableValue("");
          assert value != null;

          final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(project).getResolveHelper();
          final PsiVariable target = resolveHelper.resolveAccessibleReferencedVariable(value.getText(), instanceOfExpression);
          if (target instanceof PsiField field) {
            final CaretModel caretModel = editor.getCaretModel();
            final PsiElement elementAt = file.findElementAt(caretModel.getOffset());
            final PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class);
            if (declarationStatement != null) {
              final PsiLocalVariable variable = (PsiLocalVariable)declarationStatement.getDeclaredElements()[0];
              final PsiExpression initializer = variable.getInitializer();
              assert initializer != null;
              ApplicationManager.getApplication().runWriteAction(() -> {
                initializer.accept(new JavaRecursiveElementVisitor() {
                  @Override
                  public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
                    final PsiExpression qualifierExpression = expression.getQualifierExpression();
                    if (qualifierExpression != null) {
                      qualifierExpression.accept(this);
                    }
                    else if (expression.resolve() == variable) {
                      RefactoringChangeUtil.qualifyReference(expression, field, field.hasModifierProperty(PsiModifier.STATIC)
                                                                                ? field.getContainingClass()
                                                                                : null);
                    }
                  }
                });
              });
              PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(editor.getDocument());
            }
          }
        }

        @Override
        public void templateFinished(@NotNull Template template, boolean brokenOff) {
          Runnable action = () -> {
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());

            CaretModel caretModel = editor.getCaretModel();
            PsiElement elementAt = file.findElementAt(caretModel.getOffset());
            PsiDeclarationStatement declarationStatement = PsiTreeUtil.getParentOfType(elementAt, PsiDeclarationStatement.class);
            if (declarationStatement != null) {
              caretModel.moveToOffset(declarationStatement.getTextRange().getEndOffset());
            }
            new EnterAction().getHandler().execute(editor, null, null);
          };
          if (file.isPhysical()) {
            ApplicationManager.getApplication().runWriteAction(action);
          } else {
            action.run();
          }
        }
      });
    }
    catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
@Override
  public void invoke(final @NotNull Project project, final Editor editor, final PsiFile file)