test for VfsUtil.findRelativeFile(uri, base)
[idea/community.git] / java / java-impl / src / com / intellij / slicer / SliceUsageCellRenderer.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.slicer;
17
18 import com.intellij.openapi.util.Segment;
19 import com.intellij.openapi.util.TextRange;
20 import com.intellij.psi.*;
21 import com.intellij.psi.util.PsiFormatUtil;
22 import com.intellij.psi.util.PsiFormatUtilBase;
23 import com.intellij.psi.util.PsiTreeUtil;
24 import com.intellij.ui.SimpleTextAttributes;
25 import com.intellij.usages.TextChunk;
26 import com.intellij.util.FontUtil;
27 import com.intellij.util.Processor;
28 import com.intellij.util.SmartList;
29 import org.jetbrains.annotations.NotNull;
30
31 import java.awt.*;
32 import java.util.List;
33
34 /**
35  * @author cdr
36  */
37 public class SliceUsageCellRenderer extends SliceUsageCellRendererBase {
38
39   @Override
40   public void customizeCellRendererFor(@NotNull SliceUsage sliceUsage) {
41     boolean isForcedLeaf = sliceUsage instanceof JavaSliceDereferenceUsage;
42     //might come SliceTooComplexDFAUsage
43     JavaSliceUsage javaSliceUsage = sliceUsage instanceof JavaSliceUsage ? (JavaSliceUsage)sliceUsage : null;
44
45     TextChunk[] text = sliceUsage.getText();
46     final List<TextRange> usageRanges = new SmartList<TextRange>();
47     sliceUsage.processRangeMarkers(new Processor<Segment>() {
48       @Override
49       public boolean process(Segment segment) {
50         usageRanges.add(TextRange.create(segment));
51         return true;
52       }
53     });
54     boolean isInsideContainer = javaSliceUsage != null && javaSliceUsage.indexNesting != 0;
55     for (int i = 0, length = text.length; i < length; i++) {
56       TextChunk textChunk = text[i];
57       SimpleTextAttributes attributes = textChunk.getSimpleAttributesIgnoreBackground();
58       if (isForcedLeaf) {
59         attributes = attributes.derive(attributes.getStyle(), Color.LIGHT_GRAY, attributes.getBgColor(), attributes.getWaveColor());
60       }
61       boolean inUsage = (attributes.getFontStyle() & Font.BOLD) != 0;
62       if (isInsideContainer && inUsage) {
63         //Color darker = Color.BLACK;//attributes.getBgColor() == null ? Color.BLACK : attributes.getBgColor().darker();
64         //attributes = attributes.derive(SimpleTextAttributes.STYLE_OPAQUE, attributes.getFgColor(), UIUtil.getTreeBackground().brighter(), attributes.getWaveColor());
65         //setMyBorder(IdeBorderFactory.createRoundedBorder(10, 3));
66         //setPaintFocusBorder(true);
67       }
68       append(textChunk.getText(), attributes);
69       if(i == 0){
70         append(FontUtil.spaceAndThinSpace());
71       }
72     }
73
74     if (javaSliceUsage != null) {
75       for (int i = 0; i < javaSliceUsage.indexNesting; i++) {
76         append(
77           " (Tracking container contents" +
78           (javaSliceUsage.syntheticField.isEmpty() ? "" : " '" + javaSliceUsage.syntheticField + "'") +
79           ")",
80           SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
81       }
82     }
83
84     PsiElement element = sliceUsage.getElement();
85     PsiMethod method;
86     PsiClass aClass;
87     while (true) {
88       method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
89       aClass = method == null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : method.getContainingClass();
90       if (aClass instanceof PsiAnonymousClass) {
91         element = aClass;
92       }
93       else {
94         break;
95       }
96     }
97     int methodOptions = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS | PsiFormatUtilBase.SHOW_CONTAINING_CLASS;
98     String location = method != null
99                       ? PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, methodOptions, PsiFormatUtilBase.SHOW_TYPE, 2)
100                       : aClass != null ? PsiFormatUtil.formatClass(aClass, PsiFormatUtilBase.SHOW_NAME) : null;
101     if (location != null) {
102       SimpleTextAttributes attributes = SimpleTextAttributes.GRAY_ATTRIBUTES;
103       append(" in " + location, attributes);
104     }
105   }
106 }
107