Search in sources :

Example 1 with AnswerPlaceholderSubtaskInfo

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);
        }
    });
}
Also used : TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) AnswerPlaceholder(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder) AnswerPlaceholderSubtaskInfo(com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo) UnexpectedUndoException(com.intellij.openapi.command.undo.UnexpectedUndoException) Editor(com.intellij.openapi.editor.Editor) BasicUndoableAction(com.intellij.openapi.command.undo.BasicUndoableAction)

Example 2 with AnswerPlaceholderSubtaskInfo

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

Example 3 with AnswerPlaceholderSubtaskInfo

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);
}
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 4 with AnswerPlaceholderSubtaskInfo

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);
}
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 5 with AnswerPlaceholderSubtaskInfo

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

Aggregations

AnswerPlaceholderSubtaskInfo (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholderSubtaskInfo)6 AnswerPlaceholder (com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 Editor (com.intellij.openapi.editor.Editor)2 File (java.io.File)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 BasicUndoableAction (com.intellij.openapi.command.undo.BasicUndoableAction)1 UnexpectedUndoException (com.intellij.openapi.command.undo.UnexpectedUndoException)1 Document (com.intellij.openapi.editor.Document)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Project (com.intellij.openapi.project.Project)1 StudySerializationUtils (com.jetbrains.edu.learning.StudySerializationUtils)1 Test (org.junit.Test)1