Java Code Examples for com.intellij.psi.PsiManager#findFile()

The following examples show how to use com.intellij.psi.PsiManager#findFile() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   File: LibraryGroupNode.java    License: Apache License 2.0 6 votes vote down vote up
public static void addLibraryChildren(final OrderEntry entry, final List<AbstractTreeNode> children, Project project, ProjectViewNode node) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  VirtualFile[] files =
    entry instanceof LibraryOrderEntry ? getLibraryRoots((LibraryOrderEntry)entry) : entry.getFiles(BinariesOrderRootType.getInstance());
  for (final VirtualFile file : files) {
    if (!file.isValid()) continue;
    if (file.isDirectory()) {
      final PsiDirectory psiDir = psiManager.findDirectory(file);
      if (psiDir == null) {
        continue;
      }
      children.add(new PsiDirectoryNode(project, psiDir, node.getSettings()));
    }
    else {
      final PsiFile psiFile = psiManager.findFile(file);
      if (psiFile == null) continue;
      children.add(new PsiFileNode(project, psiFile, node.getSettings()));
    }
  }
}
 
Example 2
@Nullable
private static OCFile correlateTestToHeader(OCFile file) {
  // Quickly check foo_test.cc -> foo.h as well. "getAssociatedFileWithSameName" only does
  // foo.cc <-> foo.h. However, if you do goto-related-symbol again, it will go from
  // foo.h -> foo.cc instead of back to foo_test.cc.
  PsiManager psiManager = PsiManager.getInstance(file.getProject());
  String pathWithoutExtension = FileUtil.getNameWithoutExtension(file.getVirtualFile().getPath());
  for (String testSuffix : PartnerFilePatterns.DEFAULT_PARTNER_SUFFIXES) {
    if (pathWithoutExtension.endsWith(testSuffix)) {
      String possibleHeaderName = StringUtil.trimEnd(pathWithoutExtension, testSuffix) + ".h";
      VirtualFile virtualFile = VfsUtil.findFileByIoFile(new File(possibleHeaderName), false);
      if (virtualFile != null) {
        PsiFile psiFile = psiManager.findFile(virtualFile);
        if (psiFile instanceof OCFile) {
          return (OCFile) psiFile;
        }
      }
    }
  }
  return null;
}
 
Example 3
@NotNull
public static Collection<PsiFile> convertVirtualFilesToPsiFiles(@NotNull Project project, @NotNull Collection<VirtualFile> files) {
    Collection<PsiFile> psiFiles = new HashSet<>();

    PsiManager psiManager = null;
    for (VirtualFile file : files) {
        if(psiManager == null) {
            psiManager = PsiManager.getInstance(project);
        }

        PsiFile psiFile = psiManager.findFile(file);
        if(psiFile != null) {
            psiFiles.add(psiFile);
        }
    }

    return psiFiles;
}
 
Example 4
public static List<PsiElement> resolveTargetByName(@Nls String name, @NotNull Project project, GlobalSearchScope scope) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  final ArrayList<PsiElement> result = new ArrayList<>();
  final Collection<VirtualFile> containingFiles = FileBasedIndex.getInstance().getContainingFiles(NAME, name, scope);
  for (VirtualFile virtualFile : containingFiles) {
    final PsiFile psiFile = psiManager.findFile(virtualFile);
    if (psiFile instanceof PyFile) {
      final PyReferenceExpression referenceExpression = PantsPsiUtil.findTargetDefinitions((PyFile)psiFile).get(name);
      final PsiPolyVariantReference reference = referenceExpression != null ? referenceExpression.getReference() : null;
      final PsiElement definition = reference != null ? reference.resolve() : null;
      if (definition != null) {
        result.add(definition);
      } else if (referenceExpression != null) {
        // at least something
        result.add(referenceExpression);
      }
    }
  }
  return result;
}
 
Example 5
@Nullable
@Override
public PsiElement resolve() {
  Optional<VirtualFile> virtualFile = findFile();
  if (!virtualFile.isPresent()) {
    return null;
  }
  VirtualFile buildFileOrDirectory = PantsUtil.findBUILDFiles(virtualFile.get())
    .stream()
    .findFirst()
    .orElse(virtualFile.get());

  final PsiManager psiManager = PsiManager.getInstance(getElement().getProject());
  final PsiFile buildFile = psiManager.findFile(buildFileOrDirectory);
  return buildFile != null ? buildFile : psiManager.findDirectory(buildFileOrDirectory);
}
 
