use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyNextWindowAction method getTargetPlaceholder.
@Override
protected AnswerPlaceholder getTargetPlaceholder(@NotNull final TaskFile taskFile, int offset) {
final AnswerPlaceholder selectedAnswerPlaceholder = taskFile.getAnswerPlaceholder(offset);
final List<AnswerPlaceholder> placeholders = taskFile.getActivePlaceholders();
if (selectedAnswerPlaceholder == null) {
for (AnswerPlaceholder placeholder : placeholders) {
if (placeholder.getOffset() > offset) {
return placeholder;
}
}
} else {
int index = selectedAnswerPlaceholder.getIndex();
if (StudyUtils.indexIsValid(index, placeholders)) {
int newIndex = index + 1;
return placeholders.get(newIndex == placeholders.size() ? 0 : newIndex);
}
}
return null;
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyPrevWindowAction method getTargetPlaceholder.
@Nullable
@Override
protected AnswerPlaceholder getTargetPlaceholder(@NotNull final TaskFile taskFile, int offset) {
final AnswerPlaceholder selectedAnswerPlaceholder = taskFile.getAnswerPlaceholder(offset);
final List<AnswerPlaceholder> placeholders = taskFile.getActivePlaceholders();
if (selectedAnswerPlaceholder == null) {
for (int i = placeholders.size() - 1; i >= 0; i--) {
final AnswerPlaceholder placeholder = placeholders.get(i);
if (placeholder.getOffset() < offset) {
return placeholder;
}
}
} else {
int prevIndex = selectedAnswerPlaceholder.getIndex() - 1;
if (StudyUtils.indexIsValid(prevIndex, placeholders)) {
return placeholders.get(prevIndex);
}
}
return null;
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyWindowNavigationAction method navigateToPlaceholder.
private void navigateToPlaceholder(@NotNull final Project project) {
final Editor selectedEditor = StudyUtils.getSelectedEditor(project);
if (selectedEditor != null) {
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
final VirtualFile openedFile = fileDocumentManager.getFile(selectedEditor.getDocument());
if (openedFile != null) {
final TaskFile selectedTaskFile = StudyUtils.getTaskFile(project, openedFile);
if (selectedTaskFile != null) {
final int offset = selectedEditor.getCaretModel().getOffset();
final AnswerPlaceholder targetPlaceholder = getTargetPlaceholder(selectedTaskFile, offset);
if (targetPlaceholder == null) {
return;
}
StudyNavigator.navigateToAnswerPlaceholder(selectedEditor, targetPlaceholder);
}
}
}
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudySmartChecker method smartCheck.
public static void smartCheck(@NotNull final AnswerPlaceholder placeholder, @NotNull final Project project, @NotNull final VirtualFile answerFile, @NotNull final TaskFile answerTaskFile, @NotNull final TaskFile usersTaskFile, @NotNull final StudyTestRunner testRunner, @NotNull final VirtualFile virtualFile, @NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
File resourceFile = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
resourceFile = StudyUtils.copyResourceFile(virtualFile.getName(), windowCopy.getName(), project, usersTaskFile.getTask());
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
} catch (ExecutionException | IOException e) {
LOG.error(e);
} finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
if (resourceFile != null && resourceFile.exists() && !resourceFile.delete()) {
LOG.error("failed to delete", resourceFile.getPath());
}
}
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class EduDocumentListener method documentChanged.
@Override
public void documentChanged(DocumentEvent e) {
if (!myTaskFile.isTrackChanges()) {
return;
}
if (myAnswerPlaceholders.isEmpty())
return;
if (e instanceof DocumentEventImpl) {
DocumentEventImpl event = (DocumentEventImpl) e;
Document document = e.getDocument();
int offset = e.getOffset();
int change = event.getNewLength() - event.getOldLength();
for (AnswerPlaceholderWrapper answerPlaceholderWrapper : myAnswerPlaceholders) {
int twStart = answerPlaceholderWrapper.getTwStart();
if (twStart > offset) {
twStart += change;
}
int twEnd = answerPlaceholderWrapper.getTwEnd();
if (twEnd >= offset) {
twEnd += change;
}
AnswerPlaceholder answerPlaceholder = answerPlaceholderWrapper.getAnswerPlaceholder();
int length = twEnd - twStart;
answerPlaceholder.setOffset(twStart);
if (myTrackLength) {
if (answerPlaceholder.getUseLength()) {
answerPlaceholder.setLength(length);
} else {
if (answerPlaceholder.isActive() && myTaskFile.isTrackLengths()) {
answerPlaceholder.setPossibleAnswer(document.getText(TextRange.create(twStart, twStart + length)));
}
}
}
}
}
}
Aggregations