summaryrefslogtreecommitdiff
path: root/plugins/devkit/src/dom/impl/PluginXmlDomFileDescription.java
blob: b3d7fda2c19a0fd8efa1d5b3f6ab3af4f092f6ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright 2000-2014 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.
 */
package org.jetbrains.idea.devkit.dom.impl;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.icons.AllIcons;
import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.util.Iconable;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiField;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomFileDescription;
import com.intellij.util.xml.DomUtil;
import com.intellij.util.xml.GenericAttributeValue;
import com.intellij.util.xml.highlighting.DomElementAnnotationHolder;
import com.intellij.util.xml.highlighting.DomElementsAnnotator;
import com.intellij.util.xml.reflect.DomAttributeChildDescription;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.devkit.dom.*;

import javax.swing.*;
import java.util.List;

/**
 * @author mike
 */
public class PluginXmlDomFileDescription extends DomFileDescription<IdeaPlugin> {

  private static final DomElementsAnnotator ANNOTATOR = new DomElementsAnnotator() {
    @Override
    public void annotate(DomElement element, DomElementAnnotationHolder holder) {
      if (element instanceof Extension) {
        annotateExtension((Extension)element, holder);
      }
      else if (element instanceof Vendor) {
        annotateVendor((Vendor)element, holder);
      }
      else if (element instanceof IdeaVersion) {
        annotateIdeaVersion((IdeaVersion)element, holder);
      }
      else if (element instanceof Extensions) {
        annotateExtensions((Extensions)element, holder);
      }
    }

    private void annotateExtensions(Extensions extensions, DomElementAnnotationHolder holder) {
      final GenericAttributeValue<IdeaPlugin> xmlnsAttribute = extensions.getXmlns();
      if (!DomUtil.hasXml(xmlnsAttribute)) return;

      final Annotation annotation = holder.createAnnotation(xmlnsAttribute,
                                                            HighlightSeverity.WARNING,
                                                            "Use defaultExtensionNs instead");
      annotation.setHighlightType(ProblemHighlightType.LIKE_DEPRECATED);
    }

    private void annotateIdeaVersion(IdeaVersion ideaVersion, DomElementAnnotationHolder holder) {
      highlightNotUsedAnymore(ideaVersion.getMin(), holder);
      highlightNotUsedAnymore(ideaVersion.getMax(), holder);
    }

    private void annotateExtension(Extension extension, DomElementAnnotationHolder holder) {
      final ExtensionPoint extensionPoint = extension.getExtensionPoint();
      if (extensionPoint == null) return;
      final GenericAttributeValue<PsiClass> interfaceAttribute = extensionPoint.getInterface();
      if (DomUtil.hasXml(interfaceAttribute)) {
        final PsiClass value = interfaceAttribute.getValue();
        if (value != null && value.isDeprecated()) {
          final Annotation annotation = holder.createAnnotation(extension, HighlightSeverity.WARNING, "Deprecated EP");
          annotation.setHighlightType(ProblemHighlightType.LIKE_DEPRECATED);
          return;
        }
      }

      final List<? extends DomAttributeChildDescription> descriptions = extension.getGenericInfo().getAttributeChildrenDescriptions();
      for (DomAttributeChildDescription attributeDescription : descriptions) {
        final GenericAttributeValue attributeValue = attributeDescription.getDomAttributeValue(extension);
        if (attributeValue == null || !DomUtil.hasXml(attributeValue)) continue;

        final PsiElement declaration = attributeDescription.getDeclaration(extension.getManager().getProject());
        if (declaration instanceof PsiField) {
          PsiField psiField = (PsiField)declaration;
          if (psiField.isDeprecated()) {
            final Annotation annotation = holder.createAnnotation(attributeValue, HighlightSeverity.WARNING,
                                                                  "Deprecated attribute '" + attributeDescription.getName() + "'");
            annotation.setHighlightType(ProblemHighlightType.LIKE_DEPRECATED);
          }
        }
      }
    }

    private void annotateVendor(Vendor vendor, DomElementAnnotationHolder holder) {
      highlightNotUsedAnymore(vendor.getLogo(), holder);
    }

    private void highlightNotUsedAnymore(GenericAttributeValue attributeValue,
                                         DomElementAnnotationHolder holder) {
      if (!DomUtil.hasXml(attributeValue)) return;

      final Annotation annotation = holder.createAnnotation(attributeValue,
                                                            HighlightSeverity.WARNING,
                                                            "Not used anymore");
      annotation.setHighlightType(ProblemHighlightType.LIKE_DEPRECATED);
    }
  };

  public PluginXmlDomFileDescription() {
    super(IdeaPlugin.class, "idea-plugin");
  }

  @Override
  public Icon getFileIcon(@Iconable.IconFlags int flags) {
    return AllIcons.Nodes.Plugin;
  }

  @Nullable
  @Override
  public DomElementsAnnotator createAnnotator() {
    return ANNOTATOR;
  }

  @Override
  public boolean hasStubs() {
    return true;
  }

  @Override
  public int getStubVersion() {
    return 3;
  }
}