@Nullable
 public static IProject getFrom(@NotNull Project project) {
   IProject gsProject = project.getUserData(PROJECT_KEY);
   if (gsProject == null) {
     project.putUserData(PROJECT_KEY, gsProject = new IjProject(project));
   }
   return gsProject;
 }
 @Override
 public void removeProjectManagerListener(
     @NotNull Project project, @NotNull ProjectManagerListener listener) {
   List<ProjectManagerListener> listeners = project.getUserData(LISTENERS_IN_PROJECT_KEY);
   LOG.assertTrue(listeners != null);
   boolean removed = listeners.remove(listener);
   LOG.assertTrue(removed);
 }
 @NotNull
 public static String getCreationPlace(@NotNull Project project) {
   String place = project.getUserData(CREATION_PLACE);
   Object base;
   try {
     base = project.isDisposed() ? "" : project.getBaseDir();
   } catch (Exception e) {
     base = " (" + e + " while getting base dir)";
   }
   return project + (place != null ? place : "") + base;
 }
 @TestOnly
 public static void pushInjectors(@NotNull Project project) {
   InjectedLanguageManagerImpl cachedManager =
       (InjectedLanguageManagerImpl) project.getUserData(INSTANCE_CACHE);
   if (cachedManager == null) return;
   try {
     assert cachedManager.myInjectorsClone.isEmpty() : cachedManager.myInjectorsClone;
   } finally {
     cachedManager.myInjectorsClone.clear();
   }
   cachedManager.myInjectorsClone.putAll(cachedManager.injectors);
 }
 @Override
 public void addProjectManagerListener(
     @NotNull Project project, @NotNull ProjectManagerListener listener) {
   List<ProjectManagerListener> listeners = project.getUserData(LISTENERS_IN_PROJECT_KEY);
   if (listeners == null) {
     listeners =
         ((UserDataHolderEx) project)
             .putUserDataIfAbsent(
                 LISTENERS_IN_PROJECT_KEY,
                 ContainerUtil.<ProjectManagerListener>createLockFreeCopyOnWriteList());
   }
   listeners.add(listener);
 }
