From 66dce364ad4da06d9f2c5b998f89ff4d0868a3a0 Mon Sep 17 00:00:00 2001 From: Konstantin Kolosovsky Date: Sat, 15 Apr 2017 23:35:07 +0300 Subject: [PATCH] Hide traffic light for commit message if found problems have <= TYPO severities --- .../daemon/impl/TrafficLightRenderer.java | 14 +++++--- .../editor/impl/EditorMarkupModelImpl.java | 14 +++++++- .../openapi/vcs/ui/CommitMessage.java | 32 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) 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 79b93615678d..c81121b26772 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 @@ -90,7 +90,7 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { myDocument = document; myFile = file; mySeverityRegistrar = SeverityRegistrar.getSeverityRegistrar(myProject); - refresh(); + refresh(null); if (project != null) { final MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(document, project, true); @@ -113,7 +113,12 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { } } - private void refresh() { + @NotNull + public SeverityRegistrar getSeverityRegistrar() { + return mySeverityRegistrar; + } + + protected void refresh(@Nullable EditorMarkupModelImpl editorMarkupModel) { int maxIndex = mySeverityRegistrar.getSeverityMaxIndex(); if (errorCount != null && maxIndex + 1 == errorCount.length) return; errorCount = new int[maxIndex + 1]; @@ -130,8 +135,9 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { ErrorStripeRenderer renderer = editorMarkupModel.getErrorStripeRenderer(); if (renderer instanceof TrafficLightRenderer) { TrafficLightRenderer tlr = (TrafficLightRenderer)renderer; - tlr.refresh(); - ((EditorMarkupModelImpl)editorMarkupModel).repaintVerticalScrollBar(); + EditorMarkupModelImpl markupModelImpl = (EditorMarkupModelImpl)editorMarkupModel; + tlr.refresh(markupModelImpl); + markupModelImpl.repaintVerticalScrollBar(); if (tlr.myFile == null || tlr.myFile.isValid()) return; Disposer.dispose(tlr); } diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorMarkupModelImpl.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorMarkupModelImpl.java index 8642d9e57870..979511b03d29 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorMarkupModelImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorMarkupModelImpl.java @@ -160,6 +160,14 @@ public class EditorMarkupModelImpl extends MarkupModelImpl implements EditorMark dimensionsAreValid = scrollBarHeight != 0; } + public void setTrafficLightIconVisible(boolean value) { + MyErrorPanel errorPanel = getErrorPanel(); + if (errorPanel != null && errorPanel.myErrorStripeButton.isVisible() != value) { + errorPanel.myErrorStripeButton.setVisible(value); + repaint(-1, -1); + } + } + public void repaintTrafficLightIcon() { MyErrorPanel errorPanel = getErrorPanel(); if (errorPanel != null) { @@ -475,7 +483,11 @@ public class EditorMarkupModelImpl extends MarkupModelImpl implements EditorMark @NotNull @Override public Dimension getPreferredSize() { - return new Dimension(getErrorIconWidth() + getThinGap(), getErrorIconHeight() + getThinGap()); + return !isPreferredSizeSet() + ? isVisible() + ? new Dimension(getErrorIconWidth() + getThinGap(), getErrorIconHeight() + getThinGap()) + : JBUI.emptySize() + : super.getPreferredSize(); } } 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 3745080d2ac6..4bf666f85095 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 @@ -15,8 +15,11 @@ */ package com.intellij.openapi.vcs.ui; +import com.intellij.codeHighlighting.HighlightDisplayLevel; +import com.intellij.codeInsight.daemon.impl.TrafficLightRenderer; import com.intellij.codeInsight.intention.IntentionManager; import com.intellij.codeInspection.ex.InspectionProfileWrapper; +import com.intellij.lang.annotation.HighlightSeverity; import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.application.WriteAction; @@ -24,6 +27,7 @@ import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.editor.ex.EditorEx; +import com.intellij.openapi.editor.impl.EditorMarkupModelImpl; import com.intellij.openapi.fileTypes.FileTypes; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; @@ -48,6 +52,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import static com.intellij.util.ObjectUtils.notNull; import static com.intellij.vcs.commit.CommitMessageInspectionProfile.getBodyRightMargin; public class CommitMessage extends JPanel implements Disposable, DataProvider, CommitMessageI { @@ -223,6 +228,33 @@ public class CommitMessage extends JPanel implements Disposable, DataProvider, C profile -> new InspectionProfileWrapper(CommitMessageInspectionProfile.getInstance(myProject))); } editor.putUserData(IntentionManager.SHOW_INTENTION_OPTIONS_KEY, false); + ((EditorMarkupModelImpl)editor.getMarkupModel()) + .setErrorStripeRenderer(new ConditionalTrafficLightRenderer(myProject, editor.getDocument(), file)); + } + } + + private static class ConditionalTrafficLightRenderer extends TrafficLightRenderer { + public ConditionalTrafficLightRenderer(@NotNull Project project, @NotNull Document document, @Nullable PsiFile file) { + super(project, document, file); + } + + @Override + protected void refresh(@Nullable EditorMarkupModelImpl editorMarkupModel) { + super.refresh(editorMarkupModel); + if (editorMarkupModel != null) { + editorMarkupModel.setTrafficLightIconVisible(hasHighSeverities(errorCount)); + } + } + + private boolean hasHighSeverities(@NotNull int[] errorCount) { + HighlightSeverity minSeverity = notNull(HighlightDisplayLevel.find("TYPO"), HighlightDisplayLevel.DO_NOT_SHOW).getSeverity(); + + for (int i = 0; i < errorCount.length; i++) { + if (errorCount[i] > 0 && getSeverityRegistrar().compare(getSeverityRegistrar().getSeverityByIndex(i), minSeverity) > 0) { + return true; + } + } + return false; } } }