com.intellij.openapi.editor.FoldingGroup Java Examples

The following examples show how to use com.intellij.openapi.editor.FoldingGroup. 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: RouteFoldingBuilder.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    FoldingGroup group = FoldingGroup.newGroup("TYPO3Route");

    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);

    for (final StringLiteralExpression literalExpression : literalExpressions) {
        for (PsiReference reference : literalExpression.getReferences()) {
            if (reference instanceof RouteReference) {
                String value = literalExpression.getContents();

                FoldingDescriptor descriptor = foldRouteReferenceString(reference, value, group);
                if (descriptor != null) {
                    descriptors.add(descriptor);
                }
            }
        }


    }

    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example #2
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 6 votes vote down vote up
UpdateFoldRegionsOperation(@Nonnull Project project,
                           @Nonnull Editor editor,
                           @Nonnull PsiFile file,
                           @Nonnull List<FoldingUpdate.RegionInfo> elementsToFold,
                           @Nonnull ApplyDefaultStateMode applyDefaultState,
                           boolean keepCollapsedRegions,
                           boolean forInjected) {
  myProject = project;
  myEditor = editor;
  myFile = file;
  myApplyDefaultState = applyDefaultState;
  myKeepCollapsedRegions = keepCollapsedRegions;
  myForInjected = forInjected;
  for (FoldingUpdate.RegionInfo regionInfo : elementsToFold) {
    myElementsToFoldMap.putValue(regionInfo.element, regionInfo);
    myRegionInfos.add(regionInfo);
    FoldingGroup group = regionInfo.descriptor.getGroup();
    if (group != null) myGroupedRegionInfos.putValue(group, regionInfo);
  }
}
 
Example #3
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  EditorFoldingInfo info = EditorFoldingInfo.get(myEditor);
  FoldingModelEx foldingModel = (FoldingModelEx)myEditor.getFoldingModel();
  Map<TextRange, Boolean> rangeToExpandStatusMap = new THashMap<>();

  removeInvalidRegions(info, foldingModel, rangeToExpandStatusMap);

  Map<FoldRegion, Boolean> shouldExpand = new THashMap<>();
  Map<FoldingGroup, Boolean> groupExpand = new THashMap<>();
  List<FoldRegion> newRegions = addNewRegions(info, foldingModel, rangeToExpandStatusMap, shouldExpand, groupExpand);

  applyExpandStatus(newRegions, shouldExpand, groupExpand);

  foldingModel.clearDocumentRangesModificationStatus();
}
 
