use of com.intellij.codeInsight.completion.util.ParenthesesInsertHandler in project intellij-plugins by JetBrains.
the class DartServerCompletionContributor method createLookupElement.
private static LookupElement createLookupElement(@NotNull final Project project, @NotNull final CompletionSuggestion suggestion) {
final Element element = suggestion.getElement();
final Location location = element == null ? null : element.getLocation();
final DartLookupObject lookupObject = new DartLookupObject(project, location);
final String lookupString = suggestion.getCompletion();
LookupElementBuilder lookup = LookupElementBuilder.create(lookupObject, lookupString);
// keywords are bold
if (suggestion.getKind().equals(CompletionSuggestionKind.KEYWORD)) {
lookup = lookup.bold();
}
final int dotIndex = lookupString.indexOf('.');
if (dotIndex > 0 && dotIndex < lookupString.length() - 1 && StringUtil.isJavaIdentifier(lookupString.substring(0, dotIndex)) && StringUtil.isJavaIdentifier(lookupString.substring(dotIndex + 1))) {
// 'path.Context' should match 'Conte' prefix
lookup = lookup.withLookupString(lookupString.substring(dotIndex + 1));
}
boolean shouldSetSelection = true;
if (element != null) {
// @deprecated
if (element.isDeprecated()) {
lookup = lookup.strikeout();
}
// append type parameters
final String typeParameters = element.getTypeParameters();
if (typeParameters != null) {
lookup = lookup.appendTailText(typeParameters, false);
}
// append parameters
final String parameters = element.getParameters();
if (parameters != null) {
lookup = lookup.appendTailText(parameters, false);
}
// append return type
final String returnType = element.getReturnType();
if (!StringUtils.isEmpty(returnType)) {
lookup = lookup.withTypeText(returnType, true);
}
// icon
Icon icon = getBaseImage(element);
if (icon != null) {
icon = applyVisibility(icon, element.isPrivate());
icon = applyOverlay(icon, element.isFinal(), AllIcons.Nodes.FinalMark);
icon = applyOverlay(icon, element.isConst(), AllIcons.Nodes.FinalMark);
lookup = lookup.withIcon(icon);
}
// Prepare for typing arguments, if any.
if (CompletionSuggestionKind.INVOCATION.equals(suggestion.getKind())) {
shouldSetSelection = false;
final List<String> parameterNames = suggestion.getParameterNames();
if (parameterNames != null) {
lookup = lookup.withInsertHandler((context, item) -> {
// like in JavaCompletionUtil.insertParentheses()
final boolean needRightParenth = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET || parameterNames.isEmpty() && context.getCompletionChar() != '(';
if (parameterNames.isEmpty()) {
final ParenthesesInsertHandler<LookupElement> handler = ParenthesesInsertHandler.getInstance(false, false, false, needRightParenth, false);
handler.handleInsert(context, item);
} else {
final ParenthesesInsertHandler<LookupElement> handler = ParenthesesInsertHandler.getInstance(true, false, false, needRightParenth, false);
handler.handleInsert(context, item);
// Show parameters popup.
final Editor editor = context.getEditor();
final PsiElement psiElement = lookupObject.getElement();
if (DartCodeInsightSettings.getInstance().INSERT_DEFAULT_ARG_VALUES) {
// Insert argument defaults if provided.
final String argumentListString = suggestion.getDefaultArgumentListString();
if (argumentListString != null) {
final Document document = editor.getDocument();
int offset = editor.getCaretModel().getOffset();
// At this point caret is expected to be right after the opening paren.
// But if user was completing using Tab over the existing method call with arguments then old arguments are still there,
// if so, skip inserting argumentListString
final CharSequence text = document.getCharsSequence();
if (text.charAt(offset - 1) == '(' && text.charAt(offset) == ')') {
document.insertString(offset, argumentListString);
PsiDocumentManager.getInstance(project).commitDocument(document);
final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(context.getFile());
final int[] ranges = suggestion.getDefaultArgumentListTextRanges();
// Only proceed if ranges are provided and well-formed.
if (ranges != null && (ranges.length & 1) == 0) {
int index = 0;
while (index < ranges.length) {
final int start = ranges[index];
final int length = ranges[index + 1];
final String arg = argumentListString.substring(start, start + length);
final TextExpression expression = new TextExpression(arg);
final TextRange range = new TextRange(offset + start, offset + start + length);
index += 2;
builder.replaceRange(range, "group_" + (index - 1), expression, true);
}
builder.run(editor, true);
}
}
}
}
AutoPopupController.getInstance(project).autoPopupParameterInfo(editor, psiElement);
}
});
}
}
}
// Use selection offset / length.
if (shouldSetSelection) {
lookup = lookup.withInsertHandler((context, item) -> {
final Editor editor = context.getEditor();
final int startOffset = context.getStartOffset() + suggestion.getSelectionOffset();
final int endOffset = startOffset + suggestion.getSelectionLength();
editor.getCaretModel().moveToOffset(startOffset);
if (endOffset > startOffset) {
editor.getSelectionModel().setSelection(startOffset, endOffset);
}
});
}
return PrioritizedLookupElement.withPriority(lookup, suggestion.getRelevance());
}
Aggregations