protected boolean buildTemplateAndStart(@NotNull Collection<PsiReference> refs,
                                          @NotNull Collection<Pair<PsiElement, TextRange>> stringUsages,
                                          @NotNull PsiElement scope,
                                          @NotNull PsiFile containingFile) {
    PsiElement context = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile);
    Editor topLevelEditor = InjectedLanguageEditorUtil.getTopLevelEditor(myEditor);
    //do not change scope if it's injected fragment in separate editor (created by QuickEditAction)
    //in this case there is no top level editor and there would be no need to adjust ranges
    myScope = context == null || topLevelEditor == myEditor ? scope : context.getContainingFile();
    TemplateBuilderImpl builder = new TemplateBuilderImpl(myScope);

    PsiElement nameIdentifier = getNameIdentifier();
    int offset = topLevelEditor.getCaretModel().getOffset();
    PsiElement selectedElement = getSelectedInEditorElement(nameIdentifier, refs, stringUsages, offset);

    boolean subrefOnPrimaryElement = false;
    boolean hasReferenceOnNameIdentifier = false;
    for (PsiReference ref : refs) {
      if (isReferenceAtCaret(selectedElement, ref, offset)) {
        Expression expression = createTemplateExpression(selectedElement);
        builder.replaceElement(ref.getElement(), getRangeToRename(ref), PRIMARY_VARIABLE_NAME, expression,
                               shouldStopAtLookupExpression(expression));
        subrefOnPrimaryElement = true;
        continue;
      }
      addVariable(ref, selectedElement, builder, offset);
      hasReferenceOnNameIdentifier |= isReferenceAtCaret(nameIdentifier, ref);
    }
    if (nameIdentifier != null) {
      hasReferenceOnNameIdentifier |= selectedElement.getTextRange().contains(nameIdentifier.getTextRange());
      if (!subrefOnPrimaryElement || !hasReferenceOnNameIdentifier) {
        addVariable(nameIdentifier, selectedElement, builder);
      }
    }
    for (Pair<PsiElement, TextRange> usage : stringUsages) {
      addVariable(usage.first, usage.second, selectedElement, builder);
    }
    addAdditionalVariables(builder);

    int segmentsLimit = Registry.intValue("inplace.rename.segments.limit", -1);
    if (segmentsLimit != -1 && builder.getElementsCount() > segmentsLimit) {
      return false;
    }

    try {
      myMarkAction = startRename();
    }
    catch (StartMarkAction.AlreadyStartedException e) {
      Document oldDocument = e.getDocument();
      if (oldDocument != myEditor.getDocument()) {
        int exitCode = Messages.showYesNoCancelDialog(myProject, e.getMessage(), getCommandName(),
                                                            RefactoringBundle.message("inplace.refactoring.navigate.to.started"),
                                                            RefactoringBundle.message("inplace.refactoring.abandon.started"),
                                                            RefactoringBundle.message("inplace.refactoring.cancel.current"), Messages.getErrorIcon());
        navigateToAlreadyStarted(oldDocument, exitCode);
        return true;
      }
      else {
        revertState();
        TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
        if (templateState != null) {
          templateState.gotoEnd(true);
        }
      }
      return false;
    }

    if (myBalloon == null) {
      showBalloon();
    }

    beforeTemplateStart();

    WriteCommandAction.writeCommandAction(myProject).withName(getCommandName()).run(() -> startTemplate(builder));

    afterTemplateStart();

    return true;
  }
protected boolean buildTemplateAndStart(@NotNull Collection<PsiReference> refs,
                                          @NotNull Collection<Pair<PsiElement, TextRange>> stringUsages,
                                          @NotNull PsiElement scope,
                                          @NotNull PsiFile containingFile)