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

GoogleJavaFormatImportOptimizer: re-run import format if document.txt has changed #971

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public boolean supports(@NotNull PsiFile file) {
final String origText = document.getText();
String text;
try {
text = ImportOrderer.reorderImports(RemoveUnusedImports.removeUnusedImports(origText), style);
text = formatImports(origText, style);
} catch (FormatterException e) {
Notifications.displayParsingErrorNotification(project, file.getName());
return Runnables.doNothing();
Expand All @@ -80,11 +80,27 @@ public boolean supports(@NotNull PsiFile file) {
// getCharsSequence() as we should have `writeAction()` (which I think means effectively a
// write-lock) and it saves calling getText(), which apparently is expensive.
CharSequence newText = document.getCharsSequence();
String textToSet = text;
if (CharSequence.compare(origText, newText) != 0) {
return;
// while this only happens occasionally, it can be somewhat annoying when the file is
// not formatted correctly, even though the format seems to have completed successfully.
// re-run the import format. it's a bit expensive, but I think it's ok to occasionally run
// this on the Swing thread.
try {
textToSet = formatImports(document.getText(), style);
} catch (FormatterException e) {
Notifications.displayParsingErrorNotification(project, file.getName());
return;
}
}

document.setText(text);
document.setText(textToSet);
};
}

private String formatImports(String documentText, JavaFormatterOptions.Style style)
throws FormatterException {
return ImportOrderer.reorderImports(
RemoveUnusedImports.removeUnusedImports(documentText), style);
}
}
Loading