|
5 | 5 | import java.util.Objects;
|
6 | 6 | import java.util.Set;
|
7 | 7 | import java.util.stream.Collectors;
|
| 8 | +import java.util.stream.Stream; |
8 | 9 |
|
| 10 | +import com.intellij.openapi.application.ApplicationInfo; |
9 | 11 | import com.intellij.openapi.diagnostic.Logger;
|
10 | 12 | import com.intellij.openapi.editor.Document;
|
11 | 13 | import com.intellij.openapi.fileEditor.FileDocumentManager;
|
@@ -40,6 +42,13 @@ public void beforeAllDocumentsSaving()
|
40 | 42 | {
|
41 | 43 | LOGGER.debug(
|
42 | 44 | "[+] Start SaveActionsDocumentManagerListener#beforeAllDocumentsSaving, " + this.project.getName());
|
| 45 | + |
| 46 | + if(REQUIRES_PROJECT_LOAD_IGNORE_WORKAROUND && isInvokedFromProjectLoadBefore243()) |
| 47 | + { |
| 48 | + LOGGER.debug("Ignoring due to PROJECT_LOAD_IGNORE_WORKAROUND"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
43 | 52 | final List<Document> unsavedDocuments = Arrays.asList(FileDocumentManager.getInstance().getUnsavedDocuments());
|
44 | 53 | if(!unsavedDocuments.isEmpty())
|
45 | 54 | {
|
@@ -74,4 +83,38 @@ private synchronized void initPsiDocManager()
|
74 | 83 | this.psiDocumentManager = PsiDocumentManager.getInstance(this.project);
|
75 | 84 | }
|
76 | 85 | }
|
| 86 | + |
| 87 | + // region PROJECT_LOAD_IGNORE_WORKAROUND |
| 88 | + // https://github.com/xdev-software/intellij-plugin-save-actions/issues/145 |
| 89 | + private static final boolean REQUIRES_PROJECT_LOAD_IGNORE_WORKAROUND = |
| 90 | + determineIfRequiresProjectLoadIgnoreWorkaround(); |
| 91 | + |
| 92 | + @SuppressWarnings("checkstyle:MagicNumber") |
| 93 | + static boolean determineIfRequiresProjectLoadIgnoreWorkaround() |
| 94 | + { |
| 95 | + try |
| 96 | + { |
| 97 | + // Problem was fixed in 2024.3 |
| 98 | + return ApplicationInfo.getInstance().getBuild().getBaselineVersion() < 243; |
| 99 | + } |
| 100 | + catch(final Exception ex) |
| 101 | + { |
| 102 | + LOGGER.warn("Failed to determine IDE version", ex); |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @SuppressWarnings("checkstyle:MagicNumber") |
| 108 | + static boolean isInvokedFromProjectLoadBefore243() |
| 109 | + { |
| 110 | + // The invoking method is ProjectSettingsTracker$submitSettingsFilesRefresh is usually at index 17 and 18 |
| 111 | + return Stream.of(Thread.currentThread().getStackTrace()) |
| 112 | + .map(StackTraceElement::getClassName) |
| 113 | + .skip(16) |
| 114 | + .limit(5) |
| 115 | + .anyMatch(s -> s.startsWith( |
| 116 | + "com.intellij.openapi.externalSystem.autoimport.ProjectSettingsTracker$submitSettingsFilesRefresh")); |
| 117 | + } |
| 118 | + |
| 119 | + // endregion |
77 | 120 | }
|
0 commit comments