public static void enumerate(
      @NotNull PsiElement host,
      @NotNull PsiFile containingFile,
      boolean probeUp,
      @NotNull PsiLanguageInjectionHost.InjectedPsiVisitor visitor) {
    // do not inject into nonphysical files except during completion
    if (!containingFile.isPhysical() && containingFile.getOriginalFile() == containingFile) {
      final PsiElement context =
          InjectedLanguageManager.getInstance(containingFile.getProject())
              .getInjectionHost(containingFile);
      if (context == null) return;

      final PsiFile file = context.getContainingFile();
      if (file == null || !file.isPhysical() && file.getOriginalFile() == file) return;
    }

    if (containingFile.getViewProvider() instanceof InjectedFileViewProvider)
      return; // no injection inside injection

    PsiElement inTree = loadTree(host, containingFile);
    if (inTree != host) {
      host = inTree;
      containingFile = host.getContainingFile();
    }

    MultiHostRegistrarImpl registrar = probeElementsUp(host, containingFile, probeUp);
    if (registrar == null) {
      return;
    }
    List<Pair<Place, PsiFile>> places = registrar.getResult();
    for (Pair<Place, PsiFile> pair : places) {
      PsiFile injectedPsi = pair.second;
      visitor.visit(injectedPsi, pair.first);
    }
  }
Exemplo n.º 2
0
  @Override
  public boolean isInProject(@NotNull PsiElement element) {
    PsiFile file = element.getContainingFile();
    if (file != null
        && file.isPhysical()
        && file.getViewProvider().getVirtualFile() instanceof LightVirtualFile) return true;

    if (element instanceof PsiDirectoryContainer) {
      PsiDirectory[] dirs = ((PsiDirectoryContainer) element).getDirectories();
      for (PsiDirectory dir : dirs) {
        if (!isInProject(dir)) return false;
      }
      return true;
    }

    VirtualFile virtualFile = null;
    if (file != null) {
      virtualFile = file.getViewProvider().getVirtualFile();
    } else if (element instanceof PsiFileSystemItem) {
      virtualFile = ((PsiFileSystemItem) element).getVirtualFile();
    }

    if (virtualFile != null) {
      return myExcludedFileIndex.isInContent(virtualFile);
    }
    return false;
  }
Exemplo n.º 3
0
 @Nullable
 private static String getQuickNavigateInfo(PsiElement element) {
   final String name =
       ElementDescriptionUtil.getElementDescription(element, UsageViewShortNameLocation.INSTANCE);
   if (StringUtil.isEmpty(name)) return null;
   final String typeName =
       ElementDescriptionUtil.getElementDescription(element, UsageViewTypeLocation.INSTANCE);
   final PsiFile file = element.getContainingFile();
   final StringBuilder sb = new StringBuilder();
   if (StringUtil.isNotEmpty(typeName)) sb.append(typeName).append(" ");
   sb.append("\"").append(name).append("\"");
   if (file != null && file.isPhysical()) {
     sb.append(" [").append(file.getName()).append("]");
   }
   return sb.toString();
 }
 public static void sendAfterChildrenChangedEvent(
     @NotNull PsiManagerImpl manager,
     @NotNull PsiFile scope,
     int oldLength,
     boolean isGenericChange) {
   if (!scope.isPhysical()) {
     manager.afterChange(false);
     return;
   }
   PsiTreeChangeEventImpl event = new PsiTreeChangeEventImpl(manager);
   event.setParent(scope);
   event.setFile(scope);
   event.setOffset(0);
   event.setOldLength(oldLength);
   event.setGenericChange(isGenericChange);
   manager.childrenChanged(event);
 }
 @NotNull
 public <T extends PsiPolyVariantReference> ResolveResult[] resolveWithCaching(
     @NotNull T ref,
     @NotNull PolyVariantResolver<T> resolver,
     boolean needToPreventRecursion,
     boolean incompleteCode,
     @NotNull PsiFile containingFile) {
   ResolveResult[] result =
       resolve(
           ref,
           resolver,
           needToPreventRecursion,
           incompleteCode,
           true,
           containingFile.isPhysical());
   return result == null ? ResolveResult.EMPTY_ARRAY : result;
 }
  @NotNull
  public <T extends PsiPolyVariantReference> ResolveResult[] resolveWithCaching(
      @NotNull final T ref,
      @NotNull final PolyVariantContextResolver<T> resolver,
      boolean needToPreventRecursion,
      final boolean incompleteCode,
      @NotNull final PsiFile containingFile) {
    ProgressIndicatorProvider.checkCanceled();
    ApplicationManager.getApplication().assertReadAccessAllowed();

    int index = getIndex(containingFile.isPhysical(), incompleteCode, true);
    ConcurrentMap<T, ResolveResult[]> map = getMap(index);
    ResolveResult[] result = map.get(ref);
    if (result != null) {
      return result;
    }

    RecursionGuard.StackStamp stamp = myGuard.markStack();
    result =
        needToPreventRecursion
            ? myGuard.doPreventingRecursion(
                Pair.create(ref, incompleteCode),
                true,
                new Computable<ResolveResult[]>() {
                  @Override
                  public ResolveResult[] compute() {
                    return resolver.resolve(ref, containingFile, incompleteCode);
                  }
                })
            : resolver.resolve(ref, containingFile, incompleteCode);

    if (stamp.mayCacheNow()) {
      cache(ref, map, result);
    }
    return result == null ? ResolveResult.EMPTY_ARRAY : result;
  }
 @Override
 public Document getCachedDocument(@NotNull PsiFile file) {
   if (!file.isPhysical()) return null;
   VirtualFile vFile = file.getViewProvider().getVirtualFile();
   return FileDocumentManager.getInstance().getCachedDocument(vFile);
 }
