本文整理汇总了Java中com.intellij.openapi.roots.ProjectFileIndex.getSourceRootForFile方法的典型用法代码示例。如果您正苦于以下问题:Java ProjectFileIndex.getSourceRootForFile方法的具体用法?Java ProjectFileIndex.getSourceRootForFile怎么用?Java ProjectFileIndex.getSourceRootForFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.roots.ProjectFileIndex
的用法示例。
在下文中一共展示了ProjectFileIndex.getSourceRootForFile方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toString
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Override
public String toString() {
if (myText == null) {
Module module = ModuleUtilCore.findModuleForPsiElement(myClass);
if (module != null) {
myText = module.getName();
}
else {
VirtualFile virtualFile = myClass.getContainingFile().getVirtualFile();
final ProjectFileIndex index = ProjectRootManager.getInstance(myClass.getProject()).getFileIndex();
VirtualFile root = index.getSourceRootForFile(virtualFile);
if (root == null) {
root = index.getClassRootForFile(virtualFile);
}
if (root != null) {
myText = root.getName();
}
else {
myText = virtualFile.getPath();
}
}
}
return myText;
}
示例2: getReferencePath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static String getReferencePath(Project project, VirtualFile file) {
final LogicalRoot logicalRoot = LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(file);
if (logicalRoot != null) {
return getRelativePath(file, logicalRoot.getVirtualFile());
}
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
VirtualFile sourceRoot = fileIndex.getSourceRootForFile(file);
if (sourceRoot != null) {
return getRelativePath(file, sourceRoot);
}
VirtualFile root = fileIndex.getContentRootForFile(file);
if (root != null) {
return getRelativePath(file, root);
}
return file.getPath();
}
示例3: registerFixes
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static void registerFixes(QuickFixActionRegistrar registrar, final PsiJavaCodeReferenceElement reference) {
final PsiElement psiElement = reference.getElement();
@NonNls final String referenceName = reference.getRangeInElement().substring(psiElement.getText());
Project project = psiElement.getProject();
final PsiFile containingFile = psiElement.getContainingFile();
if (containingFile == null) return;
PsiDirectory dir = containingFile.getContainingDirectory();
if (dir == null) return;
VirtualFile classVFile = containingFile.getVirtualFile();
if (classVFile == null) return;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
final Module currentModule = fileIndex.getModuleForFile(classVFile);
if (currentModule == null) return;
List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(currentModule).getSourceRoots(JavaModuleSourceRootTypes.SOURCES);
if (sourceRoots.isEmpty()) return;
final PsiDirectory sourceDirectory = PsiManager.getInstance(project).findDirectory(sourceRoots.get(0));
if (sourceDirectory == null) return;
VirtualFile vsourceRoot = fileIndex.getSourceRootForFile(classVFile);
if (vsourceRoot == null) return;
final PsiDirectory sourceRoot = PsiManager.getInstance(project).findDirectory(vsourceRoot);
if (sourceRoot == null) return;
registrar.register(new MoveClassToModuleFix(referenceName, currentModule, sourceRoot, psiElement));
}
示例4: getRootDirectoryForPackage
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static String getRootDirectoryForPackage(PsiDirectory directory) {
PsiManager manager = directory.getManager();
final VirtualFile virtualFile = directory.getVirtualFile();
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(manager.getProject()).getFileIndex();
VirtualFile root = fileIndex.getSourceRootForFile(virtualFile);
if (root == null) {
root = fileIndex.getClassRootForFile(virtualFile);
}
if (root != null) {
return root.getPresentableUrl();
}
return null;
}
示例5: getDirUnderSameSourceRoot
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
private PsiDirectory getDirUnderSameSourceRoot(final PsiDirectory[] directories) {
final VirtualFile sourceFile = mySourceClass.getContainingFile().getVirtualFile();
if (sourceFile != null) {
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(sourceFile);
if (sourceRoot != null) {
for (PsiDirectory dir : directories) {
if (Comparing.equal(fileIndex.getSourceRootForFile(dir.getVirtualFile()), sourceRoot)) {
return dir;
}
}
}
}
return directories[0];
}
示例6: createRenameMoveProcessor
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static MoveDirectoryWithClassesProcessor createRenameMoveProcessor(final String newName,
final PsiPackage psiPackage,
final boolean searchInComments,
final boolean searchInNonJavaFiles) {
final Project project = psiPackage.getProject();
final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
final PsiDirectory[] directories = psiPackage.getDirectories();
return new MoveDirectoryWithClassesProcessor(project, directories, null, searchInComments,
searchInNonJavaFiles, false, null) {
@Override
public TargetDirectoryWrapper getTargetDirectory(final PsiDirectory dir) {
final VirtualFile vFile = dir.getVirtualFile();
final VirtualFile sourceRoot = index.getSourceRootForFile(vFile);
LOG.assertTrue(sourceRoot != null, vFile.getPath());
return new TargetDirectoryWrapper(dir.getManager().findDirectory(sourceRoot), newName.replaceAll("\\.", "\\/"));
}
@Override
protected String getTargetName() {
return newName;
}
@Override
protected String getCommandName() {
return "Rename package";
}
};
}
示例7: collectSourceRoots
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
private static void collectSourceRoots(PsiElement[] psiElements, ProjectFileIndex fileIndex, Set<VirtualFile> initialRoots) {
for (PsiElement element : psiElements) {
final VirtualFile file = PsiUtilCore.getVirtualFile(element);
if (file != null) {
final VirtualFile sourceRootForFile = fileIndex.getSourceRootForFile(file);
if (sourceRootForFile != null) {
initialRoots.add(sourceRootForFile);
}
} else if (element instanceof PsiDirectoryContainer) {
collectSourceRoots(((PsiDirectoryContainer)element).getDirectories(), fileIndex, initialRoots);
}
}
}
示例8: expandCompileScopeIfNeeded
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public static Collection<VirtualFile> expandCompileScopeIfNeeded(final Collection<VirtualFile> result, final CompileContext context) {
final ProjectFileIndex index = ProjectRootManager.getInstance(context.getProject()).getFileIndex();
final THashSet<VirtualFile> set = new THashSet<VirtualFile>();
final THashSet<Module> modules = new THashSet<Module>();
for (VirtualFile file : result) {
if (index.getSourceRootForFile(file) == null) {
set.add(file);
ContainerUtil.addIfNotNull(index.getModuleForFile(file), modules);
}
}
if (!set.isEmpty()) {
((CompileContextEx)context).addScope(new FileSetCompileScope(set, modules.toArray(new Module[modules.size()])));
}
return result;
}
示例9: getPath
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@Nullable
protected String getPath(final Object value) {
final VirtualFile file = (VirtualFile)value;
final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myFile.getProject()).getFileIndex();
if (file != null) {
VirtualFile root = fileIndex.getSourceRootForFile(file);
if (root == null) {
root = fileIndex.getContentRootForFile(file);
}
if (root != null) {
return VfsUtilCore.getRelativePath(file, root, '/');
}
}
return null;
}
示例10: DirectoryNode
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
public DirectoryNode(VirtualFile aDirectory,
Project project,
boolean compactPackages,
boolean showFQName,
VirtualFile baseDir, final VirtualFile[] contentRoots) {
super(project);
myVDirectory = aDirectory;
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
final ProjectFileIndex index = projectRootManager.getFileIndex();
String dirName = aDirectory.getName();
if (showFQName) {
final VirtualFile contentRoot = index.getContentRootForFile(myVDirectory);
if (contentRoot != null) {
if (Comparing.equal(myVDirectory, contentRoot)) {
myFQName = dirName;
}
else {
final VirtualFile sourceRoot = index.getSourceRootForFile(myVDirectory);
if (Comparing.equal(myVDirectory, sourceRoot)) {
myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
}
else if (sourceRoot != null) {
myFQName = VfsUtilCore.getRelativePath(myVDirectory, sourceRoot, '/');
}
else {
myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
}
}
if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
myFQName = getContentRootName(baseDir, myFQName);
}
}
else {
myFQName = FilePatternPackageSet.getLibRelativePath(myVDirectory, index);
}
dirName = myFQName;
} else {
if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
dirName = getContentRootName(baseDir, dirName);
}
}
myDirName = dirName;
myCompactPackages = compactPackages;
}
示例11: createBeanClass
import com.intellij.openapi.roots.ProjectFileIndex; //导入方法依赖的package包/类
@NotNull
private static PsiClass createBeanClass(final WizardData wizardData) throws MyException {
final PsiManager psiManager = PsiManager.getInstance(wizardData.myProject);
final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(wizardData.myProject);
final ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(wizardData.myFormFile);
if (sourceRoot == null) {
throw new MyException(UIDesignerBundle.message("error.form.file.is.not.in.source.root"));
}
final PsiDirectory rootDirectory = psiManager.findDirectory(sourceRoot);
LOG.assertTrue(rootDirectory != null);
final PsiPackage aPackage = JavaPsiFacade.getInstance(psiManager.getProject()).findPackage(wizardData.myPackageName);
if (aPackage == null) {
throw new MyException(UIDesignerBundle.message("error.package.does.not.exist", wizardData.myPackageName));
}
PsiDirectory targetDir = null;
final PsiDirectory[] directories = aPackage.getDirectories();
for (final PsiDirectory psiDirectory : directories) {
if (PsiTreeUtil.isAncestor(rootDirectory, psiDirectory, false)) {
targetDir = psiDirectory;
break;
}
}
if (targetDir == null) {
// todo
throw new MyException(UIDesignerBundle.message("error.cannot.find.package", wizardData.myPackageName));
}
//noinspection HardCodedStringLiteral
final String body =
"public class " + wizardData.myShortClassName + "{\n" +
"public " + wizardData.myShortClassName + "(){}\n" +
"}";
try {
PsiFile sourceFile =
PsiFileFactory.getInstance(psiManager.getProject()).createFileFromText(wizardData.myShortClassName + ".java", body);
sourceFile = (PsiFile)targetDir.add(sourceFile);
final PsiClass beanClass = ((PsiJavaFile)sourceFile).getClasses()[0];
final ArrayList<String> properties = new ArrayList<String>();
final HashMap<String, String> property2fqClassName = new HashMap<String, String>();
final FormProperty2BeanProperty[] bindings = wizardData.myBindings;
for (final FormProperty2BeanProperty binding : bindings) {
if (binding == null || binding.myBeanProperty == null) {
continue;
}
properties.add(binding.myBeanProperty.myName);
// todo: handle "casts" ?
final String propertyClassName = binding.myFormProperty.getComponentPropertyClassName();
property2fqClassName.put(binding.myBeanProperty.myName, propertyClassName);
}
generateBean(beanClass, ArrayUtil.toStringArray(properties), property2fqClassName);
return beanClass;
}
catch (IncorrectOperationException e) {
throw new MyException(e.getMessage());
}
}