Encapsulate crypto-API of casting getErrorStripeTooltip() to HighlightInfo

This commit is contained in:
Dmitry Jemerov
2018-02-27 12:05:27 +01:00
parent f8d11e71f9
commit 48ddee498e
12 changed files with 53 additions and 169 deletions
@@ -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;
@@ -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);
@@ -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);
@@ -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);
@@ -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;
}),
@@ -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<TextAttributesKey, Pair<ColorAndFontDescriptorsProvider, AttributesDescriptor>> keyMap = ContainerUtil.newHashMap();
Processor<RangeHighlighterEx> 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<ColorAndFontDescriptorsProvider, AttributesDescriptor> p =
key == null ? null : ColorSettingsPages.getInstance().getAttributeDescriptor(key);
if (p != null) keyMap.put(key, p);
@@ -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);
@@ -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();
}
@@ -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);
@@ -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<RawTextWithM
continue; // overlapping ranges withing document markup model are not supported currently
}
TextAttributes attributes = null;
Object tooltip = highlighter.getErrorStripeTooltip();
if (tooltip instanceof HighlightInfo) {
HighlightInfo info = (HighlightInfo)tooltip;
HighlightInfo info = HighlightInfo.fromRangeHighlighter(highlighter);
if (info != null) {
TextAttributesKey key = info.forcedTextAttributesKey;
if (key == null) {
HighlightInfoType type = info.type;
@@ -174,14 +174,12 @@ public class XDebuggerExpressionComboBox extends XDebuggerEditorBase {
}
void processHighlighter(@NotNull RangeHighlighterEx highlighter, boolean add) {
Object o = highlighter.getErrorStripeTooltip();
if (o instanceof HighlightInfo) {
if (HighlightSeverity.ERROR.equals(((HighlightInfo)o).getSeverity())) {
errors += add ? 1 : -1;
if (errors == 0 || errors == 1) {
myComboBox.putClientProperty("JComponent.outline", errors > 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();
}
}
}
@@ -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();
}
}