EclipseCodeFormatter
EclipseCodeFormatter copied to clipboard
Reformatting of 'Only VCS changed text' should be context-aware
Let's say that I have the following code, formatted as such because it is longer than the line length:
String result = String.join("/", first,
reallyLongVariableName);
If I rename the second argument so that it would fit on a single line, and then reformat using 'Only VCS changed text', the EclipseCodeFormatter would still not make a single line, since the first line is not changed - only the second.
I think that the formatter should be aware of its context and re-format the whole expression (just like the default Intellij formatter does).
I have attempted to make a fix, which I am currently still testing: https://github.com/krasa/EclipseCodeFormatter/compare/master...andrei-dragusanu:feature/context-aware-reformatting?expand=1
Have you tested it well? I am inclined to release it....
I have been using it ever since I made it and it has been working really well, but there could be some other code-style particularities that may need to be addressed (like method chaining that I am explaining bellow).
I also pushed all of the refinements I made so far. Some of these prevent a whole class or method of being formatted when only their signature is being changed, i.e:
public class EclipseCodeFormatter {
// method body
// If you'd rename the class, the whole body would've been reformatted. Now it is fixed.
}
Another refinement includes the chaining of methods: if you have a chained method call that expands on multiple lines and something about one method is changed (like a parameter name), then the whole chain is now being detected and reformatted, not just the line of the change.
Let me know your thoughts. Thanks!
- I would disable it for selected text, it causes huge problems, like reformatting much more than it should, and breaking selection. Also it may be required in some legacy code to not change lines just because of formatting to keep history, so not doing it for selection solves it...

- when a line with closing bracket is changed, it reformats the whole scope (method/class)
- when 2 following imports lines are changed, it reformat all imports

- there might be other edge cases for Javascript/C, so it would be maybe better to use this feature only for Java
I came up with a new approach: expand the range of the text to be reformatted only to expressions and parameter lists. I will begin testing it, but it looks quite promising, since it is much more conservative (imports, full method/class bodies should no longer be affected).
I also have disabled it for non-Java or text selections.