From 48ddee498eeaba0e7f20c54a93576d31eb18af6b Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Mon, 26 Feb 2018 18:25:57 +0100 Subject: [PATCH] Encapsulate crypto-API of casting getErrorStripeTooltip() to HighlightInfo --- .../impl/DaemonRespondToChangesTest.java | 6 ++-- .../daemon/impl/DaemonCodeAnalyzerEx.java | 21 ++------------ .../daemon/impl/HighlightInfo.java | 29 +++++++++---------- .../daemon/impl/UpdateHighlightersUtil.java | 7 ++--- .../tools/util/base/HighlightingLevel.java | 23 ++------------- .../colors/JumpToColorsAndFontsAction.java | 23 ++++----------- .../daemon/impl/DaemonListeners.java | 22 +++----------- .../impl/IdentifierHighlighterPass.java | 23 ++------------- .../daemon/impl/TrafficLightRenderer.java | 21 ++------------ .../richcopy/TextWithMarkupProcessor.java | 7 ++--- .../impl/ui/XDebuggerExpressionComboBox.java | 14 ++++----- .../XmlTagTreeHighlightingPass.java | 26 +++-------------- 12 files changed, 53 insertions(+), 169 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java index 42ed880e2f24..e425b8268429 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/impl/DaemonRespondToChangesTest.java @@ -1861,9 +1861,9 @@ public class DaemonRespondToChangesTest extends DaemonAnalyzerTestCase { model.addMarkupModelListener(getTestRootDisposable(), new MarkupModelListener.Adapter() { @Override public void beforeRemoved(@NotNull RangeHighlighterEx highlighter) { - Object tt = highlighter.getErrorStripeTooltip(); - if (!(tt instanceof HighlightInfo)) return; - String description = ((HighlightInfo)tt).getDescription(); + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info == null) return; + String description = info.getDescription(); if (errorDescription.equals(description)) { errorRemoved[0] = true; diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerEx.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerEx.java index 56561ec99e45..62483a71e797 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerEx.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/DaemonCodeAnalyzerEx.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; @@ -72,9 +58,8 @@ public abstract class DaemonCodeAnalyzerEx extends DaemonCodeAnalyzer { final SeverityRegistrar severityRegistrar = SeverityRegistrar.getSeverityRegistrar(project); MarkupModelEx model = (MarkupModelEx)DocumentMarkupModel.forDocument(document, project, true); return model.processRangeHighlightersOutside(startOffset, endOffset, marker -> { - Object tt = marker.getErrorStripeTooltip(); - if (!(tt instanceof HighlightInfo)) return true; - HighlightInfo info = (HighlightInfo)tt; + HighlightInfo info = HighlightInfo.fromRangeHighlighter(marker); + if (info == null) return true; return minSeverity != null && severityRegistrar.compare(info.getSeverity(), minSeverity) < 0 || info.getHighlighter() == null || processor.process(info); diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java index cec588bfdf55..e55352d04384 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/HighlightInfo.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl; import com.intellij.codeInsight.daemon.GutterMark; @@ -35,6 +21,7 @@ import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.editor.colors.*; import com.intellij.openapi.editor.ex.RangeHighlighterEx; import com.intellij.openapi.editor.markup.GutterIconRenderer; +import com.intellij.openapi.editor.markup.RangeHighlighter; import com.intellij.openapi.editor.markup.TextAttributes; import com.intellij.openapi.util.*; import com.intellij.openapi.util.text.StringUtil; @@ -101,6 +88,18 @@ public class HighlightInfo implements Segment { private static final byte NEEDS_UPDATE_ON_TYPING_MASK = 32; PsiElement psiElement; + /** + * Returns the HighlightInfo instance from which the given range highlighter was created, or null if there isn't any. + */ + @Nullable + public static HighlightInfo fromRangeHighlighter(@NotNull RangeHighlighter highlighter) { + Object errorStripeTooltip = highlighter.getErrorStripeTooltip(); + if (errorStripeTooltip instanceof HighlightInfo) { + return (HighlightInfo) errorStripeTooltip; + } + return null; + } + @NotNull ProperTextRange getFixTextRange() { return new ProperTextRange(fixStartOffset, fixEndOffset); diff --git a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java index c6c4ee5bf2ea..a3d4fd1e5d96 100644 --- a/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java +++ b/platform/analysis-impl/src/com/intellij/codeInsight/daemon/impl/UpdateHighlightersUtil.java @@ -476,11 +476,8 @@ public class UpdateHighlightersUtil { RangeHighlighter[] allHighlighters = markup.getAllHighlighters(); for (RangeHighlighter highlighter : allHighlighters) { if (!highlighter.isValid()) continue; - Object tooltip = highlighter.getErrorStripeTooltip(); - if (!(tooltip instanceof HighlightInfo)) { - continue; - } - final HighlightInfo info = (HighlightInfo)tooltip; + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info == null) continue; boolean contains = !DaemonCodeAnalyzerEx .processHighlights(document, project, null, info.getActualStartOffset(), info.getActualEndOffset(), highlightInfo -> BY_START_OFFSET_NODUPS.compare(highlightInfo, info) != 0); diff --git a/platform/diff-impl/src/com/intellij/diff/tools/util/base/HighlightingLevel.java b/platform/diff-impl/src/com/intellij/diff/tools/util/base/HighlightingLevel.java index 9b7f840d34ab..4bbea85cab4c 100644 --- a/platform/diff-impl/src/com/intellij/diff/tools/util/base/HighlightingLevel.java +++ b/platform/diff-impl/src/com/intellij/diff/tools/util/base/HighlightingLevel.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.diff.tools.util.base; import com.intellij.codeInsight.daemon.impl.HighlightInfo; @@ -33,11 +19,8 @@ public enum HighlightingLevel { ADVANCED("Syntax", AllIcons.Ide.HectorSyntax, rangeHighlighter -> { if (rangeHighlighter.getLayer() > HighlighterLayer.ADDITIONAL_SYNTAX) return false; - Object tooltip = rangeHighlighter.getErrorStripeTooltip(); - if (tooltip instanceof HighlightInfo) { - HighlightInfo info = (HighlightInfo)tooltip; - if (info.getSeverity().compareTo(HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING) >= 0) return false; - } + HighlightInfo info = HighlightInfo.fromRangeHighlighter(rangeHighlighter); + if (info != null && info.getSeverity().compareTo(HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING) >= 0) return false; return true; }), diff --git a/platform/lang-impl/src/com/intellij/application/options/colors/JumpToColorsAndFontsAction.java b/platform/lang-impl/src/com/intellij/application/options/colors/JumpToColorsAndFontsAction.java index 3e72cae17148..2555e5f3a92f 100644 --- a/platform/lang-impl/src/com/intellij/application/options/colors/JumpToColorsAndFontsAction.java +++ b/platform/lang-impl/src/com/intellij/application/options/colors/JumpToColorsAndFontsAction.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.application.options.colors; import com.intellij.codeInsight.daemon.impl.HighlightInfo; @@ -87,9 +73,10 @@ public class JumpToColorsAndFontsAction extends DumbAwareAction { if (project == null || editor == null) return; Map> keyMap = ContainerUtil.newHashMap(); Processor processor = r -> { - Object tt = r.getErrorStripeTooltip(); - TextAttributesKey key = tt instanceof HighlightInfo ? ObjectUtils.chooseNotNull( - ((HighlightInfo)tt).forcedTextAttributesKey, ((HighlightInfo)tt).type.getAttributesKey()) : null; + HighlightInfo info = HighlightInfo.fromRangeHighlighter(r); + TextAttributesKey key = info != null + ? ObjectUtils.chooseNotNull(info.forcedTextAttributesKey, info.type.getAttributesKey()) + : null; Pair p = key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key); if (p != null) keyMap.put(key, p); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java index eca6c0918c24..d064d769979f 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/DaemonListeners.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl; @@ -325,9 +311,9 @@ public class DaemonListeners implements Disposable { ((EditorEventMulticasterEx)eventMulticaster).addErrorStripeListener(e -> { RangeHighlighter highlighter = e.getHighlighter(); if (!highlighter.isValid()) return; - Object info = highlighter.getErrorStripeTooltip(); - if (info instanceof HighlightInfo) { - GotoNextErrorHandler.navigateToError(myProject, e.getEditor(), (HighlightInfo)info); + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info != null) { + GotoNextErrorHandler.navigateToError(myProject, e.getEditor(), info); } }, this); diff --git a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/IdentifierHighlighterPass.java b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/IdentifierHighlighterPass.java index 133784ab5646..29e313b90c7c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/IdentifierHighlighterPass.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/daemon/impl/IdentifierHighlighterPass.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl; @@ -240,11 +226,8 @@ public class IdentifierHighlighterPass extends TextEditorHighlightingPass { public static void clearMyHighlights(Document document, Project project) { MarkupModel markupModel = DocumentMarkupModel.forDocument(document, project, true); for (RangeHighlighter highlighter : markupModel.getAllHighlighters()) { - Object tooltip = highlighter.getErrorStripeTooltip(); - if (!(tooltip instanceof HighlightInfo)) { - continue; - } - HighlightInfo info = (HighlightInfo)tooltip; + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info == null) continue; if (info.type == HighlightInfoType.ELEMENT_UNDER_CARET_READ || info.type == HighlightInfoType.ELEMENT_UNDER_CARET_WRITE) { highlighter.dispose(); } 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 fdbd430fa575..2d0cba467816 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 @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl; @@ -155,9 +141,8 @@ public class TrafficLightRenderer implements ErrorStripeRenderer, Disposable { } private void incErrorCount(RangeHighlighter highlighter, int delta) { - Object o = highlighter.getErrorStripeTooltip(); - if (!(o instanceof HighlightInfo)) return; - HighlightInfo info = (HighlightInfo)o; + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info == null) return; HighlightSeverity infoSeverity = info.getSeverity(); if (infoSeverity.myVal <= HighlightSeverity.INFORMATION.myVal) return; final int severityIdx = mySeverityRegistrar.getSeverityIdx(infoSeverity); diff --git a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/TextWithMarkupProcessor.java b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/TextWithMarkupProcessor.java index b245fa63d45a..c5a5ec1ea161 100644 --- a/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/TextWithMarkupProcessor.java +++ b/platform/lang-impl/src/com/intellij/openapi/editor/richcopy/TextWithMarkupProcessor.java @@ -1,4 +1,4 @@ -// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.editor.richcopy; import com.intellij.codeInsight.daemon.impl.HighlightInfo; @@ -683,9 +683,8 @@ public class TextWithMarkupProcessor extends CopyPastePostProcessor 0 ? "error" : null); - myComboBox.repaint(); - } + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info != null && HighlightSeverity.ERROR.equals(info.getSeverity())) { + errors += add ? 1 : -1; + if (errors == 0 || errors == 1) { + myComboBox.putClientProperty("JComponent.outline", errors > 0 ? "error" : null); + myComboBox.repaint(); } } } diff --git a/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/XmlTagTreeHighlightingPass.java b/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/XmlTagTreeHighlightingPass.java index 34f46dbd8ec0..de76e4f3e6f0 100644 --- a/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/XmlTagTreeHighlightingPass.java +++ b/xml/impl/src/com/intellij/codeInsight/daemon/impl/tagTreeHighlighting/XmlTagTreeHighlightingPass.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.codeInsight.daemon.impl.tagTreeHighlighting; import com.intellij.application.options.editor.WebEditorOptions; @@ -315,13 +301,9 @@ public class XmlTagTreeHighlightingPass extends TextEditorHighlightingPass { final MarkupModel markupModel = DocumentMarkupModel.forDocument(editor.getDocument(), project, true); for (RangeHighlighter highlighter : markupModel.getAllHighlighters()) { - Object tooltip = highlighter.getErrorStripeTooltip(); - - if (!(tooltip instanceof HighlightInfo)) { - continue; - } - - if (((HighlightInfo)tooltip).type == TYPE) { + HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter); + if (info == null) continue; + if (info.type == TYPE) { highlighter.dispose(); } }