use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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.AnswerPlaceholder in project intellij-community by JetBrains.
the class CCChangePlaceholderVisibility method update.
@Override
public void update(AnActionEvent e) {
Presentation presentation = e.getPresentation();
presentation.setEnabledAndVisible(false);
CCState state = getState(e);
if (state == null) {
return;
}
AnswerPlaceholder placeholder = state.getAnswerPlaceholder();
if (placeholder == null) {
return;
}
Task task = state.getTaskFile().getTask();
if (!task.hasSubtasks()) {
return;
}
Integer minSubtaskIndex = Collections.min(placeholder.getSubtaskInfos().keySet());
if (canChangeState(placeholder, task, minSubtaskIndex)) {
presentation.setEnabledAndVisible(true);
}
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class CCShowPreviewTest method doTest.
private void doTest(String name) {
VirtualFile file = configureByTaskFile(name + CCTestsUtil.BEFORE_POSTFIX);
CCShowPreview action = new CCShowPreview();
TestActionEvent e = getActionEvent(action, PsiManager.getInstance(getProject()).findFile(file));
action.beforeActionPerformedUpdate(e);
assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
action.actionPerformed(e);
Editor editor = EditorFactory.getInstance().getAllEditors()[1];
Pair<Document, List<AnswerPlaceholder>> pair = getPlaceholders(name + CCTestsUtil.AFTER_POSTFIX);
assertEquals("Files don't match", editor.getDocument().getText(), pair.getFirst().getText());
for (AnswerPlaceholder placeholder : pair.getSecond()) {
assertNotNull("No highlighter for placeholder:" + CCTestsUtil.getPlaceholderPresentation(placeholder), getHighlighter(editor.getMarkupModel(), placeholder));
}
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder 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.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyStepicFormatTest method doStepOptionsCreationTest.
private static StepicWrappers.StepOptions doStepOptionsCreationTest(String fileName) throws IOException {
String responseString = FileUtil.loadFile(new File(getTestDataPath(), fileName));
StepicWrappers.StepSource stepSource = EduStepicClient.deserializeStepicResponse(StepicWrappers.StepContainer.class, responseString).steps.get(0);
StepicWrappers.StepOptions options = stepSource.block.options;
List<TaskFile> files = options.files;
assertTrue("Wrong number of task files", files.size() == 1);
List<AnswerPlaceholder> placeholders = files.get(0).getAnswerPlaceholders();
assertTrue("Wrong number of placeholders", placeholders.size() == 1);
Map<Integer, AnswerPlaceholderSubtaskInfo> infos = placeholders.get(0).getSubtaskInfos();
assertNotNull(infos);
assertEquals(Collections.singletonList("Type your name here."), infos.get(0).getHints());
assertEquals("Liana", infos.get(0).getPossibleAnswer());
return options;
}
Aggregations