update copyright
[fedora-idea.git] / xml / dom-impl / src / com / intellij / util / xml / highlighting / DomElementsHighlightingUtil.java
blob649524ceeb750aaee833493f13efae82efac838d
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.util.xml.highlighting;
19 import com.intellij.codeInsight.daemon.impl.AnnotationHolderImpl;
20 import com.intellij.codeInsight.daemon.impl.SeverityRegistrar;
21 import com.intellij.codeInsight.intention.IntentionAction;
22 import com.intellij.codeInspection.InspectionManager;
23 import com.intellij.codeInspection.ProblemDescriptor;
24 import com.intellij.codeInspection.ProblemHighlightType;
25 import com.intellij.codeInspection.LocalQuickFix;
26 import com.intellij.lang.annotation.Annotation;
27 import com.intellij.lang.annotation.HighlightSeverity;
28 import com.intellij.openapi.editor.colors.CodeInsightColors;
29 import com.intellij.openapi.util.Pair;
30 import com.intellij.openapi.util.TextRange;
31 import com.intellij.openapi.util.text.StringUtil;
32 import com.intellij.openapi.project.Project;
33 import com.intellij.psi.PsiElement;
34 import com.intellij.util.Function;
35 import org.jetbrains.annotations.Nullable;
37 /**
38 * User: Sergey.Vasiliev
40 public class DomElementsHighlightingUtil {
42 private static final AnnotationHolderImpl EMPTY_ANNOTATION_HOLDER = new AnnotationHolderImpl() {
43 public boolean add(final Annotation annotation) {
44 return false;
48 private DomElementsHighlightingUtil() {
51 @Nullable
52 public static ProblemDescriptor createProblemDescriptors(final InspectionManager manager,
53 final DomElementProblemDescriptor problemDescriptor) {
54 final ProblemHighlightType type = getProblemHighlightType(problemDescriptor);
55 return createProblemDescriptors(problemDescriptor, new Function<Pair<TextRange, PsiElement>, ProblemDescriptor>() {
56 public ProblemDescriptor fun(final Pair<TextRange, PsiElement> s) {
57 return manager
58 .createProblemDescriptor(s.second, s.first, problemDescriptor.getDescriptionTemplate(), type, problemDescriptor.getFixes());
60 });
63 // TODO: move it to DomElementProblemDescriptorImpl
64 private static ProblemHighlightType getProblemHighlightType(final DomElementProblemDescriptor problemDescriptor) {
65 if (problemDescriptor.getHighlightType() != null) {
66 return problemDescriptor.getHighlightType();
68 if (problemDescriptor instanceof DomElementResolveProblemDescriptor) {
69 final TextRange range = ((DomElementResolveProblemDescriptor)problemDescriptor).getPsiReference().getRangeInElement();
70 if (range.getStartOffset() != range.getEndOffset()) {
71 return ProblemHighlightType.LIKE_UNKNOWN_SYMBOL;
74 return ProblemHighlightType.GENERIC_ERROR_OR_WARNING;
77 @Nullable
78 public static Annotation createAnnotation(final DomElementProblemDescriptor problemDescriptor) {
80 return createProblemDescriptors(problemDescriptor, new Function<Pair<TextRange, PsiElement>, Annotation>() {
81 public Annotation fun(final Pair<TextRange, PsiElement> s) {
82 String text = problemDescriptor.getDescriptionTemplate();
83 if (StringUtil.isEmpty(text)) text = null;
84 final HighlightSeverity severity = problemDescriptor.getHighlightSeverity();
85 final AnnotationHolderImpl holder = EMPTY_ANNOTATION_HOLDER;
87 TextRange range = s.first;
88 if (text == null) range = TextRange.from(range.getStartOffset(), 0);
89 range = range.shiftRight(s.second.getTextRange().getStartOffset());
90 final Annotation annotation = createAnnotation(severity, holder, range, text, s.second.getProject());
92 if (problemDescriptor instanceof DomElementResolveProblemDescriptor) {
93 annotation.setTextAttributes(CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
96 for(LocalQuickFix fix:problemDescriptor.getFixes()) {
97 if (fix instanceof IntentionAction) annotation.registerFix((IntentionAction)fix);
99 return annotation;
104 private static Annotation createAnnotation(final HighlightSeverity severity,
105 final AnnotationHolderImpl holder,
106 final TextRange range,
107 final String text, final Project project) {
108 if (SeverityRegistrar.getInstance(project).compare(severity, HighlightSeverity.ERROR) >= 0) return holder.createErrorAnnotation(range, text);
109 if (SeverityRegistrar.getInstance(project).compare(severity, HighlightSeverity.WARNING) >= 0) return holder.createWarningAnnotation(range, text);
110 if (SeverityRegistrar.getInstance(project).compare(severity, HighlightSeverity.INFO) >= 0) return holder.createInformationAnnotation(range, text);
111 return holder.createInfoAnnotation(range, text);
114 @Nullable
115 private static <T> T createProblemDescriptors(final DomElementProblemDescriptor problemDescriptor,
116 final Function<Pair<TextRange, PsiElement>, T> creator) {
118 final Pair<TextRange, PsiElement> range = ((DomElementProblemDescriptorImpl)problemDescriptor).getProblemRange();
119 return range == DomElementProblemDescriptorImpl.NO_PROBLEM ? null : creator.fun(range);