Inspections #ref ancor should honor range if present.
[fedora-idea.git] / platform / lang-impl / src / com / intellij / codeInspection / ex / DescriptorComposer.java
blob217fe192784f7c519094be0e58c7eac8f1d159a1
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.codeInspection.ex;
19 import com.intellij.codeInspection.CommonProblemDescriptor;
20 import com.intellij.codeInspection.InspectionsBundle;
21 import com.intellij.codeInspection.ProblemDescriptor;
22 import com.intellij.codeInspection.QuickFix;
23 import com.intellij.codeInspection.reference.RefElement;
24 import com.intellij.codeInspection.reference.RefEntity;
25 import com.intellij.codeInspection.ui.ProblemDescriptionNode;
26 import com.intellij.openapi.diagnostic.Logger;
27 import com.intellij.openapi.editor.Document;
28 import com.intellij.openapi.fileEditor.FileDocumentManager;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.psi.PsiElement;
31 import com.intellij.util.text.CharArrayUtil;
32 import com.intellij.injected.editor.VirtualFileWindow;
34 import java.net.MalformedURLException;
35 import java.net.URL;
37 /**
38 * @author max
40 public class DescriptorComposer extends HTMLComposerImpl {
41 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.ex.DescriptorComposer");
42 private final DescriptorProviderInspection myTool;
44 public DescriptorComposer(DescriptorProviderInspection tool) {
45 myTool = tool;
48 public void compose(StringBuffer buf, RefEntity refEntity) {
49 genPageHeader(buf, refEntity);
50 if (myTool.getDescriptions(refEntity) != null) {
51 appendHeading(buf, InspectionsBundle.message("inspection.problem.synopsis"));
53 CommonProblemDescriptor[] descriptions = myTool.getDescriptions(refEntity);
55 LOG.assertTrue(descriptions != null);
57 startList(buf);
58 for (int i = 0; i < descriptions.length; i++) {
59 final CommonProblemDescriptor description = descriptions[i];
61 startListItem(buf);
62 composeDescription(description, i, buf, refEntity);
63 doneListItem(buf);
66 doneList(buf);
68 appendResolution(buf, myTool, refEntity);
70 else {
71 appendNoProblems(buf);
75 protected void composeAdditionalDescription(final StringBuffer buf, final RefEntity refEntity) {}
77 public void compose(StringBuffer buf, RefEntity refElement, CommonProblemDescriptor descriptor) {
78 CommonProblemDescriptor[] descriptions = myTool.getDescriptions(refElement);
80 int problemIdx = 0;
81 if (descriptions != null) { //server-side inspections
82 problemIdx = -1;
83 for (int i = 0; i < descriptions.length; i++) {
84 CommonProblemDescriptor description = descriptions[i];
85 if (description == descriptor) {
86 problemIdx = i;
87 break;
90 if (problemIdx == -1) return;
93 genPageHeader(buf, refElement);
94 appendHeading(buf, InspectionsBundle.message("inspection.problem.synopsis"));
95 //noinspection HardCodedStringLiteral
96 buf.append("<br>");
97 appendAfterHeaderIndention(buf);
99 composeDescription(descriptor, problemIdx, buf, refElement);
101 if (refElement instanceof RefElement && !((RefElement)refElement).isValid()) return;
103 final QuickFix[] fixes = descriptor.getFixes();
104 if (fixes != null && fixes.length > 0) {
105 //noinspection HardCodedStringLiteral
106 buf.append("<br><br>");
107 appendHeading(buf, InspectionsBundle.message("inspection.problem.resolution"));
108 //noinspection HardCodedStringLiteral
109 buf.append("<br>");
110 appendAfterHeaderIndention(buf);
112 int idx = 0;
113 for (QuickFix fix : fixes) {
114 //noinspection HardCodedStringLiteral
115 buf.append("<font style=\"font-family:verdana;\"");
116 //noinspection HardCodedStringLiteral
117 buf.append("<a HREF=\"file://bred.txt#invokelocal:" + (idx++));
118 buf.append("\">");
119 buf.append(fix.getName());
120 //noinspection HardCodedStringLiteral
121 buf.append("</a></font>");
122 //noinspection HardCodedStringLiteral
123 buf.append("<br>");
124 appendAfterHeaderIndention(buf);
129 protected void composeDescription(final CommonProblemDescriptor description, int i, StringBuffer buf, final RefEntity refElement) {
130 PsiElement expression = description instanceof ProblemDescriptor ? ((ProblemDescriptor)description).getPsiElement() : null;
131 StringBuffer anchor = new StringBuffer();
132 VirtualFile vFile = null;
134 if (expression != null) {
135 vFile = expression.getContainingFile().getVirtualFile();
136 if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate();
138 //noinspection HardCodedStringLiteral
139 anchor.append("<a HREF=\"");
140 try {
141 if (myExporter == null){
142 //noinspection HardCodedStringLiteral
143 anchor.append(new URL(vFile.getUrl() + "#descr:" + i));
144 } else {
145 anchor.append(myExporter.getURL(refElement));
148 catch (MalformedURLException e) {
149 LOG.error(e);
152 anchor.append("\">");
153 anchor.append(ProblemDescriptionNode.extractHighlightedText(description, expression).replaceAll("\\$", "\\\\\\$"));
154 //noinspection HardCodedStringLiteral
155 anchor.append("</a>");
157 else {
158 //noinspection HardCodedStringLiteral
159 anchor.append("<font style=\"font-family:verdana; font-weight:bold; color:#FF0000\";>");
160 anchor.append(InspectionsBundle.message("inspection.export.results.invalidated.item"));
161 //noinspection HardCodedStringLiteral
162 anchor.append("</font>");
165 String descriptionTemplate = description.getDescriptionTemplate();
166 //noinspection HardCodedStringLiteral
167 String res = descriptionTemplate.replaceAll("#ref", anchor.toString());
168 final int lineNumber = description instanceof ProblemDescriptor ? ((ProblemDescriptor)description).getLineNumber() : -1;
169 StringBuffer lineAnchor = new StringBuffer();
170 if (expression != null && lineNumber > 0) {
171 Document doc = FileDocumentManager.getInstance().getDocument(vFile);
172 lineAnchor.append(InspectionsBundle.message("inspection.export.results.at.line")).append(" ");
173 if (myExporter == null) {
174 //noinspection HardCodedStringLiteral
175 lineAnchor.append("<a HREF=\"");
176 try {
177 int offset = doc.getLineStartOffset(lineNumber - 1);
178 offset = CharArrayUtil.shiftForward(doc.getCharsSequence(), offset, " \t");
179 lineAnchor.append(new URL(vFile.getUrl() + "#" + offset));
181 catch (MalformedURLException e) {
182 LOG.error(e);
184 lineAnchor.append("\">");
186 lineAnchor.append(Integer.toString(lineNumber));
187 //noinspection HardCodedStringLiteral
188 lineAnchor.append("</a>");
189 //noinspection HardCodedStringLiteral
190 res = res.replaceAll("#loc", lineAnchor.toString());
192 buf.append(res);
193 buf.append(BR).append(BR);
194 composeAdditionalDescription(buf, refElement);