Skip to content

Folding for functions with parameters over multiple lines #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tungol
Copy link

@tungol tungol commented Sep 6, 2024

The default configuration fails to fold functions if the parameters are defined over multiple lines. Due to typing, this is more likely to happen these days than it used to be.

Three things here:

  • a new regex for foldingIndentedBlockStart.

The existing expression only matches when the final : is on the same line as the 'def'. We don't want to lose that requirement for the other keywords that start a block, so I added a totally separate expression that matches only functions definitions with a open-parenthesis as the final non-whitespace non-comment character on the line.

^\s*def\b.*\(\s*(#.*)?$

  • adding a foldingIndentedBlockIgnore expression

This is needed so that the closing parenthesis of the parameter declaration can be on a lower indent level, like black likes to do. It should match a closing parenthesis and colon with optional return type.

^\s*\)(\s*->\s*\w.*)?:\s*(#.*)?$

  • modifying foldingStartMarker

Still not done, because Textmate also matches def foo(\n as the start of marker-based folding due to the parenthesis, and then it gets confused. The new foldingStartMarker
expression uses a negative-lookahead to match ( only if it's not preceded by a def statement.

^(?!\s*def\s+\w+\s*).*\(\s*(#.*)?$

With those three changes in place, Textmate can now fold all functions, not just those with def and ): on the same line!

The exact expressions can probably be fine-tuned a little further - I didn't fuss with them too much. I'm creating this MR more so anyone with the same issue might find it than in any expectation it'll get merged.

The default configuration fails to fold functions if the parameters
are defined over multiple lines. Due to typing, this is more likely
to happen these days than it used to be.

Three things here:

- a new regex for foldingIndentedBlockStart.

The existing expression only matches when the final : is on the same
line as the 'def'. We don't want to lose that requirement for the other
keywords that start a block, so I added a totally separate expression
that matches only functions definitions with a open-parenthesis
as the final non-whitespace non-comment character on the line.

`^\s*def\b.*\(\s*(#.*)?$`

- adding a foldingIndentedBlockIgnore expression

This is needed so that the closing parenthesis of the parameter
declaration can be on a lower indent level, like black likes to do.
It should match a closing parenthesis and colon with optional return
type.

`^\s*\)(\s*->\s*\w.*)?:\s*(#.*)?$`

- modifying foldingStartMarker

Still not done, because Textmate also matches `def foo(\n` as
the start of marker-based folding due to the parenthesis, and
then it gets confused. I added a negative lookbehind to prevent
it from matching when we're making a def statement.

`(?<!\bdef\s+\w+\s*)`

With those three changes in place, Textmate can now fold
all functions, not just those with `def` and `):` on the same line!
@tungol
Copy link
Author

tungol commented Sep 6, 2024

I jumped the gun here - I didn't realize that variable length negative-lookbehinds were unsupported, so I actually broke folding for all braces. Working around this limitation so I can match ( but not def foo( is proving tricky. I'll update if I figure it out.

Negative look-behinds with varaible lengths aren't supported, which
I didn't realize.

This fixes the issue using a negative-lookahead instead.
@tungol
Copy link
Author

tungol commented Sep 6, 2024

I think I got it working now and the MR has been updated.

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.

1 participant