← 输入框|github.com|刷新
Skip to content

Re-format imports in the IntelliJ plugin when the document changes. - #1518

Open
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1011620892
Open

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1011620892

Conversation

@copybara-service

Copy link
Copy Markdown

Re-format imports in the IntelliJ plugin when the document changes.

In GoogleJavaFormatImportOptimizer, processFile computes the optimized imports first, and the returned Runnable later applies the result on the EDT inside a write action. Since #960, if the document text changed between processFile and the Runnable (either because doPostponedOperationsAndUnblockDocument flushed pending PSI edits into the document, or because GoogleJavaFormatFormattingService finished an asynchronous format pass in the meantime), the Runnable returned early without updating the document, silently dropping import optimization. In addition, when isDocumentBlockedByPsi was true during processFile, reading document.getText() instead of file.getText() missed uncommitted PSI import additions and caused doPostponedOperationsAndUnblockDocument in the Runnable to change the document text relative to origText.

Read file.getText() in processFile when the document is blocked by uncommitted PSI changes so that pending PSI edits are included in the initial pass, and if the document text has still changed by the time the Runnable executes on the EDT, re-run Formatter.fixImports on the updated document text instead of dropping import optimization.

Closes #971
Fixes #929
Fixes #1070
Fixes #1275

In `GoogleJavaFormatImportOptimizer`, `processFile` computes the optimized imports first, and the returned `Runnable` later applies the result on the EDT inside a write action. Since #960, if the document text changed between `processFile` and the `Runnable` (either because `doPostponedOperationsAndUnblockDocument` flushed pending PSI edits into the document, or because `GoogleJavaFormatFormattingService` finished an asynchronous format pass in the meantime), the `Runnable` returned early without updating the document, silently dropping import optimization. In addition, when `isDocumentBlockedByPsi` was true during `processFile`, reading `document.getText()` instead of `file.getText()` missed uncommitted PSI import additions and caused `doPostponedOperationsAndUnblockDocument` in the `Runnable` to change the document text relative to `origText`.

Read `file.getText()` in `processFile` when the document is blocked by uncommitted PSI changes so that pending PSI edits are included in the initial pass, and if the document text has still changed by the time the `Runnable` executes on the EDT, re-run `Formatter.fixImports` on the updated document text instead of dropping import optimization.

Closes #971
Fixes #929
Fixes #1070
Fixes #1275

PiperOrigin-RevId: 1011620892
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant