Search in sources :

Example 1 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project azure-tools-for-java by Microsoft.

the class UIHelperImpl method getOpenedFile.

@Nullable
@Override
public <T extends StorageServiceTreeItem> Object getOpenedFile(@NotNull Object projectObject, @NotNull String accountName, @NotNull T item) {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance((Project) projectObject);
    for (VirtualFile editedFile : fileEditorManager.getOpenFiles()) {
        T editedItem = editedFile.getUserData((Key<T>) name2Key.get(item.getClass()));
        StorageAccount editedStorageAccount = editedFile.getUserData(STORAGE_KEY);
        ClientStorageAccount editedClientStorageAccount = editedFile.getUserData(CLIENT_STORAGE_KEY);
        if (((editedStorageAccount != null && editedStorageAccount.name().equals(accountName)) || (editedClientStorageAccount != null && editedClientStorageAccount.getName().equals(accountName))) && editedItem != null && editedItem.getName().equals(item.getName())) {
            return editedFile;
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable)

Example 2 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-plugins by StepicOrg.

the class ProgrammingLanguageUtils method switchProgrammingLanguage.

public static void switchProgrammingLanguage(@NotNull Project project, @NotNull StepNode targetStepNode, @NotNull SupportedLanguages language) {
    if (!targetStepNode.getSupportedLanguages().contains(language)) {
        return;
    }
    SupportedLanguages currentLang = targetStepNode.getCurrentLang();
    String currentMainFileName = currentLang.getMainFileName();
    String mainFilePath = String.join("/", targetStepNode.getPath(), EduNames.SRC, currentMainFileName);
    VirtualFile mainFile = project.getBaseDir().findFileByRelativePath(mainFilePath);
    boolean mainFileExists = mainFile != null;
    if (currentLang == language && mainFileExists) {
        return;
    }
    if (currentMainFileName.equals(language.getMainFileName()) && mainFileExists) {
        targetStepNode.setCurrentLang(language);
        Metrics.switchLanguage(project, targetStepNode, SUCCESSFUL);
        return;
    }
    PsiDirectory src = getOrCreateSrcPsiDirectory(project, targetStepNode);
    if (src == null) {
        return;
    }
    PsiDirectory hide = getOrCreatePsiDirectory(project, src, EduNames.HIDE);
    if (hide == null) {
        return;
    }
    PsiFile second = findFile(src, language.getMainFileName());
    boolean moveSecond = second == null;
    if (moveSecond) {
        second = getOrCreateMainFile(project, hide.getVirtualFile(), language, targetStepNode);
        if (second == null) {
            logger.error("Can't create Main file: " + language.getMainFileName());
            return;
        }
    }
    PsiFile first = findFile(hide, currentMainFileName);
    boolean moveFirst = first == null;
    if (moveFirst) {
        first = findFile(src, currentMainFileName);
        moveFirst = !second.isEquivalentTo(first);
    }
    targetStepNode.setCurrentLang(language);
    ArrayList<VirtualFile> needClose = closeStepNodeFile(project, targetStepNode);
    PsiFile finalSecond = second;
    PsiFile finalFirst = first;
    boolean finalMoveFirst = moveFirst;
    ApplicationManager.getApplication().invokeAndWait(() -> {
        FileEditorManager.getInstance(project).openFile(finalSecond.getVirtualFile(), true);
        FileEditorManager editorManager = FileEditorManager.getInstance(project);
        needClose.forEach(editorManager::closeFile);
        exchangeFiles(src, hide, finalFirst, finalSecond, finalMoveFirst, moveSecond);
        ProjectView.getInstance(project).selectPsiElement(finalSecond, false);
    });
    Metrics.switchLanguage(project, targetStepNode, SUCCESSFUL);
}
Also used : SupportedLanguages(org.stepik.core.SupportedLanguages) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) ProjectFilesUtils.getOrCreateSrcPsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreateSrcPsiDirectory) ProjectFilesUtils.getOrCreatePsiDirectory(org.stepik.core.utils.ProjectFilesUtils.getOrCreatePsiDirectory) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 3 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager 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 4 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.

the class XPathAction method isEnabled.

