/** * Arranges given PSI root contents that belong to the given ranges. * * @param file target PSI root * @param ranges target ranges to use within the given root */ @SuppressWarnings("MethodMayBeStatic") public void arrange( @NotNull PsiFile file, @NotNull Collection<TextRange> ranges, @Nullable final ArrangementCallback callback) { final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file); if (document == null) { return; } final Rearranger<?> rearranger = Rearranger.EXTENSION.forLanguage(file.getLanguage()); if (rearranger == null) { return; } final CodeStyleSettings settings = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings(); ArrangementSettings arrangementSettings = settings.getCommonSettings(file.getLanguage()).getArrangementSettings(); if (arrangementSettings == null && rearranger instanceof ArrangementStandardSettingsAware) { arrangementSettings = ((ArrangementStandardSettingsAware) rearranger).getDefaultSettings(); } if (arrangementSettings == null) { return; } final DocumentEx documentEx; if (document instanceof DocumentEx && !((DocumentEx) document).isInBulkUpdate()) { documentEx = (DocumentEx) document; } else { documentEx = null; } final Context<? extends ArrangementEntry> context = Context.from(rearranger, document, file, ranges, arrangementSettings, settings); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { if (documentEx != null) { // documentEx.setInBulkUpdate(true); } try { doArrange(context); if (callback != null) { callback.afterArrangement(context.moveInfos); } } finally { if (documentEx != null) { // documentEx.setInBulkUpdate(false); } } } }); }