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

review: feat: Make sniper printer infer indentation style of compilation unit #3717

Merged

Conversation

slarse
Copy link
Collaborator

@slarse slarse commented Dec 3, 2020

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.

@slarse slarse changed the title wip: Make sniper printer infer indentation style of compilation unit WIP: feat: Make sniper printer infer indentation style of compilation unit Dec 3, 2020
@slarse slarse closed this Dec 3, 2020
@slarse slarse reopened this Dec 3, 2020
Comment on lines +35 to +56
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);
}
Copy link
Collaborator Author

@slarse slarse Dec 3, 2020

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.

@slarse slarse changed the title WIP: feat: Make sniper printer infer indentation style of compilation unit review: feat: Make sniper printer infer indentation style of compilation unit Dec 3, 2020
Comment on lines +139 to +141
Pair<Integer, Boolean> indentationInfo = IndentationDetector.detectIndentation(compilationUnit);
mutableTokenWriter.setOriginSourceTabulationSize(indentationInfo.getLeft());
mutableTokenWriter.setOriginSourceUsesTabulations(indentationInfo.getRight());
Copy link
Collaborator Author

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.

@slarse slarse changed the title review: feat: Make sniper printer infer indentation style of compilation unit WIP: feat: Make sniper printer infer indentation style of compilation unit Dec 3, 2020
@slarse slarse changed the title WIP: feat: Make sniper printer infer indentation style of compilation unit review: feat: Make sniper printer infer indentation style of compilation unit Dec 3, 2020
@slarse
Copy link
Collaborator Author

slarse commented Dec 3, 2020

Ping @nharrand

@nharrand
Copy link
Collaborator

nharrand commented Dec 4, 2020

LGTM! If you can just add a comment on the description of detectIndentation(CtCompilationUnit cu), I'll merge!

@slarse
Copy link
Collaborator Author

slarse commented Dec 4, 2020

@nharrand Done! Now just waiting for CI.

@nharrand nharrand merged commit b5dbe06 into INRIA:master Dec 4, 2020
@nharrand
Copy link
Collaborator

nharrand commented Dec 4, 2020

Thanks for the PR @slarse !

@slarse slarse deleted the issue/3715-sniper-printer-infer-indentation branch December 9, 2020 08:14
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

Successfully merging this pull request may close these issues.

bug: Sniper printer always prints indentation for newly added elements with tabs
2 participants