protected boolean isEnabled(AnActionEvent event, boolean checkAvailable) {
    final Project project = event.getProject();
    if (project == null) {
        // no active project
        return false;
    }
    Editor editor = CommonDataKeys.EDITOR.getData(event.getDataContext());
    if (editor == null) {
        FileEditorManager fem = FileEditorManager.getInstance(project);
        editor = fem.getSelectedTextEditor();
    }
    if (editor == null) {
        return false;
    }
    // do we have an xml file?
    final PsiDocumentManager cem = PsiDocumentManager.getInstance(project);
    final PsiFile psiFile = cem.getPsiFile(editor.getDocument());
    // this is also true for DTD documents...
    if (!(psiFile instanceof XmlFile) || psiFile.getLanguage() == StdFileTypes.DTD.getLanguage()) {
        return false;
    }
    return !checkAvailable || isEnabledAt((XmlFile) psiFile, editor.getCaretModel().getOffset());
}
Also used : Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) XmlFile(com.intellij.psi.xml.XmlFile) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Example 5 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project intellij-community by JetBrains.

the class StudyBasePluginConfigurator method getFileEditorManagerListener.

@NotNull
@Override
public FileEditorManagerListener getFileEditorManagerListener(@NotNull Project project, @NotNull StudyToolWindow toolWindow) {
    return new FileEditorManagerListener() {

        @Override
        public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
            Task task = getTask(file);
            setTaskText(task, StudyUtils.getTaskDir(file));
            if (task != null) {
                if (task.isChoiceTask()) {
                    final StudyChoiceVariantsPanel choicePanel = new StudyChoiceVariantsPanel(task);
                    toolWindow.setBottomComponent(choicePanel);
                } else {
                    toolWindow.setBottomComponent(null);
                }
            }
        }

        @Override
        public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
            for (VirtualFile openedFile : source.getOpenFiles()) {
                if (StudyUtils.getTaskFile(project, openedFile) != null) {
                    return;
                }
            }
            toolWindow.setEmptyText(project);
        }

        @Override
        public void selectionChanged(@NotNull FileEditorManagerEvent event) {
            VirtualFile file = event.getNewFile();
            if (file != null) {
                Task task = getTask(file);
                setTaskText(task, StudyUtils.getTaskDir(file));
            }
            toolWindow.setBottomComponent(null);
        }

        @Nullable
        private Task getTask(@NotNull VirtualFile file) {
            return StudyUtils.getTaskForFile(project, file);
        }

        private void setTaskText(@Nullable final Task task, @Nullable final VirtualFile taskDirectory) {
            String text = StudyUtils.getTaskTextFromTask(taskDirectory, task);
            if (text == null) {
                toolWindow.setEmptyText(project);
                return;
            }
            toolWindow.setTaskText(text, taskDirectory, project);
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Task(com.jetbrains.edu.learning.courseFormat.Task) StudyChoiceVariantsPanel(com.jetbrains.edu.learning.editor.StudyChoiceVariantsPanel) FileEditorManagerEvent(com.intellij.openapi.fileEditor.FileEditorManagerEvent) FileEditorManagerListener(com.intellij.openapi.fileEditor.FileEditorManagerListener) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)66 VirtualFile (com.intellij.openapi.vfs.VirtualFile)35 FileEditor (com.intellij.openapi.fileEditor.FileEditor)29 Editor (com.intellij.openapi.editor.Editor)22 Project (com.intellij.openapi.project.Project)21 TextEditor (com.intellij.openapi.fileEditor.TextEditor)12 PsiFile (com.intellij.psi.PsiFile)10 Document (com.intellij.openapi.editor.Document)9 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)9 Nullable (org.jetbrains.annotations.Nullable)8 TextRange (com.intellij.openapi.util.TextRange)6 NotNull (org.jetbrains.annotations.NotNull)6 FileEditorManagerListener (com.intellij.openapi.fileEditor.FileEditorManagerListener)5 PsiElement (com.intellij.psi.PsiElement)5 StructureViewComponent (com.intellij.ide.structureView.newStructureView.StructureViewComponent)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 GuiTask (org.fest.swing.edt.GuiTask)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 ResourceBundleAsVirtualFile (com.intellij.lang.properties.editor.ResourceBundleAsVirtualFile)3 PsiClass (com.intellij.psi.PsiClass)3