Search in sources :

Example 1 with TextChunk

use of com.intellij.usages.TextChunk in project intellij-community by JetBrains.

the class NameUsage method getPresentation.

@NotNull
public UsagePresentation getPresentation() {
    return new UsagePresentation() {

        @Nullable
        public Icon getIcon() {
            PyPsiUtils.assertValid(myElement);
            return myElement.isValid() ? myElement.getIcon(0) : null;
        }

        @NotNull
        public TextChunk[] getText() {
            PyPsiUtils.assertValid(myElement);
            if (myElement.isValid()) {
                TextChunk[] chunks = new TextChunk[3];
                PsiFile file = myElement.getContainingFile();
                String line_id = "...";
                final Document document = file.getViewProvider().getDocument();
                if (document != null) {
                    line_id = String.valueOf(document.getLineNumber(myElement.getTextOffset()));
                }
                chunks[0] = new TextChunk(SLANTED, "(" + line_id + ") ");
                chunks[1] = new TextChunk(TextAttributes.ERASE_MARKER, myElement.getText());
                StringBuilder sb = new StringBuilder(" would become ").append(myName);
                if (myIsPrefix)
                    sb.append(".").append(myElement.getText());
                chunks[2] = new TextChunk(SLANTED, sb.toString());
                return chunks;
            } else
                return new TextChunk[] { new TextChunk(SLANTED, "?") };
        }

        @NotNull
        public String getPlainText() {
            return myElement.getText();
        }

        public String getTooltipText() {
            return myElement.getText();
        }
    };
}
Also used : TextChunk(com.intellij.usages.TextChunk) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) UsagePresentation(com.intellij.usages.UsagePresentation) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TextChunk

use of com.intellij.usages.TextChunk in project intellij-community by JetBrains.

the class UsageListCellRenderer method customizeCellRenderer.

