FoldingBuilder behavior for single line

Hi,

I have added FoldingBuilder for my custom language. It works fine with multi-line text (see the below screenshot). 

 

When there is single line it doesn't show any visual indicator that expand/collapse option is available. But it works if I use (cmd + shift + A) -> collapse or cmd -.

Is this the expected behavior? 

I need to show the visual indicators for the single line as well. Any suggestions on how to achieve this?

1
7 comments

It's possible to make single-line folding regions have folding anchor displayed in editor by calling

editor.getSettings().setAllowSingleLogicalLineFolding(true)

0
Avatar
Permanently deleted user

Thanks for the reply. 

I am not sure where to call editor.getSettings().setAllowSingleLogicalLineFolding(true) because while building folding regions, I don't have the reference to the editor instance.

public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode astNode, @NotNull Document document) {
}

 

0

Yes, it won't be so straightforward. Probably you can try setting it for all editors corresponding to a given document, obtained via

com.intellij.openapi.editor.EditorFactory#getEditors(com.intellij.openapi.editor.Document)

0
Avatar
Permanently deleted user

I tried the above approach but it is still not showing folding anchor. 

editor.getSettings().setAllowSingleLogicalLineFolding(true)
editor.getSettings().isAllowSingleLogicalLineFolding() // returns true
0

Can you debug the following method to see why your folding region is not picked:

com.intellij.openapi.editor.impl.EditorGutterComponentImpl#doPaintFoldingTree

It's possible that explicit editor repaint will be required after setting change.

0
Avatar
Permanently deleted user

 

com.intellij.openapi.editor.impl.FoldingAnchorsOverlayStrategy#isFoldingPossible
!editor.getSoftWrapModel().getSoftWrapsForRange(startOffset, endOffsetToUse).isEmpty()
com.intellij.openapi.editor.impl.SoftWrapModelImpl.isSoftWrappingEnabled() //returns false

The folding anchor is not visible because the last call is returning false(isSoftWrappingEnabled).

How to enable soft wrap? 

 

0

Thanks. Sorry, I didn't look at this in details myself first. It turns out it's not possible currently to display folding anchors for a folding region located completely inside one visual line. The setting mentioned previously enables showing them only for single-logical-line regions spanning several visual lines due to soft-wrapping.

If that matters, folding actions (expand/collapse region) should still work fine for single-line regions.

0

Please sign in to leave a comment.