Example 6
Source Project: consulo   File: BookmarkItem.java    License: Apache License 2.0 5 votes vote down vote up
public static void setupRenderer(SimpleColoredComponent renderer, Project project, Bookmark bookmark, boolean selected) {
  VirtualFile file = bookmark.getFile();
  if (!file.isValid()) {
    return;
  }

  PsiManager psiManager = PsiManager.getInstance(project);

  PsiElement fileOrDir = file.isDirectory() ? psiManager.findDirectory(file) : psiManager.findFile(file);
  if (fileOrDir != null) {
    renderer.setIcon(IconDescriptorUpdaters.getIcon(fileOrDir, 0));
  }

  String description = bookmark.getDescription();
  if (description != null) {
    renderer.append(description + " ", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
  }

  FileStatus fileStatus = FileStatusManager.getInstance(project).getStatus(file);
  TextAttributes attributes = new TextAttributes(fileStatus.getColor(), null, null, EffectType.LINE_UNDERSCORE, Font.PLAIN);
  renderer.append(file.getName(), SimpleTextAttributes.fromTextAttributes(attributes));
  if (bookmark.getLine() >= 0) {
    renderer.append(":", SimpleTextAttributes.GRAYED_ATTRIBUTES);
    renderer.append(String.valueOf(bookmark.getLine() + 1), SimpleTextAttributes.GRAYED_ATTRIBUTES);
  }

  if (!selected) {
    FileColorManager colorManager = FileColorManager.getInstance(project);
    if (fileOrDir instanceof PsiFile) {
      Color color = colorManager.getRendererBackground((PsiFile)fileOrDir);
      if (color != null) {
        renderer.setBackground(color);
      }
    }
  }
}
 
Example 7
Source Project: EasyCode   File: FileUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 读取文件内容(文本文件)
 *
 * @param project 项目对象
 * @param file    文件对象
 * @return 文件内容
 */
public String read(Project project, File file) {
    PsiManager psiManager = PsiManager.getInstance(project);
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file);
    if (virtualFile == null) {
        return null;
    }
    PsiFile psiFile = psiManager.findFile(virtualFile);
    if (psiFile == null) {
        return null;
    }
    return psiFile.getText();
}
 
Example 8
Source Project: buck   File: BuckGotoProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiElement findPsiElementForVirtualFile(Project project, VirtualFile file) {
  PsiManager psiManager = PsiManager.getInstance(project);
  if (file.isDirectory()) {
    return psiManager.findDirectory(file);
  } else {
    return psiManager.findFile(file);
  }
}
 
Example 9
private ProtoPsiFileRoot getTarget(@NotNull String filename, @NotNull Module module) {
    Collection<PsiFileSystemItem> roots = new FilePathReferenceProvider().getRoots(module, getRoot());
    for (PsiFileSystemItem root : roots) {
        VirtualFile file = root.getVirtualFile().findFileByRelativePath(filename);
        if (file != null) {
            PsiManager psiManager = PsiManager.getInstance(getProject());
            PsiFile psiFile = psiManager.findFile(file);
            if (psiFile instanceof ProtoPsiFileRoot) {
                return (ProtoPsiFileRoot) psiFile;
            }
        }
    }
    return null;
}
 
Example 10
Source Project: consulo   File: PackageDependenciesNode.java    License: Apache License 2.0 5 votes vote down vote up
public void fillFiles(Set<PsiFile> set, boolean recursively) {
  final PsiManager psiManager = PsiManager.getInstance(myProject);
  for (VirtualFile vFile : getRegisteredFiles()) {
    final PsiFile psiFile = psiManager.findFile(vFile);
    if (psiFile != null && psiFile.isValid()) {
      set.add(psiFile);
    }
  }
}
 
Example 11
Source Project: intellij   File: BuildFileUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
static BlazePackage getBuildFile(Project project, @Nullable VirtualFile vf) {
  if (vf == null) {
    return null;
  }
  PsiManager manager = PsiManager.getInstance(project);
  PsiFileSystemItem psiFile = vf.isDirectory() ? manager.findDirectory(vf) : manager.findFile(vf);
  if (psiFile == null) {
    return null;
  }
  return BlazePackage.getContainingPackage(psiFile);
}
 
Example 12
Source Project: consulo   File: FileListPasteProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void performPaste(@Nonnull DataContext dataContext) {
  final Project project = dataContext.getData(CommonDataKeys.PROJECT);
  final IdeView ideView = dataContext.getData(LangDataKeys.IDE_VIEW);
  if (project == null || ideView == null) return;

  if (!FileCopyPasteUtil.isFileListFlavorAvailable()) return;

  final Transferable contents = CopyPasteManager.getInstance().getContents();
  if (contents == null) return;
  final List<File> fileList = FileCopyPasteUtil.getFileList(contents);
  if (fileList == null) return;

  final List<PsiElement> elements = new ArrayList<PsiElement>();
  for (File file : fileList) {
    final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
    if (vFile != null) {
      final PsiManager instance = PsiManager.getInstance(project);
      PsiFileSystemItem item = vFile.isDirectory() ? instance.findDirectory(vFile) : instance.findFile(vFile);
      if (item != null) {
        elements.add(item);
      }
    }
  }

  if (elements.size() > 0) {
    final PsiDirectory dir = ideView.getOrChooseDirectory();
    if (dir != null) {
      final boolean move = LinuxDragAndDropSupport.isMoveOperation(contents);
      if (move) {
        new MoveFilesOrDirectoriesHandler().doMove(PsiUtilCore.toPsiElementArray(elements), dir);
      }
      else {
        new CopyFilesOrDirectoriesHandler().doCopy(PsiUtilCore.toPsiElementArray(elements), dir);
      }
    }
  }
}
 
