Search in sources :

Example 21 with AnswerPlaceholder

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);
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) AnswerPlaceholderSubtaskInfo(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo) SelectionModel(com.intellij.openapi.editor.SelectionModel) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document)

Example 22 with AnswerPlaceholder

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);
    }
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) Task(com.jetbrains.edu.learning.courseFormat.Task) Presentation(com.intellij.openapi.actionSystem.Presentation)

Example 23 with AnswerPlaceholder

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));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) List(java.util.List) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) TestActionEvent(com.intellij.testFramework.TestActionEvent)

Example 24 with AnswerPlaceholder

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);
}
Also used : AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) AnswerPlaceholderSubtaskInfo(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo) GsonBuilder(com.google.gson.GsonBuilder) StudySerializationUtils(com.jetbrains.edu.learning.StudySerializationUtils) Gson(com.google.gson.Gson) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File) Test(org.junit.Test)

Example 25 with AnswerPlaceholder

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;
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) AnswerPlaceholderSubtaskInfo(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) File(java.io.File)

Aggregations

AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)25 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)12 Document (com.intellij.openapi.editor.Document)9 Editor (com.intellij.openapi.editor.Editor)7 Project (com.intellij.openapi.project.Project)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)4 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)3 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiFile (com.intellij.psi.PsiFile)3 File (java.io.File)3 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)2 StudyState (com.jetbrains.edu.learning.StudyState)2 EduDocumentListener (com.jetbrains.edu.learning.core.EduDocumentListener)2 Course (com.jetbrains.edu.learning.courseFormat.Course)2 StudyEditor (com.jetbrains.edu.learning.editor.StudyEditor)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 Gson (com.google.gson.Gson)1