void calcResults(final boolean isQuick) {
    if (getSegments().isInvalid()) {
      gotoEnd(true);
    }

    if (myProcessor != null && myCurrentVariableNumber >= 0) {
      final String variableName = getTemplate().getVariableNameAt(myCurrentVariableNumber);
      final TextResult value = getVariableValue(variableName);
      if (value != null && !value.getText().isEmpty()) {
        if (!myProcessor.process(variableName, value.getText())) {
          finishTemplate(false); // nextTab(); ?
          return;
        }
      }
    }

    fixOverlappedSegments(myCurrentSegmentNumber);

    Runnable action = () -> {
      if (isDisposed()) {
        return;
      }
      BitSet calcedSegments = new BitSet();
      int maxAttempts = (getTemplate().getVariableCount() + 1) * 3;

      do {
        maxAttempts--;
        calcedSegments.clear();
        for (int i = myCurrentVariableNumber + 1; i < getTemplate().getVariableCount(); i++) {
          String variableName = getTemplate().getVariableNameAt(i);
          final int segmentNumber = getTemplate().getVariableSegmentNumber(variableName);
          if (segmentNumber < 0) continue;
          final Expression expression = getTemplate().getExpressionAt(i);
          final Expression defaultValue = getTemplate().getDefaultValueAt(i);
          String oldValue = getVariableValueText(variableName);
          DumbService.getInstance(myProject).withAlternativeResolveEnabled(
            () -> recalcSegment(segmentNumber, isQuick, expression, defaultValue));
          final TextResult value = getVariableValue(variableName);
          assert value != null : "name=" + variableName + "\ntext=" + getTemplate().getTemplateText();
          String newValue = value.getText();
          if (!newValue.equals(oldValue)) {
            calcedSegments.set(segmentNumber);
          }
        }

        List<TemplateDocumentChange> changes = new ArrayList<>();
        boolean selectionCalculated = false;
        for (int i = 0; i < getTemplate().getSegmentsCount(); i++) {
          if (!calcedSegments.get(i)) {
            String variableName = getTemplate().getSegmentName(i);
            if (variableName.equals(Template.SELECTION)) {
              if (mySelectionCalculated) {
                continue;
              }
              selectionCalculated = true;
            }
            if (Template.END.equals(variableName)) continue; // No need to update end since it can be placed over some other variable
            String newValue = getVariableValueText(variableName);
            int start = getSegments().getSegmentStart(i);
            int end = getSegments().getSegmentEnd(i);
            changes.add(new TemplateDocumentChange(newValue, start, end, i));
          }
        }
        executeChanges(changes);
        if (selectionCalculated) {
          mySelectionCalculated = true;
        }
      }
      while (!calcedSegments.isEmpty() && maxAttempts >= 0);
    };
    if (requiresWriteAction()) {
      WriteCommandAction.runWriteCommandAction(myProject, null, null, action);
    } else {
      action.run();
    }
  }
void calcResults(final boolean isQuick)