Exemplo n.º 8
0
  private void fireEvent(@NotNull PsiTreeChangeEventImpl event) {
    boolean isRealTreeChange =
        event.getCode() != PsiTreeChangeEventImpl.PsiEventType.PROPERTY_CHANGED
            && event.getCode() != PsiTreeChangeEventImpl.PsiEventType.BEFORE_PROPERTY_CHANGE;

    PsiFile file = event.getFile();
    if (file == null || file.isPhysical()) {
      ApplicationManager.getApplication().assertWriteAccessAllowed();
    }
    if (isRealTreeChange) {
      LOG.assertTrue(
          !myTreeChangeEventIsFiring, "Changes to PSI are not allowed inside event processing");
      myTreeChangeEventIsFiring = true;
    }
    try {
      for (PsiTreeChangePreprocessor preprocessor : myTreeChangePreprocessors) {
        preprocessor.treeChanged(event);
      }

      for (PsiTreeChangeListener listener : myTreeChangeListeners) {
        try {
          switch (event.getCode()) {
            case BEFORE_CHILD_ADDITION:
              listener.beforeChildAddition(event);
              break;

            case BEFORE_CHILD_REMOVAL:
              listener.beforeChildRemoval(event);
              break;

            case BEFORE_CHILD_REPLACEMENT:
              listener.beforeChildReplacement(event);
              break;

            case BEFORE_CHILD_MOVEMENT:
              listener.beforeChildMovement(event);
              break;

            case BEFORE_CHILDREN_CHANGE:
              listener.beforeChildrenChange(event);
              break;

            case BEFORE_PROPERTY_CHANGE:
              listener.beforePropertyChange(event);
              break;

            case CHILD_ADDED:
              listener.childAdded(event);
              break;

            case CHILD_REMOVED:
              listener.childRemoved(event);
              break;

            case CHILD_REPLACED:
              listener.childReplaced(event);
              break;

            case CHILD_MOVED:
              listener.childMoved(event);
              break;

            case CHILDREN_CHANGED:
              listener.childrenChanged(event);
              break;

            case PROPERTY_CHANGED:
              listener.propertyChanged(event);
              break;
          }
        } catch (Exception e) {
          LOG.error(e);
        }
      }
    } finally {
      if (isRealTreeChange) {
        myTreeChangeEventIsFiring = false;
      }
    }
  }
 @Override
 public boolean isPhysical() {
   PsiFile file = getContainingFile();
   return file != null && file.isPhysical();
 }
  public void testCommitInBackground() {
    PsiFile file = getPsiManager().findFile(createFile());
    assertTrue(file.isPhysical());
    final Document document = getPsiDocumentManager().getDocument(file);

    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    getPsiDocumentManager()
        .performWhenAllCommitted(
            new Runnable() {
              @Override
              public void run() {
                assertTrue(getPsiDocumentManager().isCommitted(document));
                semaphore.up();
              }
            });
    waitAndPump(semaphore);
    assertTrue(getPsiDocumentManager().isCommitted(document));

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                document.insertString(0, "class X {}");
              }
            });

    semaphore.down();
    getPsiDocumentManager()
        .performWhenAllCommitted(
            new Runnable() {
              @Override
              public void run() {
                assertTrue(getPsiDocumentManager().isCommitted(document));
                semaphore.up();
              }
            });
    waitAndPump(semaphore);
    assertTrue(getPsiDocumentManager().isCommitted(document));

    final AtomicInteger count = new AtomicInteger();
    final Runnable action =
        new Runnable() {
          @Override
          public void run() {
            count.incrementAndGet();
          }
        };

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                document.insertString(0, "/**/");
                boolean executed =
                    getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", action);
                assertFalse(executed);
                executed = getPsiDocumentManager().cancelAndRunWhenAllCommitted("xxx", action);
                assertFalse(executed);
                assertEquals(0, count.get());
              }
            });

    while (!getPsiDocumentManager().isCommitted(document)) {
      UIUtil.dispatchAllInvocationEvents();
    }
    assertTrue(getPsiDocumentManager().isCommitted(document));
    assertEquals(1, count.get());

    count.set(0);
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                document.insertString(0, "/**/");
                boolean executed = getPsiDocumentManager().performWhenAllCommitted(action);
                assertFalse(executed);
                executed = getPsiDocumentManager().performWhenAllCommitted(action);
                assertFalse(executed);
                assertEquals(0, count.get());
              }
            });

    while (!getPsiDocumentManager().isCommitted(document)) {
      UIUtil.dispatchAllInvocationEvents();
    }
    assertTrue(getPsiDocumentManager().isCommitted(document));
    assertEquals(2, count.get());
  }