completion contributor trigger

Hi,

I'm trying to get symbol completions working. For example, I want a user to be able to type in a ':' and have the

`addCompletions(..)' method trigger. So of course I've extended CompletionContributor and implemented this. 

I'm having quite a bit of trouble with the pattern though. For motivation, here is some code:

public MathSymbolCompletionContributor() {
extend(CompletionType.BASIC, symbolInsertCommandPattern(), new CompletionProvider<CompletionParameters>() {
@Override
protected void addCompletions(@NotNull final CompletionParameters parameters,
ProcessingContext context,
@NotNull CompletionResultSet result) {
//POPULATE RESULT SET WITH STUFF That USERS CAN COMPLETE WITH...
}
});
}

private static PsiElementPattern.Capture<PsiElement> symbolInsertCommandPattern() {
return psiElement().withElementType(ResTypes.COLON);
}

Here's the problem: I can't (for the life of me) get the damn capture pattern firing when it needs to! If I change the pattern to psiElement() it of course triggers for arbitrary psiElements idents (like when I type 'p', 's', etc). But whenever I try to get it to fire for something like ':', ',' or any other non-identifier like thing it never seems to work... (these ARE psi elements, right!?) Psi viewer makes them out like they are. So I'm just puzzled as to why it doesn't go through for these. 

So far, to test, I've been setting breakpoints within where it says 

//POPULATE RESULT SET...

 

0
3 comments

Hmm, still am having a lot of trouble with the pattern. The problem is, I can't get ANY patterns which don't start with letters to trigger this thing... What in the world could I be doing wrong here?

Wouldn't the pattern just be:

private static PsiElementPattern.Capture<PsiElement> symbolInsertCommandPattern() {
return psiElement().withElementType(ResTypes.COLON);
}

ResTypes is the interface generated by grammarkit containing all of my language's tokens and composite types for all grammar rules... I even tried the following:

private ElementPattern<? extends PsiElement> foo() {
    return psiElement(LeafPsiElement.class).withElementType(ResTypes.COLON).withText(":");
}

and the following:

private ElementPattern<? extends PsiElement> foo() {
    return psiElement(LeafPsiElement.class).withElementType(ResTypes.COLON).withText(":").withParent(PsiErrorElement.class)
}

I just don't understand why I can only seem to match letters (in short, identifiers...) surely everything in my Psi Is a match-able psi element, right?

Please, any help would be much appreciated. This is kinda driving me crazy (even if it is obvious).

0

Ah! So I think I finally figured this out. The trick was to implement autopopup (as follows):

public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
return typeChar == ':';
}

Assuming the pattern for addCompletions() is something that will match most things (i.e., just psiElement()) inside addCompletions() you can check to ensure that a ':' indeed immediately preceeds the ident typed in. 

In other words, if you only want addCompletions adding the completions if the user typed ':' first, then you can check that manually in addCompletions() by examining the previous char. Seems to work.

0

Hello Daniel,

 

The auto-complete can be activated when you type the `:`  ?why my code do not work?  my question

Can you give some suggestion?

Thanks,

ML

0

Please sign in to leave a comment.