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

The following examples show how to use com.intellij.psi.PsiManager#getInstance() . 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 File: PsiElementUtils.java    From Thinkphp5-Plugin with MIT License 6 votes vote down vote up
@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 2
Source File: FieldReferenceProviderImpl.java    From protobuf-jetbrains-plugin with Apache License 2.0 6 votes vote down vote up
@Nullable
private PsiFile loadInMemoryDescriptorProto() {
    if (inm == null) {
        for (Module module : ModuleManager.getInstance(project).getModules()) {
            ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
            VirtualFile[] allSourceRoots = moduleRootManager.orderEntries().getAllSourceRoots();
            for (VirtualFile allSourceRoot : allSourceRoots) {
                PsiDirectory directory = PsiManager.getInstance(project).findDirectory(allSourceRoot);
                if (directory != null && directory.isValid()) {
                    String relPath = "google/protobuf/descriptor.proto";
                    VirtualFile file = directory.getVirtualFile().findFileByRelativePath(relPath);
                    if (file != null) {
                        PsiManager psiManager = PsiManager.getInstance(project);
                        PsiFile psiFile = psiManager.findFile(file);
                        if (psiFile instanceof ProtoPsiFileRoot) {
                            inm = psiFile;
                            return (ProtoPsiFileRoot) psiFile;
                        }
                    }
                }
            }
        }
    }
    return inm;
}
 
Example 3
Source File: ThriftDeclarationIndex.java    From intellij-thrift with Apache License 2.0 6 votes vote down vote up
public static List<ThriftDeclaration> findDeclaration(@NotNull final String name,
                                                      @NotNull Project project,
                                                      @NotNull GlobalSearchScope scope) {
  final List<ThriftDeclaration> result = new ArrayList<ThriftDeclaration>();
  final PsiManager manager = PsiManager.getInstance(project);
  FileBasedIndex.getInstance().getFilesWithKey(
    THRIFT_DECLARATION_INDEX,
    Collections.singleton(name),
    new Processor<VirtualFile>() {
      @Override
      public boolean process(VirtualFile file) {
        PsiFile psiFile = manager.findFile(file);
        if (psiFile != null) {
          for (PsiElement child : psiFile.getChildren()) {
            if (child instanceof ThriftTopLevelDeclaration && name.equals(((ThriftDeclaration)child).getName())) {
              result.add((ThriftDeclaration)child);
            }
          }
        }
        return true;
      }
    },
    scope
  );
  return result;
}
 
Example 4
Source File: PsiFileReferenceHelper.java    From consulo with Apache License 2.0 6 votes vote down vote up
static Collection<PsiFileSystemItem> getContextsForModule(@Nonnull Module module, @Nonnull String packageName, @Nullable GlobalSearchScope scope) {
  List<PsiFileSystemItem> result = null;
  Query<VirtualFile> query = DirectoryIndex.getInstance(module.getProject()).getDirectoriesByPackageName(packageName, false);
  PsiManager manager = null;

  for (VirtualFile file : query) {
    if (scope != null && !scope.contains(file)) continue;
    if (result == null) {
      result = new ArrayList<>();
      manager = PsiManager.getInstance(module.getProject());
    }
    PsiDirectory psiDirectory = manager.findDirectory(file);
    if (psiDirectory != null) result.add(psiDirectory);
  }

  return result != null ? result : Collections.emptyList();
}
 
Example 5
Source File: HaskellFileIndex.java    From intellij-haskforce with Apache License 2.0 6 votes vote down vote up
public static List<HaskellFile> getPsiFiles(@NotNull Project project) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  return ContainerUtil.map(
    getVirtualFiles(project),
    new Function<VirtualFile, HaskellFile>() {
      @Override
      public HaskellFile fun(VirtualFile vFile) {
        PsiFile psiFile = psiManager.findFile(vFile);
        if (psiFile == null) {
          throw new AssertionError("Could not find psi file for " + vFile);
        }
        if (psiFile instanceof HaskellFile) return (HaskellFile) psiFile;
        throw new AssertionError("Expected HaskellFile, got " + psiFile + " from " + vFile);
      }
    }
  );
}
 
Example 6
Source File: FilenameIndex.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean processFilesByName(@Nonnull final String name,
                                         boolean directories,
                                         boolean caseSensitively,
                                         @Nonnull Processor<? super PsiFileSystemItem> processor,
                                         @Nonnull final GlobalSearchScope scope,
                                         @Nonnull final Project project,
                                         @Nullable IdFilter idFilter) {
  final Collection<VirtualFile> files;

  if (caseSensitively) {
    files = getService().getVirtualFilesByName(project, name, scope, idFilter);
  }
  else {
    files = getVirtualFilesByNameIgnoringCase(name, scope, project, idFilter);
  }

  if (files.isEmpty()) return false;
  PsiManager psiManager = PsiManager.getInstance(project);
  int processedFiles = 0;

  for (VirtualFile file : files) {
    if (!file.isValid()) continue;
    if (!directories && !file.isDirectory()) {
      PsiFile psiFile = psiManager.findFile(file);
      if (psiFile != null) {
        if (!processor.process(psiFile)) return true;
        ++processedFiles;
      }
    }
    else if (directories && file.isDirectory()) {
      PsiDirectory dir = psiManager.findDirectory(file);
      if (dir != null) {
        if (!processor.process(dir)) return true;
        ++processedFiles;
      }
    }
  }
  return processedFiles > 0;
}
 
