Template (Template template) { String text = getDocumentTextFragment(myContainerElement.getStartOffset(),myContainerElement.getEndOffset()); final int containerStart = myContainerElement.getStartOffset(); int start = 0; for (final RangeMarker element : myElements) { int offset = element.getStartOffset() - containerStart; if (start > offset) { LOG.error("file: " + myFile + " container: " + myContainerElement + " markers: " + StringUtil.join(myElements, rangeMarker -> { final String docString = myDocument.getText(rangeMarker.getTextRange()); return "[[" + docString + "]" + rangeMarker.getStartOffset() + ", " + rangeMarker.getEndOffset() + "]"; }, ", ")); } template.addTextSegment(text.substring(start, offset)); if (element == mySelection) { template.addSelectionStartVariable(); template.addTextSegment(getDocumentTextFragment(mySelection.getStartOffset(), mySelection.getEndOffset())); template.addSelectionEndVariable(); } else if (element == myEndElement) { template.addEndVariable(); start = offset; continue; } else { Boolean stop = myAlwaysStopAtMap.get(element); final boolean alwaysStopAt = stop == null || stop.booleanValue(); final Expression expression = myExpressions.get(element); final String variableName = myVariableNamesMap.get(element) == null ? String.valueOf(expression.hashCode()) : myVariableNamesMap.get(element); if (expression != null) { final Boolean skipOnStart = mySkipOnStartMap.get(element); template.addVariable(variableName, expression, expression, alwaysStopAt, skipOnStart != null && skipOnStart.booleanValue()); } else { template.addVariableSegment(variableName); } } start = element.getEndOffset() - containerStart; } template.addTextSegment(text.substring(start)); for (final RangeMarker element : myElements) { final String dependantVariable = myVariableExpressions.get(element); if (dependantVariable != null) { Boolean stop = myAlwaysStopAtMap.get(element); final boolean alwaysStopAt = stop == null || stop.booleanValue(); final Expression expression = myExpressions.get(element); final String variableName = myVariableNamesMap.get(element) == null ? String.valueOf(expression.hashCode()) : myVariableNamesMap.get(element); template.addVariable(variableName, dependantVariable, dependantVariable, alwaysStopAt); } } template.setToIndent(false); template.setToReformat(false); orderTemplateVariables(template); return template; }