[code style] [prettier] WEB-74445: Add prepareForFormatting to PrettierFormattingService, avoid unnecessary file saves

(cherry picked from commit 51d6dc4ffdfcb565cb1edb12084d35babe3a122b)

IJ-CR-173729

GitOrigin-RevId: 917bbc7ca1f9ccbdb70b48b556666a5966df5fda
This commit is contained in:
Denis Bezrukov
2025-08-22 12:09:24 +03:00
committed by intellij-monorepo-bot
parent 862b2a5983
commit 2e55e84d50
2 changed files with 10 additions and 1 deletions

View File

@@ -303,6 +303,7 @@ a:com.intellij.formatting.service.AsyncDocumentFormattingService
- p:getTimeout():java.time.Duration
- p:getTimeoutActions(com.intellij.formatting.FormattingContext):com.intellij.openapi.actionSystem.AnAction[]
- p:needToUpdate():Z
- p:prepareForFormatting(com.intellij.openapi.editor.Document,com.intellij.formatting.FormattingContext):V
p:com.intellij.formatting.service.AsyncDocumentFormattingService$FormattingTask
- java.lang.Runnable
- a:cancel():Z

View File

@@ -72,6 +72,7 @@ public abstract class AsyncDocumentFormattingService extends AbstractDocumentFor
return;
}
}
prepareForFormatting(document, formattingContext);
FormattingRequestImpl formattingRequest = new FormattingRequestImpl(formattingContext, document, formattingRanges,
canChangeWhiteSpaceOnly, quickFormat);
FormattingTask formattingTask = createFormattingTask(formattingRequest);
@@ -134,6 +135,14 @@ public abstract class AsyncDocumentFormattingService extends AbstractDocumentFor
*/
protected abstract @NotNull @NlsSafe String getName();
/**
* Hook called before creating a formatting request. Default implementation saves the document
* to ensure external formatters working with IO files see the latest content.
*/
protected void prepareForFormatting(@NotNull Document document, @NotNull FormattingContext formattingContext) {
FileDocumentManager.getInstance().saveDocument(document);
}
/**
* @return A duration to wait for the service to respond (call either {@code onTextReady()} or {@code onError()}).
*/
@@ -177,7 +186,6 @@ public abstract class AsyncDocumentFormattingService extends AbstractDocumentFor
myRanges = ranges;
myCanChangeWhitespaceOnly = canChangeWhitespaceOnly;
myQuickFormat = quickFormat;
FileDocumentManager.getInstance().saveDocument(myDocument);
myInitialModificationStamp = document.getModificationStamp();
}