-
-
Notifications
You must be signed in to change notification settings - Fork 354
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
review: feat: Make sniper printer infer indentation style of compilation unit #3717
review: feat: Make sniper printer infer indentation style of compilation unit #3717
Conversation
private class SniperPrinterHelper extends PrinterHelper { | ||
private final Environment env; | ||
|
||
SniperPrinterHelper(Environment env) { | ||
super(env); | ||
this.env = env; | ||
} | ||
|
||
/** | ||
* We override this method to use the correct style of indentation for new elements. | ||
*/ | ||
@Override | ||
protected void autoWriteTabs() { | ||
int setTabulationSize = env.getTabulationSize(); | ||
env.useTabulations(originSourceUsesTabulations); | ||
env.setTabulationSize(originSourceTabulationSize); | ||
|
||
super.autoWriteTabs(); | ||
|
||
env.setTabulationSize(setTabulationSize); | ||
env.useTabulations(true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual thing that overrides the printing of tabs with whatever indentation style is present in the source file. I'm not completely satisfied with this solution as it seems a bit hacky, but I couldn't figure out a better way to do it. Suggestions are welcome.
Pair<Integer, Boolean> indentationInfo = IndentationDetector.detectIndentation(compilationUnit); | ||
mutableTokenWriter.setOriginSourceTabulationSize(indentationInfo.getLeft()); | ||
mutableTokenWriter.setOriginSourceUsesTabulations(indentationInfo.getRight()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The token writer is updated with indentation style for each CU to be printed.
Ping @nharrand |
LGTM! If you can just add a comment on the description of |
@nharrand Done! Now just waiting for CI. |
Thanks for the PR @slarse ! |
Fix #3715
This PR introduces indentation style inference to the sniper printer. It's simple in principle: look at the whitespace preceding the top-level type members and make a best guess as to what the indentation style is out of 1, 2 or 4 spaces/tabs. The original set of source code fragments are used to detect indentation.
If for any given compilation unit no top-level elements can be found, indentation detection defaults to tabs.
We will start using this ASAP in Sorald, preferably with the new beta release this Saturday if we can get it merged by then. Therefore, we should be able to detect any obvious issues this might introduce relatively quickly.
Note: This will crash and burn if you try to sniper print a compilation unit that does not have an origin source (e.g. if you add a CU through the Spoon API). That appears to be the case before this addition as well. Just thought I'd make note of it.