question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to print a node to string?

See original GitHub issue

Is there a way to convert a node (e.g. MethodDeclaration) into a string, either pretty-print (aka, reformatting) or print the original code?

I didn’t find such methods in javalang. I had experiences with Roslyn in .net and javaparse. Both support this functionality.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
levivcommented, May 12, 2020

This is very old but this is the solution I came up with for my problem.

    def get_code_snippet (lines, index):
            openingBrackets = 0;
            closingBrackets = 0;

            # Resulting snippet
            snippet = ""

            # loop until we find the final closing bracket, or go over the limit
            while index < len(lines):
                line = lines[index]
                for letter in line:
                    if letter == '}':
                        closingBrackets += 1
                    elif letter == '{':
                        openingBrackets += 1

                    snippet += letter

                    # Early end if we have reached last bracket
                    if closingBrackets == openingBrackets and openingBrackets != 0:
                        return snippet
                    
                index += 1

            return snippet

I called the method in this way:

lines = code.split("\n")
for child in node.body:
    get_code_snippet (lines, child.position[0] - 1)

I hope it can help anyone who stumbles upon this!

0reactions
foresightyjcommented, Aug 22, 2017

Thanks for the response 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Output to the command line using Node.js
The most basic and most used method is console.log() , which prints the string you pass to it to the console. If you...
Read more >
Printing out a linked list using toString - java - Stack Overflow
A very simple solution is to override the toString() method in the Node . Then, you can call print by passing LinkedList 's...
Read more >
Print String - The Basics Of Nodes In Unreal Engine 4 - YouTube
Hey guys, in today's video, I'm going to be showing you what the Print String node is, how it works and how to...
Read more >
5.4. Using a Node 's File Name as a String - SCons
Printing a Node 's name as described in the previous section works because the string representation of a Node object is the name...
Read more >
Print nodes of linked list at given indexes - GeeksforGeeks
The task is to print the elements of the second linked list according to the position pointed ... Print the node at position...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found