mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
[code] Belonging to source root, module, or directory
This commit is contained in:
parent
2aea928ffa
commit
f1020ad145
@ -29,5 +29,9 @@
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="ToolsMenu" anchor="after"/>
|
||||
</action>
|
||||
<action id="ProjectModel.ProjectFileIndex" class="com.intellij.plugins.project.model.ProjectFileIndexSampleAction" text="FileProjectIndex in Action"
|
||||
description="Illustrates how to get source roots">
|
||||
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
|
||||
</action>
|
||||
</actions>
|
||||
</idea-plugin>
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user