diff --git a/project_model/META-INF/plugin.xml b/project_model/META-INF/plugin.xml index 1fe22e326..fe948d7a2 100644 --- a/project_model/META-INF/plugin.xml +++ b/project_model/META-INF/plugin.xml @@ -29,5 +29,9 @@ description="Illustrates how to get source roots"> + + + \ No newline at end of file diff --git a/project_model/src/com/intellij/plugins/project/model/ProjectFileIndexSampleAction.java b/project_model/src/com/intellij/plugins/project/model/ProjectFileIndexSampleAction.java new file mode 100644 index 000000000..a521d7f2e --- /dev/null +++ b/project_model/src/com/intellij/plugins/project/model/ProjectFileIndexSampleAction.java @@ -0,0 +1,54 @@ +package com.intellij.plugins.project.model; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileEditor.FileDocumentManager; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.roots.ProjectFileIndex; +import com.intellij.openapi.roots.ProjectRootManager; +import com.intellij.openapi.ui.Messages; +import com.intellij.openapi.vfs.VirtualFile; + +/** + * @author Anna Bulenkova + */ +public class ProjectFileIndexSampleAction extends AnAction { + @Override + public void update(final AnActionEvent event) { + Project project = event.getProject(); + final Editor editor = event.getData(CommonDataKeys.EDITOR); + boolean visibility = project != null && editor != null; + event.getPresentation().setEnabledAndVisible(visibility); + } + + @Override + public void actionPerformed(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()); + } + } +}