Skip to content

feat(expert): support folding ranges#649

Merged
katafrakt merged 1 commit into
mainfrom
folding-range
Apr 30, 2026
Merged

feat(expert): support folding ranges#649
katafrakt merged 1 commit into
mainfrom
folding-range

Conversation

@katafrakt
Copy link
Copy Markdown
Member

While attempting to address expert-lsp/vscode-expert#45 I realized that while we can fix the particular issue that was reported, we can't really have reliable folding based on what VSCode offers (which is basically regexes to determine start of the fold and end of the fold). For example, this will always be problematic:

def foo do
  """
  abc
  """
  :foo
end

In this example there's no way to tell if """ is opening or closing, using just regexes.

That lead me to investigate foldingRange request in LSP spec. I realized that it's much more fitting for Elixir, probably, and decided to try to implement it.

It works pretty well in my tests and VSCode automatically picks this up when the server advertises its capabilities of defining the ranges. I haven't checked other editors yet though.

@doorgan
Copy link
Copy Markdown
Contributor

doorgan commented Apr 29, 2026

I'll give this a proper review tomorrow, but there's also #36 which is a draft I started last year to see how hard it'd be to port this feature from ElixirLS, you might want to take a look to see if we're covering all the cases

@RedCMD
Copy link
Copy Markdown

RedCMD commented Apr 29, 2026

shouldn't do/end already have the correct folding because they're brackets?

@katafrakt
Copy link
Copy Markdown
Member Author

@doorgan I did not see the other PR, sorry! From the brief analysis, I think the scope of the other one is much bigger. I was mostly after what the extension currently does. So my solution does not fold, for example:

  • Many consecutive commented out lines – I had it started, but I figured it's not a proper "comment block", so I removed it
  • Branches on case/cond statements
  • Pipe expressions
  • Probably more

So if you like, we can instead try to get your PR to mergeable state. I'm not sure about this licensing thing mentioned there though.

@RedCMD not sure I understand. For now the server does not provide any folding guidance so the editors might do it on their own, either from an Expert extension (VSCode) or by other means, like tree sitter.

@RedCMD
Copy link
Copy Markdown

RedCMD commented Apr 29, 2026

my bad
I always thought VSCode would apply folding ranges by default to brackets
but it does not seem to be the case

might make a report on it

@katafrakt
Copy link
Copy Markdown
Member Author

@RedCMD from my tests yesterday it looked like

  • if the language server provides folding ranges, VSCode uses them (when I just added advertising the capability, always returning empty list, I had no folds in VSCode)
  • if the server does not provide, it uses definitions from the extension, especially foldingStartMarker and foldingStopMarker from lines 5-6
  • perhaps if this is not available, it does what you wrote

Not an expert here, just what I observed while investigating the issue with folding.

@RedCMD
Copy link
Copy Markdown

RedCMD commented Apr 29, 2026

would be these definitions

editor.foldingStrategy:

  1. auto - Use a language-specific folding strategy if available, else the indentation-based one.
  2. indentation - Use the indentation-based folding strategy.

Copy link
Copy Markdown
Contributor

@doorgan doorgan left a comment

Choose a reason for hiding this comment

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

Works well for what it implements and code looks good

We can add the missing features from #36 in a separate PR. The old one exists because I was testing how hard would it be to port ElixirLS features to Expert(not that hard tbh) but I deprioritized to follow the milestones roadmap. Feel free to take over or reimplement it though.

The licensing comments in that PR are because I mostly copy-pasted ElixirLS implementation, and that warrants attribution

@mhanberg
Copy link
Copy Markdown
Member

Please update the tracking issue with any further enhancements that need to be added before closing.

@katafrakt
Copy link
Copy Markdown
Member Author

Updated #146

@katafrakt katafrakt merged commit 684d19a into main Apr 30, 2026
38 checks passed
@katafrakt katafrakt deleted the folding-range branch April 30, 2026 16:06
@mhanberg
Copy link
Copy Markdown
Member

Please update the website with this new feature

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.

4 participants