do not use old service
[idea/community.git] / python / educational-core / course-creator / src / com / jetbrains / edu / coursecreator / CCRefactoringElementListenerProvider.java
index ed9b404e04a540d4fcf046256ca7797ea3d15ab0..7dbd894d898035e49963d815ce836eece1e01b87 100644 (file)
 package com.jetbrains.edu.coursecreator;
 
 import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.diagnostic.Logger;
+import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.PsiDirectory;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.refactoring.listeners.RefactoringElementAdapter;
 import com.intellij.refactoring.listeners.RefactoringElementListener;
 import com.intellij.refactoring.listeners.RefactoringElementListenerProvider;
+import com.jetbrains.edu.learning.StudyTaskManager;
+import com.jetbrains.edu.learning.StudyUtils;
 import com.jetbrains.edu.learning.core.EduNames;
 import com.jetbrains.edu.learning.courseFormat.Course;
 import com.jetbrains.edu.learning.courseFormat.Lesson;
 import com.jetbrains.edu.learning.courseFormat.Task;
 import com.jetbrains.edu.learning.courseFormat.TaskFile;
-import com.jetbrains.edu.coursecreator.actions.CCRunTestsAction;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.io.IOException;
 import java.util.Map;
 
 public class CCRefactoringElementListenerProvider implements RefactoringElementListenerProvider {
+  private static final Logger LOG = Logger.getInstance(CCRefactoringElementListenerProvider.class);
+
   @Nullable
   @Override
   public RefactoringElementListener getListener(PsiElement element) {
@@ -62,8 +68,7 @@ public class CCRefactoringElementListenerProvider implements RefactoringElementL
 
     private static void tryToRenameTaskFile(PsiFile file, String oldName) {
       final PsiDirectory taskDir = file.getContainingDirectory();
-      final CCProjectService service = CCProjectService.getInstance(file.getProject());
-      Course course = service.getCourse();
+      Course course = StudyTaskManager.getInstance(file.getProject()).getCourse();
       if (course == null) {
         return;
       }
@@ -82,16 +87,29 @@ public class CCRefactoringElementListenerProvider implements RefactoringElementL
       if (task == null) {
         return;
       }
+      Map<String, TaskFile> taskFiles = task.getTaskFiles();
+      TaskFile taskFile = task.getTaskFile(oldName);
+      if (taskFile == null) {
+        return;
+      }
       ApplicationManager.getApplication().runWriteAction(new Runnable() {
         @Override
         public void run() {
-          CCRunTestsAction.clearTestEnvironment(taskDir.getVirtualFile(), taskDir.getProject());
+          VirtualFile patternFile = StudyUtils.getPatternFile(taskFile, oldName);
+          if (patternFile != null) {
+            try {
+              patternFile.delete(CCRefactoringElementListenerProvider.class);
+            }
+            catch (IOException e) {
+              LOG.info(e);
+            }
+          }
         }
       });
-      Map<String, TaskFile> taskFiles = task.getTaskFiles();
-      TaskFile taskFile = task.getTaskFile(oldName);
+
       taskFiles.remove(oldName);
       taskFiles.put(file.getName(), taskFile);
+      CCUtils.createResourceFile(file.getVirtualFile(), course, taskDir.getVirtualFile());
     }
 
     @Override