@Override
protected void customizeCellRenderer(@NotNull final JList list, final Object value, final int index, final boolean selected, final boolean hasFocus) {
    Usage usage = (Usage) value;
    UsagePresentation presentation = usage.getPresentation();
    setIcon(presentation.getIcon());
    VirtualFile virtualFile = getVirtualFile(usage);
    if (virtualFile != null) {
        append(virtualFile.getName() + ": ", SimpleTextAttributes.REGULAR_ATTRIBUTES);
        setIcon(virtualFile.getFileType().getIcon());
        PsiFile psiFile = PsiManager.getInstance(myProject).findFile(virtualFile);
        if (psiFile != null) {
            setIcon(psiFile.getIcon(0));
        }
    }
    TextChunk[] text = presentation.getText();
    for (TextChunk textChunk : text) {
        SimpleTextAttributes simples = textChunk.getSimpleAttributesIgnoreBackground();
        append(textChunk.getText(), simples);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Usage(com.intellij.usages.Usage) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) PsiFile(com.intellij.psi.PsiFile) TextChunk(com.intellij.usages.TextChunk) UsagePresentation(com.intellij.usages.UsagePresentation)

Example 3 with TextChunk

use of com.intellij.usages.TextChunk in project intellij-community by JetBrains.

the class SliceUsageCellRenderer method customizeCellRendererFor.

@Override
public void customizeCellRendererFor(@NotNull SliceUsage sliceUsage) {
    boolean isForcedLeaf = sliceUsage instanceof JavaSliceDereferenceUsage;
    //might come SliceTooComplexDFAUsage
    JavaSliceUsage javaSliceUsage = sliceUsage instanceof JavaSliceUsage ? (JavaSliceUsage) sliceUsage : null;
    TextChunk[] text = sliceUsage.getText();
    boolean isInsideContainer = javaSliceUsage != null && javaSliceUsage.indexNesting != 0;
    for (int i = 0, length = text.length; i < length; i++) {
        TextChunk textChunk = text[i];
        SimpleTextAttributes attributes = textChunk.getSimpleAttributesIgnoreBackground();
        if (isForcedLeaf) {
            attributes = attributes.derive(attributes.getStyle(), JBColor.LIGHT_GRAY, attributes.getBgColor(), attributes.getWaveColor());
        }
        boolean inUsage = BitUtil.isSet(attributes.getFontStyle(), Font.BOLD);
        if (isInsideContainer && inUsage) {
        //Color darker = Color.BLACK;//attributes.getBgColor() == null ? Color.BLACK : attributes.getBgColor().darker();
        //attributes = attributes.derive(SimpleTextAttributes.STYLE_OPAQUE, attributes.getFgColor(), UIUtil.getTreeBackground().brighter(), attributes.getWaveColor());
        //setMyBorder(IdeBorderFactory.createRoundedBorder(10, 3));
        //setPaintFocusBorder(true);
        }
        append(textChunk.getText(), attributes);
        if (i == 0) {
            append(FontUtil.spaceAndThinSpace());
        }
    }
    if (javaSliceUsage != null && javaSliceUsage.indexNesting != 0) {
        append(" (Tracking container '" + getContainerName(javaSliceUsage) + (javaSliceUsage.syntheticField.isEmpty() ? "" : "." + javaSliceUsage.syntheticField) + "' contents)", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
    }
    PsiElement element = sliceUsage.getElement();
    PsiMethod method;
    PsiClass aClass;
    while (true) {
        method = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
        aClass = method == null ? PsiTreeUtil.getParentOfType(element, PsiClass.class) : method.getContainingClass();
        if (aClass instanceof PsiAnonymousClass) {
            element = aClass;
        } else {
            break;
        }
    }
    int methodOptions = PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS | PsiFormatUtilBase.SHOW_CONTAINING_CLASS;
    String location = method != null ? PsiFormatUtil.formatMethod(method, PsiSubstitutor.EMPTY, methodOptions, PsiFormatUtilBase.SHOW_TYPE, 2) : aClass != null ? PsiFormatUtil.formatClass(aClass, PsiFormatUtilBase.SHOW_NAME) : null;
    if (location != null) {
        SimpleTextAttributes attributes = SimpleTextAttributes.GRAY_ATTRIBUTES;
        append(" in " + location, attributes);
    }
    Language language = element == null ? JavaLanguage.INSTANCE : element.getLanguage();
    if (language != JavaLanguage.INSTANCE) {
        SliceLanguageSupportProvider foreignSlicing = LanguageSlicing.getProvider(element);
        if (foreignSlicing == null) {
            append(" (in " + language.getDisplayName() + " file - stopped here)", SimpleTextAttributes.EXCLUDED_ATTRIBUTES);
        }
    }
}
Also used : TextChunk(com.intellij.usages.TextChunk) Language(com.intellij.lang.Language) JavaLanguage(com.intellij.lang.java.JavaLanguage) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes)

Example 4 with TextChunk

use of com.intellij.usages.TextChunk in project intellij-community by JetBrains.

the class SliceLeafValueRootNode method appendElementText.

private static void appendElementText(@NotNull UsageInfo2UsageAdapter usage, @NotNull final PsiElement element, @NotNull final SliceUsageCellRendererBase renderer) {
    PsiFile file = element.getContainingFile();
    List<TextChunk> result = new ArrayList<>();
    ChunkExtractor.getExtractor(element.getContainingFile()).createTextChunks(usage, file.getText(), element.getTextRange().getStartOffset(), element.getTextRange().getEndOffset(), false, result);
    for (TextChunk chunk : result) {
        renderer.append(chunk.getText(), chunk.getSimpleAttributesIgnoreBackground());
    }
}
Also used : ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) TextChunk(com.intellij.usages.TextChunk)

Example 5 with TextChunk

use of com.intellij.usages.TextChunk in project intellij-community by JetBrains.

the class ShowUsagesTableCellRenderer method getTableCellRendererComponent.

@Override
public Component getTableCellRendererComponent(JTable list, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    UsageNode usageNode = value instanceof UsageNode ? (UsageNode) value : null;
    Usage usage = usageNode == null ? null : usageNode.getUsage();
    Color fileBgColor = getBackgroundColor(isSelected, usage);
    Color bg = UIUtil.getListSelectionBackground();
    Color fg = UIUtil.getListSelectionForeground();
    Color panelBackground = isSelected ? bg : fileBgColor == null ? list.getBackground() : fileBgColor;
    Color panelForeground = isSelected ? fg : list.getForeground();
    SimpleColoredComponent textChunks = new SimpleColoredComponent();
    if (usageNode == null || usageNode instanceof ShowUsagesAction.StringNode) {
        textChunks.append(ObjectUtils.notNull(value, "").toString(), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
        return textComponentSpanningWholeRow(textChunks, panelBackground, panelForeground, column, list);
    }
    if (usage == ShowUsagesAction.MORE_USAGES_SEPARATOR) {
        textChunks.append("...<");
        textChunks.append("more usages", SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
        textChunks.append(">...");
        return textComponentSpanningWholeRow(textChunks, panelBackground, panelForeground, column, list);
    }
    if (usage == ShowUsagesAction.USAGES_OUTSIDE_SCOPE_SEPARATOR) {
        textChunks.append("...<");
        textChunks.append(UsageViewManagerImpl.outOfScopeMessage(myOutOfScopeUsages.get(), mySearchScope), SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES);
        textChunks.append(">...");
        return textComponentSpanningWholeRow(textChunks, panelBackground, panelForeground, column, list);
    }
    boolean lineNumberColumn = column == 1;
    JPanel panel = new JPanel(new FlowLayout(lineNumberColumn ? FlowLayout.RIGHT : FlowLayout.LEFT, 0, 0) {

        @Override
        public void layoutContainer(Container container) {
            super.layoutContainer(container);
            for (Component component : container.getComponents()) {
                // align inner components
                Rectangle b = component.getBounds();
                Insets insets = container.getInsets();
                component.setBounds(b.x, b.y, b.width, container.getSize().height - insets.top - insets.bottom);
            }
        }
    });
    panel.setFont(null);
    panel.setBackground(panelBackground);
    panel.setForeground(panelForeground);
    // greying the current usage you originated your "find usages" from is turned off by @nik orders
    //!myUsageView.isOriginUsage(usage);
    boolean isEnabled = true;
    if (!isEnabled) {
        fg = UIUtil.getLabelDisabledForeground();
    }
    if (column == 0) {
        appendGroupText(list, (GroupNode) usageNode.getParent(), panel, fileBgColor, isSelected);
    } else {
        if (usage != ShowUsagesAction.MORE_USAGES_SEPARATOR && usage != ShowUsagesAction.USAGES_OUTSIDE_SCOPE_SEPARATOR) {
            UsagePresentation presentation = usage.getPresentation();
            TextChunk[] text = presentation.getText();
            if (lineNumberColumn) {
                // line number
                if (text.length != 0) {
                    TextChunk chunk = text[0];
                    textChunks.append(chunk.getText(), getAttributes(isSelected, fileBgColor, bg, fg, chunk));
                }
            } else if (column == 2) {
                Icon icon = presentation.getIcon();
                textChunks.setIcon(icon == null ? EmptyIcon.ICON_16 : icon);
                textChunks.append("").appendTextPadding(JBUI.scale(16 + 5));
                for (int i = 1; i < text.length; i++) {
                    TextChunk chunk = text[i];
                    textChunks.append(chunk.getText(), getAttributes(isSelected, fileBgColor, bg, fg, chunk));
                }
            } else {
                assert false : column;
            }
        }
        SpeedSearchUtil.applySpeedSearchHighlighting(list, textChunks, false, isSelected);
        panel.add(textChunks);
    }
    if (!isEnabled) {
        GuiUtils.enableChildren(panel, false);
    }
    return panel;
}
Also used : Usage(com.intellij.usages.Usage) TextChunk(com.intellij.usages.TextChunk) UsagePresentation(com.intellij.usages.UsagePresentation) UsageNode(com.intellij.usages.impl.UsageNode) EmptyIcon(com.intellij.util.ui.EmptyIcon) SimpleColoredComponent(com.intellij.ui.SimpleColoredComponent) SimpleColoredComponent(com.intellij.ui.SimpleColoredComponent)

Aggregations

TextChunk (com.intellij.usages.TextChunk)6 PsiFile (com.intellij.psi.PsiFile)3 UsagePresentation (com.intellij.usages.UsagePresentation)3 SimpleTextAttributes (com.intellij.ui.SimpleTextAttributes)2 Usage (com.intellij.usages.Usage)2 NotNull (org.jetbrains.annotations.NotNull)2 Language (com.intellij.lang.Language)1 JavaLanguage (com.intellij.lang.java.JavaLanguage)1 Document (com.intellij.openapi.editor.Document)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 SimpleColoredComponent (com.intellij.ui.SimpleColoredComponent)1 UsageNode (com.intellij.usages.impl.UsageNode)1 EmptyIcon (com.intellij.util.ui.EmptyIcon)1 ArrayList (java.util.ArrayList)1