private void doIndexing(
     @NotNull final VirtualFile virtualFile, @NotNull final ProgressIndicator indicator) {
   final int fileId = FileBasedIndex.getFileId(virtualFile);
   indicator.setText("IntelliJence Advisor plugin indexing...");
   indicator.setText2(virtualFile.getCanonicalPath());
   final Long timestamp = getTimestamp(fileId);
   ProgressManager.checkCanceled();
   final long currentTimeStamp = virtualFile.getModificationStamp();
   if (timestamp == null || timestamp != currentTimeStamp) {
     putTimestamp(fileId, currentTimeStamp);
     final ClassReader reader;
     try {
       reader = new ClassReader(virtualFile.contentsToByteArray());
     } catch (IOException e) {
       removeTimestamp(fileId);
       return;
     }
     try {
       for (final AdvisorBaseIndex index : indexes) {
         index.update(fileId, reader);
       }
     } catch (RuntimeException e) {
       log.error(String.format("can't index file: %s", virtualFile.getCanonicalPath()));
       // TODO delete it
       e.printStackTrace();
       throw e;
     }
   }
 }
  @Override
  public void findTargets(
      TargetKind kind,
      Collection<SModel> scope,
      Consumer<NavigationTarget> consumer,
      Consumer<SModel> processedConsumer) {
    final ID<Integer, List<SNodeDescriptor>> indexName = RootNodeNameIndex.NAME;
    final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();

    for (SModel sm : scope) {
      if (sm instanceof EditableSModel && ((EditableSModel) sm).isChanged()) {
        continue;
      }

      DataSource source = sm.getSource();
      if (!(source instanceof FileDataSource)) {
        continue;
      }

      IFile modelFile = ((FileDataSource) source).getFile();
      String ext = FileUtil.getExtension(modelFile.getName());
      if (ext == null
          || modelFile.isDirectory()
          || !(supportedExtensions.contains(ext.toLowerCase()))) {
        continue;
      }

      VirtualFile vf = VirtualFileUtils.getVirtualFile(modelFile);
      if (vf == null) continue; // e.g. model was deleted

      int fileId = FileBasedIndex.getFileId(vf);
      ConcreteFilesGlobalSearchScope fileScope =
          new ConcreteFilesGlobalSearchScope(Collections.singleton(vf));
      List<List<SNodeDescriptor>> descriptors =
          fileBasedIndex.getValues(indexName, fileId, fileScope);
      if (descriptors.isEmpty()) continue;

      boolean needToLoad = false;
      for (NavigationTarget snd : descriptors.get(0)) {
        PropertyConstraintsDescriptor descriptor =
            ConceptRegistry.getInstance()
                .getConstraintsDescriptor(snd.getConcept().getQualifiedName())
                .getProperty(SNodeUtil.property_INamedConcept_name);
        if (descriptor instanceof BasePropertyConstraintsDescriptor
            && !((BasePropertyConstraintsDescriptor) descriptor).isGetterDefault()) {
          needToLoad = true;
          break;
        }
      }

      if (!needToLoad) {
        for (SNodeDescriptor desc : descriptors.get(0)) {
          consumer.consume(desc);
        }
        processedConsumer.consume(sm);
      }
    }
  }
  @Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready = FileBasedIndexImpl.isFileIndexed(vFile, StubUpdatingIndex.INDEX_ID);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      Stub stub;
      try {
        stub = datas.get(0).getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo(
          "created from index: "
              + StubUpdatingIndex.getIndexingStampInfo(vFile)
              + ", wasIndexedAlready="
              + wasIndexedAlready
              + ", queried at "
              + vFile.getTimeStamp());
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 @Override
 public boolean isGenerated(VirtualFile file) {
   if (myGeneratedSources.contains(FileBasedIndex.getFileId(file))) {
     return true;
   }
   if (isUnderRoots(myRootToModuleMap.keySet(), file)) {
     return true;
   }
   final Module module = getModuleByFile(file);
   if (module != null) {
     for (AdditionalOutputDirectoriesProvider provider :
         AdditionalOutputDirectoriesProvider.EP_NAME.getExtensions()) {
       for (String path : provider.getOutputDirectories(getProject(), module)) {
         if (path != null
             && VfsUtilCore.isAncestor(new File(path), new File(file.getPath()), true)) {
           return true;
         }
       }
     }
   }
   return false;
 }
Esempio n. 5
0
  @NotNull
  public static List<LinkReferenceResult> getReferencedFiles(
      @NotNull final VirtualFile _file, @NotNull final Project project) {
    final List<LinkReferenceResult> result = new ArrayList<LinkReferenceResult>();
    if (!(_file.getFileSystem() instanceof LocalFileSystem)) {
      return result;
    }

    FileBasedIndex.getInstance()
        .processValues(
            INDEX_ID,
            FileBasedIndex.getFileId(_file),
            null,
            new FileBasedIndex.ValueProcessor<InfoHolder<LinkInfo>>() {
              public boolean process(final VirtualFile file, final InfoHolder<LinkInfo> value) {
                final PsiManager psiManager = PsiManager.getInstance(project);
                final PsiFile psiFile = psiManager.findFile(file);
                if (psiFile != null) {
                  for (final LinkInfo linkInfo : value.myValues) {
                    if (linkInfo.value != null || linkInfo.scripted) {
                      final PsiFileSystemItem[] item = new PsiFileSystemItem[] {null};
                      if (linkInfo.value != null) {
                        final LeafElement newValueElement =
                            Factory.createSingleLeafElement(
                                XmlTokenType.XML_ATTRIBUTE_VALUE_TOKEN,
                                "\"" + linkInfo.value + "\"",
                                0,
                                linkInfo.value.length() + 2,
                                null,
                                psiManager,
                                psiFile);
                        final PsiElement element = newValueElement.getPsi();
                        final FileReferenceSet set =
                            new FileReferenceSet(
                                StringUtil.stripQuotesAroundValue(element.getText()),
                                element,
                                1,
                                null,
                                true);

                        final FileReference lastReference = set.getLastReference();

                        if (lastReference != null) {
                          final PsiFileSystemItem resolved = lastReference.resolve();
                          if (resolved instanceof PsiFile) {
                            item[0] = resolved;
                          }
                        }
                      }

                      result.add(new MyLinkReferenceResult(item, linkInfo, psiFile));
                    }
                  }
                }
                return true;
              }
            },
            GlobalSearchScope.allScope(project));

    return result;
  }
  @Override
  @Nullable
  public ObjectStubTree readFromVFile(Project project, final VirtualFile vFile) {
    if (DumbService.getInstance(project).isDumb()) {
      return null;
    }

    final int id = Math.abs(FileBasedIndex.getFileId(vFile));
    if (id <= 0) {
      return null;
    }

    boolean wasIndexedAlready =
        ((FileBasedIndexImpl) FileBasedIndex.getInstance()).isFileUpToDate(vFile);

    Document document = FileDocumentManager.getInstance().getCachedDocument(vFile);
    boolean saved =
        document == null || !FileDocumentManager.getInstance().isDocumentUnsaved(document);

    final List<SerializedStubTree> datas =
        FileBasedIndex.getInstance()
            .getValues(StubUpdatingIndex.INDEX_ID, id, GlobalSearchScope.fileScope(project, vFile));
    final int size = datas.size();

    if (size == 1) {
      SerializedStubTree stubTree = datas.get(0);

      if (!stubTree.contentLengthMatches(
          vFile.getLength(), getCurrentTextContentLength(project, vFile, document))) {
        return processError(
            vFile,
            "Outdated stub in index: "
                + StubUpdatingIndex.getIndexingStampInfo(vFile)
                + ", doc="
                + document
                + ", docSaved="
                + saved
                + ", wasIndexedAlready="
                + wasIndexedAlready
                + ", queried at "
                + vFile.getTimeStamp(),
            null);
      }

      Stub stub;
      try {
        stub = stubTree.getStub(false);
      } catch (SerializerNotFoundException e) {
        return processError(
            vFile, "No stub serializer: " + vFile.getPresentableUrl() + ": " + e.getMessage(), e);
      }
      ObjectStubTree tree =
          stub instanceof PsiFileStub
              ? new StubTree((PsiFileStub) stub)
              : new ObjectStubTree((ObjectStubBase) stub, true);
      tree.setDebugInfo("created from index");
      return tree;
    } else if (size != 0) {
      return processError(
          vFile,
          "Twin stubs: "
              + vFile.getPresentableUrl()
              + " has "
              + size
              + " stub versions. Should only have one. id="
              + id,
          null);
    }

    return null;
  }
 @Override
 public void markGenerated(Collection<VirtualFile> files) {
   for (final VirtualFile file : files) {
     myGeneratedSources.add(FileBasedIndex.getFileId(file));
   }
 }