Example 13
Source Project: intellij   File: BuildReferenceManager.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public PsiFileSystemItem resolveFile(File file) {
  VirtualFile vf =
      VirtualFileSystemProvider.getInstance().getSystem().findFileByPath(file.getPath());
  if (vf == null) {
    return null;
  }
  PsiManager manager = PsiManager.getInstance(project);
  return vf.isDirectory() ? manager.findDirectory(vf) : manager.findFile(vf);
}
 
Example 14
Source Project: intellij   File: BlazeCoverageEngine.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiFileSystemItem resolveFile(Project project, @Nullable File file) {
  if (file == null) {
    return null;
  }
  PsiManager manager = PsiManager.getInstance(project);
  VirtualFile vf =
      VirtualFileSystemProvider.getInstance().getSystem().findFileByPath(file.getPath());
  if (vf == null) {
    return null;
  }
  return vf.isDirectory() ? manager.findDirectory(vf) : manager.findFile(vf);
}
 
Example 15
Source Project: consulo   File: GoToLinkTargetAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(AnActionEvent e) {
  Project project = getEventProject(e);
  VirtualFile file = e.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE);
  if (project != null && file != null && file.is(VFileProperty.SYMLINK)) {
    VirtualFile target = file.getCanonicalFile();
    if (target != null) {
      PsiManager psiManager = PsiManager.getInstance(project);
      PsiFileSystemItem psiFile = target.isDirectory() ? psiManager.findDirectory(target) : psiManager.findFile(target);
      if (psiFile != null) {
        ProjectView.getInstance(project).select(psiFile, target, false);
      }
    }
  }
}
 
Example 16
private PsiElement findPsi(@Nonnull VirtualFile vFile) {
  if (!vFile.isValid()) {
    return null;
  }
  PsiManager psiManager = PsiManager.getInstance(myProject);
  return vFile.isDirectory() ? psiManager.findDirectory(vFile) : psiManager.findFile(vFile);
}
 
Example 17
@Nullable
@Override
public PsiElement getDocumentationElementForLookupItem(PsiManager psiManager, Object object, PsiElement element) {
	if (object instanceof StringTableKey) {
		return ((StringTableKey) object).getIDXmlTag();
	}
	if (SQFParserDefinition.isCommand(element.getNode().getElementType())) {
		return element;
	}
	if (element instanceof PsiFile) {
		return element;
	}
	if (object instanceof HeaderConfigFunction) {
		HeaderConfigFunction function = (HeaderConfigFunction) object;
		if (!function.getFunctionFileExtension().equals(".sqf")) {
			return null;
		}
		List<VirtualFile> rootConfigVirtFiles = ArmaPluginUtil.getConfigVirtualFiles(element);
		if (rootConfigVirtFiles.isEmpty()) {
			return null;
		}
		VirtualFile functionVirtFile = HeaderConfigFunctionUtil.locateConfigFunctionVirtualFile(rootConfigVirtFiles, function);
		if (functionVirtFile == null) {
			return null;
		}
		return psiManager.findFile(functionVirtFile);
	}
	return null;
}
 
Example 18
Source Project: consulo   File: ProjectViewModuleNode.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
@RequiredReadAction
public Collection<AbstractTreeNode> getChildren() {
  Module module = getValue();
  if (module == null || module.isDisposed()) {
    return Collections.emptyList();
  }
  ModuleRootManager rootManager = ModuleRootManager.getInstance(module);
  ModuleFileIndex moduleFileIndex = rootManager.getFileIndex();

  final VirtualFile[] contentRoots = rootManager.getContentRoots();
  final List<AbstractTreeNode> children = new ArrayList<>(contentRoots.length + 1);
  final PsiManager psiManager = PsiManager.getInstance(module.getProject());
  for (final VirtualFile contentRoot : contentRoots) {
    if (!moduleFileIndex.isInContent(contentRoot)) continue;

    if (contentRoot.isDirectory()) {
      PsiDirectory directory = psiManager.findDirectory(contentRoot);
      if(directory != null) {
        children.add(new PsiDirectoryNode(getProject(), directory, getSettings()));
      }
    }
    else {
      PsiFile file = psiManager.findFile(contentRoot);
      if(file != null) {
        children.add(new PsiFileNode(getProject(), file, getSettings()));
      }
    }
  }
  return children;
}
 
Example 19
private void processFile(Project project, VirtualFile file) {
  if (StdFileTypes.JAVA.equals(file.getFileType())) {
    final PsiManager psiManager = PsiManager.getInstance(project);
    PsiJavaFile psiFile = (PsiJavaFile) psiManager.findFile(file);
    if (psiFile != null) {
      process(project, psiFile);
    }
  }
}
 
Example 20
Source Project: consulo   File: FileReferenceHelper.java    License: Apache License 2.0 4 votes vote down vote up
public static PsiFileSystemItem getPsiFileSystemItem(PsiManager psiManager, VirtualFile file) {
  return file.isDirectory() ? psiManager.findDirectory(file) : psiManager.findFile(file);
}