8d709065e49ce1d4ba9b86c666d138de44fb80e9
[idea/community.git] / platform / core-impl / src / com / intellij / core / CoreProjectEnvironment.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.core;
3
4 import com.intellij.lang.injection.InjectedLanguageManager;
5 import com.intellij.mock.MockDumbService;
6 import com.intellij.mock.MockFileIndexFacade;
7 import com.intellij.mock.MockProject;
8 import com.intellij.mock.MockResolveScopeManager;
9 import com.intellij.openapi.Disposable;
10 import com.intellij.openapi.extensions.ExtensionPointName;
11 import com.intellij.openapi.project.DumbService;
12 import com.intellij.openapi.roots.FileIndexFacade;
13 import com.intellij.openapi.util.Disposer;
14 import com.intellij.psi.PsiDocumentManager;
15 import com.intellij.psi.PsiFileFactory;
16 import com.intellij.psi.PsiManager;
17 import com.intellij.psi.SmartPointerManager;
18 import com.intellij.psi.impl.*;
19 import com.intellij.psi.impl.file.PsiDirectoryFactory;
20 import com.intellij.psi.impl.file.PsiDirectoryFactoryImpl;
21 import com.intellij.psi.impl.smartPointers.SmartPointerManagerImpl;
22 import com.intellij.psi.impl.source.resolve.ResolveCache;
23 import com.intellij.psi.search.ProjectScopeBuilder;
24 import com.intellij.psi.util.CachedValuesManager;
25 import com.intellij.psi.util.PsiModificationTracker;
26 import com.intellij.util.CachedValuesManagerImpl;
27 import com.intellij.util.messages.MessageBus;
28 import org.jetbrains.annotations.NotNull;
29 import org.picocontainer.PicoContainer;
30
31 public class CoreProjectEnvironment {
32   private final Disposable myParentDisposable;
33   private final CoreApplicationEnvironment myEnvironment;
34
35   protected final FileIndexFacade myFileIndexFacade;
36   protected final PsiManagerImpl myPsiManager;
37   protected final MockProject myProject;
38   protected final MessageBus myMessageBus;
39
40   public CoreProjectEnvironment(@NotNull Disposable parentDisposable, @NotNull CoreApplicationEnvironment applicationEnvironment) {
41     myParentDisposable = parentDisposable;
42     myEnvironment = applicationEnvironment;
43     myProject = createProject(myEnvironment.getApplication().getPicoContainer(), myParentDisposable);
44
45     preregisterServices();
46
47     myFileIndexFacade = createFileIndexFacade();
48     myMessageBus = myProject.getMessageBus();
49
50     PsiModificationTrackerImpl modificationTracker = new PsiModificationTrackerImpl(myProject);
51     myProject.registerService(PsiModificationTracker.class, modificationTracker);
52     myProject.registerService(FileIndexFacade.class, myFileIndexFacade);
53     myProject.registerService(ResolveCache.class, new ResolveCache(myProject));
54
55     myPsiManager = new PsiManagerImpl(myProject);
56     registerProjectComponent(PsiManager.class, myPsiManager);
57     myProject.registerService(SmartPointerManager.class, SmartPointerManagerImpl.class);
58     registerProjectComponent(PsiDocumentManager.class, new CorePsiDocumentManager(myProject, myPsiManager,
59                                                                                   myMessageBus,
60                                                                                   new MockDocumentCommitProcessor()));
61
62     myProject.registerService(ResolveScopeManager.class, createResolveScopeManager(myPsiManager));
63
64     myProject.registerService(PsiFileFactory.class, new PsiFileFactoryImpl(myPsiManager));
65     myProject.registerService(CachedValuesManager.class, new CachedValuesManagerImpl(myProject, new PsiCachedValuesFactory(myPsiManager)));
66     myProject.registerService(PsiDirectoryFactory.class, new PsiDirectoryFactoryImpl(myPsiManager));
67     myProject.registerService(ProjectScopeBuilder.class, createProjectScopeBuilder());
68     myProject.registerService(DumbService.class, new MockDumbService(myProject));
69     myProject.registerService(CoreEncodingProjectManager.class, CoreEncodingProjectManager.class);
70     myProject.registerService(InjectedLanguageManager.class, new CoreInjectedLanguageManager());
71   }
72
73   @NotNull
74   protected MockProject createProject(@NotNull PicoContainer parent, @NotNull Disposable parentDisposable) {
75     return new MockProject(parent, parentDisposable);
76   }
77
78   @NotNull
79   protected ProjectScopeBuilder createProjectScopeBuilder() {
80     return new CoreProjectScopeBuilder(myProject, myFileIndexFacade);
81   }
82
83   protected void preregisterServices() {
84
85   }
86
87   @NotNull
88   protected FileIndexFacade createFileIndexFacade() {
89     return new MockFileIndexFacade(myProject);
90   }
91
92   @NotNull
93   protected ResolveScopeManager createResolveScopeManager(@NotNull PsiManager psiManager) {
94     return new MockResolveScopeManager(psiManager.getProject());
95   }
96
97   public <T> void registerProjectExtensionPoint(@NotNull ExtensionPointName<T> extensionPointName,
98                                                 @NotNull Class<? extends T> aClass) {
99     CoreApplicationEnvironment.registerExtensionPoint(myProject.getExtensionArea(), extensionPointName, aClass);
100   }
101
102   public <T> void addProjectExtension(@NotNull ExtensionPointName<T> name, @NotNull final T extension) {
103     //noinspection TestOnlyProblems
104     name.getPoint(myProject).registerExtension(extension, myParentDisposable);
105   }
106
107   public <T> void registerProjectComponent(@NotNull Class<T> interfaceClass, @NotNull T implementation) {
108     CoreApplicationEnvironment.registerComponentInstance(myProject.getPicoContainer(), interfaceClass, implementation);
109     if (implementation instanceof Disposable) {
110       Disposer.register(myProject, (Disposable) implementation);
111     }
112   }
113
114   @NotNull
115   public Disposable getParentDisposable() {
116     return myParentDisposable;
117   }
118
119   @NotNull
120   public CoreApplicationEnvironment getEnvironment() {
121     return myEnvironment;
122   }
123
124   @NotNull
125   public MockProject getProject() {
126     return myProject;
127   }
128 }