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;
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations