void (@NotNull CodeInsightTestFixture fixture, @NotNull String templateKey, @NotNull String templateGroup, @NotNull String answerFilePath) { TemplateImpl template = TemplateSettings.getInstance().getTemplate(templateKey, templateGroup); String templateId = templateKey + "@" + templateGroup; TestCase.assertNotNull("Unable to find template: " + templateId, template); TestCase.assertTrue(templateId + " is not a selection template", template.isSelectionTemplate()); Editor editor = fixture.getEditor(); SelectionModel selectionModel = editor.getSelectionModel(); Document document = editor.getDocument(); CharSequence documentCharsSequence = document.getCharsSequence(); CaretModel caretModel = editor.getCaretModel(); int caretOffset = caretModel.getOffset(); PsiFile psiFile = fixture.getFile(); boolean isAvailable = false; List<Pair<Integer, String>> markers = new ArrayList<>(); for (int offset = 0; offset < documentCharsSequence.length(); offset++) { selectionModel.setSelection(Math.min(offset, caretOffset), Math.max(offset, caretOffset)); if (caretOffset == offset) { markers.add(Pair.create(offset, "<caret>")); } TemplateActionContext templateActionContext = TemplateActionContext.surrounding(psiFile, editor); if (TemplateManagerImpl.isApplicable(template, templateActionContext)) { if (!isAvailable) { isAvailable = true; markers.add(Pair.create(offset, "<available>")); } } else if (isAvailable) { markers.add(Pair.create(offset - 1, "</available>")); isAvailable = false; } } if (isAvailable) { markers.add(Pair.create(documentCharsSequence.length(), "</available>")); } StringBuilder result = new StringBuilder(documentCharsSequence); ContainerUtil.reverse(markers).forEach(it -> result.insert(it.first, it.second)); UsefulTestCase.assertSameLinesWithFile(answerFilePath, result.toString()); }