ModTemplateBuilder () { if (!myTemplateFields.isEmpty()) { throw new IllegalStateException("Template was already created"); } return new ModTemplateBuilder() { @Override public @NotNull ModTemplateBuilder field(@NotNull PsiElement element, @NotNull Expression expression) { TextRange elementRange = getRange(element); if (elementRange == null) { throw new IllegalStateException("Unable to restore element for template"); } TextRange range = mapRange(elementRange); Result result = expression.calculateResult(new DummyContext(range, element)); myTemplateFields.add(new ModStartTemplate.ExpressionField(range, expression)); if (result != null) { myTracker.myPositionDocument.replaceString(range.getStartOffset(), range.getEndOffset(), result.toString()); } return this; } @Override public @NotNull ModTemplateBuilder field(@NotNull PsiElement element, @NotNull String varName, @NotNull String dependantVariableName, boolean alwaysStopAt) { TextRange elementRange = getRange(element); if (elementRange == null) { throw new IllegalStateException("Unable to restore element for template"); } TextRange range = mapRange(elementRange); myTemplateFields.add(new ModStartTemplate.DependantVariableField(range, varName, dependantVariableName, alwaysStopAt)); return this; } }; }