use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class ReferenceInForm method updateRangeText.
protected void updateRangeText(final String text) {
final Document document = myRangeMarker.getDocument();
document.replaceString(myRangeMarker.getStartOffset(), myRangeMarker.getEndOffset(), text);
PsiDocumentManager.getInstance(myFile.getProject()).commitDocument(document);
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class Task method getTestsText.
@NotNull
public String getTestsText(@NotNull final Project project) {
final VirtualFile taskDir = getTaskDir(project);
if (taskDir != null) {
final VirtualFile file = taskDir.findChild(EduNames.TESTS_FILE);
if (file == null)
return "";
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document != null) {
return document.getImmutableCharSequence().toString();
}
}
return "";
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class StudyCheckTask method rememberAnswers.
private void rememberAnswers(int nextSubtaskIndex) {
VirtualFile taskDir = myTask.getTaskDir(myProject);
if (taskDir == null) {
return;
}
VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
if (srcDir != null) {
taskDir = srcDir;
}
for (Map.Entry<String, TaskFile> entry : myTask.getTaskFiles().entrySet()) {
TaskFile taskFile = entry.getValue();
VirtualFile virtualFile = taskDir.findFileByRelativePath(entry.getKey());
if (virtualFile == null) {
continue;
}
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
continue;
}
for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
if (placeholder.getSubtaskInfos().containsKey(nextSubtaskIndex - 1)) {
int offset = placeholder.getOffset();
String answer = document.getText(TextRange.create(offset, offset + placeholder.getRealLength()));
placeholder.getSubtaskInfos().get(nextSubtaskIndex - 1).setAnswer(answer);
}
}
}
}
use of com.intellij.openapi.editor.Document 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.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class EduAnswerPlaceholderPainter method createGuardedBlocks.
public static void createGuardedBlocks(@NotNull final Editor editor, AnswerPlaceholder placeholder) {
Document document = editor.getDocument();
if (document instanceof DocumentImpl) {
DocumentImpl documentImpl = (DocumentImpl) document;
List<RangeMarker> blocks = documentImpl.getGuardedBlocks();
Pair<Integer, Integer> offsets = StudyUtils.getPlaceholderOffsets(placeholder, editor.getDocument());
Integer start = offsets.first;
Integer end = offsets.second;
if (start != 0) {
createGuardedBlock(editor, blocks, start - 1, start);
}
if (end != document.getTextLength()) {
createGuardedBlock(editor, blocks, end, end + 1);
}
}
}
Aggregations