com.intellij.codeInsight.completion.util.ParenthesesInsertHandler Java Examples

The following examples show how to use com.intellij.codeInsight.completion.util.ParenthesesInsertHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeywordCompletionProvider.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
@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));
    }
}
 
Example #2
Source File: JSGraphQLEndpointCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: CGCompletionContributor.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
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)));
			}
		}
	});
}
 
Example #4
Source File: CypherBuiltInFunctionElement.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #5
Source File: CypherProcedureElement.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #6
Source File: CypherUserFunctionElement.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@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()));
}
 
Example #7
Source File: CommonMacroCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
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);
  };
}
 
Example #8
Source File: BuiltInFunctionCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #9
Source File: GraphQLCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
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);
}
 
Example #10
Source File: CSharpTypeLikeLookupElement.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@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);
		}
	}
}