Skip to content
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

Setting the position of nodes during pretty printing #4300

Open
ftomassetti opened this issue Jan 26, 2024 · 1 comment
Open

Setting the position of nodes during pretty printing #4300

ftomassetti opened this issue Jan 26, 2024 · 1 comment
Labels
Improvement Not a bug, but a way that JP can be be enhanced to work better.

Comments

@ftomassetti
Copy link
Member

When pretty printing some AST nodes, it would be useful to get access to their position in the generated code. The SourcePrinter tracks the position (see getCursor). So one could add an option in the PrettyPrinterConfiguration and when the option is set one could do something like:

 @Override
    public void visit(final NameExpr n, final Void arg) {
        val myStartPos = printer.getCursor();
        // current logic
        val myEndPos = printer.getCursor();
        // store the start and end pos in the node itself, if the option is set
    }

This is one case where an approach based on visitors make this work cumbersome as one has to repeat the same logic in all visit methods.

What do you think? Would this be useful? Is there an alternative way to get this result?

@jlerbsc
Copy link
Collaborator

jlerbsc commented Jan 28, 2024

I've never come across this scenario, but it could be a good idea. Why not offer a PR :-)?

@jlerbsc jlerbsc added the Improvement Not a bug, but a way that JP can be be enhanced to work better. label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Improvement Not a bug, but a way that JP can be be enhanced to work better.
Projects
None yet
Development

No branches or pull requests

2 participants