Example #4
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node               The node to which the folding region is related. The node is then passed to
 *                           {@link FoldingBuilder#getPlaceholderText(ASTNode)} and
 *                           {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range              The folded text range in file
 * @param group              Regions with the same group instance expand and collapse together.
 * @param dependencies       folding dependencies: other files or elements that could change, see <a href="#Dependencies">Dependencies</a>
 * @param neverExpands       shall be true for fold regions that must not be ever expanded.
 * @param placeholderText    Text displayed instead of folded text, when the region is collapsed
 * @param collapsedByDefault Whether the region should be collapsed for newly opened files
 */
public FoldingDescriptor(@Nonnull ASTNode node,
                         @Nonnull TextRange range,
                         @Nullable FoldingGroup group,
                         @Nonnull Set<Object> dependencies,
                         boolean neverExpands,
                         @Nullable String placeholderText,
                         @Nullable Boolean collapsedByDefault) {
  assert range.getLength() > 0 : range + ", text: " + node.getText() + ", language = " + node.getPsi().getLanguage();
  myElement = node;
  myRange = range;
  myGroup = group;
  myDependencies = dependencies;
  assert !myDependencies.contains(null);
  myPlaceholderText = placeholderText;
  setFlag(FLAG_NEVER_EXPANDS, neverExpands);
  if (collapsedByDefault != null) {
    setFlag(FLAG_COLLAPSED_BY_DEFAULT_DEFINED, true);
    setFlag(FLAG_COLLAPSED_BY_DEFAULT, collapsedByDefault);
  }
}
 
Example #5
Source File: RouteFoldingBuilder.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@Nullable
private FoldingDescriptor foldRouteReferenceString(PsiReference reference, String value, FoldingGroup group) {
    PsiElement element = reference.getElement();
    TextRange foldingRange = new TextRange(element.getTextRange().getStartOffset() + 1, element.getTextRange().getEndOffset() - 1);

    if (!RouteIndex.hasRoute(element.getProject(), value)) {
        return null;
    }

    Collection<RouteStub> route = RouteIndex.getRoute(element.getProject(), value);
    if (route.size() == 0) {
        return null;
    }

    RouteStub routeDef = route.iterator().next();

    return new FoldingDescriptor(element.getNode(), foldingRange, group) {
        @Nullable
        @Override
        public String getPlaceholderText() {
            if (routeDef.getPath() == null) {
                return routeDef.getController() + "::" + routeDef.getMethod();
            }

            return routeDef.getPath();
        }
    };
}
 
Example #6
Source File: TranslationFoldingBuilder.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    if (!TYPO3CMSProjectSettings.isEnabled(root) || !TYPO3CMSProjectSettings.getInstance(root.getProject()).translationEnableTextFolding) {
        return FoldingDescriptor.EMPTY;
    }

    FoldingGroup group = FoldingGroup.newGroup("TYPO3Translation");

    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<StringLiteralExpression> literalExpressions = PsiTreeUtil.findChildrenOfType(root, StringLiteralExpression.class);

    for (final StringLiteralExpression literalExpression : literalExpressions) {
        String value = literalExpression.getContents();

        if (value.startsWith("LLL:")) {
            final String transResult = Translator.translateLLLString(literalExpression);

            if (transResult != null) {
                TextRange foldingRange = new TextRange(literalExpression.getTextRange().getStartOffset() + 1, literalExpression.getTextRange().getEndOffset() - 1);
                descriptors.add(new FoldingDescriptor(literalExpression.getNode(), foldingRange, group) {
                    @Override
                    public String getPlaceholderText() {

                        return transResult;
                    }
                });
            }
        }
    }

    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example #7
Source File: NamedFoldingDescriptor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public NamedFoldingDescriptor(@Nonnull ASTNode node,
                              @Nonnull final TextRange range,
                              @Nullable FoldingGroup group,
                              @Nonnull String placeholderText) {
  super(node, range, group);
  myPlaceholderText = placeholderText;
}
 
Example #8
Source File: JsonnetFoldingBuilder.java    From intellij-jsonnet with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull PsiElement root, @NotNull Document document, boolean quick) {
    List<FoldingDescriptor> descriptors = new ArrayList<>();
    Collection<PsiElement> literalExpressions = PsiTreeUtil.findChildrenOfType(root, JsonnetObj.class);
    literalExpressions.addAll(PsiTreeUtil.findChildrenOfType(root, JsonnetArr.class));
    literalExpressions.addAll(PsiTreeUtil.findChildrenOfType(root, JsonnetArrcomp.class));
    for (final PsiElement literalExpression : literalExpressions) {
        FoldingGroup group = FoldingGroup.newGroup(
                "jsonnet-" + literalExpression.getTextRange().getStartOffset() +
                        "-" + literalExpression.getTextRange().getEndOffset()
        );
        int start = literalExpression.getTextRange().getStartOffset() + 1;
        int end = literalExpression.getTextRange().getEndOffset() - 1;
        if (end > start)
            descriptors.add(
                    new FoldingDescriptor(
                            literalExpression.getNode(),
                            new TextRange(start, end),
                            group
                    ) {
                        @Override
                        public String getPlaceholderText() {
                            return "...";
                        }
                    }
            );
    }
    return descriptors.toArray(new FoldingDescriptor[0]);
}
 
Example #9
Source File: FoldingModelWindow.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public List<FoldRegion> getGroupedRegions(FoldingGroup group) {
  List<FoldRegion> hostRegions = myDelegate.getGroupedRegions(group);
  List<FoldRegion> result = new ArrayList<>();
  for (FoldRegion hostRegion : hostRegions) {
    FoldingRegionWindow window = getWindowRegion(hostRegion);
    if (window != null) result.add(window);
  }
  return result;
}
 
Example #10
Source File: FoldingModelWindow.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public FoldRegion createFoldRegion(int startOffset, int endOffset, @Nonnull String placeholder, FoldingGroup group, boolean neverExpands) {
  TextRange hostRange = myDocumentWindow.injectedToHost(new TextRange(startOffset, endOffset));
  if (hostRange.getLength() < 2) return null;
  FoldRegion hostRegion = myDelegate.createFoldRegion(hostRange.getStartOffset(), hostRange.getEndOffset(), placeholder, group, neverExpands);
  if (hostRegion == null) return null;
  int startShift = Math.max(0, myDocumentWindow.hostToInjected(hostRange.getStartOffset()) - startOffset);
  int endShift = Math.max(0, endOffset - myDocumentWindow.hostToInjected(hostRange.getEndOffset()) - startShift);
  FoldingRegionWindow window = new FoldingRegionWindow(myDocumentWindow, myEditorWindow, hostRegion, startShift, endShift);
  hostRegion.putUserData(FOLD_REGION_WINDOW, window);
  return window;
}
 
Example #11
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean regionOrGroupCanBeRemovedWhenCollapsed(FoldRegion region) {
  FoldingGroup group = region.getGroup();
  List<FoldRegion> affectedRegions = group != null && myEditor instanceof EditorEx ? ((EditorEx)myEditor).getFoldingModel().getGroupedRegions(group) : Collections.singletonList(region);
  for (FoldRegion affectedRegion : affectedRegions) {
    if (regionCanBeRemovedWhenCollapsed(affectedRegion)) return true;
  }
  return false;
}
 
Example #12
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void applyExpandStatus(@Nonnull List<? extends FoldRegion> newRegions, @Nonnull Map<FoldRegion, Boolean> shouldExpand, @Nonnull Map<FoldingGroup, Boolean> groupExpand) {
  for (final FoldRegion region : newRegions) {
    final FoldingGroup group = region.getGroup();
    final Boolean expanded = group == null ? shouldExpand.get(region) : groupExpand.get(group);

    if (expanded != null) {
      region.setExpanded(expanded.booleanValue());
    }
  }
}
 
Example #13
Source File: NamedFoldingDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public NamedFoldingDescriptor(@Nonnull ASTNode node, int start, int end, @Nullable FoldingGroup group, @Nonnull String placeholderText) {
  this(node, new TextRange(start, end), group, placeholderText);
}
 
Example #14
Source File: FoldingModelEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
FoldRegion createFoldRegion(int startOffset, int endOffset, @Nonnull String placeholder, @Nullable FoldingGroup group, boolean neverExpands);
 
Example #15
Source File: UpdateFoldRegionsOperation.java    From consulo with Apache License 2.0 4 votes vote down vote up
private List<FoldRegion> addNewRegions(@Nonnull EditorFoldingInfo info,
                                       @Nonnull FoldingModelEx foldingModel,
                                       @Nonnull Map<TextRange, Boolean> rangeToExpandStatusMap,
                                       @Nonnull Map<FoldRegion, Boolean> shouldExpand,
                                       @Nonnull Map<FoldingGroup, Boolean> groupExpand) {
  List<FoldRegion> newRegions = new ArrayList<>();
  SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject);
  for (FoldingUpdate.RegionInfo regionInfo : myRegionInfos) {
    ProgressManager.checkCanceled();
    FoldingDescriptor descriptor = regionInfo.descriptor;
    FoldingGroup group = descriptor.getGroup();
    TextRange range = descriptor.getRange();
    String placeholder = null;
    try {
      placeholder = descriptor.getPlaceholderText();
    }
    catch (IndexNotReadyException ignore) {
    }
    if (range.getEndOffset() > myEditor.getDocument().getTextLength()) {
      LOG.error(String.format("Invalid folding descriptor detected (%s). It ends beyond the document range (%d)", descriptor, myEditor.getDocument().getTextLength()));
      continue;
    }
    FoldRegion region = foldingModel.createFoldRegion(range.getStartOffset(), range.getEndOffset(), placeholder == null ? "..." : placeholder, group, descriptor.isNonExpandable());
    if (region == null) continue;

    if (descriptor.isNonExpandable()) region.putUserData(FoldingModelImpl.SELECT_REGION_ON_CARET_NEARBY, Boolean.TRUE);

    PsiElement psi = descriptor.getElement().getPsi();

    if (psi == null || !psi.isValid() || !myFile.isValid()) {
      region.dispose();
      continue;
    }

    region.setGutterMarkEnabledForSingleLine(descriptor.isGutterMarkEnabledForSingleLine());

    if (descriptor.canBeRemovedWhenCollapsed()) region.putUserData(CAN_BE_REMOVED_WHEN_COLLAPSED, Boolean.TRUE);
    region.putUserData(COLLAPSED_BY_DEFAULT, regionInfo.collapsedByDefault);
    region.putUserData(SIGNATURE, ObjectUtils.chooseNotNull(regionInfo.signature, NO_SIGNATURE));

    info.addRegion(region, smartPointerManager.createSmartPsiElementPointer(psi));
    newRegions.add(region);

    boolean expandStatus = !descriptor.isNonExpandable() && shouldExpandNewRegion(range, rangeToExpandStatusMap, regionInfo.collapsedByDefault);
    if (group == null) {
      shouldExpand.put(region, expandStatus);
    }
    else {
      final Boolean alreadyExpanded = groupExpand.get(group);
      groupExpand.put(group, alreadyExpanded == null ? expandStatus : alreadyExpanded.booleanValue() || expandStatus);
    }
  }

  return newRegions;
}
 
