Search in sources :

Example 81 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-sdk-docs by JetBrains.

the class ProjectFileIndexSampleAction method actionPerformed.

@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
    Project project = event.getProject();
    final Editor editor = event.getData(CommonDataKeys.EDITOR);
    if (project == null || editor == null) {
        return;
    }
    Document document = editor.getDocument();
    FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    VirtualFile virtualFile = fileDocumentManager.getFile(document);
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    if (virtualFile != null) {
        Module module = projectFileIndex.getModuleForFile(virtualFile);
        String moduleName;
        moduleName = module != null ? module.getName() : "No module defined for file";
        VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFile);
        boolean isLibraryFile = projectFileIndex.isLibraryClassFile(virtualFile);
        boolean isInLibraryClasses = projectFileIndex.isInLibraryClasses(virtualFile);
        boolean isInLibrarySource = projectFileIndex.isInLibrarySource(virtualFile);
        Messages.showInfoMessage("Module: " + moduleName + "\n" + "Module content root: " + moduleContentRoot + "\n" + "Is library file: " + isLibraryFile + "\n" + "Is in library classes: " + isInLibraryClasses + ", Is in library source: " + isInLibrarySource, "Main File Info for" + virtualFile.getName());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Editor(com.intellij.openapi.editor.Editor) Document(com.intellij.openapi.editor.Document) Module(com.intellij.openapi.module.Module)

Example 82 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-common by redhat-developer.

the class RestoreYAMLClutterActionHandlerTest method ExecuteWriteAction_FileIsTekton_EditorTextIsUpdated.

