Java Code Examples for com.intellij.psi.util.CachedValueProvider#Result

The following examples show how to use com.intellij.psi.util.CachedValueProvider#Result . 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
Source File: CachedValueBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private Data<T> computeData(@Nullable CachedValueProvider.Result<T> result) {
  if (result == null) {
    return new Data<>(null, ArrayUtilRt.EMPTY_OBJECT_ARRAY, ArrayUtil.EMPTY_LONG_ARRAY);
  }
  T value = result.getValue();
  Object[] inferredDependencies = normalizeDependencies(result);
  long[] inferredTimeStamps = new long[inferredDependencies.length];
  for (int i = 0; i < inferredDependencies.length; i++) {
    inferredTimeStamps[i] = getTimeStamp(inferredDependencies[i]);
  }

  if (CachedValueProfiler.canProfile()) {
    ProfilingInfo profilingInfo = CachedValueProfiler.getInstance().getTemporaryInfo(result);
    if (profilingInfo != null) {
      return new ProfilingData<>(value, inferredDependencies, inferredTimeStamps, profilingInfo);
    }
  }

  return new Data<>(value, inferredDependencies, inferredTimeStamps);
}
 
Example 2
Source File: PsiCachedValue.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
protected Object[] normalizeDependencies(@Nonnull CachedValueProvider.Result<T> result) {
  Object[] dependencies = super.normalizeDependencies(result);
  if (dependencies.length > 0 && ContainerUtil.and(dependencies, this::anyChangeImpliesPsiCounterChange)) {
    return ArrayUtil.prepend(PSI_MOD_COUNT_OPTIMIZATION, dependencies);
  }
  return dependencies;
}
 
Example 3
Source File: CachedValueBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
protected Object[] normalizeDependencies(@Nonnull CachedValueProvider.Result<T> result) {
  Object[] items = result.getDependencyItems();
  T value = result.getValue();
  Object[] rawDependencies = myTrackValue && value != null ? ArrayUtil.append(items, value) : items;

  List<Object> flattened = new NotNullList<>(rawDependencies.length);
  collectDependencies(flattened, rawDependencies);
  return ArrayUtil.toObjectArray(flattened);
}
 
Example 4
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static CachedValueProvider.Result<Runnable> getUpdateResult(PsiFile file,
                                                                    @Nonnull Document document,
                                                                    boolean quick,
                                                                    final Project project,
                                                                    final Editor editor,
                                                                    final boolean applyDefaultState) {

  final List<RegionInfo> elementsToFold = getFoldingsFor(file, document, quick);
  final UpdateFoldRegionsOperation operation = new UpdateFoldRegionsOperation(project, editor, file, elementsToFold, applyDefaultStateMode(applyDefaultState), !applyDefaultState, false);
  int documentLength = document.getTextLength();
  AtomicBoolean alreadyExecuted = new AtomicBoolean();
  Runnable runnable = () -> {
    if (alreadyExecuted.compareAndSet(false, true)) {
      int curLength = editor.getDocument().getTextLength();
      boolean committed = PsiDocumentManager.getInstance(project).isCommitted(document);
      if (documentLength != curLength || !committed) {
        LOG.error("Document has changed since fold regions were calculated: " + "lengths " + documentLength + " vs " + curLength + ", " + "document=" + document + ", " + "committed=" + committed);
      }
      editor.getFoldingModel().runBatchFoldingOperationDoNotCollapseCaret(operation);
    }
  };
  Set<Object> dependencies = new HashSet<>();
  dependencies.add(file);
  dependencies.add(editor.getFoldingModel());
  for (RegionInfo info : elementsToFold) {
    dependencies.addAll(info.descriptor.getDependencies());
  }
  return CachedValueProvider.Result.create(runnable, ArrayUtil.toObjectArray(dependencies));
}
 
Example 5
Source File: FileIncludeManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public CachedValueProvider.Result<VirtualFile[]> compute(PsiFile psiFile) {
  VirtualFile[] value = computeFiles(psiFile, myRuntimeOnly);
  // todo: we need "url modification tracker" for VirtualFile
  List<Object> deps = new ArrayList<>(Arrays.asList(value));
  deps.add(psiFile);
  deps.add(VirtualFileManager.getInstance());

  return CachedValueProvider.Result.create(value, deps);
}
 
