consulo.codeInsight.completion.CompletionProvider Java Examples

The following examples show how to use consulo.codeInsight.completion.CompletionProvider. 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: ShaderLabCGCompletionContributor.java    From consulo-unity3d with Apache License 2.0 6 votes vote down vote up
public ShaderLabCGCompletionContributor()
{
	extend(CompletionType.BASIC, StandardPatterns.psiElement().withLanguage(CGLanguage.INSTANCE), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull final CompletionResultSet result)
		{
			Place shreds = InjectedLanguageUtil.getShreds(parameters.getOriginalFile());

			for(PsiLanguageInjectionHost.Shred shred : shreds)
			{
				PsiLanguageInjectionHost host = shred.getHost();
				if(host instanceof ShaderCGScript)
				{
					ShaderLabFile containingFile = (ShaderLabFile) host.getContainingFile();
					ShaderReference.consumeProperties(containingFile, result::addElement);
				}
			}
		}
	});
}
 
Example #2
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 #3
Source File: CSharpLinqCompletionContributor.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
static void extend(CompletionContributor contributor)
{
	contributor.extend(CompletionType.BASIC, CSharpPatterns.expressionStart(), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull CompletionResultSet result)
		{
			PsiElement position = parameters.getPosition();
			if(!CSharpModuleUtil.findLanguageVersion(position).isAtLeast(CSharpLanguageVersion._3_0))
			{
				return;
			}
			CSharpReferenceExpressionEx parent = (CSharpReferenceExpressionEx) position.getParent();
			if(parent.getQualifier() != null || parent.kind() != CSharpReferenceExpression.ResolveToKind.ANY_MEMBER)
			{
				return;
			}

			CSharpCompletionUtil.elementToLookup(result, CSharpSoftTokens.FROM_KEYWORD, null, null);
		}
	});
}
 
Example #4
Source File: CSharpDocCompletionContributor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
public CSharpDocCompletionContributor()
{
	extend(CompletionType.BASIC, StandardPatterns.psiElement(CSharpDocTokenType.XML_NAME), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull CompletionResultSet result)
		{
			PsiElement parent = parameters.getPosition().getParent();
			if(parent instanceof CSharpDocTagImpl)
			{
				Collection<CSharpDocTagInfo> tags = CSharpDocTagManager.getInstance().getTags();
				for(CSharpDocTagInfo tag : tags)
				{
					result.addElement(LookupElementBuilder.create(tag.getName()));
				}
			}
			else if(parent instanceof CSharpDocAttribute)
			{
				CSharpDocTagInfo tagInfo = ((CSharpDocAttribute) parent).getTagInfo();
				if(tagInfo == null)
				{
					return;
				}
				for(CSharpDocAttributeInfo attributeInfo : tagInfo.getAttributes())
				{
					result.addElement(LookupElementBuilder.create(attributeInfo.getName()));
				}
			}
		}
	});
}
 
Example #5
Source File: CustomFileTypeCompletionContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public CustomFileTypeCompletionContributor() {
  extend(CompletionType.BASIC, psiElement().inFile(psiFile().withFileType(instanceOf(CustomSyntaxTableFileType.class))), new CompletionProvider() {
    @RequiredReadAction
    @Override
    public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull CompletionResultSet result) {
      if (inCommentOrLiteral(parameters)) {
        return;
      }

      FileType fileType = parameters.getOriginalFile().getFileType();
      if (!(fileType instanceof CustomSyntaxTableFileType)) {
        return;
      }

      SyntaxTable syntaxTable = ((CustomSyntaxTableFileType)fileType).getSyntaxTable();
      String prefix = findPrefix(parameters.getPosition(), parameters.getOffset());
      CompletionResultSet resultSetWithPrefix = result.withPrefixMatcher(prefix);

      addVariants(resultSetWithPrefix, syntaxTable.getKeywords1());
      addVariants(resultSetWithPrefix, syntaxTable.getKeywords2());
      addVariants(resultSetWithPrefix, syntaxTable.getKeywords3());
      addVariants(resultSetWithPrefix, syntaxTable.getKeywords4());

      WordCompletionContributor.addWordCompletionVariants(resultSetWithPrefix, parameters, Collections.<String>emptySet());
    }
  });
}
 
Example #6
Source File: PostfixTemplateCompletionContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
public PostfixTemplateCompletionContributor() {
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider() {
    @RequiredReadAction
    @Override
    public void addCompletions(@Nonnull CompletionParameters parameters, ProcessingContext context, @Nonnull CompletionResultSet result) {
      Editor editor = parameters.getEditor();
      if (!isCompletionEnabled(parameters) || LiveTemplateCompletionContributor.shouldShowAllTemplates() ||
          editor.getCaretModel().getCaretCount() != 1) {
        /**
         * disabled or covered with {@link com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor}
         */
        return;
      }

      PsiFile originalFile = parameters.getOriginalFile();
      PostfixLiveTemplate postfixLiveTemplate = PostfixTemplateCompletionContributor.getPostfixLiveTemplate(originalFile, editor);
      if (postfixLiveTemplate != null) {
        postfixLiveTemplate.addCompletions(parameters, result.withPrefixMatcher(new MyPrefixMatcher(result.getPrefixMatcher().getPrefix())));
        String possibleKey = postfixLiveTemplate.computeTemplateKeyWithoutContextChecking(new CustomTemplateCallback(editor, originalFile));
        if (possibleKey != null) {
          result = result.withPrefixMatcher(possibleKey);
          result.restartCompletionOnPrefixChange(
                  StandardPatterns.string().oneOf(postfixLiveTemplate.getAllTemplateKeys(originalFile, parameters.getOffset())));
        }
      }
    }
  });
}
 