Example #16
Source File: FoldingRegionWindow.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FoldingGroup getGroup() {
  return myHostRegion.getGroup();
}
 
Example #17
Source File: WebFoldingModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public FoldRegion createFoldRegion(int startOffset, int endOffset, @Nonnull String placeholder, @Nullable FoldingGroup group, boolean neverExpands) {
  return null;
}
 
Example #18
Source File: WebFoldingModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public List<FoldRegion> getGroupedRegions(FoldingGroup group) {
  return Collections.emptyList();
}
 
Example #19
Source File: NamedFoldingDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public NamedFoldingDescriptor(@Nonnull PsiElement e, int start, int end, @Nullable FoldingGroup group, @Nonnull String placeholderText) {
  this(e.getNode(), new TextRange(start, end), group, placeholderText);
}
 
Example #20
Source File: FoldingModelEx.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
List<FoldRegion> getGroupedRegions(FoldingGroup group);
 
Example #21
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public FoldingGroup getGroup() {
  return myGroup;
}
 
Example #22
Source File: MockFoldRegion.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public FoldingGroup getGroup() {
  throw new UnsupportedOperationException("FoldRegion.getGroup() is not implemented yet at " + getClass());
}
 
Example #23
Source File: NutzLocalizationFoldingDescriptor.java    From NutzCodeInsight with Apache License 2.0 4 votes vote down vote up
public NutzLocalizationFoldingDescriptor(@NotNull ASTNode node, @NotNull TextRange range, String value) {
    super(node, range, FoldingGroup.newGroup("Nutz"));
    this.value = value;
}
 
