how to use "CollapseExpandDocCommentsHandler"

Answered

I would like to have a shortcut to fold some specific regions (custom fold regions)

- How to use CollapseExpandDocCommentsHandler ?

- Do I have to create and register a FoldingBuilder ?
 (I've read the "FolderBuilding" doc)

- Do I have to use the PSI to identify the regions ?

Should I submit a request ?

1 comment
Comment actions Permalink

Finally found a good code example on the web :https://www.programcreek.com/java-api-examples/?api=com.intellij.codeInsight.folding.CodeFoldingManager  

Wich gives the following code :

@Override
public void actionPerformed(@NotNull AnActionEvent prEvent)
{
Project vProject = prEvent.getProject();
if (vProject==null)
return;

Editor vEditor = prEvent.getData(CommonDataKeys.EDITOR);
if (vEditor==null)
return;

CodeFoldingManager foldingManager = CodeFoldingManager.getInstance(vProject);
foldingManager.updateFoldRegions(vEditor);

final FoldRegion[] vFoldRegions = vEditor.getFoldingModel().getAllFoldRegions();

Runnable vFoldProcess = new Runnable()
{
@Override
public void run()
{
for (FoldRegion vRegion : vFoldRegions)
{
//PsiElement element = EditorFoldingInfo.get(vEditor).getPsiElement(vRegion);
String vText=vRegion.getPlaceholderText();
if (wUtils.wStrContains(vText,"_gx_"))
{
if ( (wUtils.wStrContains(vText,"interface")) || (wUtils.wStrContains(vText,"initialization")) )
vRegion.setExpanded(false);
}
}//for
}//run
};

vEditor.getFoldingModel().runBatchFoldingOperation(vFoldProcess);
}//[*]
0

Please sign in to leave a comment.