Example 7
Source File: GoToLinkTargetAction.java    From consulo with 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 8
Source File: BlazeGoPackage.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<PsiFile> files() {
  PsiManager psiManager = PsiManager.getInstance(getProject());
  files.replaceAll(
      (file, oldGoFile) ->
          oldGoFile.filter(PsiFile::isValid).isPresent()
              ? oldGoFile
              : Optional.ofNullable(VfsUtils.resolveVirtualFile(file, false))
                  .map(psiManager::findFile)
                  .filter(GoFile.class::isInstance));
  return files.values().stream()
      .flatMap(Streams::stream)
      .filter(PsiFile::isValid)
      .collect(toImmutableSet());
}
 
Example 9
Source File: AbstractSingularHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Project project) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  final PsiType elementType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager);

  return PsiTypeUtil.createCollectionType(psiManager, CommonClassNames.JAVA_UTIL_ARRAY_LIST, elementType);
}
 
Example 10
Source File: ExternalLibrariesNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public static void addLibraryChildren(final LibraryOrderEntry entry, final List<AbstractTreeNode> children, Project project, ProjectViewNode node) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  final VirtualFile[] files = entry.getFiles(BinariesOrderRootType.getInstance());
  for (final VirtualFile file : files) {
    final PsiDirectory psiDir = psiManager.findDirectory(file);
    if (psiDir == null) {
      continue;
    }
    children.add(new PsiDirectoryNode(project, psiDir, node.getSettings()));
  }
}
 
Example 11
Source File: BlazeJavascriptTestLocator.java    From intellij with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private static List<Location> findJasmineTestCase(
    Project project, TargetIdeInfo target, String suiteName, @Nullable String testName) {
  if (!searchForJasmineSources.getValue()) {
    return ImmutableList.of();
  }
  BlazeProjectData projectData =
      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
  if (projectData == null) {
    return ImmutableList.of();
  }
  PsiManager psiManager = PsiManager.getInstance(project);
  return OutputArtifactResolver.resolveAll(
          project,
          projectData.getArtifactLocationDecoder(),
          getJasmineSources(projectData, target))
      .stream()
      .map(f -> VfsUtils.resolveVirtualFile(f, /* refreshIfNeeded= */ false))
      .filter(Objects::nonNull)
      .map(psiManager::findFile)
      .filter(JSFile.class::isInstance)
      .map(JSFile.class::cast)
      .map(JasmineFileStructureBuilder.getInstance()::buildTestFileStructure)
      .map(file -> findJasmineSuiteByName(file, suiteName))
      .filter(Objects::nonNull)
      .map(testSuite -> maybeFindJasmineTestCase(testSuite, testName))
      .map(AbstractJasmineElement::getEnclosingPsiElement)
      .map(PsiLocation::new)
      .collect(ImmutableList.toImmutableList());
}
 
Example 12
Source File: PackageDependenciesNode.java    From consulo with 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 13
Source File: SingularGuavaMapHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
protected PsiType getBuilderFieldType(@NotNull PsiType psiFieldType, @NotNull Project project) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  final PsiType keyType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 0);
  final PsiType valueType = PsiTypeUtil.extractOneElementType(psiFieldType, psiManager, CommonClassNames.JAVA_UTIL_MAP, 1);

  return PsiTypeUtil.createCollectionType(psiManager, collectionQualifiedName + ".Builder", keyType, valueType);
}
 
Example 14
Source File: FileUtils.java    From EasyCode with 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 15
Source File: CSharpLightElementBuilder.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
public CSharpLightElementBuilder(Project project)
{
	super(PsiManager.getInstance(project), CSharpLanguage.INSTANCE);
}
 
Example 16
Source File: FileReferenceHelper.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public PsiFileSystemItem getPsiFileSystemItem(final Project project, @Nonnull final VirtualFile file) {
  final PsiManager psiManager = PsiManager.getInstance(project);
  return getPsiFileSystemItem(psiManager, file);
}
 
Example 17
Source File: BlazeLightResourceClassService.java    From intellij with Apache License 2.0 4 votes vote down vote up
public Builder(Project project) {
  this.psiManager = PsiManager.getInstance(project);
}
 
Example 18
Source File: DummyType.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
public DummyType(Project project, PsiElement scope, SomeType someType)
{
	super(PsiManager.getInstance(project), CSharpLanguage.INSTANCE);
	myScope = scope;
	mySomeType = someType;
}
 
Example 19
Source File: ChangeSignatureProcessorBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected ChangeSignatureProcessorBase(Project project, @Nullable Runnable prepareSuccessfulCallback, ChangeInfo changeInfo) {
  super(project, prepareSuccessfulCallback);
  myChangeInfo = changeInfo;
  myManager = PsiManager.getInstance(project);
}
 
Example 20
Source File: ExtensionQualifierAsCallArgumentWrapper.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
public ExtensionQualifierAsCallArgumentWrapper(Project project, DotNetTypeRef qualifierTypeRef)
{
	super(PsiManager.getInstance(project), CSharpLanguage.INSTANCE);
	myExpression = new CSharpLightExpression(getManager(), qualifierTypeRef);
}