Esempio n. 6
0
  private static boolean wasEverInitialized(@NotNull Project project) {
    Boolean was = project.getUserData(PROJECT_WAS_EVER_INITIALIZED);
    if (was == null) {
      if (project.isInitialized()) {
        was = Boolean.valueOf(true);
        project.putUserData(PROJECT_WAS_EVER_INITIALIZED, was);
      } else {
        was = Boolean.valueOf(false);
      }
    }

    return was.booleanValue();
  }
 private static Set<String> collectDevPatternClassNames(@NotNull final Project project) {
   CachedValue<Set<String>> cachedValue = project.getUserData(PATTERN_CLASSES);
   if (cachedValue == null) {
     cachedValue =
         CachedValuesManager.getManager(project)
             .createCachedValue(
                 new CachedValueProvider<Set<String>>() {
                   @Nullable
                   @Override
                   public Result<Set<String>> compute() {
                     return Result.create(
                         calcDevPatternClassNames(project),
                         PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
                   }
                 },
                 false);
     project.putUserData(PATTERN_CLASSES, cachedValue);
   }
   return cachedValue.getValue();
 }
 @TestOnly
 public static void checkInjectorsAreDisposed(@NotNull Project project) {
   InjectedLanguageManagerImpl cachedManager =
       (InjectedLanguageManagerImpl) project.getUserData(INSTANCE_CACHE);
   if (cachedManager == null) return;
   try {
     for (Map.Entry<Class, MultiHostInjector[]> entry : cachedManager.injectors.entrySet()) {
       Class key = entry.getKey();
       if (cachedManager.myInjectorsClone.isEmpty()) return;
       MultiHostInjector[] oldInjectors = cachedManager.myInjectorsClone.get(key);
       for (MultiHostInjector injector : entry.getValue()) {
         if (!ArrayUtil.contains(injector, oldInjectors)) {
           throw new AssertionError("Injector was not disposed: " + key + " -> " + injector);
         }
       }
     }
   } finally {
     cachedManager.myInjectorsClone.clear();
   }
 }
 /**
  * Analyze project with string cache for the whole project. All given files will be analyzed only
  * for descriptors.
  */
 public static AnalyzeExhaust analyzeProjectWithCache(
     @NotNull final Project project, @NotNull final Collection<JetFile> files) {
   // Need lock for getValue(), because parallel threads can start evaluation of compute()
   // simultaneously
   synchronized (lock) {
     CachedValue<AnalyzeExhaust> bindingContextCachedValue = project.getUserData(BINDING_CONTEXT);
     if (bindingContextCachedValue == null) {
       bindingContextCachedValue =
           CachedValuesManager.getManager(project)
               .createCachedValue(
                   new CachedValueProvider<AnalyzeExhaust>() {
                     @Override
                     public Result<AnalyzeExhaust> compute() {
                       try {
                         AnalyzeExhaust analyzeExhaust =
                             analyzeFilesWithJavaIntegration(
                                 project,
                                 files,
                                 Predicates.<PsiFile>alwaysFalse(),
                                 JetControlFlowDataTraceFactory.EMPTY);
                         return new Result<AnalyzeExhaust>(
                             analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT);
                       } catch (ProcessCanceledException e) {
                         throw e;
                       } catch (Throwable e) {
                         DiagnosticUtils.throwIfRunningOnServer(e);
                         LOG.error(e);
                         BindingTraceContext bindingTraceContext = new BindingTraceContext();
                         AnalyzeExhaust analyzeExhaust =
                             new AnalyzeExhaust(bindingTraceContext.getBindingContext(), null);
                         return new Result<AnalyzeExhaust>(
                             analyzeExhaust, PsiModificationTracker.MODIFICATION_COUNT);
                       }
                     }
                   },
                   false);
       project.putUserData(BINDING_CONTEXT, bindingContextCachedValue);
     }
     return bindingContextCachedValue.getValue();
   }
 }
  @NotNull
  private static PsiFile getRootByClasses(@NotNull PsiFile file, @NotNull Class[] classes) {
    final Project project = file.getProject();
    SoftFactoryMap<Class[], PsiFile> map = project.getUserData(PATTERN_INJECTION_CONTEXT);
    if (map == null) {
      map =
          new SoftFactoryMap<Class[], PsiFile>() {

            @Override
            protected PsiFile create(Class[] key) {
              String text =
                  PatternCompilerFactory.getFactory()
                      .getPatternCompiler(key)
                      .dumpContextDeclarations();
              return PsiFileFactory.getInstance(project)
                  .createFileFromText("context.groovy", GroovyFileType.GROOVY_FILE_TYPE, text);
            }
          };
      project.putUserData(PATTERN_INJECTION_CONTEXT, map);
    }
    return map.get(classes);
  }
  public static void scheduleDartPackageRootsUpdate(@NotNull final Project project) {
    if (Registry.is("dart.projects.without.pubspec", false)) return;

    if (project.getUserData(DART_PACKAGE_ROOTS_UPDATE_SCHEDULED_OR_IN_PROGRESS) == Boolean.TRUE) {
      return;
    }

    setDartPackageRootUpdateScheduledOrInProgress(project, Boolean.TRUE);

    DumbService.getInstance(project)
        .smartInvokeLater(
            new Runnable() {
              @Override
              public void run() {
                try {
                  final DartSdk sdk = DartSdk.getDartSdk(project);
                  final Library library = actualizePackagesLibrary(project, sdk);

                  if (library == null) {
                    removeDartPackagesLibraryAndDependencies(project);
                  } else {
                    final Condition<Module> moduleFilter =
                        new Condition<Module>() {
                          @Override
                          public boolean value(final Module module) {
                            return DartSdkGlobalLibUtil.isDartSdkEnabled(module);
                          }
                        };

                    updateDependenciesOnDartPackagesLibrary(project, moduleFilter, library);
                  }
                } finally {
                  setDartPackageRootUpdateScheduledOrInProgress(project, false);
                }
              }
            },
            ModalityState.NON_MODAL);
  }
Esempio n. 12
0
  /** Returns the same as {@link #getGlobalDartSdk()} but much faster */
  @Nullable
  public static DartSdk getDartSdk(@NotNull final Project project) {
    CachedValue<DartSdk> cachedValue = project.getUserData(CACHED_DART_SDK_KEY);

    if (cachedValue == null) {
      cachedValue =
          CachedValuesManager.getManager(project)
              .createCachedValue(
                  () -> {
                    final DartSdk sdk = getGlobalDartSdk();
                    if (sdk == null) {
                      return new CachedValueProvider.Result<DartSdk>(
                          null, DartProjectComponent.getProjectRootsModificationTracker(project));
                    }

                    List<Object> dependencies = new ArrayList<Object>(3);
                    dependencies.add(
                        DartProjectComponent.getProjectRootsModificationTracker(project));
                    ContainerUtil.addIfNotNull(
                        dependencies,
                        LocalFileSystem.getInstance()
                            .findFileByPath(sdk.getHomePath() + "/version"));
                    ContainerUtil.addIfNotNull(
                        dependencies,
                        LocalFileSystem.getInstance()
                            .findFileByPath(sdk.getHomePath() + "/lib/core/core.dart"));

                    return new CachedValueProvider.Result<DartSdk>(
                        sdk, ArrayUtil.toObjectArray(dependencies));
                  },
                  false);

      project.putUserData(CACHED_DART_SDK_KEY, cachedValue);
    }

    return cachedValue.getValue();
  }
 @TestOnly
 public static void checkInjectorsAreDisposed(@NotNull Project project) {
   InjectedLanguageManagerImpl cachedManager =
       (InjectedLanguageManagerImpl) project.getUserData(INSTANCE_CACHE);
   if (cachedManager == null) {
     return;
   }
   try {
     ClassMapCachingNulls<MultiHostInjector> cached = cachedManager.cachedInjectors;
     if (cached == null) return;
     for (Map.Entry<Class, MultiHostInjector[]> entry : cached.getBackingMap().entrySet()) {
       Class key = entry.getKey();
       if (cachedManager.myInjectorsClone.isEmpty()) return;
       MultiHostInjector[] oldInjectors = cachedManager.myInjectorsClone.get(key);
       for (MultiHostInjector injector : entry.getValue()) {
         if (ArrayUtil.indexOf(oldInjectors, injector) == -1) {
           throw new AssertionError("Injector was not disposed: " + key + " -> " + injector);
         }
       }
     }
   } finally {
     cachedManager.myInjectorsClone.clear();
   }
 }
 @NotNull
 private static List<ProjectManagerListener> getListeners(Project project) {
   List<ProjectManagerListener> array = project.getUserData(LISTENERS_IN_PROJECT_KEY);
   if (array == null) return Collections.emptyList();
   return array;
 }
  @Override
  public Collection<LineExtensionInfo> getLineExtensions(
      @NotNull Project project, @NotNull VirtualFile file, int lineNumber) {
    if (!Registry.is("ide.debugger.inline")) {
      return null;
    }

    final Map<Pair<VirtualFile, Integer>, Set<XValueNodeImpl>> map =
        project.getUserData(XVariablesView.DEBUG_VARIABLES);
    final Map<VirtualFile, Long> timestamps =
        project.getUserData(XVariablesView.DEBUG_VARIABLES_TIMESTAMPS);
    final Document doc = FileDocumentManager.getInstance().getDocument(file);

    if (map == null || timestamps == null || doc == null) {
      return null;
    }

    Map<Variable, VariableValue> oldValues = project.getUserData(CACHE);
    if (oldValues == null) {
      oldValues = new HashMap<Variable, VariableValue>();
      project.putUserData(CACHE, oldValues);
    }
    final Long timestamp = timestamps.get(file);
    if (timestamp == null || timestamp < doc.getModificationStamp()) {
      return null;
    }
    Set<XValueNodeImpl> values = map.get(Pair.create(file, lineNumber));
    if (values != null && !values.isEmpty()) {
      ArrayList<LineExtensionInfo> result = new ArrayList<LineExtensionInfo>();
      for (XValueNodeImpl value : values) {
        SimpleColoredText text = new SimpleColoredText();
        XValueTextRendererImpl renderer = new XValueTextRendererImpl(text);
        final XValuePresentation presentation = value.getValuePresentation();
        if (presentation == null) continue;
        try {
          if (presentation instanceof XValueCompactPresentation) {
            ((XValueCompactPresentation) presentation).renderValue(renderer, value);
          } else {
            presentation.renderValue(renderer);
          }
        } catch (Exception e) {
          continue;
        }
        final Color color = getForeground();
        final String name = value.getName();
        result.add(new LineExtensionInfo("  " + name + ": ", color, null, null, Font.PLAIN));

        Variable var = new Variable(name, lineNumber);
        VariableValue variableValue = oldValues.get(var);
        if (variableValue == null) {
          variableValue = new VariableValue(text.toString(), null, value.hashCode());
          oldValues.put(var, variableValue);
        }
        if (variableValue.valueNodeHashCode != value.hashCode()) {
          variableValue.old = variableValue.actual;
          variableValue.actual = text.toString();
          variableValue.valueNodeHashCode = value.hashCode();
        }

        if (!variableValue.isChanged()) {
          for (String s : text.getTexts()) {
            result.add(new LineExtensionInfo(s, color, null, null, Font.PLAIN));
          }
        } else {
          variableValue.produceChangedParts(result);
        }
      }
      return result;
    }

    return null;
  }