@Nullable private static AnswerPlaceholder getAnswerPlaceholder(AnActionEvent e) { final Project project = e.getProject(); if (project == null) { return null; } StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project); final StudyState studyState = new StudyState(studyEditor); if (studyEditor == null || !studyState.isValid()) { return null; } final Editor editor = studyState.getEditor(); final TaskFile taskFile = studyState.getTaskFile(); return taskFile.getAnswerPlaceholder(editor.getCaretModel().getOffset()); }
@Override public void actionPerformed(AnActionEvent e) { Project project = e.getProject(); if (project == null) { return; } for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) { listener.beforeCheck(e); } final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e); if (answerPlaceholder == null) { return; } StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project); final StudyState studyState = new StudyState(studyEditor); if (answerPlaceholder.getTaskFile().getTask().hasSubtasks()) { StudySubtaskUtils.refreshPlaceholder(studyState.getEditor(), answerPlaceholder); return; } Document patternDocument = StudyUtils.getPatternDocument( answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName()); if (patternDocument == null) { return; } AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState(); int startOffset = initialState.getOffset(); final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.getLength())); CommandProcessor.getInstance() .executeCommand( project, () -> ApplicationManager.getApplication() .runWriteAction( () -> { Document document = studyState.getEditor().getDocument(); int offset = answerPlaceholder.getOffset(); document.deleteString(offset, offset + answerPlaceholder.getRealLength()); document.insertString(offset, text); }), NAME, null); }