com.intellij.lang.folding.FoldingBuilder Java Examples

The following examples show how to use com.intellij.lang.folding.FoldingBuilder. 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: DocumentFoldingInfo.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static Map<PsiElement, FoldingDescriptor> buildRanges(@Nonnull Editor editor, @Nonnull PsiFile psiFile) {
  final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(psiFile.getLanguage());
  final ASTNode node = psiFile.getNode();
  if (node == null) return Collections.emptyMap();
  final FoldingDescriptor[] descriptors = LanguageFolding.buildFoldingDescriptors(foldingBuilder, psiFile, editor.getDocument(), true);
  Map<PsiElement, FoldingDescriptor> ranges = new HashMap<>();
  for (FoldingDescriptor descriptor : descriptors) {
    final ASTNode ast = descriptor.getElement();
    final PsiElement psi = ast.getPsi();
    if (psi != null) {
      ranges.put(psi, descriptor);
    }
  }
  return ranges;
}
 
Example #2
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static void diagnoseIncorrectRange(@Nonnull PsiFile file, @Nonnull Document document, Language language, FoldingBuilder foldingBuilder, FoldingDescriptor descriptor, PsiElement psiElement) {
  String message = "Folding descriptor " +
                   descriptor +
                   " made by " +
                   foldingBuilder +
                   " for " +
                   language +
                   " is outside document range" +
                   ", PSI element: " +
                   psiElement +
                   ", PSI element range: " +
                   psiElement.getTextRange() +
                   "; " +
                   DebugUtil.diagnosePsiDocumentInconsistency(psiElement, document);
  LOG.error(message, ApplicationManager.getApplication().isInternal() ? new Attachment[]{AttachmentFactory.createAttachment(document),
          consulo.logging.attachment.AttachmentFactory.get().create("psiTree.txt", DebugUtil.psiToString(file, false, true))} : Attachment.EMPTY_ARRAY);
}
 
Example #3
Source File: FoldingPolicy.java    From consulo with Apache License 2.0 5 votes vote down vote up
static boolean isCollapsedByDefault(@Nonnull FoldingDescriptor foldingDescriptor, @Nonnull FoldingBuilder foldingBuilder) {
  try {
    return foldingBuilder.isCollapsedByDefault(foldingDescriptor);
  }
  catch (IndexNotReadyException e) {
    LOG.error(e);
    return false;
  }
}
 
Example #4
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Checks the ability to initialize folding in the Dumb Mode for file.
 *
 * @param file the file to test
 * @return true  if folding initialization available in the Dumb Mode
 */
private static boolean supportsDumbModeFolding(@Nonnull PsiFile file) {
  final FileViewProvider viewProvider = file.getViewProvider();
  for (final Language language : viewProvider.getLanguages()) {
    final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
    if (foldingBuilder != null && !DumbService.isDumbAware(foldingBuilder)) return false;
  }
  return true;
}
 
Example #5
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void getFoldingsFor(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull List<? super RegionInfo> elementsToFold, boolean quick) {
  final FileViewProvider viewProvider = file.getViewProvider();
  TextRange docRange = TextRange.from(0, document.getTextLength());
  Comparator<Language> preferBaseLanguage = Comparator.comparing((Language l) -> l != viewProvider.getBaseLanguage());
  List<Language> languages = ContainerUtil.sorted(viewProvider.getLanguages(), preferBaseLanguage.thenComparing(Language::getID));

  DocumentEx copyDoc = languages.size() > 1 ? new DocumentImpl(document.getImmutableCharSequence()) : null;
  List<RangeMarker> hardRefToRangeMarkers = new ArrayList<>();

  for (Language language : languages) {
    final PsiFile psi = viewProvider.getPsi(language);
    final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
    if (psi != null && foldingBuilder != null) {
      for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) {
        PsiElement psiElement = descriptor.getElement().getPsi();
        if (psiElement == null) {
          LOG.error("No PSI for folding descriptor " + descriptor);
          continue;
        }
        TextRange range = descriptor.getRange();
        if (!docRange.contains(range)) {
          diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement);
          continue;
        }

        if (copyDoc != null && !addNonConflictingRegion(copyDoc, range, hardRefToRangeMarkers)) {
          continue;
        }

        RegionInfo regionInfo = new RegionInfo(descriptor, psiElement, foldingBuilder);
        elementsToFold.add(regionInfo);
      }
    }
  }
}
 
Example #6
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
private RegionInfo(@Nonnull FoldingDescriptor descriptor, @Nonnull PsiElement psiElement, @Nonnull FoldingBuilder foldingBuilder) {
  this.descriptor = descriptor;
  element = psiElement;
  Boolean hardCoded = descriptor.isCollapsedByDefault();
  collapsedByDefault = hardCoded == null ? FoldingPolicy.isCollapsedByDefault(descriptor, foldingBuilder) : hardCoded;
  signature = createSignature(psiElement);
}
 
Example #7
Source File: CustomRegionStructureUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
private static boolean isCustomRegionCommentCandidate(@Nonnull PsiElement element) {
  Language language = element.getLanguage();
  if (!Language.ANY.is(language)) {
    for (FoldingBuilder builder : LanguageFolding.INSTANCE.allForLanguage(language)) {
      if (builder instanceof CustomFoldingBuilder) {
        return ((CustomFoldingBuilder)builder).isCustomFoldingCandidate(element);
      }
    }
  }
  return false;
}