openrewrite / rewrite

Automated mass refactoring of source code.

Home Page:https://docs.openrewrite.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Split method signature line into multiple lines based on the length

ilozano2 opened this issue · comments

What problem are you trying to solve?

Having https://github.com/spring-projects/spring-petclinic with spring-javaformat-maven-plugin validating the Java code

There is a splitLine feature that will split the line if it is over 120 characters: https://github.com/spring-io/spring-javaformat/blob/7db7c9765fbad3e1c0b8f021dc16533f9d5c10f2/spring-javaformat/spring-javaformat-formatter/src/main/resources/io/spring/javaformat/formatter/eclipse/formatter.prefs#L324

Applying org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_4 on https://github.com/spring-projects/spring-petclinic
causes the next change

        @PostMapping("/owners/{ownerId}/edit")
        public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result,
-                       @PathVariable("ownerId") int ownerId) {
+                       @PathVariable int ownerId) {
                if (result.hasErrors()) {
                        return VIEWS_OWNER_CREATE_OR_UPDATE_FORM;
                }

Because the new method signature is less than 120 characters, the validation plugin provokes an error. The @PathVariable int ownerId parameter should be in the same line of the method signature because it will become less than 120 characters.

This can be found in other validators. Example: eclipse jdt

What precondition(s) should be checked before applying this recipe?

The length of the line of a MethodDeclaration and the styleConfig to applied (120 by default)

Describe the situation before applying the recipe

class Test {
	public static void example(Object o1, String thisisabigparameter1, String thisisasecondbigparameter, String iDontcareThisName, String iWantToExceedTheLenght, String formattingThing) {
		System.out.println("foo bar");
	}
}

Describe the situation after applying the recipe

class Test {
	public static void example(Object o1, String thisisabigparameter1, String thisisasecondbigparameter, 
	String iDontcareThisName, String iWantToExceedTheLenght, String formattingThing) {
		System.out.println("foo bar");
	}
}

After the 4th parameter reaches 120 characters -> the split is done before the 4th parameter.

Have you considered any alternatives or workarounds?

I tried to run IntelliJ style with AutoFormat ->, but it didn't work
I can also run my own plugin to format the code, but I would like to get things working directly from OpenRewrite

Any additional context

I think this could be extrapolated to other code sentences (example: class declaration with lots of interfaces over 120 characters)

Are you interested in contributing this recipe to OpenRewrite?

I am happy to contribute.

Hi @ilozano2 ; thanks for the suggestion! Can indeed see how you'd like a recipe to produce a change that's then next accepted by formatter checks. As you probably know we do a lot around style detection already; while I haven't looked into the specific I think could fit in there. It seems you're targeting both splitting and joining lines as necessary right?