use of com.intellij.codeInsight.lookup.LookupElementBuilder in project intellij-community by JetBrains.
the class PydevConsoleReference method getVariants.
@NotNull
public Object[] getVariants() {
Map<String, LookupElement> variants = Maps.newHashMap();
try {
final List<PydevCompletionVariant> completions = myCommunication.getCompletions(getText(), myPrefix);
for (PydevCompletionVariant completion : completions) {
final PsiManager manager = myElement.getManager();
final String name = completion.getName();
final int type = completion.getType();
LookupElementBuilder builder = LookupElementBuilder.create(new PydevConsoleElement(manager, name, completion.getDescription())).withIcon(PyCodeCompletionImages.getImageForType(type));
String args = completion.getArgs();
if (args.equals("(%)")) {
builder.withPresentableText("%" + completion.getName());
builder = builder.withInsertHandler(new InsertHandler<LookupElement>() {
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Editor editor = context.getEditor();
final Document document = editor.getDocument();
int offset = context.getStartOffset();
if (offset == 0 || !"%".equals(document.getText(TextRange.from(offset - 1, 1)))) {
document.insertString(offset, "%");
}
}
});
args = "";
} else if (!StringUtil.isEmptyOrSpaces(args)) {
builder = builder.withTailText(args);
}
// Set function insert handler
if (type == IToken.TYPE_FUNCTION || args.endsWith(")")) {
builder = builder.withInsertHandler(ParenthesesInsertHandler.WITH_PARAMETERS);
}
variants.put(name, builder);
}
} catch (Exception e) {
//LOG.error(e);
}
return variants.values().toArray();
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project intellij-community by JetBrains.
the class ChooseTypeExpression method calculateLookupItems.
@Override
public LookupElement[] calculateLookupItems(ExpressionContext context) {
List<LookupElement> result = ContainerUtil.newArrayList();
for (SmartTypePointer item : myItems) {
PsiType type = TypesUtil.unboxPrimitiveTypeWrapper(item.getType());
if (type == null)
continue;
PsiTypeLookupItem lookupItem = PsiTypeLookupItem.createLookupItem(type, null, PsiTypeLookupItem.isDiamond(type), IMPORT_FIXER);
result.add(lookupItem);
}
if (myForGroovy) {
LookupElementBuilder def = LookupElementBuilder.create(GrModifier.DEF).bold();
if (mySelectDef) {
result.add(0, def);
} else {
result.add(def);
}
}
return result.toArray(new LookupElement[result.size()]);
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project intellij-community by JetBrains.
the class CompleteReferenceExpression method createPropertyLookupElement.
@Nullable
public static LookupElementBuilder createPropertyLookupElement(@NotNull PsiMethod accessor, @Nullable GroovyResolveResult resolveResult, @Nullable PrefixMatcher matcher) {
String propName;
PsiType propType;
final boolean getter = GroovyPropertyUtils.isSimplePropertyGetter(accessor, null);
if (getter) {
propName = GroovyPropertyUtils.getPropertyNameByGetter(accessor);
} else if (GroovyPropertyUtils.isSimplePropertySetter(accessor, null)) {
propName = GroovyPropertyUtils.getPropertyNameBySetter(accessor);
} else {
return null;
}
assert propName != null;
if (!PsiUtil.isValidReferenceName(propName)) {
propName = "'" + propName + "'";
}
if (matcher != null && !matcher.prefixMatches(propName)) {
return null;
}
if (getter) {
propType = PsiUtil.getSmartReturnType(accessor);
} else {
propType = accessor.getParameterList().getParameters()[0].getType();
}
final PsiType substituted = resolveResult != null ? resolveResult.getSubstitutor().substitute(propType) : propType;
LookupElementBuilder builder = LookupElementBuilder.create(generatePropertyResolveResult(propName, accessor, propType, resolveResult), propName).withIcon(JetgroovyIcons.Groovy.Property);
if (substituted != null) {
builder = builder.withTypeText(substituted.getPresentableText());
}
return builder;
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project intellij-community by JetBrains.
the class ResourceBundleReference method getVariants.
@Override
@NotNull
public Object[] getVariants() {
final ProjectFileIndex projectFileIndex = ProjectFileIndex.SERVICE.getInstance(getElement().getProject());
final PropertiesReferenceManager referenceManager = PropertiesReferenceManager.getInstance(getElement().getProject());
final Set<String> bundleNames = new HashSet<>();
final List<LookupElement> variants = new SmartList<>();
PropertiesFileProcessor processor = new PropertiesFileProcessor() {
@Override
public boolean process(String baseName, PropertiesFile propertiesFile) {
if (!bundleNames.add(baseName))
return true;
final LookupElementBuilder builder = LookupElementBuilder.create(baseName).withIcon(AllIcons.Nodes.ResourceBundle);
boolean isInContent = projectFileIndex.isInContent(propertiesFile.getVirtualFile());
variants.add(isInContent ? PrioritizedLookupElement.withPriority(builder, Double.MAX_VALUE) : builder);
return true;
}
};
referenceManager.processPropertiesFiles(myElement.getResolveScope(), processor, this);
return variants.toArray(new LookupElement[variants.size()]);
}
use of com.intellij.codeInsight.lookup.LookupElementBuilder in project intellij-community by JetBrains.
the class MapArgumentCompletionProvider method addCompletions.
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
PsiElement mapOrArgumentList = findMapOrArgumentList(parameters);
if (mapOrArgumentList == null) {
return;
}
if (isMapKeyCompletion(parameters)) {
result.stopHere();
}
final Map<String, NamedArgumentDescriptor> map = ContainerUtil.newHashMap(calculateNamedArguments(mapOrArgumentList));
for (GrNamedArgument argument : getSiblingNamedArguments(mapOrArgumentList)) {
map.remove(argument.getLabelName());
}
for (Map.Entry<String, NamedArgumentDescriptor> entry : map.entrySet()) {
LookupElementBuilder lookup = LookupElementBuilder.create(entry.getValue(), entry.getKey()).withInsertHandler(NamedArgumentInsertHandler.INSTANCE).withTailText(":");
if (entry.getValue().getPriority() == NamedArgumentDescriptor.Priority.UNLIKELY) {
lookup = lookup.withItemTextForeground(GroovySyntaxHighlighter.MAP_KEY.getDefaultAttributes().getForegroundColor());
} else {
lookup = lookup.withIcon(JetgroovyIcons.Groovy.DynamicProperty);
}
LookupElement customized = entry.getValue().customizeLookupElement(lookup);
result.addElement(customized == null ? lookup : customized);
}
}
Aggregations