Java Code Examples for com.intellij.psi.impl.PsiManagerImpl

The following examples show how to use com.intellij.psi.impl.PsiManagerImpl. 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
@Inject
public CSharpPartialElementManager(@Nonnull Project project)
{
	myProject = project;
	project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter()
	{
		@Override
		public void beforePsiChanged(boolean isPhysical)
		{
			myCache.clear();
		}
	});

	project.getMessageBus().connect().subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter()
	{
		@Override
		public void rootsChanged(ModuleRootEvent event)
		{
			myCache.clear();
		}
	});
}
 
Example 2
Source Project: consulo   Source File: CodeInsightTestFixtureImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();

  UIUtil.invokeAndWaitIfNeeded(new Runnable() {
    @Override
    public void run() {
      try {
        myProjectFixture.setUp();
        myTempDirFixture.setUp();
      }
      catch (Exception e) {
        throw new RuntimeException(e);
      }
      myPsiManager = (PsiManagerImpl)PsiManager.getInstance(getProject());
      configureInspections(myInspections == null ? LocalInspectionTool.EMPTY_ARRAY : myInspections);

      DaemonCodeAnalyzerImpl daemonCodeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject());
      daemonCodeAnalyzer.prepareForTest();

      DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(false);
      ensureIndexesUpToDate(getProject());
      ((StartupManagerImpl)StartupManagerEx.getInstanceEx(getProject())).runPostStartupActivities(UIAccess.get());
    }
  });
}
 
Example 3
Source Project: consulo   Source File: ResolveScopeManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Inject
public ResolveScopeManagerImpl(Project project, ProjectRootManager projectRootManager, PsiManager psiManager) {
  myProject = project;
  myProjectRootManager = projectRootManager;
  myManager = psiManager;

  myDefaultResolveScopesCache = ConcurrentFactoryMap.createMap((key) -> {
    GlobalSearchScope scope = null;
    for (ResolveScopeProvider resolveScopeProvider : ResolveScopeProvider.EP_NAME.getExtensions()) {
      scope = resolveScopeProvider.getResolveScope(key, myProject);
      if (scope != null) break;
    }
    if (scope == null) scope = getInherentResolveScope(key);
    for (ResolveScopeEnlarger enlarger : ResolveScopeEnlarger.EP_NAME.getExtensions()) {
      final SearchScope extra = enlarger.getAdditionalResolveScope(key, myProject);
      if (extra != null) {
        scope = scope.union(extra);
      }
    }

    return scope;
  });

  ((PsiManagerImpl)psiManager).registerRunnableToRunOnChange(myDefaultResolveScopesCache::clear);
}
 
Example 4
Source Project: consulo   Source File: PsiFileImplUtil.java    License: Apache License 2.0 6 votes vote down vote up
@RequiredUIAccess
public static PsiFile setName(final PsiFile file, String newName) throws IncorrectOperationException {
  VirtualFile vFile = file.getViewProvider().getVirtualFile();
  PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

  try{
    final FileType newFileType = FileTypeRegistry.getInstance().getFileTypeByFileName(newName);
    if (UnknownFileType.INSTANCE.equals(newFileType) || newFileType.isBinary()) {
      // before the file becomes unknown or a binary (thus, not openable in the editor), save it to prevent data loss
      final FileDocumentManager fdm = FileDocumentManager.getInstance();
      final Document doc = fdm.getCachedDocument(vFile);
      if (doc != null) {
        fdm.saveDocumentAsIs(doc);
      }
    }

    vFile.rename(manager, newName);
  }
  catch(IOException e){
    throw new IncorrectOperationException(e);
  }

  return file.getViewProvider().isPhysical() ? manager.findFile(vFile) : file;
}
 
Example 5
Source Project: consulo   Source File: FileBasedIndexImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void registerIndexableSet(@Nonnull IndexableFileSet set, @Nullable Project project) {
  myIndexableSets.add(set);
  myIndexableSetToProjectMap.put(set, project);
  if (project != null) {
    ((PsiManagerImpl)PsiManager.getInstance(project)).addTreeChangePreprocessor(event -> {
      if (event.isGenericChange() && event.getCode() == PsiTreeChangeEventImpl.PsiEventType.CHILDREN_CHANGED) {
        PsiFile file = event.getFile();

        if (file != null) {
          VirtualFile virtualFile = file.getVirtualFile();
          if (virtualFile instanceof VirtualFileWithId) {
            getChangedFilesCollector().getEventMerger().recordTransientStateChangeEvent(virtualFile);
          }
        }
      }
    });
  }
}
 