Example #24
Source File: DefaultPropertyFoldingBuilder.java    From litho with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(
    @NotNull PsiElement root, @NotNull Document document, boolean quick) {
  FoldingGroup group = FoldingGroup.newGroup(FOLDING_GROUP_NAME);

  final Map<String, PsiExpression> defaultProps =
      PsiTreeUtil.findChildrenOfType(root, PsiField.class).stream()
          .filter(LithoPluginUtils::isPropDefault)
          .filter(field -> field.getInitializer() != null)
          .collect(Collectors.toMap(PsiField::getName, PsiField::getInitializer));

  if (defaultProps.isEmpty()) {
    return FoldingDescriptor.EMPTY;
  }

  return PsiTreeUtil.findChildrenOfType(root, PsiParameter.class).stream()
      .filter(LithoPluginUtils::isProp)
      .map(
          parameter -> {
            String name = parameter.getName();
            if (name == null) {
              return null;
            }
            PsiExpression nameExpression = defaultProps.get(name);
            if (nameExpression == null) {
              return null;
            }
            PsiIdentifier nameIdentifier = parameter.getNameIdentifier();
            if (nameIdentifier == null) {
              return null;
            }
            return new FoldingDescriptor(
                nameIdentifier.getNode(), nameIdentifier.getTextRange(), group) {
              @Override
              public String getPlaceholderText() {
                return name + ": " + nameExpression.getText();
              }
            };
          })
      .filter(Objects::nonNull)
      .toArray(FoldingDescriptor[]::new);
}
 
