Issue #21998 has been updated by Eregon (Benoit Daloze). headius (Charles Nutter) wrote in #note-12:
The target of the break is a semantic detail, not a syntactic one, and it is not determined at parse time.
It does, it is determined at parse time. Break is a syntactic construct.
I'll turn this question around: how can I get just the range of the block syntactic element if it includes all this other stuff unrelated to the block?
We can easily use `Prism.find(Method|UnboundMethod|Proc).block` for this. Similar if one wants to get the end position including the heredoc, `Prism.find` is the way too. BTW another interesting case is: ```ruby for i in enum p i abc end ``` There we must include at least from the `i` just after `for ` as that's the block's parameters. What both Prism and RubyVM::AST do in that case is include from the beginning of `for`, which makes sense. If one just wants the body without parameters, they can use `Prism.find(...).statements`. The only choice for `Prism.find` there is to return the `ForNode`, and so it is consistent to return a `CallNode` for `proc/lambda/foo { ... }` and a `LambdaNode` for `-> { ... }`. Another reason is returning the CallNode there is useful for many cases, and having to walk the AST again to find it seems needlessly inefficient. It's the same philosophy as the heredoc end: make it easy to locate the node with Prism, and access sub-parts as wanted from there. ---------------------------------------- Feature #21998: Add {Method,UnboundMethod,Proc}#source_range https://bugs.ruby-lang.org/issues/21998#change-117126 * Author: Eregon (Benoit Daloze) * Status: Open ---------------------------------------- I'm using matz's suggestion almost as-is from https://bugs.ruby-lang.org/issues/6012#note-53. The only change is the proposed class name. ## Use Cases Use cases have been discussed extensively and matz said:
The use cases are real and I want to support them
So I think we don't need to discuss that anymore :) ## Background Adding column and last line information to `#source_location` is deemed too incompatible given the usages of `obj.source_location.last` which expect the start line (they would get the end column instead). ## Proposal So instead we add a new method, `{Method,UnboundMethod,Proc}#source_range`, which returns a `Ruby::SourceRange` and has these methods: * `start_line`: 1-indexed (same as `source_location.first`) * `end_line`: 1-indexed * `start_column`: in bytes, 0-indexed, I think `start_byte_column` could be good for extra clarity * `end_column`: in bytes, 0-indexed, I think `end_byte_column` could be good for extra clarity * `inspect` which shows something like `#<Ruby::SourceRange (1,0)-(2,10)>`. For the edge case of a heredoc spanning further than the end of a method/block, we would not include it in the `end_line` & `end_column` methods, but instead document it and provide a small code snippet in the docs to compute that if desired with Prism. ## Alternative An alternative could be a `Ruby::SourceLocation` class and `obj.source_location(object: true)`/`obj.source_location(extended: true)` but it feels less nice. Since we are designing something new I think it's best to go for the cleanest design. ## Consistency The above methods match the name of methods on `Prism::Node` and have the same semantics, which is good for consistency and to avoid confusion. ## Scope The scope is intentionally minimal to keep the discussion focused. ## Implementation I'm happy to implement this, it should be pretty trivial and very similar to the extended `source_location`. I would prefer to get an approval for this feature before implementing. -- https://bugs.ruby-lang.org/