void (@NotNull PsiFile file, @NotNull Map<String, String> defaultValues) { Editor editor = EditorHelper.openInEditor(file); if (editor == null) return; TemplateImpl template = new TemplateImpl("", file.getText(), ""); template.setInline(true); int count = template.getSegmentsCount(); if (count == 0) return; // Using LinkedHashSet for a saving variables orders Set<String> variables = new LinkedHashSet<>(); for (int i = 0; i < count; i++) { variables.add(template.getSegmentName(i)); } variables.removeAll(TemplateImpl.INTERNAL_VARS_SET); for (String variable : variables) { String defaultValue = defaultValues.getOrDefault(variable, variable); template.addVariable(variable, null, '"' + defaultValue + '"', true); } Project project = file.getProject(); WriteCommandAction.runWriteCommandAction(project, () -> editor.getDocument().setText(template.getTemplateText())); editor.getCaretModel().moveToOffset(0); // ensures caret at the start of the template TemplateManager.getInstance(project).startTemplate(editor, template); }