use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyRefreshAnswerPlaceholder method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
Project project = e.getProject();
if (project == null) {
return;
}
for (StudyActionListener listener : Extensions.getExtensions(StudyActionListener.EP_NAME)) {
listener.beforeCheck(e);
}
final AnswerPlaceholder answerPlaceholder = getAnswerPlaceholder(e);
if (answerPlaceholder == null) {
return;
}
StudyEditor studyEditor = StudyUtils.getSelectedStudyEditor(project);
final StudyState studyState = new StudyState(studyEditor);
if (answerPlaceholder.getTaskFile().getTask().hasSubtasks()) {
StudySubtaskUtils.refreshPlaceholder(studyState.getEditor(), answerPlaceholder);
return;
}
Document patternDocument = StudyUtils.getPatternDocument(answerPlaceholder.getTaskFile(), studyState.getVirtualFile().getName());
if (patternDocument == null) {
return;
}
AnswerPlaceholder.MyInitialState initialState = answerPlaceholder.getInitialState();
int startOffset = initialState.getOffset();
final String text = patternDocument.getText(new TextRange(startOffset, startOffset + initialState.getLength()));
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
Document document = studyState.getEditor().getDocument();
int offset = answerPlaceholder.getOffset();
document.deleteString(offset, offset + answerPlaceholder.getRealLength());
document.insertString(offset, text);
}), NAME, null);
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyShowHintAction method showHint.
public void showHint(Project project) {
Course course = StudyTaskManager.getInstance(project).getCourse();
if (course == null) {
return;
}
StudyState studyState = new StudyState(StudyUtils.getSelectedStudyEditor(project));
if (!studyState.isValid()) {
return;
}
PsiFile file = PsiManager.getInstance(project).findFile(studyState.getVirtualFile());
final Editor editor = studyState.getEditor();
int offset = editor.getCaretModel().getOffset();
AnswerPlaceholder answerPlaceholder = studyState.getTaskFile().getAnswerPlaceholder(offset);
if (file == null) {
return;
}
EduUsagesCollector.hintShown();
final StudyToolWindow hintComponent = getHint(project, answerPlaceholder).getStudyToolWindow();
hintComponent.setPreferredSize(new Dimension(400, 150));
showHintPopUp(project, studyState, editor, hintComponent);
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyCheckUtils method getCopyWithAnswers.
private static Pair<VirtualFile, TaskFile> getCopyWithAnswers(@NotNull final VirtualFile taskDir, @NotNull final VirtualFile file, @NotNull final TaskFile source) {
try {
VirtualFile answerFile = file.copy(taskDir, taskDir, file.getNameWithoutExtension() + EduNames.ANSWERS_POSTFIX + "." + file.getExtension());
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document document = documentManager.getDocument(answerFile);
if (document != null) {
TaskFile answerTaskFile = source.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(file));
if (answerTaskFile == null) {
return null;
}
EduDocumentListener listener = new EduDocumentListener(answerTaskFile);
document.addDocumentListener(listener);
for (AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
final int start = answerPlaceholder.getOffset();
final int end = start + answerPlaceholder.getRealLength();
final String text = answerPlaceholder.getPossibleAnswer();
document.replaceString(start, end, text);
}
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(document));
return Pair.create(answerFile, answerTaskFile);
}
} catch (IOException e) {
LOG.error(e);
}
return null;
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class StudyCheckUtils method runSmartTestProcess.
public static void runSmartTestProcess(@NotNull final VirtualFile taskDir, @NotNull final StudyTestRunner testRunner, @NotNull final String taskFileName, @NotNull final TaskFile taskFile, @NotNull final Project project) {
final VirtualFile virtualFile = taskDir.findFileByRelativePath(taskFileName);
if (virtualFile == null) {
return;
}
Pair<VirtualFile, TaskFile> pair = getCopyWithAnswers(taskDir, virtualFile, taskFile);
if (pair == null) {
return;
}
VirtualFile answerFile = pair.getFirst();
TaskFile answerTaskFile = pair.getSecond();
try {
for (final AnswerPlaceholder answerPlaceholder : answerTaskFile.getActivePlaceholders()) {
final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
continue;
}
StudySmartChecker.smartCheck(answerPlaceholder, project, answerFile, answerTaskFile, taskFile, testRunner, virtualFile, document);
}
} finally {
StudyUtils.deleteFile(answerFile);
}
}
use of com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder in project intellij-community by JetBrains.
the class EduDocumentListener method beforeDocumentChange.
@Override
public void beforeDocumentChange(DocumentEvent e) {
if (!myTaskFile.isTrackChanges()) {
return;
}
myTaskFile.setHighlightErrors(true);
myAnswerPlaceholders.clear();
for (AnswerPlaceholder answerPlaceholder : myTaskFile.getAnswerPlaceholders()) {
int twStart = answerPlaceholder.getOffset();
int length = answerPlaceholder.getRealLength();
int twEnd = twStart + length;
myAnswerPlaceholders.add(new AnswerPlaceholderWrapper(answerPlaceholder, twStart, twEnd));
}
}
Aggregations