Skip to content

Conversation

Kareeeegg
Copy link

@Kareeeegg Kareeeegg commented Feb 28, 2025

Added a FoldingBuilder
fixed a bug in featureLocationManager

Motivation and description of the problem to be solved:

IntelliJ supports the feature of folding code from e.g., classes or methods which hides the code related to the class or method. Using this mechanism, it may be beneficial in some situations to be able to hide code to a specific feature.
During implementation it has come to light that the featureLocationBlocks did not work correctly; only the last annotation block of any given feature in a file would be saved as a featureLocationBlock. The fault lay with the add method of the FeatureFileMapping Class; Given a feature and a file path, any featureLocationBlock with the same feature and file path after the first featureLocationBlock would overwrite it's previous entry instead of being added. This issue has been fixed.

Related Issue

Related issue: #100

##Implemented Functionality

We developed a feature of code folding that specifically allows folding of feature annotation blocks. Any feature annotation block (that is any block that is saved as a featureLocationBlock) can now be folded. When folded, it will instead display the feature of the feature annotation block which has been folded. Feature annotation lines are not foldable however. This implementation is a new feature with non breaking change.
The user can use folding to "fold away" currently irrelevant and/or finished features. This could also help the user's orientation when searching for specific features.

How Has This Been Tested?

Various manual integration testing has been done using a simulated environment using the plugin. Console logs and stack traces have been referenced to confirm expected inner workings and resulting effects.

  • Tests are created and passing
  • [x ] Manual testing

Screenshots (if appropriate):

Types of changes

  • Pipeline changes
  • [x ] Bug fix (non-breaking change which fixes an issue)
  • [x ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [ x] My code follows the code style of this project.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • I have updated the CONTRIBUTORS file.
  • I have updated the CHANGELOG.

Demo Link: https://youtu.be/WekOyfbLxy0

@sidrasali
Copy link

A few more things worth mentioning:

  1. We tried the languages CodeAnnotations and JAVA for registering the folding builer. With CodeAnnotations we had the lanugage injection problem which was quite complex and since we where able to solve the task with JAVA, we went with JAVA. For us, this displayed an error in the plugin.xml file but when we disable the Plugin DevKit, the error disappears, not sure why.
  2. One Issue remaining now: We observed that sometimes the default fold regions, e.g. for consecutive line comments, override the custom fold regions of the annotated blocks. Here is an example:
Screen.Recording.2025-03-02.at.19.18.24.mov

This seems to be a common issue, e.g. see this forum post. We could not find a way to solve this issue completely.
3. In the last commit it seems like we accidentally removed this action from the plugin file (this was not intended):

<action text="Surround with Feature Annotation" class="com.intellij.codeInsight.template.impl.actions.SurroundWithTemplateAction" use-shortcut-of="SurroundWithLiveTemplate" >
            <add-to-group
                    group-id="EditorPopupMenu" anchor="first" />

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are theese fleet files for?

Copy link

@sidrasali sidrasali Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They weren't supposed to be there. I have removed the files related to task 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. How does it work with nested features?
  2. Did you try changing the the code annotation language files in a manner related to this post:
    https://platform.jetbrains.com/t/injected-language-tags-that-sorounds-base-code/490

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Nested features can also be folded individually. Basically every line with a begin marker can be folded up until its corresponding end marker. The placeholder text always contains the name of the outer feature but it can be changed to contain the names of all nested feature by modifying the FeatureLocation classes to also store the nested feature locations.
  2. The project already has a language injector (CodeAnnotationInjector) so we didn't have to write one. But: to create a folding region, a root node has to be passed:
new FoldingDescriptor(
    root,
    range,
    group,
    placeholder);

We discovered that this doesn't necessarily need to be related to the end node in the AST (otherwise it wouldn't work with java language). The problem is rather that this root node cannot be located in the file since (from our observations) its position seems to be relative to the injected code. Of course the line numbers can be gotten from the feature file mappings but the folding descriptor also needs the root node to exist in the given range. We tried folding every possible AST node (with all possible ranges from FeatureFileMapping) and nothing happened. We did experiment a lot with both language options but since java worked we didn't see the need to investigate the language injection thing further. And the overriding problem would still be there regardless of the language.


<!-- &begin[CodeFolding] -->
<lang.foldingBuilder
language="JAVA" order="first"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we have someother language like xml or C#?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then nothing happens unless there is XML/C# code in the project files. The folding builder only detects PsiElements in the specified language and calls buildFoldRegions() on them

@johmara johmara self-requested a review March 7, 2025 13:53
@sidrasali sidrasali force-pushed the main branch 2 times, most recently from df9041a to 51575d6 Compare March 7, 2025 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants