Search in sources :

Example 91 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-community by JetBrains.

the class XsltDebuggerSession method openLocation.

@Nullable
public static Editor openLocation(Project project, @NotNull String uri, int lineNumber) {
    try {
        final VirtualFile file = VfsUtil.findFileByURL(new URI(uri).toURL());
        final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, lineNumber, 0);
        descriptor.navigate(true);
        return FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    } catch (MalformedURLException | URISyntaxException e) {
        //To change body of catch statement use File | Settings | File Templates.
        e.printStackTrace();
        return null;
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MalformedURLException(java.net.MalformedURLException) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Nullable(org.jetbrains.annotations.Nullable)

Example 92 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-community by JetBrains.

the class CCProjectComponent method transformFiles.

private static void transformFiles(Course course, Project project) {
    List<VirtualFile> files = getAllAnswerTaskFiles(course, project);
    for (VirtualFile answerFile : files) {
        ApplicationManager.getApplication().runWriteAction(() -> {
            String answerName = answerFile.getName();
            String nameWithoutExtension = FileUtil.getNameWithoutExtension(answerName);
            String name = FileUtil.getNameWithoutExtension(nameWithoutExtension) + "." + FileUtilRt.getExtension(answerName);
            VirtualFile parent = answerFile.getParent();
            VirtualFile file = parent.findChild(name);
            try {
                if (file != null) {
                    file.delete(CCProjectComponent.class);
                }
                VirtualFile windowsDescrFile = parent.findChild(FileUtil.getNameWithoutExtension(name) + EduNames.WINDOWS_POSTFIX);
                if (windowsDescrFile != null) {
                    windowsDescrFile.delete(CCProjectComponent.class);
                }
                answerFile.rename(CCProjectComponent.class, name);
            } catch (IOException e) {
                LOG.error(e);
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException)

Example 93 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-community by JetBrains.

the class CCStudyActionListener method beforeCheck.

@Override
public void beforeCheck(AnActionEvent event) {
    Project project = event.getProject();
    if (project == null) {
        return;
    }
    if (!CCUtils.isCourseCreator(project)) {
        return;
    }
    VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(event.getDataContext());
    if (virtualFile == null) {
        return;
    }
    TaskFile taskFile = StudyUtils.getTaskFile(project, virtualFile);
    if (taskFile == null) {
        return;
    }
    Task task = taskFile.getTask();
    VirtualFile taskDir = StudyUtils.getTaskDir(virtualFile);
    if (taskDir == null) {
        return;
    }
    CCUtils.updateResources(project, task, taskDir);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TaskFile(com.jetbrains.edu.learning.courseFormat.TaskFile) Project(com.intellij.openapi.project.Project) Task(com.jetbrains.edu.learning.courseFormat.Task)

Example 94 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-community by JetBrains.

the class CCSubtaskChangeListener method subtaskChanged.

@Override
public void subtaskChanged(@NotNull Project project, @NotNull Task task, int oldSubtaskNumber, int newSubtaskNumber) {
    VirtualFile taskDir = task.getTaskDir(project);
    if (taskDir == null) {
        return;
    }
    Course course = StudyTaskManager.getInstance(project).getCourse();
    if (course == null) {
        return;
    }
    StudyLanguageManager manager = StudyUtils.getLanguageManager(course);
    if (manager == null) {
        return;
    }
    String testFileName = manager.getTestFileName();
    if (newSubtaskNumber != 0) {
        String nameWithoutExtension = FileUtil.getNameWithoutExtension(testFileName);
        String extension = FileUtilRt.getExtension(testFileName);
        testFileName = nameWithoutExtension + EduNames.SUBTASK_MARKER + newSubtaskNumber + "." + extension;
    }
    VirtualFile newTestFile = taskDir.findChild(testFileName);
    if (newTestFile == null) {
        return;
    }
    FileEditorManager editorManager = FileEditorManager.getInstance(project);
    List<VirtualFile> testFiles = ContainerUtil.filter(taskDir.getChildren(), file -> CCUtils.isTestsFile(project, file) && editorManager.isFileOpen(file));
    if (testFiles.isEmpty()) {
        return;
    }
    Editor selectedTextEditor = editorManager.getSelectedTextEditor();
    for (VirtualFile testFile : testFiles) {
        editorManager.closeFile(testFile);
    }
    editorManager.openFile(newTestFile, true);
    if (selectedTextEditor != null) {
        reopenSelectedEditor(editorManager, selectedTextEditor);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Course(com.jetbrains.edu.learning.courseFormat.Course) Editor(com.intellij.openapi.editor.Editor) StudyLanguageManager(com.jetbrains.edu.learning.StudyLanguageManager)

Example 95 with VirtualFile

use of com.intellij.openapi.vfs.VirtualFile in project intellij-community by JetBrains.

the class CCUtils method updateHigherElements.

/**
   * 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 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
   */
public static void updateHigherElements(VirtualFile[] dirs, @NotNull final Function<VirtualFile, ? extends StudyItem> getStudyItem, final int threshold, final String prefix, final int delta) {
    ArrayList<VirtualFile> dirsToRename = new ArrayList<>(Collections2.filter(Arrays.asList(dirs), new Predicate<VirtualFile>() {

        @Override
        public boolean apply(VirtualFile dir) {
            final StudyItem item = getStudyItem.fun(dir);
            if (item == null) {
                return false;
            }
            int index = item.getIndex();
            return index > threshold;
        }
    }));
    Collections.sort(dirsToRename, (o1, o2) -> {
        StudyItem item1 = getStudyItem.fun(o1);
        StudyItem item2 = getStudyItem.fun(o2);
        //if we delete some dir we should start increasing numbers in dir names from the end
        return (-delta) * EduUtils.INDEX_COMPARATOR.compare(item1, item2);
    });
    for (final VirtualFile dir : dirsToRename) {
        final StudyItem item = getStudyItem.fun(dir);
        final int newIndex = item.getIndex() + delta;
        item.setIndex(newIndex);
        ApplicationManager.getApplication().runWriteAction(new Runnable() {

            @Override
            public void run() {
                try {
                    dir.rename(this, prefix + newIndex);
                } catch (IOException e) {
                    LOG.error(e);
                }
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StudyItem(com.jetbrains.edu.learning.courseFormat.StudyItem) Predicate(com.google.common.base.Predicate)

Aggregations

VirtualFile (com.intellij.openapi.vfs.VirtualFile)5465 File (java.io.File)762 Project (com.intellij.openapi.project.Project)720 Nullable (org.jetbrains.annotations.Nullable)720 NotNull (org.jetbrains.annotations.NotNull)703 PsiFile (com.intellij.psi.PsiFile)571 Module (com.intellij.openapi.module.Module)501 IOException (java.io.IOException)327 ArrayList (java.util.ArrayList)260 Document (com.intellij.openapi.editor.Document)244 PsiElement (com.intellij.psi.PsiElement)209 Test (org.junit.Test)196 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)124 PsiDirectory (com.intellij.psi.PsiDirectory)124 XmlFile (com.intellij.psi.xml.XmlFile)124 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)116 Editor (com.intellij.openapi.editor.Editor)115 FileChooserDescriptor (com.intellij.openapi.fileChooser.FileChooserDescriptor)101 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)91 List (java.util.List)90