use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo in project intellij-community by JetBrains.
the class CCSubtaskPlaceholderAction method performAnswerPlaceholderAction.
@Override
protected void performAnswerPlaceholderAction(@NotNull CCState state) {
Editor editor = state.getEditor();
final int offset = editor.getCaretModel().getOffset();
TaskFile taskFile = state.getTaskFile();
int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
AnswerPlaceholder existingPlaceholder = StudyUtils.getAnswerPlaceholder(offset, taskFile.getAnswerPlaceholders());
if (existingPlaceholder == null) {
return;
}
AnswerPlaceholderSubtaskInfo info = getInfo(state, subtaskIndex, existingPlaceholder);
if (info == null) {
return;
}
EduUtils.runUndoableAction(state.getProject(), getTitle(), new BasicUndoableAction(state.getEditor().getDocument()) {
@Override
public void undo() throws UnexpectedUndoException {
undoAction(existingPlaceholder, subtaskIndex, info);
StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
}
@Override
public void redo() throws UnexpectedUndoException {
redoAction(existingPlaceholder, subtaskIndex, info);
StudyUtils.drawAllAnswerPlaceholders(editor, taskFile);
}
});
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo in project intellij-community by JetBrains.
the class CCActivatePlaceholder method getInfo.
@Override
protected AnswerPlaceholderSubtaskInfo getInfo(@NotNull CCState state, int subtaskIndex, @NotNull AnswerPlaceholder existingPlaceholder) {
int visibleLength = existingPlaceholder.getVisibleLength(subtaskIndex);
int placeholderOffset = existingPlaceholder.getOffset();
String possibleAnswer = state.getEditor().getDocument().getText(TextRange.create(placeholderOffset, placeholderOffset + visibleLength));
AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
info.setPossibleAnswer(possibleAnswer);
return info;
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo in project intellij-community by JetBrains.
the class CCAddAnswerPlaceholder method addPlaceholder.
private void addPlaceholder(@NotNull CCState state) {
Editor editor = state.getEditor();
Project project = state.getProject();
Document document = editor.getDocument();
FileDocumentManager.getInstance().saveDocument(document);
final SelectionModel model = editor.getSelectionModel();
final int offset = model.hasSelection() ? model.getSelectionStart() : editor.getCaretModel().getOffset();
TaskFile taskFile = state.getTaskFile();
int subtaskIndex = state.getTaskFile().getTask().getActiveSubtaskIndex();
final AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
answerPlaceholder.getSubtaskInfos().put(subtaskIndex, info);
int index = taskFile.getAnswerPlaceholders().size();
answerPlaceholder.setIndex(index);
answerPlaceholder.setTaskFile(taskFile);
taskFile.sortAnswerPlaceholders();
answerPlaceholder.setOffset(offset);
answerPlaceholder.setUseLength(false);
String defaultPlaceholderText = "type here";
CCCreateAnswerPlaceholderDialog dlg = createDialog(project, answerPlaceholder);
if (!dlg.showAndGet()) {
return;
}
String answerPlaceholderText = dlg.getTaskText();
answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
answerPlaceholder.setTaskText(StringUtil.notNullize(answerPlaceholderText));
answerPlaceholder.setLength(StringUtil.notNullize(answerPlaceholderText).length());
answerPlaceholder.setHints(dlg.getHints());
if (!model.hasSelection()) {
DocumentUtil.writeInRunUndoTransparentAction(() -> document.insertString(offset, defaultPlaceholderText));
}
answerPlaceholder.setPossibleAnswer(model.hasSelection() ? model.getSelectedText() : defaultPlaceholderText);
AddAction action = new AddAction(answerPlaceholder, taskFile, editor);
EduUtils.runUndoableAction(project, "Add Answer Placeholder", action);
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo in project intellij-community by JetBrains.
the class StudyStepicFormatTest method placeholderSerialization.
@Test
public void placeholderSerialization() throws IOException {
final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(AnswerPlaceholder.class, new StudySerializationUtils.Json.StepicAnswerPlaceholderAdapter()).create();
AnswerPlaceholder answerPlaceholder = new AnswerPlaceholder();
answerPlaceholder.setOffset(1);
answerPlaceholder.setLength(10);
AnswerPlaceholderSubtaskInfo info1 = createSubtaskInfo("type here", "answer1", ContainerUtil.list("hint 1", "hint 2"));
AnswerPlaceholderSubtaskInfo info2 = createSubtaskInfo("type here1", "answer2", ContainerUtil.list("hint 11", "hint 22"));
answerPlaceholder.setSubtaskInfos(ContainerUtil.newHashMap(ContainerUtil.list(0, 1), ContainerUtil.list(info1, info2)));
final String placeholderSerialization = gson.toJson(answerPlaceholder);
String expected = FileUtil.loadFile(new File(getTestDataPath(), "placeholder.json"));
assertEquals(expected, placeholderSerialization);
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo in project intellij-community by JetBrains.
the class StudyStepicFormatTest method createSubtaskInfo.
private static AnswerPlaceholderSubtaskInfo createSubtaskInfo(String placeholderText, String possibleAnswer, List<String> hints) {
AnswerPlaceholderSubtaskInfo info = new AnswerPlaceholderSubtaskInfo();
info.setPlaceholderText(placeholderText);
info.setPossibleAnswer(possibleAnswer);
info.setHints(hints);
info.setNeedInsertText(true);
return info;
}
Aggregations