Example 6
Source File: PsiCachedValueImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected <P> CachedValueProvider.Result<T> doCompute(P param) {
  return myProvider.compute();
}
 
Example 7
Source File: PsiParameterizedCachedValue.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected <X> CachedValueProvider.Result<T> doCompute(X param) {
  return myProvider.compute((P)param);
}
 
Example 8
Source File: CachedValueImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected <P> CachedValueProvider.Result<T> doCompute(P param) {
  return myProvider.compute();
}
 
Example 9
Source File: CachedValueBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
public T setValue(@Nonnull CachedValueProvider.Result<T> result) {
  Data<T> data = computeData(result);
  setData(data);
  return data.getValue();
}
 
Example 10
Source File: ParameterizedCachedValueImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected <X> CachedValueProvider.Result<T> doCompute(X param) {
  return myProvider.compute((P)param);
}
 
Example 11
Source File: ConcatenationInjectorManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void injectLanguages(@Nonnull MultiHostRegistrar registrar, @Nonnull PsiElement context) {
  ConcatenationInjectorManager manager = getInstance(myProject);
  if (manager.myConcatenationInjectors.isEmpty()) {
    return;
  }

  final PsiFile containingFile = ((InjectionRegistrarImpl)registrar).getHostPsiFile();
  Project project = containingFile.getProject();
  long modificationCount = PsiManager.getInstance(project).getModificationTracker().getModificationCount();
  Pair<PsiElement, PsiElement[]> pair = computeAnchorAndOperands(context);
  PsiElement anchor = pair.first;
  PsiElement[] operands = pair.second;
  Integer noInjectionTimestamp = anchor.getUserData(NO_CONCAT_INJECTION_TIMESTAMP);

  InjectionResult result;
  ParameterizedCachedValue<InjectionResult, PsiElement> data = null;
  if (operands.length == 0 || noInjectionTimestamp != null && noInjectionTimestamp == modificationCount) {
    result = null;
  }
  else {
    data = anchor.getUserData(INJECTED_PSI_IN_CONCATENATION);

    result = data == null ? null : data.getValue(context);
    if (result == null || !result.isValid()) {
      result = doCompute(containingFile, project, anchor, operands);
    }
  }
  if (result != null) {
    ((InjectionRegistrarImpl)registrar).addToResults(result);

    if (data == null) {
      CachedValueProvider.Result<InjectionResult> cachedResult = CachedValueProvider.Result.create(result, manager);
      data = CachedValuesManager.getManager(project).createParameterizedCachedValue(context1 -> {
        PsiFile containingFile1 = context1.getContainingFile();
        Project project1 = containingFile1.getProject();
        Pair<PsiElement, PsiElement[]> pair1 = computeAnchorAndOperands(context1);
        InjectionResult result1 = pair1.second.length == 0 ? null : doCompute(containingFile1, project1, pair1.first, pair1.second);
        return result1 == null ? null : CachedValueProvider.Result.create(result1, manager);
      }, false);
      ((PsiParameterizedCachedValue<InjectionResult, PsiElement>)data).setValue(cachedResult);

      anchor.putUserData(INJECTED_PSI_IN_CONCATENATION, data);
      if (anchor.getUserData(NO_CONCAT_INJECTION_TIMESTAMP) != null) {
        anchor.putUserData(NO_CONCAT_INJECTION_TIMESTAMP, null);
      }
    }
  }
  else {
    // cache no-injection flag
    if (anchor.getUserData(INJECTED_PSI_IN_CONCATENATION) != null) {
      anchor.putUserData(INJECTED_PSI_IN_CONCATENATION, null);
    }
    anchor.putUserData(NO_CONCAT_INJECTION_TIMESTAMP, (int)modificationCount);
  }
}
 
Example 12
Source File: CachedValueBase.java    From consulo with Apache License 2.0 votes vote down vote up
protected abstract <P> CachedValueProvider.Result<T> doCompute(P param);