Don't cache PsiFile in TrafficLightRenderer as it may change while the editor is open

GitOrigin-RevId: 6e747550ffd6c8eab5e7a6911121c379f4020a89
This commit is contained in:
Dmitry Jemerov
2019-09-17 14:02:01 +00:00
committed by intellij-monorepo-bot
parent 6ff5610c5c
commit f8d3cf19d3
5 changed files with 30 additions and 17 deletions
@@ -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) {
@@ -81,6 +81,6 @@ public class ErrorStripeUpdateManager {
return renderer;
}
}
return new TrafficLightRenderer(myProject, editor.getDocument(), file);
return new TrafficLightRenderer(myProject, editor.getDocument());
}
}
@@ -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<Language> 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);
@@ -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
@@ -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