EDU-547 Get rid of answer extension for Task Files
authorliana.bakradze <liana.bakradze@jetbrains.com>
Sat, 27 Feb 2016 18:19:00 +0000 (21:19 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Sat, 27 Feb 2016 18:19:00 +0000 (21:19 +0300)
Use actual task file name instead of name.answer

19 files changed:
python/educational-core/course-creator/resources/META-INF/plugin.xml
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCFileDeletedListener.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectComponent.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCProjectService.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRefactoringElementListenerProvider.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java [deleted file]
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCUtils.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCAddAsTaskFile.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateCourseArchive.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCCreateTaskFile.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCFromCourseArchive.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCRunTestsAction.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/actions/CCShowPreview.java
python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/projectView/CCTreeStructureProvider.java
python/educational-core/src/com/jetbrains/edu/EduUtils.java
python/educational-core/src/com/jetbrains/edu/stepic/EduStepicConnector.java
python/educational-python/course-creator-python/resources/fileTemplates/internal/task.py.ft [moved from python/educational-python/course-creator-python/resources/fileTemplates/internal/task.answer.py.ft with 100% similarity]
python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/PyCCLanguageManager.java
python/educational-python/course-creator-python/src/com/jetbrains/edu/coursecreator/actions/PyCCRunTestsAction.java

index 0f8051b07c20fc2b4865395ff646e7f9652789a9..193cd710f76952f62e28adb6f82ea5faf8480130 100644 (file)
@@ -27,7 +27,6 @@
     <treeStructureProvider implementation="com.jetbrains.edu.coursecreator.projectView.CCTreeStructureProvider"/>
     <refactoring.elementListenerProvider implementation="com.jetbrains.edu.coursecreator.CCRefactoringElementListenerProvider"/>
     <renameHandler implementation="com.jetbrains.edu.coursecreator.CCRenameHandler"/>
-    <renameInputValidator implementation="com.jetbrains.edu.coursecreator.CCRenameInputValidator"/>
     <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCLessonMoveHandlerDelegate" order="first"/>
     <refactoring.moveHandler implementation="com.jetbrains.edu.coursecreator.CCTaskMoveHandlerDelegate" order="first"/>
   </extensions>
index ae59f4f32462b10b3bd7631f2278e5936a550d2d..36f8da820c2e97c38a9e0850668bbfeb9c4f9e20 100644 (file)
@@ -1,6 +1,5 @@
 package com.jetbrains.edu.coursecreator;
 
-import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileAdapter;
@@ -8,7 +7,6 @@ import com.intellij.openapi.vfs.VirtualFileEvent;
 import com.intellij.util.Function;
 import com.jetbrains.edu.EduNames;
 import com.jetbrains.edu.courseFormat.*;
-import com.jetbrains.edu.coursecreator.actions.CCRunTestsAction;
 import org.jetbrains.annotations.NotNull;
 
 class CCFileDeletedListener extends VirtualFileAdapter {
@@ -30,7 +28,7 @@ class CCFileDeletedListener extends VirtualFileAdapter {
     }
     final TaskFile taskFile = CCProjectService.getInstance(myProject).getTaskFile(removedFile);
     if (taskFile != null) {
-      deleteAnswerFile(removedFile, taskFile);
+      deleteTaskFile(removedFile, taskFile);
       return;
     }
     Course course = CCProjectService.getInstance(myProject).getCourse();
@@ -82,21 +80,11 @@ class CCFileDeletedListener extends VirtualFileAdapter {
     lesson.getTaskList().remove(task);
   }
 
-  private void deleteAnswerFile(@NotNull final VirtualFile removedAnswerFile, TaskFile taskFile) {
-    ApplicationManager.getApplication().runWriteAction(new Runnable() {
-      @Override
-      public void run() {
-        VirtualFile taskDir = removedAnswerFile.getParent();
-        if (taskDir != null) {
-          CCRunTestsAction.clearTestEnvironment(taskDir, myProject);
-        }
-      }
-    });
-    String name = CCProjectService.getRealTaskFileName(removedAnswerFile.getName());
+  private static void deleteTaskFile(@NotNull final VirtualFile removedTaskFile, TaskFile taskFile) {
     Task task = taskFile.getTask();
     if (task == null) {
       return;
     }
-    task.getTaskFiles().remove(name);
+    task.getTaskFiles().remove(removedTaskFile.getName());
   }
 }
index 37122176dea170fa69ed3005083a0a79cf0aa75a..7064cfe0adb10761cdbf4e9629c412d06fd9417d 100644 (file)
@@ -1,13 +1,12 @@
 package com.jetbrains.edu.coursecreator;
 
-import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.components.ProjectComponent;
-import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.editor.EditorFactory;
 import com.intellij.openapi.editor.event.EditorFactoryEvent;
 import com.intellij.openapi.editor.impl.EditorFactoryImpl;
-import com.intellij.openapi.fileEditor.*;
+import com.intellij.openapi.fileEditor.FileEditor;
+import com.intellij.openapi.fileEditor.FileEditorManager;
 import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
@@ -17,10 +16,7 @@ import com.intellij.openapi.vfs.VirtualFileManager;
 import com.jetbrains.edu.courseFormat.Course;
 import org.jetbrains.annotations.NotNull;
 
-import java.io.IOException;
-
 public class CCProjectComponent implements ProjectComponent {
-  private static final Logger LOG = Logger.getInstance(CCProjectComponent.class.getName());
   private final Project myProject;
   private CCFileDeletedListener myListener;
 
@@ -46,30 +42,6 @@ public class CCProjectComponent implements ProjectComponent {
         final Course course = CCProjectService.getInstance(myProject).getCourse();
         if (course != null) {
           course.initCourse(true);
-          myProject.getMessageBus().connect(myProject).subscribe(
-            FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
-              @Override
-              public void selectionChanged(@NotNull FileEditorManagerEvent event) {
-                final VirtualFile oldFile = event.getOldFile();
-                if (oldFile == null) {
-                  return;
-                }
-                if (CCProjectService.getInstance(myProject).isTaskFile(oldFile)) {
-                  FileEditorManager.getInstance(myProject).closeFile(oldFile);
-                  ApplicationManager.getApplication().runWriteAction(new Runnable() {
-                    @Override
-                    public void run() {
-                      try {
-                        oldFile.delete(myProject);
-                      }
-                      catch (IOException e) {
-                        LOG.error(e);
-                      }
-                    }
-                  });
-                }
-              }
-            });
           myListener = new CCFileDeletedListener(myProject);
           VirtualFileManager.getInstance().addVirtualFileListener(myListener);
           final CCEditorFactoryListener editorFactoryListener = new CCEditorFactoryListener();
index 4553d89735b8b730a3d7ed78244e20d788b61639..6b470682629ead386fbe531c8e27a3f95a721c34 100644 (file)
@@ -23,8 +23,6 @@ import com.intellij.openapi.components.Storage;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.io.FileUtil;
-import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.ui.JBColor;
 import com.intellij.util.xmlb.XmlSerializerUtil;
@@ -72,8 +70,7 @@ public class CCProjectService implements PersistentStateComponent<CCProjectServi
     if (task == null) {
       return null;
     }
-    String fileName = getRealTaskFileName(virtualFile.getName());
-    return task.getTaskFile(fileName);
+    return task.getTaskFile(virtualFile.getName());
   }
 
   public void drawAnswerPlaceholders(@NotNull final VirtualFile virtualFile, @NotNull final Editor editor) {
@@ -129,28 +126,11 @@ public class CCProjectService implements PersistentStateComponent<CCProjectServi
     return null;
   }
 
-  public boolean isAnswerFile(VirtualFile file) {
-    Task task = getTask(file);
-    String fileName = getRealTaskFileName(file.getName());
-    return task != null && fileName != null && task.isTaskFile(fileName);
-  }
-
   public boolean isTaskFile(VirtualFile file) {
     Task task = getTask(file);
     return task != null && task.isTaskFile(file.getName());
   }
 
-  @Nullable
-  public static String getRealTaskFileName(String name) {
-    String nameWithoutExtension = FileUtil.getNameWithoutExtension(name);
-    String extension = FileUtilRt.getExtension(name);
-    if (!nameWithoutExtension.endsWith(".answer")) {
-      return null;
-    }
-    int nameEnd = name.indexOf(".answer");
-    return name.substring(0, nameEnd) + "." + extension;
-  }
-
   public static boolean setCCActionAvailable(@NotNull AnActionEvent e) {
     final Project project = e.getProject();
     if (project == null) {
index 0d30a0fe817b212b814a020a9887f0d3818f4783..64b3d1ea390da5801ef318f685b2d7a9975946aa 100644 (file)
@@ -56,14 +56,11 @@ public class CCRefactoringElementListenerProvider implements RefactoringElementL
     protected void elementRenamedOrMoved(@NotNull PsiElement newElement) {
       if (newElement instanceof PsiFile && myElementName != null) {
         PsiFile psiFile = (PsiFile)newElement;
-        if (myElementName.contains(".answer")) {
-          //this is task file
-          renameTaskFile(psiFile, myElementName);
-        }
+        tryToRenameTaskFile(psiFile, myElementName);
       }
     }
 
-    private static void renameTaskFile(PsiFile file, String oldName) {
+    private static void tryToRenameTaskFile(PsiFile file, String oldName) {
       final PsiDirectory taskDir = file.getContainingDirectory();
       final CCProjectService service = CCProjectService.getInstance(file.getProject());
       Course course = service.getCourse();
@@ -92,10 +89,9 @@ public class CCRefactoringElementListenerProvider implements RefactoringElementL
         }
       });
       Map<String, TaskFile> taskFiles = task.getTaskFiles();
-      String realOldName = CCProjectService.getRealTaskFileName(oldName);
-      TaskFile taskFile = task.getTaskFile(realOldName);
-      taskFiles.remove(realOldName);
-      taskFiles.put(CCProjectService.getRealTaskFileName(file.getName()), taskFile);
+      TaskFile taskFile = task.getTaskFile(oldName);
+      taskFiles.remove(oldName);
+      taskFiles.put(file.getName(), taskFile);
     }
 
     @Override
diff --git a/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java b/python/educational-core/course-creator/src/com/jetbrains/edu/coursecreator/CCRenameInputValidator.java
deleted file mode 100644 (file)
index f6a8b54..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.jetbrains.edu.coursecreator;
-
-import com.intellij.patterns.ElementPattern;
-import com.intellij.patterns.PlatformPatterns;
-import com.intellij.psi.PsiElement;
-import com.intellij.refactoring.rename.RenameInputValidator;
-import com.intellij.util.ProcessingContext;
-
-public class CCRenameInputValidator implements RenameInputValidator {
-  @Override
-  public ElementPattern<? extends PsiElement> getPattern() {
-    return PlatformPatterns.psiFile();
-  }
-
-  @Override
-  public boolean isInputValid(String newName, PsiElement element, ProcessingContext context) {
-    if (!CCUtils.isAnswerFile(element)) {
-      return true;
-    }
-    return newName.contains(".answer.");
-  }
-}
index 16b71bbc0be1da3e664390378a8322684f37bace..e392934226058b85f644fef699f61fb6d9c1de5c 100644 (file)
@@ -15,9 +15,8 @@ import com.intellij.openapi.roots.ModifiableRootModel;
 import com.intellij.openapi.roots.ModuleRootManager;
 import com.intellij.openapi.util.Ref;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.openapi.vfs.VirtualFileEvent;
 import com.intellij.psi.PsiDirectory;
-import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiFile;
 import com.intellij.util.Function;
 import com.jetbrains.edu.EduUtils;
 import com.jetbrains.edu.courseFormat.Course;
@@ -34,6 +33,8 @@ import java.util.Comparator;
 public class CCUtils {
   private static final Logger LOG = Logger.getInstance(CCUtils.class);
   public static final String GENERATED_FILES_FOLDER = ".coursecreator";
+  public static final String TESTS = "coursecreatortests";
+  public static final String RESOURCES = "coursecreatorresources";
 
   @Nullable
   public static CCLanguageManager getStudyLanguageManager(@NotNull final Course course) {
@@ -41,21 +42,14 @@ public class CCUtils {
     return language == null ? null : CCLanguageManager.INSTANCE.forLanguage(language);
   }
 
-  public static boolean isAnswerFile(PsiElement element) {
-    if (!(element instanceof PsiFile)) {
-      return false;
-    }
-    VirtualFile file = ((PsiFile)element).getVirtualFile();
-    return CCProjectService.getInstance(element.getProject()).isAnswerFile(file);
-  }
-
   /**
    * This method decreases index and updates directory names of
    * all tasks/lessons that have higher index than specified object
-   * @param dirs              directories that are used to get tasks/lessons
+   *
+   * @param dirs         directories that are used to get tasks/lessons
    * @param getStudyItem function that is used to get task/lesson from VirtualFile. This function can return null
-   * @param threshold         index is used as threshold
-   * @param prefix            task or lesson directory name prefix
+   * @param threshold    index is used as threshold
+   * @param prefix       task or lesson directory name prefix
    */
   public static void updateHigherElements(VirtualFile[] dirs,
                                           @NotNull final Function<VirtualFile, ? extends StudyItem> getStudyItem,
@@ -149,4 +143,30 @@ public class CCUtils {
     });
     return generatedRoot.get();
   }
+
+  /**
+   * @param requestor {@link VirtualFileEvent#getRequestor}
+   */
+  @Nullable
+  public static VirtualFile generateFolder(@NotNull Project project, @NotNull Module module, @Nullable Object requestor, String name) {
+    VirtualFile generatedRoot = getGeneratedFilesFolder(project, module);
+    if (generatedRoot == null) {
+      return null;
+    }
+
+    final Ref<VirtualFile> folder = new Ref<>(generatedRoot.findChild(name));
+    //need to delete old folder
+    ApplicationManager.getApplication().runWriteAction(() -> {
+      try {
+        if (folder.get() != null) {
+          folder.get().delete(requestor);
+        }
+        folder.set(generatedRoot.createChildDirectory(requestor, name));
+      }
+      catch (IOException e) {
+        LOG.info("Failed to generate folder " + name, e);
+      }
+    });
+    return folder.get();
+  }
 }
index 43ea510845106f0f58ef5a0ce8ce92b9d9d69be7..68eef74e5eb5edef043749a07ed6bce73710b2b8 100644 (file)
@@ -4,18 +4,12 @@ import com.intellij.openapi.actionSystem.AnAction;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
 import com.intellij.openapi.actionSystem.Presentation;
-import com.intellij.openapi.application.ApplicationManager;
-import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.jetbrains.edu.courseFormat.Task;
 import com.jetbrains.edu.coursecreator.CCProjectService;
 
-import java.io.IOException;
-
 public class CCAddAsTaskFile extends AnAction {
-  private static final Logger LOG = Logger.getInstance(CCAddAsTaskFile.class);
-
   @Override
   public void actionPerformed(final AnActionEvent e) {
     Project project = e.getProject();
@@ -31,19 +25,6 @@ public class CCAddAsTaskFile extends AnAction {
       return;
     }
     task.addTaskFile(file.getName(), task.getTaskFiles().size());
-    ApplicationManager.getApplication().runWriteAction(new Runnable() {
-      @Override
-      public void run() {
-        String name = file.getNameWithoutExtension();
-        String extension = file.getExtension();
-        try {
-          file.rename(this, name + ".answer." + extension);
-        }
-        catch (IOException e1) {
-          LOG.error(e1);
-        }
-      }
-    });
   }
 
 
@@ -56,12 +37,7 @@ public class CCAddAsTaskFile extends AnAction {
       return;
     }
     VirtualFile file = CommonDataKeys.VIRTUAL_FILE.getData(e.getDataContext());
-    if (file == null || file.isDirectory() || CCProjectService.getInstance(project).isAnswerFile(file)) {
-      presentation.setEnabledAndVisible(false);
-      return;
-    }
-    Task task = CCProjectService.getInstance(project).getTask(file);
-    if (task == null) {
+    if (file == null || file.isDirectory() || CCProjectService.getInstance(project).getTaskFile(file) != null) {
       presentation.setEnabledAndVisible(false);
     }
   }
index e47a1026cf9188218845061c43c3b401a072265f..720a5814584f719e99e06adc9b2b2d61173b6d89 100644 (file)
@@ -14,9 +14,7 @@ import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.ui.Messages;
-import com.intellij.openapi.util.Ref;
 import com.intellij.openapi.util.io.FileUtil;
-import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.vfs.VfsUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
@@ -33,7 +31,6 @@ import com.jetbrains.edu.coursecreator.CCProjectService;
 import com.jetbrains.edu.coursecreator.CCUtils;
 import com.jetbrains.edu.coursecreator.ui.CreateCourseArchiveDialog;
 import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
 
 import java.io.*;
 import java.util.List;
@@ -83,7 +80,7 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     }
     final VirtualFile baseDir = project.getBaseDir();
 
-    VirtualFile archiveFolder = getArchiveFolder(project, module);
+    VirtualFile archiveFolder = CCUtils.generateFolder(project, module, this, myZipName);
     if (archiveFolder == null) {
       return;
     }
@@ -142,16 +139,6 @@ public class CCCreateCourseArchive extends DumbAwareAction {
               }
               String taskFileName = entry.getKey();
               EduUtils.createStudentFileFromAnswer(project, userFileDir, taskDir, taskFileName, taskFile);
-              VirtualFile answerFile = userFileDir.findChild(
-                FileUtilRt.getNameWithoutExtension(taskFileName) + ".answer." + FileUtilRt.getExtension(taskFileName));
-              if (answerFile != null) {
-                try {
-                  answerFile.delete(this);
-                }
-                catch (IOException e) {
-                  LOG.info(e);
-                }
-              }
             }
           }
         }
