void (@NotNull FileViewProvider key) { List<ASTNode> astNodes = getContext().myReformatElements.remove(key); Document document = key.getDocument(); // Sort ranges by end offsets so that we won't need any offset adjustment after reformat or reindent if (document == null) { return; } VirtualFile virtualFile = key.getVirtualFile(); if (!virtualFile.isValid()) { return; } PsiManager manager = key.getManager(); if (manager instanceof PsiManagerEx) { FileManager fileManager = ((PsiManagerEx)manager).getFileManager(); FileViewProvider viewProvider = fileManager.findCachedViewProvider(virtualFile); if (viewProvider != key) { // viewProvider was invalidated e.g. due to language level change if (viewProvider == null) { viewProvider = fileManager.findViewProvider(virtualFile); } if (viewProvider != null) { key = viewProvider; astNodes = getContext().myReformatElements.remove(key); } } } Collection<Disposable> toDispose = Collections.emptyList(); try { // process all roots in viewProvider to find marked for reformat before elements and create appropriate range markers Set<PostprocessFormattingTask> postProcessTasks = new TreeSet<>(); handleReformatMarkers(key, postProcessTasks); toDispose = new ArrayList<>(postProcessTasks); // then we create ranges by changed nodes. One per node. There ranges can intersect. Ranges are sorted by end offset. if (astNodes != null) { createActionsMap(astNodes, key, postProcessTasks); } while (!postProcessTasks.isEmpty()) { // now we have to normalize actions so that they not intersect and ordered in most appropriate way // (free reformatting -> reindent -> formatting under reindent) List<PostponedAction> normalizedActions = normalizeAndReorderPostponedActions(postProcessTasks, document); toDispose.addAll(normalizedActions); // only in following loop real changes in document are made FileViewProvider viewProvider = key; if (!normalizedActions.isEmpty()) { CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); for (PostponedAction normalizedAction : normalizedActions) { codeStyleManager.runWithDocCommentFormattingDisabled(viewProvider.getPsi(viewProvider.getBaseLanguage()), () -> { normalizedAction.execute(viewProvider); }); } } } reparseByTextIfNeeded(key, document); } finally { for (Disposable disposable : toDispose) { //noinspection SSBasedInspection disposable.dispose(); } } }