Example #7
Source File: CSharpAccessorCompletionContributor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
static void extend(CompletionContributor contributor)
{
	contributor.extend(CompletionType.BASIC, psiElement().andNot(psiElement().inside(DotNetXAccessor.class)), new CompletionProvider()
	{
		@RequiredReadAction
		@Override
		public void addCompletions(@Nonnull CompletionParameters completionParameters, ProcessingContext processingContext, @Nonnull CompletionResultSet resultSet)
		{
			PsiElement position = completionParameters.getPosition();
			final CSharpXAccessorOwner accessorOwner = PsiTreeUtil.getParentOfType(position, CSharpXAccessorOwner.class);
			if(accessorOwner == null)
			{
				return;
			}
			PsiElement leftBrace = accessorOwner.getLeftBrace();
			if(leftBrace == null)
			{
				return;
			}

			int textOffset = position.getTextOffset();
			PsiElement rightBrace = accessorOwner.getRightBrace();
			int rightTextRange = rightBrace == null ? -1 : rightBrace.getTextOffset();

			if((rightTextRange == -1 || textOffset < rightTextRange) && textOffset > leftBrace.getTextOffset())
			{
				if(accessorOwner.hasModifier(DotNetModifier.ABSTRACT))
				{
					buildAccessorKeywordsCompletion(resultSet, accessorOwner, null);
				}
				else
				{
					buildAccessorKeywordsCompletion(resultSet, accessorOwner, new InsertHandler<LookupElement>()
					{
						@Override
						@RequiredWriteAction
						public void handleInsert(InsertionContext context, LookupElement item)
						{
							if(context.getCompletionChar() == '{')
							{
								context.setAddCompletionChar(false);

								Editor editor = context.getEditor();

								CaretModel caretModel = editor.getCaretModel();
								int offset = caretModel.getOffset();

								context.getDocument().insertString(offset, "{\n}");
								caretModel.moveToOffset(offset + 1);

								PsiElement elementAt = context.getFile().findElementAt(offset - 1);

								context.commitDocument();

								DotNetXAccessor accessor = PsiTreeUtil.getParentOfType(elementAt, DotNetXAccessor.class);
								if(accessor != null)
								{
									CodeStyleManager.getInstance(context.getProject()).reformat(accessor);
								}

								EditorWriteActionHandler actionHandler = (EditorWriteActionHandler) EditorActionManager.getInstance().getActionHandler(IdeActions.ACTION_EDITOR_ENTER);
								actionHandler.executeWriteAction(editor, DataManager.getInstance().getDataContext(editor.getContentComponent()));
							}
						}
					});
				}
			}
		}
	});
}
 
Example #8
Source File: CompletionContributor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public final void extend(@javax.annotation.Nullable CompletionType type, final ElementPattern<? extends PsiElement> place, CompletionProvider provider) {
  myMap.putValue(type, Pair.create(place, provider));
}
 
Example #9
Source File: LiveTemplateCompletionContributor.java    From consulo with Apache License 2.0 4 votes vote down vote up
public LiveTemplateCompletionContributor() {
  extend(CompletionType.BASIC, PlatformPatterns.psiElement(), new CompletionProvider() {
    @RequiredReadAction
    @Override
    public void addCompletions(@Nonnull final CompletionParameters parameters,
                                  ProcessingContext context,
                                  @Nonnull CompletionResultSet result) {
      final PsiFile file = parameters.getPosition().getContainingFile();
      if (file instanceof PsiPlainTextFile && parameters.getEditor().getComponent().getParent() instanceof EditorTextField) {
        return;
      }

      Editor editor = parameters.getEditor();
      int offset = editor.getCaretModel().getOffset();
      final List<TemplateImpl> availableTemplates = TemplateManagerImpl.listApplicableTemplates(file, offset, false);
      final Map<TemplateImpl, String> templates = filterTemplatesByPrefix(availableTemplates, editor, offset, false, false);
      if (showAllTemplates()) {
        final AtomicBoolean templatesShown = new AtomicBoolean(false);
        final CompletionResultSet finalResult = result;
        result.runRemainingContributors(parameters, new Consumer<CompletionResult>() {
          @Override
          public void consume(CompletionResult completionResult) {
            finalResult.passResult(completionResult);
            ensureTemplatesShown(templatesShown, templates, parameters, finalResult);
          }
        });

        ensureTemplatesShown(templatesShown, templates, parameters, result);
        return;
      }

      if (parameters.getInvocationCount() > 0) return; //only in autopopups for now

      // custom templates should handle this situation by itself (return true from hasCompletionItems() and provide lookup element)
      // regular templates won't be shown in this case
      if (!customTemplateAvailableAndHasCompletionItem(null, editor, file, offset)) {
        TemplateImpl template = findFullMatchedApplicableTemplate(editor, offset, availableTemplates);
        if (template != null) {
          result.withPrefixMatcher(result.getPrefixMatcher().cloneWithPrefix(template.getKey()))
                  .addElement(new LiveTemplateLookupElementImpl(template, true));
        }
      }

      for (Map.Entry<TemplateImpl, String> possible : templates.entrySet()) {
        String templateKey = possible.getKey().getKey();
        String currentPrefix = possible.getValue();
        result.withPrefixMatcher(result.getPrefixMatcher().cloneWithPrefix(currentPrefix))
                .restartCompletionOnPrefixChange(templateKey);
      }
    }
  });
}