Completion replacement with symbols

Answered

I'm using code-completion to suggest a set of commands for my DSL. However, the language requires a command to be prefixed with the AT @ symbol. This gives some unexpected behavior in the completion which I can work around but I was wondering if there are better ways to do this.

example:

1) empty line -> suggests '@command' -> results in @command :)

2) when at @ -> suggests '@command' -> results in @@command :(

3) when at @com -> suggests '@command' -> results in @@command :(

In line 3 the com part is highlighted in the completion so it does recognize it. However, the @ is not and therefore probably always persisted.

The pattern fires by looking at the position of the placeholder in the psi tree using the PlatformPatterns, based on the parent class of the placeholder the @command is suggested. In all cases the placeholder is IntelliJRulezzz.

 

My workaround is to use an InsertHandler that will check if the resulting context has '@@' at startOffset -1 to +1 and then uses the PsiDocumentManager to remove the excess @. This looks like it's working but I'm always a bit hesistant when it comes to accessing the document directly with insertString / deleteString.

So the question is, is this the way to do it, or is there an easier way to include the @ symbol in the string that is replaced when the completion intention is invoked?

0
3 comments

I have a better solution which checks the previous visible node of the current position and removes the leading symbol from the suggestion if it's already there. That already looks a lot nicer than having to remove it after insertion but I'm still wondering if there is a better solution :)

0

Looks like last item in Q&A on com.intellij.codeInsight.completion.CompletionContributor Javadoc.

0

I think that one describes the completion process itself, for example when you have a completion result that is 'key:value' to prevent the completion from disappearing once you enter the colon. Also it's not really clear how to even use this since I cannot find any part of the completion process giving access to a CharFilter.

What I'm looking for is something that will include the symbol in the prefix matcher. I could use the withPrefixMatcher probably but that would also require me to determine if the symbol is actually there (using the workaround I added as comment)

0

Please sign in to leave a comment.