mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extracted common flushWindows
This commit is contained in:
-99
@@ -1,99 +0,0 @@
|
||||
package com.jetbrains.edu.coursecreator;
|
||||
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.event.DocumentAdapter;
|
||||
import com.intellij.openapi.editor.event.DocumentEvent;
|
||||
import com.intellij.openapi.editor.impl.event.DocumentEventImpl;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
|
||||
import com.jetbrains.edu.courseFormat.TaskFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Listens changes in study files and updates
|
||||
* coordinates of all the windows in current task file
|
||||
*/
|
||||
public abstract class CCDocumentListener extends DocumentAdapter {
|
||||
private final TaskFile myTaskFile;
|
||||
private final List<AnswerPlaceholedrWrapper> myAnswerPlaceholders = new ArrayList<AnswerPlaceholedrWrapper>();
|
||||
|
||||
|
||||
public CCDocumentListener(TaskFile taskFile) {
|
||||
myTaskFile = taskFile;
|
||||
}
|
||||
|
||||
|
||||
//remembering old end before document change because of problems
|
||||
// with fragments containing "\n"
|
||||
@Override
|
||||
public void beforeDocumentChange(DocumentEvent e) {
|
||||
Document document = e.getDocument();
|
||||
myAnswerPlaceholders.clear();
|
||||
for (AnswerPlaceholder answerPlaceholder : myTaskFile.getAnswerPlaceholders()) {
|
||||
int twStart = answerPlaceholder.getRealStartOffset(document);
|
||||
int length = useLength() ? answerPlaceholder.getLength() : answerPlaceholder.getPossibleAnswerLength();
|
||||
int twEnd = twStart + length;
|
||||
myAnswerPlaceholders.add(new AnswerPlaceholedrWrapper(answerPlaceholder, twStart, twEnd));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void documentChanged(DocumentEvent e) {
|
||||
if (e instanceof DocumentEventImpl) {
|
||||
DocumentEventImpl event = (DocumentEventImpl)e;
|
||||
Document document = e.getDocument();
|
||||
int offset = e.getOffset();
|
||||
int change = event.getNewLength() - event.getOldLength();
|
||||
for (AnswerPlaceholedrWrapper answerPlaceholedrWrapper : myAnswerPlaceholders) {
|
||||
int twStart = answerPlaceholedrWrapper.getTwStart();
|
||||
if (twStart > offset) {
|
||||
twStart += change;
|
||||
}
|
||||
int twEnd = answerPlaceholedrWrapper.getTwEnd();
|
||||
if (twEnd >= offset) {
|
||||
twEnd += change;
|
||||
}
|
||||
AnswerPlaceholder answerPlaceholder = answerPlaceholedrWrapper.getAnswerPlaceholder();
|
||||
int line = document.getLineNumber(twStart);
|
||||
int start = twStart - document.getLineStartOffset(line);
|
||||
int length = twEnd - twStart;
|
||||
answerPlaceholder.setLine(line);
|
||||
answerPlaceholder.setStart(start);
|
||||
if (useLength()) {
|
||||
answerPlaceholder.setLength(length);
|
||||
} else {
|
||||
answerPlaceholder.setPossibleAnswer(document.getText(TextRange.create(start, start + length)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean useLength();
|
||||
|
||||
private static class AnswerPlaceholedrWrapper {
|
||||
public AnswerPlaceholder myAnswerPlaceholder;
|
||||
public int myTwStart;
|
||||
public int myTwEnd;
|
||||
|
||||
public AnswerPlaceholedrWrapper(AnswerPlaceholder answerPlaceholder, int twStart, int twEnd) {
|
||||
myAnswerPlaceholder = answerPlaceholder;
|
||||
myTwStart = twStart;
|
||||
myTwEnd = twEnd;
|
||||
}
|
||||
|
||||
public int getTwStart() {
|
||||
return myTwStart;
|
||||
}
|
||||
|
||||
public int getTwEnd() {
|
||||
return myTwEnd;
|
||||
}
|
||||
|
||||
public AnswerPlaceholder getAnswerPlaceholder() {
|
||||
return myAnswerPlaceholder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-42
@@ -24,10 +24,8 @@ import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.io.FileUtilRt;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
@@ -36,15 +34,17 @@ import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.containers.HashMap;
|
||||
import com.jetbrains.edu.EduNames;
|
||||
import com.jetbrains.edu.courseFormat.*;
|
||||
import com.jetbrains.edu.EduUtils;
|
||||
import com.jetbrains.edu.courseFormat.Course;
|
||||
import com.jetbrains.edu.courseFormat.Lesson;
|
||||
import com.jetbrains.edu.courseFormat.Task;
|
||||
import com.jetbrains.edu.courseFormat.TaskFile;
|
||||
import com.jetbrains.edu.coursecreator.CCLanguageManager;
|
||||
import com.jetbrains.edu.coursecreator.CCProjectService;
|
||||
import com.jetbrains.edu.coursecreator.CCUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class CCRunTestsAction extends AnAction {
|
||||
@@ -175,7 +175,7 @@ public abstract class CCRunTestsAction extends AnAction {
|
||||
oldTaskFile.delete(project);
|
||||
}
|
||||
answerFile.copy(project, taskDir, fileName);
|
||||
flushWindows(taskFile, answerFile);
|
||||
EduUtils.flushWindows(taskFile, answerFile, false);
|
||||
createResourceFiles(answerFile, project);
|
||||
}
|
||||
catch (IOException e) {
|
||||
@@ -254,40 +254,4 @@ public abstract class CCRunTestsAction extends AnAction {
|
||||
return targetDir;
|
||||
}
|
||||
|
||||
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
||||
private static void flushWindows(TaskFile taskFile, VirtualFile file) {
|
||||
VirtualFile taskDir = file.getParent();
|
||||
final Document document = FileDocumentManager.getInstance().getDocument(file);
|
||||
if (document == null) {
|
||||
LOG.debug("Couldn't flush windows");
|
||||
return;
|
||||
}
|
||||
if (taskDir != null) {
|
||||
String name = file.getNameWithoutExtension() + "_windows";
|
||||
PrintWriter printWriter = null;
|
||||
try {
|
||||
final VirtualFile windowsFile = taskDir.createChildData(taskFile, name);
|
||||
printWriter = new PrintWriter(new FileOutputStream(windowsFile.getPath()));
|
||||
for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
|
||||
int start = answerPlaceholder.getRealStartOffset(document);
|
||||
String windowDescription = document.getText(new TextRange(start, start + answerPlaceholder.getPossibleAnswerLength()));
|
||||
printWriter.println("#educational_plugin_window = " + windowDescription);
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FileDocumentManager.getInstance().saveDocument(document);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
finally {
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-46
@@ -7,7 +7,6 @@ import com.intellij.ide.SaveAndSyncHandlerImpl;
|
||||
import com.intellij.lang.Language;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -20,7 +19,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.ui.popup.Balloon;
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
@@ -128,50 +126,6 @@ public class StudyUtils {
|
||||
VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
||||
@Nullable
|
||||
public static VirtualFile flushWindows(@NotNull final TaskFile taskFile, @NotNull final VirtualFile file) {
|
||||
final VirtualFile taskDir = file.getParent();
|
||||
VirtualFile fileWindows = null;
|
||||
final Document document = FileDocumentManager.getInstance().getDocument(file);
|
||||
if (document == null) {
|
||||
LOG.debug("Couldn't flush windows");
|
||||
return null;
|
||||
}
|
||||
if (taskDir != null) {
|
||||
final String name = file.getNameWithoutExtension() + "_windows";
|
||||
PrintWriter printWriter = null;
|
||||
try {
|
||||
fileWindows = taskDir.createChildData(taskFile, name);
|
||||
printWriter = new PrintWriter(new FileOutputStream(fileWindows.getPath()));
|
||||
for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
|
||||
if (!answerPlaceholder.isValid(document)) {
|
||||
printWriter.println("#educational_plugin_window = ");
|
||||
continue;
|
||||
}
|
||||
int start = answerPlaceholder.getRealStartOffset(document);
|
||||
final String windowDescription = document.getText(new TextRange(start, start + answerPlaceholder.getLength()));
|
||||
printWriter.println("#educational_plugin_window = " + windowDescription);
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FileDocumentManager.getInstance().saveDocument(document);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
finally {
|
||||
closeSilently(printWriter);
|
||||
synchronize();
|
||||
}
|
||||
}
|
||||
return fileWindows;
|
||||
}
|
||||
|
||||
public static void deleteFile(@NotNull final VirtualFile file) {
|
||||
try {
|
||||
file.delete(StudyUtils.class);
|
||||
|
||||
+2
-1
@@ -31,6 +31,7 @@ import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.openapi.wm.ex.StatusBarEx;
|
||||
import com.intellij.openapi.wm.ex.WindowManagerEx;
|
||||
import com.jetbrains.edu.EduDocumentListener;
|
||||
import com.jetbrains.edu.EduUtils;
|
||||
import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
|
||||
import com.jetbrains.edu.courseFormat.Task;
|
||||
import com.jetbrains.edu.courseFormat.TaskFile;
|
||||
@@ -66,7 +67,7 @@ public class StudyCheckAction extends DumbAwareAction {
|
||||
if (virtualFile == null) {
|
||||
continue;
|
||||
}
|
||||
StudyUtils.flushWindows(taskFile, virtualFile);
|
||||
EduUtils.flushWindows(taskFile, virtualFile, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.jetbrains.edu.EduDocumentListener;
|
||||
import com.jetbrains.edu.EduNames;
|
||||
import com.jetbrains.edu.EduUtils;
|
||||
import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
|
||||
import com.jetbrains.edu.courseFormat.TaskFile;
|
||||
import com.jetbrains.edu.learning.StudyTaskManager;
|
||||
@@ -62,7 +63,7 @@ public class StudySmartChecker {
|
||||
documentManager.saveDocument(windowDocument);
|
||||
}
|
||||
});
|
||||
VirtualFile fileWindows = StudyUtils.flushWindows(windowTaskFile, windowCopy);
|
||||
VirtualFile fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy, true);
|
||||
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
|
||||
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess);
|
||||
final ProcessOutput output = handler.runProcess();
|
||||
|
||||
@@ -1,18 +1,30 @@
|
||||
package com.jetbrains.edu;
|
||||
|
||||
import com.intellij.ide.SaveAndSyncHandlerImpl;
|
||||
import com.intellij.ide.projectView.actions.MarkRootActionBase;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.Presentation;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ContentEntry;
|
||||
import com.intellij.openapi.roots.ModifiableRootModel;
|
||||
import com.intellij.openapi.roots.ModuleRootManager;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
|
||||
import com.jetbrains.edu.courseFormat.TaskFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public class EduUtils {
|
||||
private EduUtils() {
|
||||
@@ -61,4 +73,58 @@ public class EduUtils {
|
||||
}
|
||||
return Integer.parseInt(fullName.substring(logicalName.length())) - 1;
|
||||
}
|
||||
|
||||
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
|
||||
@Nullable
|
||||
public static VirtualFile flushWindows(@NotNull final TaskFile taskFile, @NotNull final VirtualFile file,
|
||||
boolean useLength) {
|
||||
final VirtualFile taskDir = file.getParent();
|
||||
VirtualFile fileWindows = null;
|
||||
final Document document = FileDocumentManager.getInstance().getDocument(file);
|
||||
if (document == null) {
|
||||
LOG.debug("Couldn't flush windows");
|
||||
return null;
|
||||
}
|
||||
if (taskDir != null) {
|
||||
final String name = file.getNameWithoutExtension() + "_windows";
|
||||
PrintWriter printWriter = null;
|
||||
try {
|
||||
fileWindows = taskDir.createChildData(taskFile, name);
|
||||
printWriter = new PrintWriter(new FileOutputStream(fileWindows.getPath()));
|
||||
for (AnswerPlaceholder answerPlaceholder : taskFile.getAnswerPlaceholders()) {
|
||||
if (!answerPlaceholder.isValid(document)) {
|
||||
printWriter.println("#educational_plugin_window = ");
|
||||
continue;
|
||||
}
|
||||
int start = answerPlaceholder.getRealStartOffset(document);
|
||||
int length = useLength ? answerPlaceholder.getLength() : answerPlaceholder.getPossibleAnswerLength();
|
||||
final String windowDescription = document.getText(new TextRange(start, start + length));
|
||||
printWriter.println("#educational_plugin_window = " + windowDescription);
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
FileDocumentManager.getInstance().saveDocument(document);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (IOException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
finally {
|
||||
if (printWriter != null) {
|
||||
printWriter.close();
|
||||
}
|
||||
synchronize();
|
||||
}
|
||||
}
|
||||
return fileWindows;
|
||||
}
|
||||
|
||||
public static void synchronize() {
|
||||
FileDocumentManager.getInstance().saveAllDocuments();
|
||||
SaveAndSyncHandlerImpl.refreshOpenFiles();
|
||||
VirtualFileManager.getInstance().refreshWithoutFileWatcher(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user