From f8d3cf19d3291f1706090daf1f1e9a7fb3506bdf Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 17 Sep 2019 14:04:41 +0200 Subject: [PATCH] Don't cache PsiFile in TrafficLightRenderer as it may change while the editor is open GitOrigin-RevId: 6e747550ffd6c8eab5e7a6911121c379f4020a89 --- .../options/colors/FontEditorPreview.java | 2 +- .../daemon/impl/ErrorStripeUpdateManager.java | 2 +- .../daemon/impl/TrafficLightRenderer.java | 35 +++++++++++++------ .../openapi/vcs/ui/CommitMessage.java | 6 ++-- .../highlighting/DomElementsErrorPanel.java | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/FontEditorPreview.java b/platform/lang-impl/src/com/intellij/application/options/colors/FontEditorPreview.java index eefdceac9d19..4f98fc76ec28 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/FontEditorPreview.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/FontEditorPreview.java @@ -66,7 +66,7 @@ public class FontEditorPreview implements PreviewPanel{ } static void installTrafficLights(@NotNull EditorEx editor) { - TrafficLightRenderer renderer = new TrafficLightRenderer(null, null,null) { + TrafficLightRenderer renderer = new TrafficLightRenderer(null, null) { @NotNull @Override protected DaemonCodeAnalyzerStatus getDaemonCodeAnalyzerStatus(@NotNull SeverityRegistrar severityRegistrar) { diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorStripeUpdateManager.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorStripeUpdateManager.java index a7a8ec88a83a..0f74f2f35db7 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorStripeUpdateManager.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/ErrorStripeUpdateManager.java @@ -81,6 +81,6 @@ public class ErrorStripeUpdateManager { return renderer; } } - return new TrafficLightRenderer(myProject, editor.getDocument(), file); + return new TrafficLightRenderer(myProject, editor.getDocument()); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java index 0d671304d926..77180a21abe1 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/TrafficLightRenderer.java @@ -27,6 +27,7 @@ import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.FileViewProvider; import com.intellij.psi.PsiCompiledElement; +import com.intellij.psi.PsiDocumentManager; import com.intellij.psi.PsiFile; import com.intellij.util.ArrayUtil; import com.intellij.util.ArrayUtilRt; @@ -46,7 +47,6 @@ import java.util.*; public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { private final Project myProject; private final Document myDocument; - private final PsiFile myFile; private final DaemonCodeAnalyzerImpl myDaemonCodeAnalyzer; private final SeverityRegistrar mySeverityRegistrar; private Icon icon; @@ -66,11 +66,18 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { */ protected int[] errorCount; - public TrafficLightRenderer(@Nullable Project project, Document document, PsiFile file) { + /** + * @deprecated Please use the constructor not taking PsiFile parameter + */ + @Deprecated + public TrafficLightRenderer(@Nullable Project project, Document document, PsiFile psiFile) { + this(project, document); + } + + public TrafficLightRenderer(@Nullable Project project, Document document) { myProject = project; myDaemonCodeAnalyzer = project == null ? null : (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(project); myDocument = document; - myFile = file; mySeverityRegistrar = SeverityRegistrar.getSeverityRegistrar(myProject); refresh(null); @@ -95,6 +102,10 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { } } + private PsiFile getPsiFile() { + return myProject == null ? null : PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument); + } + @NotNull public SeverityRegistrar getSeverityRegistrar() { return mySeverityRegistrar; @@ -122,7 +133,8 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { } public boolean isValid() { - return myFile == null || myFile.isValid(); + PsiFile file = getPsiFile(); + return file == null || file.isValid(); } protected static class DaemonCodeAnalyzerStatus { @@ -152,7 +164,8 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { @NotNull protected DaemonCodeAnalyzerStatus getDaemonCodeAnalyzerStatus(@NotNull SeverityRegistrar severityRegistrar) { DaemonCodeAnalyzerStatus status = new DaemonCodeAnalyzerStatus(); - if (myFile == null) { + PsiFile psiFile = getPsiFile(); + if (psiFile == null) { status.reasonWhyDisabled = "No file"; status.errorAnalyzingFinished = true; return status; @@ -162,18 +175,18 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { status.errorAnalyzingFinished = true; return status; } - if (!myDaemonCodeAnalyzer.isHighlightingAvailable(myFile)) { - if (!myFile.isPhysical()) { + if (!myDaemonCodeAnalyzer.isHighlightingAvailable(psiFile)) { + if (!psiFile.isPhysical()) { status.reasonWhyDisabled = "File is generated"; status.errorAnalyzingFinished = true; return status; } - if (myFile instanceof PsiCompiledElement) { + if (psiFile instanceof PsiCompiledElement) { status.reasonWhyDisabled = "File is decompiled"; status.errorAnalyzingFinished = true; return status; } - final FileType fileType = myFile.getFileType(); + final FileType fileType = psiFile.getFileType(); if (fileType.isBinary()) { status.reasonWhyDisabled = "File is binary"; status.errorAnalyzingFinished = true; @@ -184,7 +197,7 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { return status; } - FileViewProvider provider = myFile.getViewProvider(); + FileViewProvider provider = psiFile.getViewProvider(); Set languages = provider.getLanguages(); HighlightingSettingsPerFile levelSettings = HighlightingSettingsPerFile.getInstance(myProject); boolean shouldHighlight = languages.isEmpty(); @@ -221,7 +234,7 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { if (pass.getProgress() < 0) continue; status.passStati.add(pass); } - status.errorAnalyzingFinished = myDaemonCodeAnalyzer.isAllAnalysisFinished(myFile); + status.errorAnalyzingFinished = myDaemonCodeAnalyzer.isAllAnalysisFinished(psiFile); status.reasonWhySuspended = myDaemonCodeAnalyzer.isUpdateByTimerEnabled() ? null : "Highlighting is paused temporarily"; fillDaemonCodeAnalyzerErrorsStatus(status, severityRegistrar); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/ui/CommitMessage.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/ui/CommitMessage.java index 5971d81696f2..7c6a12ddd8db 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/ui/CommitMessage.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/ui/CommitMessage.java @@ -283,13 +283,13 @@ public class CommitMessage extends JPanel implements Disposable, DataProvider, C } editor.putUserData(IntentionManager.SHOW_INTENTION_OPTIONS_KEY, false); ((EditorMarkupModelImpl)editor.getMarkupModel()) - .setErrorStripeRenderer(new ConditionalTrafficLightRenderer(myProject, editor.getDocument(), file)); + .setErrorStripeRenderer(new ConditionalTrafficLightRenderer(myProject, editor.getDocument())); } } private static class ConditionalTrafficLightRenderer extends TrafficLightRenderer { - ConditionalTrafficLightRenderer(@NotNull Project project, @NotNull Document document, @Nullable PsiFile file) { - super(project, document, file); + ConditionalTrafficLightRenderer(@NotNull Project project, @NotNull Document document) { + super(project, document); } @Override diff --git a/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java b/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java index 57ffce0e07eb..343b567dcad5 100644 --- a/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java +++ b/xml/dom-impl/src/com/intellij/util/xml/highlighting/DomElementsErrorPanel.java @@ -141,7 +141,7 @@ public class DomElementsErrorPanel extends JPanel implements CommittablePanel, H private class DomElementsTrafficLightRenderer extends TrafficLightRenderer { DomElementsTrafficLightRenderer(@NotNull XmlFile xmlFile) { super(xmlFile.getProject(), - PsiDocumentManager.getInstance(xmlFile.getProject()).getDocument(xmlFile), xmlFile); + PsiDocumentManager.getInstance(xmlFile.getProject()).getDocument(xmlFile)); } @NotNull