@Test
public void ExecuteWriteAction_FileIsTekton_EditorTextIsUpdated() {
    RestoreYAMLClutterActionHandler restoreYAMLClutterActionHandler = mock(RestoreYAMLClutterActionHandler.class, InvocationOnMock::callRealMethod);
    VirtualFile vf = new LightVirtualFile("name", "content");
    vf.putUserData(CONTENT, "content");
    FileDocumentManager fileDocumentManager = mock(FileDocumentManager.class);
    when(fileDocumentManager.getFile(any())).thenReturn(vf);
    try (MockedStatic<FileDocumentManager> fileDocumentManagerMockedStatic = mockStatic(FileDocumentManager.class)) {
        when(restoreYAMLClutterActionHandler.getUpdatedContent(anyString(), anyString())).thenReturn("content");
        fileDocumentManagerMockedStatic.when(FileDocumentManager::getInstance).thenReturn(fileDocumentManager);
        restoreYAMLClutterActionHandler.executeWriteAction(editor, null, dataContext);
        verify(document, times(1)).setText(anyString());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Test(org.junit.Test)

Example 83 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-common by redhat-developer.

the class RestoreYAMLClutterActionHandlerTest method ExecuteWriteAction_FileIsNotTekton_EditorTextIsNotUpdated.

@Test
public void ExecuteWriteAction_FileIsNotTekton_EditorTextIsNotUpdated() {
    RestoreYAMLClutterActionHandler restoreYAMLClutterActionHandler = new RestoreYAMLClutterActionHandler();
    VirtualFile vf = new LightVirtualFile("name", "content");
    FileDocumentManager fileDocumentManager = mock(FileDocumentManager.class);
    when(fileDocumentManager.getFile(any())).thenReturn(vf);
    try (MockedStatic<FileDocumentManager> fileDocumentManagerMockedStatic = mockStatic(FileDocumentManager.class)) {
        fileDocumentManagerMockedStatic.when(FileDocumentManager::getInstance).thenReturn(fileDocumentManager);
        restoreYAMLClutterActionHandler.executeWriteAction(editor, null, dataContext);
        verify(document, times(0)).setText(anyString());
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Test(org.junit.Test)

Example 84 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugin-v4 by antlr.

the class GenerateParserAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        LOG.error("actionPerformed no project for " + e);
        // whoa!
        return;
    }
    VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e);
    LOG.info("actionPerformed " + (grammarFile == null ? "NONE" : grammarFile));
    if (grammarFile == null)
        return;
    String title = "ANTLR Code Generation";
    boolean canBeCancelled = true;
    // commit changes to PSI and file system
    PsiDocumentManager psiMgr = PsiDocumentManager.getInstance(project);
    FileDocumentManager docMgr = FileDocumentManager.getInstance();
    Document doc = docMgr.getDocument(grammarFile);
    if (doc == null)
        return;
    boolean unsaved = !psiMgr.isCommitted(doc) || docMgr.isDocumentUnsaved(doc);
    if (unsaved) {
        // save event triggers ANTLR run if autogen on
        psiMgr.commitDocument(doc);
        docMgr.saveDocument(doc);
    }
    // from action, they really mean it
    boolean forceGeneration = true;
    RunANTLROnGrammarFile gen = new RunANTLROnGrammarFile(grammarFile, project, title, canBeCancelled, forceGeneration);
    boolean autogen = ANTLRv4GrammarPropertiesStore.getGrammarProperties(project, grammarFile).shouldAutoGenerateParser();
    if (!unsaved || !autogen) {
        // if everything already saved (not stale) then run ANTLR
        // if had to be saved and autogen NOT on, then run ANTLR
        // Otherwise, the save file event will have or will run ANTLR.
        // , "Generating", canBeCancelled, e.getData(PlatformDataKeys.PROJECT));
        ProgressManager.getInstance().run(gen);
        // refresh from disk to see new files
        Set<File> generatedFiles = new HashSet<>();
        generatedFiles.add(new File(gen.getOutputDirName()));
        LocalFileSystem.getInstance().refreshIoFiles(generatedFiles, true, true, null);
        // pop up a notification
        Notification notification = new Notification(RunANTLROnGrammarFile.groupDisplayId, "parser for " + grammarFile.getName() + " generated", "to " + gen.getOutputDirName(), NotificationType.INFORMATION);
        Notifications.Bus.notify(notification, project);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) RunANTLROnGrammarFile(org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) RunANTLROnGrammarFile(org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile) Notification(com.intellij.notification.Notification) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) HashSet(java.util.HashSet)

Example 85 with FileDocumentManager

use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugin-v4 by antlr.

the class ANTLRv4PluginController method getEditor.

public Editor getEditor(VirtualFile vfile) {
    final FileDocumentManager fdm = FileDocumentManager.getInstance();
    final Document doc = fdm.getDocument(vfile);
    if (doc == null)
        return null;
    EditorFactory factory = EditorFactory.getInstance();
    final Editor[] editors = factory.getEditors(doc, previewPanel.project);
    if (editors.length == 0) {
        // in order we'd expect.
        return null;
    }
    // hope just one
    return editors[0];
}
Also used : EditorFactory(com.intellij.openapi.editor.EditorFactory) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) Editor(com.intellij.openapi.editor.Editor)

Aggregations

FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)98 Document (com.intellij.openapi.editor.Document)64 VirtualFile (com.intellij.openapi.vfs.VirtualFile)54 IOException (java.io.IOException)24 Editor (com.intellij.openapi.editor.Editor)15 Project (com.intellij.openapi.project.Project)15 File (java.io.File)13 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)11 PsiFile (com.intellij.psi.PsiFile)9 MockVirtualFile (com.intellij.mock.MockVirtualFile)8 Nullable (org.jetbrains.annotations.Nullable)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 DiscordIntegrationProjectComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent)5 FileDocumentManagerImpl (com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl)4 Module (com.intellij.openapi.module.Module)4 TaskFile (com.jetbrains.edu.learning.courseFormat.TaskFile)4 ArrayList (java.util.ArrayList)4 NotNull (org.jetbrains.annotations.NotNull)4 Application (com.intellij.openapi.application.Application)3 Result (com.intellij.openapi.application.Result)3