@@ -179,32 +166,6 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     }
   }
 
-  @Nullable
-  private VirtualFile getArchiveFolder(@NotNull Project project, @NotNull Module module) {
-    VirtualFile generatedFilesRoot = CCUtils.getGeneratedFilesFolder(project, module);
-    if (generatedFilesRoot == null) {
-      return null;
-    }
-    VirtualFile zipRoot = generatedFilesRoot.findChild(myZipName);
-    final Ref<VirtualFile> archiveFolder = new Ref<>();
-    ApplicationManager.getApplication().runWriteAction(new Runnable() {
-      @Override
-      public void run() {
-        try {
-          if (zipRoot != null) {
-            zipRoot.delete(this);
-          }
-          archiveFolder.set(generatedFilesRoot.createChildDirectory(this, myZipName));
-        }
-        catch (IOException e) {
-          LOG.error("Failed to get zip root for " + myZipName, e);
-        }
-      }
-    });
-    return archiveFolder.get();
-  }
-
-
   private static void resetTaskFiles(Map<TaskFile, TaskFile> savedTaskFiles) {
     for (Map.Entry<TaskFile, TaskFile> entry : savedTaskFiles.entrySet()) {
       entry.getKey().setAnswerPlaceholders(entry.getValue().getAnswerPlaceholders());
index 4ca408f648c92386f8fda7e659ea19f179175ef8..1a04f0f65fc3d874637bda1bad675618abf21745 100644 (file)
@@ -63,8 +63,8 @@ public class CCCreateTaskFile extends DumbAwareAction {
     if (dialog.getExitCode() != OK_EXIT_CODE) {
       return;
     }
-    final String taskFileName = dialog.getFileName();
-    if (taskFileName == null) return;
+    final String name = dialog.getFileName();
+    if (name == null) return;
     FileType type = dialog.getFileType();
     if (type == null) {
       return;
@@ -78,15 +78,15 @@ public class CCCreateTaskFile extends DumbAwareAction {
       @Override
       public void run() {
         final FileTemplate taskTemplate = CCLanguageManager.getTaskFileTemplateForExtension(project, extension);
-        final String answerFileName = taskFileName + ".answer." + extension;
+        final String taskFileName = name + "." + extension;
         try {
           if (taskTemplate == null) {
-            VirtualFile file = taskDir.getVirtualFile().createChildData(this, answerFileName);
+            VirtualFile file = taskDir.getVirtualFile().createChildData(this, taskFileName);
             ProjectView.getInstance(project).select(file, file, false);
             FileEditorManager.getInstance(project).openFile(file, true);
           }
           else {
-            final PsiElement taskFile = FileTemplateUtil.createFromTemplate(taskTemplate, answerFileName, null, taskDir);
+            final PsiElement taskFile = FileTemplateUtil.createFromTemplate(taskTemplate, taskFileName, null, taskDir);
             ApplicationManager.getApplication().invokeLater(new Runnable() {
               @Override
               public void run() {
@@ -95,7 +95,7 @@ public class CCCreateTaskFile extends DumbAwareAction {
               }
             });
           }
-          task.addTaskFile(taskFileName + "." + extension, index);
+          task.addTaskFile(taskFileName, index);
         }
         catch (Exception ignored) {
         }
index 4b524f2a49ddf13cb4b5624391c162d12897d0cc..96bd6911ea79fb7e25e6c32c28ad07b0ee9eff1e 100644 (file)
@@ -134,29 +134,11 @@ public class CCFromCourseArchive extends DumbAwareAction {
     final TaskFile taskFile = taskFileEntry.getValue();
     VirtualFile file = userFileDir.findChild(name);
     assert file != null;
-    String answerFileName = file.getNameWithoutExtension() + ".answer." + file.getExtension();
-    VirtualFile answerFile = answerFileDir.findChild(answerFileName);
-    if (answerFile != null) {
-      try {
-        answerFile.delete(project);
-      }
-      catch (IOException e) {
-        LOG.error(e);
-      }
-    }
-    try {
-      answerFile = userFileDir.createChildData(project, answerFileName);
-    }
-    catch (IOException e) {
-      LOG.error(e);
-    }
-    if (answerFile == null) return;
-
     final Document originDocument = FileDocumentManager.getInstance().getDocument(file);
     if (originDocument == null) {
       return;
     }
-    final Document document = FileDocumentManager.getInstance().getDocument(answerFile);
+    final Document document = FileDocumentManager.getInstance().getDocument(file);
     if (document == null) return;
 
     CommandProcessor.getInstance().executeCommand(project, new Runnable() {
index 164e1bef0f0b48d34f111101ad0e24ca0e16035a..cb69bf344d4541637e9e77f0b9ea687486f64167 100644 (file)
@@ -27,7 +27,6 @@ import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.io.FileUtil;
-import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
@@ -77,10 +76,8 @@ public abstract class CCRunTestsAction extends AnAction {
     if (taskFile == null) {
       return;
     }
-    if (psiFile.getName().contains(".answer")) {
-      presentation.setEnabledAndVisible(true);
-      presentation.setText("Run tests from '" + FileUtil.getNameWithoutExtension(psiFile.getName()) + "'");
-    }
+    presentation.setEnabledAndVisible(true);
+    presentation.setText("Run tests from '" + FileUtil.getNameWithoutExtension(psiFile.getName()) + "'");
   }
 
   public void actionPerformed(@NotNull AnActionEvent e) {
@@ -137,32 +134,20 @@ public abstract class CCRunTestsAction extends AnAction {
 
   private static void createTaskFileForTest(@NotNull final VirtualFile taskDir, final String fileName, @NotNull final TaskFile taskFile,
                                             @NotNull final Project project) {
-    try {
-      String answerFileName = FileUtil.getNameWithoutExtension(fileName) + ".answer";
-      final String extension = FileUtilRt.getExtension(fileName);
-      final VirtualFile answerFile = taskDir.findChild(answerFileName + "." + extension);
-      if (answerFile == null) {
-        LOG.debug("could not find answer file " + answerFileName);
-        return;
-      }
-      ApplicationManager.getApplication().runWriteAction(new Runnable() {
-        @Override
-        public void run() {
-          final FileDocumentManager documentManager = FileDocumentManager.getInstance();
-          documentManager.saveAllDocuments();
-        }
-      });
-      final VirtualFile oldTaskFile = taskDir.findChild(fileName);
-      if (oldTaskFile != null) {
-        oldTaskFile.delete(project);
-      }
-      VirtualFile copy = answerFile.copy(project, taskDir, fileName);
-      EduUtils.flushWindows(taskFile, copy, false);
-      createResourceFiles(answerFile, project);
-    }
-    catch (IOException e) {
-      LOG.error(e);
+    final VirtualFile answerFile = taskDir.findChild(fileName);
+    if (answerFile == null) {
+      LOG.debug("could not find answer file " + fileName);
+      return;
     }
+    ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      @Override
+      public void run() {
+        final FileDocumentManager documentManager = FileDocumentManager.getInstance();
+        documentManager.saveAllDocuments();
+      }
+    });
+    EduUtils.flushWindows(taskFile, answerFile, false);
+    createResourceFiles(answerFile, project);
   }
 
   public static void clearTestEnvironment(@NotNull final VirtualFile taskDir, @NotNull final Project project) {
@@ -181,9 +166,6 @@ public abstract class CCRunTestsAction extends AnAction {
         if (file.getName().contains(EduNames.WINDOWS_POSTFIX)) {
           file.delete(project);
         }
-        if (CCProjectService.getInstance(project).isTaskFile(file)) {
-          file.delete(project);
-        }
       }
     }
     catch (IOException e) {
@@ -192,9 +174,9 @@ public abstract class CCRunTestsAction extends AnAction {
   }
 
   protected abstract void executeTests(@NotNull final Project project,
-                                   @NotNull final VirtualFile virtualFile,
-                                   @NotNull final VirtualFile taskDir,
-                                   @NotNull final VirtualFile testFile);
+                                       @NotNull final VirtualFile virtualFile,
+                                       @NotNull final VirtualFile taskDir,
+                                       @NotNull final VirtualFile testFile);
 
   //some tests could compare task files after user modifications with initial task files
   private static void createResourceFiles(@NotNull final VirtualFile file, @NotNull final Project project) {
@@ -229,12 +211,12 @@ public abstract class CCRunTestsAction extends AnAction {
     }
   }
 
-  private static VirtualFile findOrCreateDir(@NotNull final Project project, @NotNull final VirtualFile dir, String name) throws IOException {
+  private static VirtualFile findOrCreateDir(@NotNull final Project project, @NotNull final VirtualFile dir, String name)
+    throws IOException {
     VirtualFile targetDir = dir.findChild(name);
     if (targetDir == null) {
       targetDir = dir.createChildDirectory(project, name);
     }
     return targetDir;
   }
-
 }
index 1c8654ce2bfa099e552e01a815be1a7ba13fed18..040ae838d62e18e0bc30aa93c6f2b9551e4080a9 100644 (file)
@@ -18,6 +18,7 @@ package com.jetbrains.edu.coursecreator.actions;
 import com.intellij.openapi.Disposable;
 import com.intellij.openapi.actionSystem.AnActionEvent;
 import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.actionSystem.LangDataKeys;
 import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.diagnostic.Logger;
@@ -26,6 +27,7 @@ import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.EditorFactory;
 import com.intellij.openapi.editor.ex.EditorEx;
 import com.intellij.openapi.fileEditor.FileDocumentManager;
+import com.intellij.openapi.module.Module;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.ui.FrameWrapper;
@@ -41,6 +43,7 @@ import com.jetbrains.edu.courseFormat.AnswerPlaceholder;
 import com.jetbrains.edu.courseFormat.Course;
 import com.jetbrains.edu.courseFormat.TaskFile;
 import com.jetbrains.edu.coursecreator.CCProjectService;
+import com.jetbrains.edu.coursecreator.CCUtils;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
@@ -61,30 +64,28 @@ public class CCShowPreview extends DumbAwareAction {
     if (!CCProjectService.setCCActionAvailable(e)) {
       return;
     }
+    Project project = e.getProject();
+    if (project == null) {
+      return;
+    }
     Presentation presentation = e.getPresentation();
     presentation.setEnabledAndVisible(false);
     final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
-    if (file != null && file.getName().contains(".answer")) {
+    if (file != null && CCProjectService.getInstance(project).getTaskFile(file.getVirtualFile()) != null) {
       presentation.setEnabledAndVisible(true);
     }
   }
 
   @Override
   public void actionPerformed(@NotNull AnActionEvent e) {
+    //TODO: need to rewrite this action using new GENERATED_ROOT_FOLDER
     final Project project = e.getProject();
-    if (project == null) {
+    Module module = LangDataKeys.MODULE.getData(e.getDataContext());
+    if (project == null || module == null) {
       return;
     }
     final PsiFile file = CommonDataKeys.PSI_FILE.getData(e.getDataContext());
-    if (file == null || !file.getName().contains(".answer")) {
-      return;
-    }
-    final PsiDirectory taskDir = file.getContainingDirectory();
-    if (taskDir == null) {
-      return;
-    }
-    PsiDirectory lessonDir = taskDir.getParentDirectory();
-    if (lessonDir == null) {
+    if (file == null) {
       return;
     }
     final CCProjectService service = CCProjectService.getInstance(project);
@@ -92,37 +93,49 @@ public class CCShowPreview extends DumbAwareAction {
     if (course == null) {
       return;
     }
-    TaskFile taskFile = service.getTaskFile(file.getVirtualFile());
+    VirtualFile virtualFile = file.getVirtualFile();
+    TaskFile taskFile = service.getTaskFile(virtualFile);
     if (taskFile == null) {
       return;
     }
+    final PsiDirectory taskDir = file.getContainingDirectory();
+    if (taskDir == null) {
+      return;
+    }
+    PsiDirectory lessonDir = taskDir.getParentDirectory();
+    if (lessonDir == null) {
+      return;
+    }
+
+
     if (taskFile.getAnswerPlaceholders().isEmpty()) {
       Messages.showInfoMessage("Preview is available for task files with answer placeholders only", "No Preview for This File");
+      return;
     }
     final TaskFile taskFileCopy = new TaskFile();
     TaskFile.copy(taskFile, taskFileCopy);
-    final String taskFileName = CCProjectService.getRealTaskFileName(file.getVirtualFile().getName());
-    if (taskFileName == null) {
+
+
+    VirtualFile generatedFilesFolder = CCUtils.getGeneratedFilesFolder(project, module);
+
+    if (generatedFilesFolder == null) {
       return;
     }
+
     ApplicationManager.getApplication().runWriteAction(new Runnable() {
       @Override
       public void run() {
-        EduUtils.createStudentFileFromAnswer(project, taskDir.getVirtualFile(), taskDir.getVirtualFile(), taskFileName, taskFileCopy);
+        EduUtils.createStudentFileFromAnswer(project, generatedFilesFolder, taskDir.getVirtualFile(), virtualFile.getName(), taskFileCopy);
       }
     });
 
-    String userFileName = CCProjectService.getRealTaskFileName(file.getName());
-    if (userFileName == null) {
-      return;
-    }
-    VirtualFile userFile = taskDir.getVirtualFile().findChild(userFileName);
+    VirtualFile userFile = generatedFilesFolder.findChild(virtualFile.getName());
     if (userFile == null) {
-      LOG.info("Generated file " + userFileName + "was not found");
+      LOG.info("Generated file " + virtualFile.getName() + "was not found");
       return;
     }
     final FrameWrapper showPreviewFrame = new FrameWrapper(project);
-    showPreviewFrame.setTitle(userFileName);
+    showPreviewFrame.setTitle(virtualFile.getName());
     LabeledEditor labeledEditor = new LabeledEditor(null);
     final EditorFactory factory = EditorFactory.getInstance();
     Document document = FileDocumentManager.getInstance().getDocument(userFile);
index ea30845de4617a854d9cefe5bed0c18fce8756f1..b39e29250e907182bd2c1f7c2ced34a461a2645e 100644 (file)
@@ -45,8 +45,7 @@ public class CCTreeStructureProvider implements TreeStructureProvider, DumbAware
           if (virtualFile == null) {
             continue;
           }
-          if (CCProjectService.getInstance(project).isTaskFile(virtualFile)
-              || virtualFile.getName().contains(EduNames.WINDOWS_POSTFIX)) {
+          if (virtualFile.getName().contains(EduNames.WINDOWS_POSTFIX)) {
             continue;
           }
         }
index 7d8c9a447b8f38c7312fdb3371f67a922c24a3a9..4f453c8ab0e73d55263fffb50da559ed83840269 100644 (file)
@@ -13,7 +13,6 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Pair;
 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;
 import com.intellij.openapi.vfs.VirtualFileManager;
 import com.intellij.psi.PsiDirectory;
@@ -146,9 +145,8 @@ public class EduUtils {
     }
 
     if (taskFile.getAnswerPlaceholders().isEmpty()) {
-      String extension = FileUtilRt.getExtension(taskFileName);
-      String nameWithoutExtension = FileUtilRt.getNameWithoutExtension(taskFileName);
-      VirtualFile answerFile = answerFileDir.findChild(nameWithoutExtension + ".answer." + extension);
+      //do not need to replace anything, just copy
+      VirtualFile answerFile = answerFileDir.findChild(taskFileName);
       if (answerFile != null) {
         try {
           answerFile.copy(answerFileDir, userFileDir, taskFileName);
@@ -167,9 +165,11 @@ public class EduUtils {
     }
 
     file = userFileDir.findChild(taskFileName);
-    assert file != null;
-    String answerFileName = file.getNameWithoutExtension() + ".answer." + file.getExtension();
-    VirtualFile answerFile = answerFileDir.findChild(answerFileName);
+    if (file == null) {
+      LOG.info("Failed to find task file " + taskFileName);
+      return;
+    }
+    VirtualFile answerFile = answerFileDir.findChild(taskFileName);
     if (answerFile == null) {
       return;
     }
index 851fd88a81be4b92c192c084e5c970590a6ab420..98753e523f6eb287339b9708fbb04238254fcb82 100644 (file)
@@ -721,14 +721,16 @@ public class EduStepicConnector {
           public void run() {
             final VirtualFile taskDir = task.getTaskDir(project);
             assert taskDir != null;
-            EduUtils.createStudentFileFromAnswer(project, taskDir, taskDir, entry.getKey(), taskFile);
+            VirtualFile ideaDir = project.getBaseDir().findChild(".idea");
+            assert ideaDir != null;
+            EduUtils.createStudentFileFromAnswer(project, ideaDir, taskDir, entry.getKey(), taskFile);
           }
         });
         taskFile.name = entry.getKey();
 
-        final VirtualFile taskDirectory = task.getTaskDir(project);
-        if (taskDirectory == null) return null;
-        final VirtualFile file = taskDirectory.findChild(taskFile.name);
+        VirtualFile ideaDir = project.getBaseDir().findChild(".idea");
+        if (ideaDir == null) return null;
+        final VirtualFile file = ideaDir.findChild(taskFile.name);
         try {
           if (file != null) {
             if (EduUtils.isImage(taskFile.name)) {
index 7ebbe8e81a5d28ae75d5844e63ec6d50eeade1b3..f71f13190e9c620a747b6033f84d8d3bd7ada95e 100644 (file)
@@ -22,7 +22,7 @@ public class PyCCLanguageManager implements CCLanguageManager {
     if (!extension.equals("py")) {
       return null;
     }
-    return getInternalTemplateByName(project, "task.answer.py");
+    return getInternalTemplateByName(project, "task.py");
   }
 
   @Nullable
index 1a8f22eaea469fe9e7322c08192db7267922d113..ebda0f8976f1a3fdc170a6b64b8dd7fe114c6134 100644 (file)
@@ -26,7 +26,6 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileManager;
 import com.intellij.util.PathUtil;
-import com.jetbrains.edu.coursecreator.CCProjectService;
 import com.jetbrains.python.run.PythonConfigurationType;
 import com.jetbrains.python.run.PythonRunConfiguration;
 import org.jetbrains.annotations.NotNull;
@@ -45,10 +44,7 @@ public class PyCCRunTestsAction extends CCRunTestsAction {
     final PythonRunConfiguration configuration = (PythonRunConfiguration)settings.getConfiguration();
     configuration.setScriptName(testFile.getPath());
     configuration.setWorkingDirectory(taskDir.getPath());
-    String taskFileName = CCProjectService.getRealTaskFileName(virtualFile.getName());
-    if (taskFileName == null) {
-      return;
-    }
+    String taskFileName = virtualFile.getName();
     VirtualFile userFile = taskDir.findChild(taskFileName);
     if (userFile == null) {
       return;