@NotNull
  public TemplateImpl buildTemplate() {
    List<Variable> variables = getListWithLimit(myVariables);
    if (!findVarOccurence(TemplateImpl.END)) {
      if (myLastEndVarName == null) {
        for (Variable variable : variables) {
          if (isEndVariable(variable.getName())) {
            myLastEndVarName = variable.getName();
            break;
          }
        }
      }
      if (myLastEndVarName != null) {
        int endOffset = -1;
        if (myAddEndVariableAtTheEndOfTemplate) {
          endOffset = myText.length();
        }
        else {
          Iterator<VarOccurence> it = myVariableOccurrences.iterator();
          while (it.hasNext()) {
            VarOccurence occurence = it.next();
            if (occurence.myName.equals(myLastEndVarName)) {
              endOffset = occurence.myOffset;
              break;
            }
          }
          if (endOffset >= 0) {
            for (Iterator<Variable> it1 = variables.iterator(); it1.hasNext(); ) {
              Variable variable = it1.next();
              if (myLastEndVarName.equals(variable.getName()) && variable.isAlwaysStopAt()) {
                it.remove();
                it1.remove();
              }
            }
          }
        }
        if (endOffset >= 0) {
          myVariableOccurrences.add(new VarOccurence(TemplateImpl.END, endOffset));
        }
      }
    }
    TemplateImpl template = new TemplateImpl("", "");
    for (Variable variable : variables) {
      template.addVariable(variable.getName(), variable.getExpressionString(), variable.getDefaultValueString(), variable.isAlwaysStopAt());
    }

    List<VarOccurence> variableOccurrences = getListWithLimit(myVariableOccurrences);
    variableOccurrences.sort(Comparator.comparingInt(o -> o.myOffset));
    int last = 0;
    for (VarOccurence occurence : variableOccurrences) {
      template.addTextSegment(myText.substring(last, occurence.myOffset));
      template.addVariableSegment(occurence.myName);
      last = occurence.myOffset;
    }
    template.addTextSegment(myText.substring(last));
    template.setToReformat(myIsToReformat);
    return template;
  }
@NotNull
  public TemplateImpl buildTemplate()