Skip to content

Commit 929f3a9

Browse files
committed
Workaround that the plugin is always executed during project load [<2024.3]
``ProjectSettingsTracker#submitSettingsFilesRefresh`` saves all open files during project load. The fix is to ignore this on the affect IntelliJ versions (< 2024.3) Related: * #145 * JetBrains/intellij-community@765caa7
1 parent 50d7569 commit 929f3a9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

src/main/java/software/xdev/saveactions/core/listener/SaveActionsDocumentManagerListener.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.util.Objects;
66
import java.util.Set;
77
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
89

10+
import com.intellij.openapi.application.ApplicationInfo;
911
import com.intellij.openapi.diagnostic.Logger;
1012
import com.intellij.openapi.editor.Document;
1113
import com.intellij.openapi.fileEditor.FileDocumentManager;
@@ -40,6 +42,13 @@ public void beforeAllDocumentsSaving()
4042
{
4143
LOGGER.debug(
4244
"[+] 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+
4352
final List<Document> unsavedDocuments = Arrays.asList(FileDocumentManager.getInstance().getUnsavedDocuments());
4453
if(!unsavedDocuments.isEmpty())
4554
{
@@ -74,4 +83,38 @@ private synchronized void initPsiDocManager()
7483
this.psiDocumentManager = PsiDocumentManager.getInstance(this.project);
7584
}
7685
}
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
77120
}

0 commit comments

Comments
 (0)