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());
}
}
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());
}
}
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());
}
}
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);
}
}
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];
}
Aggregations