Example 6
public GraphQLPsiSearchHelper(@NotNull final Project project) {
    myProject = project;
    mySettings = GraphQLSettings.getSettings(project);
    psiManager = PsiManager.getInstance(myProject);
    graphQLInjectionSearchHelper = ServiceManager.getService(GraphQLInjectionSearchHelper.class);
    injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
    graphQLConfigManager = GraphQLConfigManager.getService(myProject);
    pluginDescriptor = PluginManager.getPlugin(PluginId.getId("com.intellij.lang.jsgraphql"));

    final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(myProject);
    defaultProjectFile = (GraphQLFile) psiFileFactory.createFileFromText("Default schema file", GraphQLLanguage.INSTANCE, "");

    GlobalSearchScope defaultProjectFileScope = GlobalSearchScope.fileScope(defaultProjectFile);
    GlobalSearchScope builtInSchemaScope = GlobalSearchScope.fileScope(project, getBuiltInSchema().getVirtualFile());
    GlobalSearchScope builtInRelaySchemaScope = GlobalSearchScope.fileScope(project, getRelayModernDirectivesSchema().getVirtualFile());
    allBuiltInSchemaScopes = builtInSchemaScope
            .union(new ConditionalGlobalSearchScope(builtInRelaySchemaScope, mySettings::isEnableRelayModernFrameworkSupport))
            .union(defaultProjectFileScope)
    ;

    final FileType[] searchScopeFileTypes = GraphQLFindUsagesUtil.getService().getIncludedFileTypes().toArray(FileType.EMPTY_ARRAY);
    searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), searchScopeFileTypes).union(allBuiltInSchemaScopes);
    project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() {
        @Override
        public void beforePsiChanged(boolean isPhysical) {
            // clear the cache on each PSI change
            fileNameToSchemaScope.clear();
        }
    });
}
 
Example 7
public GraphQLReferenceService(@NotNull final Project project) {
    psiSearchHelper = GraphQLPsiSearchHelper.getService(project);
    project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() {
        @Override
        public void beforePsiChanged(boolean isPhysical) {
            // clear the cache on each PSI change
            logicalTypeNameToReference.clear();
        }
    });
}
 
Example 8
Source Project: consulo   Source File: LightProjectBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void registerServices(@Nonnull InjectingContainerBuilder builder) {
  builder.bind(PsiFileFactory.class).to(PsiFileFactoryImpl.class);
  builder.bind(PsiManager.class).to(PsiManagerImpl.class);
  builder.bind(FileIndexFacade.class).to(LightFileIndexFacade.class);
  builder.bind(PsiModificationTracker.class).to(PsiModificationTrackerImpl.class);
}
 
Example 9
Source Project: consulo   Source File: CodeInsightTestFixtureImpl.java    License: Apache License 2.0 5 votes vote down vote up
private long collectAndCheckHighlighting(@Nonnull ExpectedHighlightingData data) {
    final Project project = getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFileImpl file = (PsiFileImpl)getHostFile();
    FileElement hardRefToFileElement = file.calcTreeElement();//to load text

    //to initialize caches
    if (!DumbService.isDumb(project)) {
      CacheManager.getInstance(project).getFilesWithWord(XXX, UsageSearchContext.IN_COMMENTS, GlobalSearchScope.allScope(project), true);
    }

    List<HighlightInfo> infos;
    final long start = System.currentTimeMillis();
    try {
      ((PsiManagerImpl)PsiManager.getInstance(project)).setAssertOnFileLoadingFilter(myJavaFilesFilter, myTestRootDisposable);

//    ProfilingUtil.startCPUProfiling();
      infos = doHighlighting();
      removeDuplicatedRangesForInjected(infos);
//    ProfilingUtil.captureCPUSnapshot("testing");
    }
    finally {
      ((PsiManagerImpl)PsiManager.getInstance(project)).setAssertOnFileLoadingFilter(VirtualFileFilter.NONE, myTestRootDisposable);
    }
    final long elapsed = System.currentTimeMillis() - start;

    data.checkResult(infos, file.getText());
    hardRefToFileElement.hashCode(); // use it so gc won't collect it
    return elapsed;
  }
 
Example 10
Source Project: consulo   Source File: ResolveCache.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public ResolveCache(@Nonnull Project project) {
  project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() {
    @Override
    public void beforePsiChanged(boolean isPhysical) {
      clearCache(isPhysical);
    }
  });
}
 
Example 11
Source Project: consulo   Source File: PsiFileImplUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void doDelete(final PsiFile file) throws IncorrectOperationException {
  final PsiManagerImpl manager = (PsiManagerImpl)file.getManager();

  final VirtualFile vFile = file.getVirtualFile();
  try{
    vFile.delete(manager);
  }
  catch(IOException e){
    throw new IncorrectOperationException(e);
  }
}
 
Example 12
Source Project: consulo   Source File: PsiEventWrapperAspect.java    License: Apache License 2.0 5 votes vote down vote up
private static void sendAfterEvents(TreeChangeEvent changeSet) {
  ASTNode rootElement = changeSet.getRootElement();
  PsiFile file = (PsiFile)rootElement.getPsi();
  if (!file.isPhysical()) {
    promoteNonPhysicalChangesToDocument(rootElement, file);
    ((PsiManagerImpl)file.getManager()).afterChange(false);
    return;
  }

  ((TreeChangeEventImpl)changeSet).fireEvents();
}
 