Example #25
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node               The node to which the folding region is related. The node is then passed to
 *                           {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range              The folded text range in file
 * @param group              Regions with the same group instance expand and collapse together.
 * @param placeholderText    Text displayed instead of folded text, when the region is collapsed
 * @param collapsedByDefault Whether the region should be collapsed for newly opened files
 * @param dependencies       folding dependencies: other files or elements that could change, see <a href="#Dependencies">Dependencies</a>
 */
public FoldingDescriptor(@Nonnull ASTNode node,
                         @Nonnull TextRange range,
                         @Nullable FoldingGroup group,
                         @Nonnull String placeholderText,
                         @Nullable Boolean collapsedByDefault,
                         @Nonnull Set<Object> dependencies) {
  this(node, range, group, dependencies, false, placeholderText, collapsedByDefault);
}
 
Example #26
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node            The node to which the folding region is related. The node is then passed to
 *                        {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range           The folded text range in file
 * @param group           Regions with the same group instance expand and collapse together.
 * @param placeholderText Text displayed instead of folded text, when the region is collapsed
 */
public FoldingDescriptor(@Nonnull ASTNode node, @Nonnull TextRange range, @Nullable FoldingGroup group, @Nonnull String placeholderText) {
  this(node, range, group, Collections.emptySet(), false, placeholderText, null);
}
 
Example #27
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param e               PSI element to which the folding region is related.
 * @param start           Folded text range's start offset in file
 * @param end             Folded text range's end offset in file
 * @param group           Regions with the same group instance expand and collapse together.
 * @param placeholderText Text displayed instead of folded text, when the region is collapsed
 */
public FoldingDescriptor(@Nonnull PsiElement e, int start, int end, @Nullable FoldingGroup group, @Nonnull String placeholderText) {
  this(e.getNode(), new TextRange(start, end), group, placeholderText);
}
 
Example #28
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node         The node to which the folding region is related. The node is then passed to
 *                     {@link FoldingBuilder#getPlaceholderText(ASTNode)} and
 *                     {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range        The folded text range in file
 * @param group        Regions with the same group instance expand and collapse together.
 * @param dependencies folding dependencies: other files or elements that could change, see <a href="#Dependencies">Dependencies</a>
 * @param neverExpands shall be true for fold regions that must not be ever expanded.
 */
public FoldingDescriptor(@Nonnull ASTNode node, @Nonnull TextRange range, @Nullable FoldingGroup group, Set<Object> dependencies, boolean neverExpands) {
  this(node, range, group, dependencies, neverExpands, null, null);
}
 
Example #29
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node         The node to which the folding region is related. The node is then passed to
 *                     {@link FoldingBuilder#getPlaceholderText(ASTNode)} and
 *                     {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range        The folded text range in file
 * @param group        Regions with the same group instance expand and collapse together.
 * @param dependencies folding dependencies: other files or elements that could change
 *                     folding description, see <a href="#Dependencies">Dependencies</a>
 */
public FoldingDescriptor(@Nonnull ASTNode node, @Nonnull TextRange range, @Nullable FoldingGroup group, Set<Object> dependencies) {
  this(node, range, group, dependencies, false);
}
 
Example #30
Source File: FoldingDescriptor.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a folding region related to the specified AST node and covering the specified
 * text range.
 *
 * @param node  The node to which the folding region is related. The node is then passed to
 *              {@link FoldingBuilder#getPlaceholderText(ASTNode)} and
 *              {@link FoldingBuilder#isCollapsedByDefault(ASTNode)}.
 * @param range The folded text range in file
 * @param group Regions with the same group instance expand and collapse together.
 */
public FoldingDescriptor(@Nonnull ASTNode node, @Nonnull TextRange range, @Nullable FoldingGroup group) {
  this(node, range, group, Collections.emptySet());
}