Conversation
3346802 to
9769281
Compare
|
dhruvmanila
left a comment
There was a problem hiding this comment.
(lunch break, will continue with the review after that.)
| fn add_import_ranges(&mut self, stmts: &[Stmt]) { | ||
| let mut import_range: Option<TextRange> = None; | ||
|
|
||
| for stmt in stmts { |
There was a problem hiding this comment.
I think this will only create the range for top-level import blocks as it doesn't traverse into the stmt, so the following won't create the range:
def foo():
import sys
from math import prod
def bar():
passThere was a problem hiding this comment.
Can we use the single visitor with state handling instead? That would also means that we do one pass over the AST instead of multiple.
There was a problem hiding this comment.
So I made the nested case work without moving state handling into the visitor. I added a TODO to consider moving it into the visitor, but I'm not totally convinced that it would be meaningfully faster. In particular, this is just doing a shallow scan across a sequence of statements (now all sequences of statements to handle the nested case), so it's not re-doing all of the AST visiting work. And if this were moved into the AST visitor, there would need to be more state tracking.
To be clear, I don't feel strong about this. My instincts here might be wrong!
| } | ||
|
|
||
| /// Add a folding range for a docstring if present at the start of a body. | ||
| fn add_docstring_range(&mut self, body: &[Stmt]) { |
There was a problem hiding this comment.
We might not need this function? Because the visitor should visit these string expressions and it already has branch for strings and f-strings. We can expand them to cover bytes and t-strings as well.
There was a problem hiding this comment.
That was my thought too. But we ideally need to distinguish between "normal" string literals and string literals that serve as documentation. In the latter case, we can attach the FoldingRangeKind::Comment kind. And that enables things like "collapse all comment blocks."
There was a problem hiding this comment.
Oh, interesting. Is "collapse all comment blocks" an editor feature? This makes sense but I think I'd find it a bit confusing as to why did a function doc gets collapsed along with other comments but we can iterate on this based on user feedback.
There was a problem hiding this comment.
Yeah! I'm just realizing that I forgot to post my demo video. Whoops.
Before:
2026-02-18T11.33.01-05.00.mp4
After:
2026-02-18T11.34.17-05.00.mp4
I think I'd be more surprised if "fold all comment blocks" didn't include docstrings personally. But yeah, happy to iterate in response to user feedback.
dhruvmanila
left a comment
There was a problem hiding this comment.
This is really cool!
I think we should highlight this in https://docs.astral.sh/ty/features/language-server/.
| fn add_import_ranges(&mut self, stmts: &[Stmt]) { | ||
| let mut import_range: Option<TextRange> = None; | ||
|
|
||
| for stmt in stmts { |
There was a problem hiding this comment.
Can we use the single visitor with state handling instead? That would also means that we do one pass over the AST instead of multiple.
9769281 to
f68fe00
Compare
This PR implements the `textDocument/foldingRange` LSP request, enabling code folding in editors. We also support tagging each folding range with its "kind." So for example, this enables one to ask your editor to "collapse all block comments." The implementation works by doing a simple AST traversal to identify "blocks" in a Python program. We also do a line oriented search to extract ranges that are more difficult to do from the AST: blocks of comments, blocks of imports and special custom "regions." Closes astral-sh/ty#2588
f68fe00 to
b0e6582
Compare
This PR implements the
textDocument/foldingRangeLSP request,enabling code folding in editors. We also support tagging each
folding range with its "kind." So for example, this enables one
to ask your editor to "collapse all block comments."
The implementation works by doing a simple AST traversal to identify
"blocks" in a Python program. We also do a line oriented search to
extract ranges that are more difficult to do from the AST: blocks of
comments, blocks of imports and special custom "regions."
Closes astral-sh/ty#2588