Example 13
Source Project: consulo   Source File: PsiVFSListener.java    License: Apache License 2.0 5 votes vote down vote up
@Inject
public PsiVFSListener(@Nonnull Project project, Provider<ProjectFileIndex> fileIndex) {
  installGlobalListener();

  myProject = project;
  myFileTypeManager = FileTypeManager.getInstance();
  myFileIndex = fileIndex;
  myManager = (PsiManagerImpl)PsiManager.getInstance(project);
  myFileManager = (FileManagerImpl)myManager.getFileManager();

  if (project.isDefault()) {
    return;
  }

  // events must handled only after pre-startup (https://upsource.jetbrains.com/intellij/review/IDEA-CR-47395)
  StartupManager.getInstance(project).registerPreStartupActivity(() -> {
    MessageBusConnection connection = project.getMessageBus().connect();
    connection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyModuleRootListener());
    connection.subscribe(FileTypeManager.TOPIC, new FileTypeListener() {
      @Override
      public void fileTypesChanged(@Nonnull FileTypeEvent e) {
        myFileManager.processFileTypesChanged(e.getRemovedFileType() != null);
      }
    });
    connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new MyFileDocumentManagerAdapter());
  });
}
 
Example 14
Source Project: consulo   Source File: LightPlatformTestCase.java    License: Apache License 2.0 4 votes vote down vote up
public static void doTearDown(@Nonnull final Project project, ApplicationStarter application, boolean checkForEditors) throws Exception {
  DocumentCommitThread.getInstance().clearQueue();
  CodeStyleSettingsManager.getInstance(project).dropTemporarySettings();
  checkAllTimersAreDisposed();
  UsefulTestCase.doPostponedFormatting(project);

  LookupManager lookupManager = LookupManager.getInstance(project);
  if (lookupManager != null) {
    lookupManager.hideActiveLookup();
  }
  ((StartupManagerImpl)StartupManager.getInstance(project)).prepareForNextTest();
  InspectionProfileManager.getInstance().deleteProfile(PROFILE);
  assertNotNull("Application components damaged", ProjectManager.getInstance());

  new WriteCommandAction.Simple(project) {
    @Override
    @RequiredWriteAction
    protected void run() throws Throwable {
      if (ourSourceRoot != null) {
        try {
          final VirtualFile[] children = ourSourceRoot.getChildren();
          for (VirtualFile child : children) {
            child.delete(this);
          }
        }
        catch (IOException e) {
          //noinspection CallToPrintStackTrace
          e.printStackTrace();
        }
      }
      EncodingManager encodingManager = EncodingManager.getInstance();
      if (encodingManager instanceof EncodingManagerImpl) ((EncodingManagerImpl)encodingManager).clearDocumentQueue();

      FileDocumentManager manager = FileDocumentManager.getInstance();

      ApplicationManager.getApplication().runWriteAction(EmptyRunnable.getInstance()); // Flush postponed formatting if any.
      manager.saveAllDocuments();
      if (manager instanceof FileDocumentManagerImpl) {
        ((FileDocumentManagerImpl)manager).dropAllUnsavedDocuments();
      }
    }
  }.execute().throwException();

  assertFalse(PsiManager.getInstance(project).isDisposed());

  PsiDocumentManagerImpl documentManager = clearUncommittedDocuments(project);
  ((HintManagerImpl)HintManager.getInstance()).cleanup();
  DocumentCommitThread.getInstance().clearQueue();

  UIUtil.invokeAndWaitIfNeeded(new Runnable() {
    @Override
    public void run() {
      ((UndoManagerImpl)UndoManager.getGlobalInstance()).dropHistoryInTests();
      ((UndoManagerImpl)UndoManager.getInstance(project)).dropHistoryInTests();

      UIUtil.dispatchAllInvocationEvents();
    }
  });

  TemplateDataLanguageMappings.getInstance(project).cleanupForNextTest();

  ProjectManagerEx.getInstanceEx().closeTestProject(project);
  //application.setDataProvider(null);
  ourTestCase = null;
  ((PsiManagerImpl)PsiManager.getInstance(project)).cleanupForNextTest();

  CompletionProgressIndicator.cleanupForNextTest();

  if (checkForEditors) {
    checkEditorsReleased();
  }
  if (isLight(project)) {
    // mark temporarily as disposed so that rogue component trying to access it will fail
    ((ProjectImpl)project).setTemporarilyDisposed(true);
    documentManager.clearUncommittedDocuments();
  }
}
 
Example 15
Source Project: consulo   Source File: ChangeInfoImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static PsiManagerImpl getPsiManagerImpl(PsiTreeChangeEventImpl e) {
  return (PsiManagerImpl)e.getSource();
}
 
Example 16
Source Project: consulo   Source File: PsiLargeBinaryFileImpl.java    License: Apache License 2.0 4 votes vote down vote up
public PsiLargeBinaryFileImpl(PsiManagerImpl manager, FileViewProvider viewProvider) {
  super(manager, viewProvider);
}
 
Example 17
Source Project: consulo   Source File: PsiDirectoryImpl.java    License: Apache License 2.0 4 votes vote down vote up
public PsiDirectoryImpl(PsiManagerImpl manager, @Nonnull VirtualFile file) {
  myManager = manager;
  myFile = file;
}