-
Notifications
You must be signed in to change notification settings - Fork 159
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
Feature request: get number of lines in a block? #60
Comments
Also looking for such feature, what's the progress? |
I didn't end up pursuing this. For this and a few other reasons (unrelated to javalang), I ended up using Java and the Eclipse JDT API to do what I needed. |
I've been using the following piece of code (previously suggested by @lyriccoder's in here) to find the line number of the last line of code in a def find_end_line_number(node):
"""Finds end line of a node."""
max_line = node.position.line
def traverse(node):
for child in node.children:
if isinstance(child, list) and (len(child) > 0):
for item in child:
traverse(item)
else:
if hasattr(child, '_position'):
nonlocal max_line
if child._position.line > max_line:
max_line = child._position.line
return
traverse(node)
return max_line You can then do something like Although this function seems to be working just fine, it returns the line number of the last line of code, not necessary the last line number of a node. One may expect the last line to be the line where It would be great to have such feature out-of-box. |
Apologies if this features exists already. Given some kind of code block (like a
MethodDeclaration
for example), I'd like to get the number of lines in that block. The AST gives me the start position (line, column) of the block, but not end position.If this feature exists, I would appreciate pointers to it. If it doesn't I'm happy to discuss or contribute with a PR.
Thanks for your work on this project!
The text was updated successfully, but these errors were encountered: