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

[Feature request] Add an annotation to allow ignoring comments for Refaster templates #4284

Open
trebele-tibco opened this issue Feb 9, 2024 · 2 comments

Comments

@trebele-tibco
Copy link

I'm using error_prone_refaster-2.24.1.jar and error_prone_core-2.24.1-with-dependencies.jar to refactor code. Given the following class to refactor:

import java.util.*;
public class RefasterExample {
	public void method2() {
		Arrays.asList(//
				"bad");
		Arrays.asList(/**/
				"bad");
		Arrays.asList("good");
	}
}

and the following template:

	static class ExampleRule {
		@BeforeTemplate List<?> match(String o) {
			return Arrays.asList(o);
		}

		@AfterTemplate List<?> optimizedMethod(String o) {
			return Arrays.asList("debug: " + o);
		}
	}

then only the "good" line will be refactored:

@@ -6,5 +7,5 @@
 		Arrays.asList(/**/
 				"bad");
-		Arrays.asList("good");
+		Arrays.asList("debug: " + "good");
 	}
 }

It would be nice to have a possibility to ignore comments. Maybe with a parameter to configure which comments should be ignored (A regular expression? Predicate<String>?).

@Stephan202
Copy link
Contributor

Alternatively, perhaps would-be-removed comments could be prepended to the replacement code. (This will in some cases yield misplaced/misleading/incorrect comments, but it would be handy i.c.w. patch mode.)

@cushon
Copy link
Collaborator

cushon commented Feb 10, 2024

Thanks, I agree this is an area that could be improved. The current behaviour is supposed to be a safe default so we don't actually remove worthwhile comments, but I don't have objections to making that configurable, or to having a mode that keeps the comments and moves.

I went looking for the place where the current logic is implemented, and found a relevant TODO :)

boolean rejectMatchesWithComments() {
return true; // TODO(lowasser): worth making configurable?
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants