-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Change the AST nodes or create new ones to modify source code like refactoring. #22
Comments
Well, I think we could do that by dumping back the source code. It is already possible but original formatting is lost (we do not store whitespaces) while comments are not lost but they are re-arranged. I think we spenti energy on comments attribution also for this reason, to support refactoring. Maybe we should think about additional methods that could help with refactoring and provide some examples in the documentation. What do you think? |
Hi, I think preserving whitespace would be a great feature to have. Júlio stated back on the google page that the DumpVisitor needs to be updated to respect token positions. As I can see, ASTNodes already store row and column information. |
Yes but if you do refactoring how that affects the whitespaces? I think it becomes quite tricky... |
That's a good point. If you change a node's length then you'll likely have to adjust the position of every node after it by that amount. e.g. if you add a line to a method, you need to update the position of every node after that node by adding 1 to the line positions. I think another possibility is to give whitespace its own token. Do either of those sound feasible? |
yes but if you add one line it should have the indentation of the line above or below (what if they are different?). Now, suppose you refactor this code: ''' You extract the second parameter of foo with something else, while moving the second parameter outside, like that: ''' What about the space to put in fron of 10? And the space to put in front of a+b? |
To be clear: I think it would be VERY nice to have refactoring preserving indentation, I just think that in practice it is not as easy as it seems. We can give it a try however. |
I think it will have to be template based. So if you commit to having a class refactored, you are committing to the white-space conforming a determined standard - (and probably applied to the rest of the class as well not just the refactored section). |
I'm trying to use javaparser to auto generate pull requests, so I'm trying to keep my diff's as tight as possible and not obliterate different peoples' projects' java styles -- unfortunately, as is-- I can't use. To this discussion - shouldn't it be possible to just worry about the formatting of the things you're adjusting while leaving everything else alone? And for those things you're adjusting, you can do some basic guessing on formatting intentions from the initial load of the compilation unit, i.e. determining tab/space-indent and code width limit (those two are probably the most important and would likely get you 99% of the way there, though you could probably glean other formatting preference subtelties as well). Wish I had time to help out, but offering my personal usecase here in case it helps... thx for keeping this project alive! |
This issue is over 1 year old, let's celebrate :D Personally I am interested in this topic and I would love to work in that direction, I am just not sure if refactoring should be part of Javaparser or should be part of a derived project. What do you think? |
I just checked when my first commit was: Oct 30, 2011 8-O Wasn't somebody using javaparser exactly for this purpose? |
@matozoid I was. During 2012-2013, I developed a tool for my work to do what I call "massive refactorings". As for whitespace preserving, at that time, I had a custom DumpVisitor that used heuristics based on the positions but this was really dirty and not working correctly every time. I have some better ideas to preserve white-spaces now and I started to develop a proof of concept there: https://github.com/ptitjes/javaparser/tree/lex-preserving |
I think we have discussed this at length now and it is generally agreed that this would be part of a separate library with similar language tools. |
This was Júlio's plan with the project.
The text was updated successfully, but these errors were encountered: