Search in sources :

Example 51 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class LookupCellRenderer method getListCellRendererComponent.

@Override
public Component getListCellRendererComponent(final JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
    boolean nonFocusedSelection = isSelected && myLookup.getFocusDegree() == LookupImpl.FocusDegree.SEMI_FOCUSED;
    if (!myLookup.isFocused()) {
        isSelected = false;
    }
    myIsSelected = isSelected;
    final LookupElement item = (LookupElement) value;
    final Color foreground = getForegroundColor(isSelected);
    final Color background = nonFocusedSelection ? SELECTED_NON_FOCUSED_BACKGROUND_COLOR : isSelected ? SELECTED_BACKGROUND_COLOR : BACKGROUND_COLOR;
    int allowedWidth = list.getWidth() - calcSpacing(myNameComponent, myEmptyIcon) - calcSpacing(myTailComponent, null) - calcSpacing(myTypeLabel, null);
    FontMetrics normalMetrics = getRealFontMetrics(item, false);
    FontMetrics boldMetrics = getRealFontMetrics(item, true);
    final LookupElementPresentation presentation = new RealLookupElementPresentation(isSelected ? getMaxWidth() : allowedWidth, normalMetrics, boldMetrics, myLookup);
    AccessToken token = ReadAction.start();
    try {
        if (item.isValid()) {
            try {
                item.renderElement(presentation);
            } catch (ProcessCanceledException e) {
                LOG.info(e);
                presentation.setItemTextForeground(JBColor.RED);
                presentation.setItemText("Error occurred, see the log in Help | Show Log");
            } catch (Exception | Error e) {
                LOG.error(e);
            }
        } else {
            presentation.setItemTextForeground(JBColor.RED);
            presentation.setItemText("Invalid");
        }
    } finally {
        token.finish();
    }
    myNameComponent.clear();
    myNameComponent.setBackground(background);
    allowedWidth -= setItemTextLabel(item, new JBColor(isSelected ? SELECTED_FOREGROUND_COLOR : presentation.getItemTextForeground(), presentation.getItemTextForeground()), isSelected, presentation, allowedWidth);
    Font font = myLookup.getCustomFont(item, false);
    if (font == null) {
        font = myNormalFont;
    }
    myTailComponent.setFont(font);
    myTypeLabel.setFont(font);
    myNameComponent.setIcon(augmentIcon(myLookup.getEditor(), presentation.getIcon(), myEmptyIcon));
    myTypeLabel.clear();
    if (allowedWidth > 0) {
        allowedWidth -= setTypeTextLabel(item, background, foreground, presentation, isSelected ? getMaxWidth() : allowedWidth, isSelected, nonFocusedSelection, normalMetrics);
    }
    myTailComponent.clear();
    myTailComponent.setBackground(background);
    if (isSelected || allowedWidth >= 0) {
        setTailTextLabel(isSelected, presentation, foreground, isSelected ? getMaxWidth() : allowedWidth, nonFocusedSelection, normalMetrics);
    }
    if (mySelected.containsKey(index)) {
        if (!isSelected && mySelected.get(index)) {
            myPanel.setUpdateExtender(true);
        }
    }
    mySelected.put(index, isSelected);
    final double w = myNameComponent.getPreferredSize().getWidth() + myTailComponent.getPreferredSize().getWidth() + myTypeLabel.getPreferredSize().getWidth();
    boolean useBoxLayout = isSelected && w > list.getWidth() && ((JBList) list).getExpandableItemsHandler().isEnabled();
    if (useBoxLayout != myPanel.getLayout() instanceof BoxLayout) {
        myPanel.removeAll();
        if (useBoxLayout) {
            myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.X_AXIS));
            myPanel.add(myNameComponent);
            myPanel.add(myTailComponent);
            myPanel.add(myTypeLabel);
        } else {
            myPanel.setLayout(new BorderLayout());
            myPanel.add(myNameComponent, BorderLayout.WEST);
            myPanel.add(myTailComponent, BorderLayout.CENTER);
            myPanel.add(myTypeLabel, BorderLayout.EAST);
        }
    }
    AccessibleContextUtil.setCombinedName(myPanel, myNameComponent, "", myTailComponent, " - ", myTypeLabel);
    AccessibleContextUtil.setCombinedDescription(myPanel, myNameComponent, "", myTailComponent, " - ", myTypeLabel);
    return myPanel;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) LookupValueWithUIHint(com.intellij.codeInsight.lookup.LookupValueWithUIHint) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) RealLookupElementPresentation(com.intellij.codeInsight.lookup.RealLookupElementPresentation) LookupElementPresentation(com.intellij.codeInsight.lookup.LookupElementPresentation) AccessToken(com.intellij.openapi.application.AccessToken) JBList(com.intellij.ui.components.JBList) RealLookupElementPresentation(com.intellij.codeInsight.lookup.RealLookupElementPresentation) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 52 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class MyLookupExpression method initLookupItems.

private static LookupElement[] initLookupItems(LinkedHashSet<String> names, PsiNamedElement elementToRename, PsiElement nameSuggestionContext, final boolean shouldSelectAll) {
    if (names == null) {
        names = new LinkedHashSet<>();
        for (NameSuggestionProvider provider : Extensions.getExtensions(NameSuggestionProvider.EP_NAME)) {
            final SuggestedNameInfo suggestedNameInfo = provider.getSuggestedNames(elementToRename, nameSuggestionContext, names);
            if (suggestedNameInfo != null && provider instanceof PreferrableNameSuggestionProvider && !((PreferrableNameSuggestionProvider) provider).shouldCheckOthers()) {
                break;
            }
        }
    }
    final LookupElement[] lookupElements = new LookupElement[names.size()];
    final Iterator<String> iterator = names.iterator();
    for (int i = 0; i < lookupElements.length; i++) {
        final String suggestion = iterator.next();
        lookupElements[i] = LookupElementBuilder.create(suggestion).withInsertHandler(new InsertHandler<LookupElement>() {

            @Override
            public void handleInsert(InsertionContext context, LookupElement item) {
                if (shouldSelectAll)
                    return;
                final Editor topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(context.getEditor());
                final TemplateState templateState = TemplateManagerImpl.getTemplateState(topLevelEditor);
                if (templateState != null) {
                    final TextRange range = templateState.getCurrentVariableRange();
                    if (range != null) {
                        topLevelEditor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), suggestion);
                    }
                }
            }
        });
    }
    return lookupElements;
}
Also used : PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) NameSuggestionProvider(com.intellij.refactoring.rename.NameSuggestionProvider) TextRange(com.intellij.openapi.util.TextRange) PreferrableNameSuggestionProvider(com.intellij.refactoring.rename.PreferrableNameSuggestionProvider) LookupElement(com.intellij.codeInsight.lookup.LookupElement) InsertionContext(com.intellij.codeInsight.completion.InsertionContext) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) InsertHandler(com.intellij.codeInsight.completion.InsertHandler) Editor(com.intellij.openapi.editor.Editor)

Example 53 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class TemplateExpressionLookupElement method doHandleInsert.

private void doHandleInsert(InsertionContext context) {
    LookupElement item = getDelegate();
    PsiDocumentManager.getInstance(context.getProject()).commitAllDocuments();
    TextRange range = myState.getCurrentVariableRange();
    final TemplateLookupSelectionHandler handler = item.getUserData(TemplateLookupSelectionHandler.KEY_IN_LOOKUP_ITEM);
    if (handler != null && range != null) {
        handler.itemSelected(item, context.getFile(), context.getDocument(), range.getStartOffset(), range.getEndOffset());
    } else {
        super.handleInsert(context);
    }
}
Also used : TemplateLookupSelectionHandler(com.intellij.codeInsight.template.TemplateLookupSelectionHandler) TextRange(com.intellij.openapi.util.TextRange) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement)

Example 54 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class TemplateExpressionLookupElement method createInsertionContext.

private static InsertionContext createInsertionContext(LookupElement item, PsiFile psiFile, List<? extends LookupElement> elements, Editor editor, final char completionChar) {
    final OffsetMap offsetMap = new OffsetMap(editor.getDocument());
    final InsertionContext context = new InsertionContext(offsetMap, completionChar, elements.toArray(new LookupElement[elements.size()]), psiFile, editor, false);
    context.setTailOffset(editor.getCaretModel().getOffset());
    offsetMap.addOffset(CompletionInitializationContext.START_OFFSET, context.getTailOffset() - item.getLookupString().length());
    offsetMap.addOffset(CompletionInitializationContext.SELECTION_END_OFFSET, context.getTailOffset());
    offsetMap.addOffset(CompletionInitializationContext.IDENTIFIER_END_OFFSET, context.getTailOffset());
    return context;
}
Also used : InsertionContext(com.intellij.codeInsight.completion.InsertionContext) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) OffsetMap(com.intellij.codeInsight.completion.OffsetMap)

Example 55 with LookupElement

use of com.intellij.codeInsight.lookup.LookupElement in project intellij-community by JetBrains.

the class LookupOffsets method checkMinPrefixLengthChanges.

void checkMinPrefixLengthChanges(Collection<LookupElement> items, LookupImpl lookup) {
    if (myStableStart)
        return;
    if (!lookup.isCalculating() && !items.isEmpty()) {
        myStableStart = true;
    }
    int minPrefixLength = items.isEmpty() ? 0 : Integer.MAX_VALUE;
    for (final LookupElement item : items) {
        if (!(item instanceof EmptyLookupItem)) {
            minPrefixLength = Math.min(lookup.itemMatcher(item).getPrefix().length(), minPrefixLength);
        }
    }
    int start = getPivotOffset() - minPrefixLength - myAdditionalPrefix.length() + myRemovedPrefix;
    start = Math.max(Math.min(start, myEditor.getDocument().getTextLength()), 0);
    if (myLookupStartMarker.isValid() && myLookupStartMarker.getStartOffset() == start && myLookupStartMarker.getEndOffset() == start) {
        return;
    }
    myLookupStartMarker.dispose();
    myLookupStartMarker = createLeftGreedyMarker(start);
    myStartDisposeTrace = null;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)183 PsiElement (com.intellij.psi.PsiElement)33 NotNull (org.jetbrains.annotations.NotNull)32 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)20 Nullable (org.jetbrains.annotations.Nullable)17 ArrayList (java.util.ArrayList)14 Project (com.intellij.openapi.project.Project)12 LookupElementPresentation (com.intellij.codeInsight.lookup.LookupElementPresentation)10 Document (com.intellij.openapi.editor.Document)10 PsiFile (com.intellij.psi.PsiFile)10 Editor (com.intellij.openapi.editor.Editor)9 THashSet (gnu.trove.THashSet)8 LinkedHashSet (java.util.LinkedHashSet)8 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)7 XmlTag (com.intellij.psi.xml.XmlTag)7 Consumer (com.intellij.util.Consumer)7 HashSet (java.util.HashSet)7 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)6 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6