下面列出了怎么用 com.intellij.codeInsight.completion.util.ParenthesesInsertHandler 的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
LOG.debug(m_debugName + " expression completion");
for (String keyword : m_keywords) {
InsertHandler<LookupElement> insertHandler;
if ("Some".equals(keyword)) {
insertHandler = ParenthesesInsertHandler.getInstance(true);
} else {
insertHandler = KEYWORD_WITH_POPUP.contains(keyword) ? INSERT_SPACE_POPUP : INSERT_SPACE;
}
LookupElementBuilder builder = LookupElementBuilder.create(keyword).
withInsertHandler(insertHandler).
bold();
result.addElement(PrioritizedLookupElement.withPriority(builder, KEYWORD_PRIORITY));
}
}
private boolean completeAnnotations(@NotNull CompletionResultSet result, boolean autoImport, PsiFile file, PsiElement completionElement) {
final JSGraphQLEndpointFieldDefinition field = PsiTreeUtil.getNextSiblingOfType(completionElement, JSGraphQLEndpointFieldDefinition.class);
final JSGraphQLEndpointProperty property = PsiTreeUtil.getNextSiblingOfType(completionElement, JSGraphQLEndpointProperty.class);
final JSGraphQLEndpointAnnotation nextAnnotation = PsiTreeUtil.getNextSiblingOfType(completionElement, JSGraphQLEndpointAnnotation.class);
final boolean afterAtAnnotation = completionElement.getNode().getElementType() == JSGraphQLEndpointTokenTypes.AT_ANNOTATION;
final boolean isTopLevelCompletion = completionElement.getParent() instanceof JSGraphQLEndpointFile;
if (afterAtAnnotation || isTopLevelCompletion || field != null || nextAnnotation != null || property != null) {
final JSGraphQLConfigurationProvider configurationProvider = JSGraphQLConfigurationProvider.getService(file.getProject());
for (JSGraphQLSchemaEndpointAnnotation endpointAnnotation : configurationProvider.getEndpointAnnotations(file)) {
String completion = endpointAnnotation.name;
if (!afterAtAnnotation) {
completion = "@" + completion;
}
LookupElementBuilder element = LookupElementBuilder.create(completion).withIcon(JSGraphQLIcons.Schema.Attribute);
if(endpointAnnotation.arguments != null && endpointAnnotation.arguments.size() > 0) {
element = element.withInsertHandler(ParenthesesInsertHandler.WITH_PARAMETERS);
}
result.addElement(element);
}
return true;
}
return false;
}
public CGCompletionContributor()
{
extend(CompletionType.BASIC, StandardPatterns.psiElement().withLanguage(CGLanguage.INSTANCE), new CompletionProvider()
{
@RequiredReadAction
@Override
public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull CompletionResultSet result)
{
for(String keyword : CGKeywords.KEYWORDS)
{
result.addElement(LookupElementBuilder.create(keyword).bold());
}
for(String m : ourMethods)
{
result.addElement(LookupElementBuilder.create(m + "()").withIcon((Image) AllIcons.Nodes.Method).withInsertHandler(ParenthesesInsertHandler.getInstance(true)));
}
}
});
}
@Override
public LookupElement getLookupElement() {
return LookupElementBuilder
.create(invokable.getName())
.bold()
.withIcon(GraphIcons.Nodes.FUNCTION)
.withTailText(invokable.getSignature())
.withTypeText(invokable.getReturnTypeString())
.withInsertHandler(ParenthesesInsertHandler.getInstance(invokable.hasParameters()));
}
@Override
public LookupElement getLookupElement() {
return LookupElementBuilder
.create(name)
.bold()
.withIcon(GraphIcons.Nodes.STORED_PROCEDURE)
.withTailText(invokableInformation.getSignature())
.withTypeText(invokableInformation.getReturnTypeString())
.withInsertHandler(ParenthesesInsertHandler.getInstance(invokableInformation.hasParameters()));
}
@Override
public LookupElement getLookupElement() {
return LookupElementBuilder
.create(name)
.bold()
.withIcon(GraphIcons.Nodes.USER_FUNCTION)
.withTailText(invokableInformation.getSignature())
.withTypeText(invokableInformation.getReturnTypeString())
.withInsertHandler(ParenthesesInsertHandler.getInstance(invokableInformation.hasParameters()));
}
private static InsertHandler<LookupElement> getInsertHandler(
BuildFile file, String packageLocation, String symbol) {
ParenthesesInsertHandler<LookupElement> base =
ParenthesesInsertHandler.getInstance(/* hasParameters= */ true);
return (context, item) -> {
base.handleInsert(context, item);
insertLoadStatement(context, file, packageLocation, symbol);
};
}
private static InsertHandler<LookupElement> getInsertHandler(
String ruleName, @Nullable BuildLanguageSpec spec) {
if (spec == null) {
return ParenthesesInsertHandler.getInstance(true);
}
return RulesTemplates.templateForRule(ruleName, spec)
.map(BuiltInFunctionCompletionContributor::createTemplateInsertHandler)
.orElse(ParenthesesInsertHandler.getInstance(true));
}
private void completeFieldNames() {
CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
// move "__*" fields to bottom
final CompletionResultSet orderedResult = updateResult(parameters, result);
final PsiElement completionElement = Optional.ofNullable(parameters.getOriginalPosition()).orElse(parameters.getPosition());
GraphQLTypeScopeProvider typeScopeProvider = PsiTreeUtil.getParentOfType(completionElement, GraphQLTypeScopeProvider.class);
// check for incomplete field name, in which case we want the parent element as type scope to find this field name
GraphQLField completionField = null;
if (completionElement.getNode().getElementType() == GraphQLElementTypes.NAME) {
completionField = PsiTreeUtil.getParentOfType(completionElement, GraphQLField.class);
}
if (typeScopeProvider == completionField) {
// completed on incomplete field name, use parent
typeScopeProvider = PsiTreeUtil.getParentOfType(typeScopeProvider, GraphQLTypeScopeProvider.class);
}
if (typeScopeProvider != null) {
PsiElement textBeforeCompletion = PsiTreeUtil.prevLeaf(completionElement);
boolean isSpread = textBeforeCompletion != null && textBeforeCompletion.getText().startsWith(".");
if (!isSpread) {
GraphQLType typeScope = typeScopeProvider.getTypeScope();
if (typeScope != null) {
// we need the raw type to get the fields
typeScope = GraphQLUtil.getUnmodifiedType(typeScope);
}
if (typeScope instanceof GraphQLFieldsContainer) {
((GraphQLFieldsContainer) typeScope).getFieldDefinitions().forEach(field -> {
LookupElementBuilder element = LookupElementBuilder
.create(field.getName())
.withBoldness(true)
.withTypeText(SchemaIDLUtil.typeString(field.getType()));
if (field.getDescription() != null) {
final String fieldDocumentation = GraphQLDocumentationMarkdownRenderer.getDescriptionAsPlainText(field.getDescription(), true);
element = element.withTailText(" - " + fieldDocumentation, true);
}
if (field.isDeprecated()) {
element = element.strikeout();
if (field.getDeprecationReason() != null) {
final String deprecationReason = GraphQLDocumentationMarkdownRenderer.getDescriptionAsPlainText(field.getDeprecationReason(), true);
element = element.withTailText(" - Deprecated: " + deprecationReason, true);
}
}
for (graphql.schema.GraphQLArgument fieldArgument : field.getArguments()) {
if (fieldArgument.getType() instanceof GraphQLNonNull) {
// on of the field arguments are required, so add the '()' for arguments
element = element.withInsertHandler((ctx, item) -> {
ParenthesesInsertHandler.WITH_PARAMETERS.handleInsert(ctx, item);
AutoPopupController.getInstance(ctx.getProject()).autoPopupMemberLookup(ctx.getEditor(), null);
});
break;
}
}
orderedResult.addElement(element);
});
}
if (!(typeScopeProvider instanceof GraphQLOperationDefinition)) {
// show the '...' except when top level selection in an operation
orderedResult.addElement(LookupElementBuilder.create("...").withInsertHandler((ctx, item) -> {
AutoPopupController.getInstance(ctx.getProject()).autoPopupMemberLookup(ctx.getEditor(), null);
}));
// and add the built-in __typename option
orderedResult.addElement(LookupElementBuilder.create("__typename"));
}
}
}
}
};
extend(CompletionType.BASIC, psiElement(GraphQLElementTypes.NAME).withSuperParent(2, GraphQLField.class), provider);
}
@Override
@RequiredWriteAction
public void handleInsert(InsertionContext context)
{
super.handleInsert(context);
if(myAfterNew)
{
context.commitDocument();
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
boolean hasParameters = false;
if(myExtractor != DotNetGenericExtractor.EMPTY)
{
PsiElement psiElement = getPsiElement();
if(psiElement instanceof CSharpTypeDeclaration)
{
DotNetNamedElement[] members = ((CSharpTypeDeclaration) psiElement).getMembers();
for(DotNetNamedElement member : members)
{
if(member instanceof CSharpConstructorDeclaration)
{
int length = ((CSharpConstructorDeclaration) member).getParameters().length;
if(length > 0)
{
hasParameters = true;
break;
}
}
}
}
else if(CSharpMethodUtil.isDelegate(psiElement))
{
hasParameters = true;
}
}
CaretModel caretModel = context.getEditor().getCaretModel();
int oldCaretOffset = caretModel.getOffset();
ParenthesesInsertHandler.getInstance(true).handleInsert(context, this);
if(!hasParameters)
{
caretModel.moveToOffset(oldCaretOffset + 2);
}
}
}