How to use code completion in comments
Answered
EN:
How to use code completion in comments
When I enter "," in the comment, code completion does not appear. Is there any way to achieve this?
CN:
当我在注释中输入","时,不会出现代码完成。 有什么办法可以实现这一点吗?
Please sign in to leave a comment.
Please always provide the full context and code.
Hi, I want to type "," or "@" when writing a comment to prompt some code completion. Is there any way to do this? It would be nice to have a simple example.
I use the following code to trigger code completion. But it has no effect in the comments
Why do you override
fillCompletionVariants()
instead of implementing providers and registering them withCompletionContributor.extend()
?https://plugins.jetbrains.com/docs/intellij/completion-contributor.html
When I try the following code. It still didn't work.
The effect is the same as the previous picture.
If I remember correctly, I tried "CompletionContributor.extend()" I just tried again.
It seems that
JavaDocCompletionContributor
stops completion in some cases, see stopHere occurrences in https://github.com/JetBrains/intellij-community/blob/master/java/java-impl/src/com/intellij/codeInsight/completion/JavaDocCompletionContributor.java.To fix it, add
order="first"
for your contributor in your plugin.xml (and always provide the full context, including registration in plugin.xml).After I modified the plugin.xml file. The effect is the same as before.
I don't know what can be the problem. It works for me perfectly.
Registration:
Implementation:
Screenshot:
Ok. Thank you.
Hi Guys, I am having same Issue, Code Completion triggers out side comment but when I try totrigger it in comments, It does not work.
class MyCompletionContributor extends CompletionContributor {
class MyCompletionContributor extends CompletionContributor {
public MyCompletionContributor() {
extend(CompletionType.BASIC,
PlatformPatterns.psiElement().inside(PsiComment.class),
new CompletionProvider<CompletionParameters>() {
@Override
public void addCompletions(@NotNull CompletionParameters parameters,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
resultSet.addElement(LookupElementBuilder.create("Hello"));
resultSet.addElement(LookupElementBuilder.create("World"));
resultSet.addElement(LookupElementBuilder.create("UniversalCompletionItem"));
}
}
);
}
}
<completion.contributor
language="JAVA" order="first"
implementationClass="org.intellij.sdk.language.MyCompletionContributor"/>
I also tried replacing
PlatformPatterns.psiElement().inside(PsiComment.class)
withpsiElement()
with no luck.However when I use
PlatformPatterns.psiElement(PsiElement.class).withElementType(JavaTokenType.IDENTIFIER)
It doe trigger code completion but not inside the comment section.
Yasirfarhan
Hi. I used another way to solve the problem.
plugin.xml
Result: A prompt appears when I type ",". I hope that will be helpful.
Thanks for the input.
Here are my observations about TypedHandlerDelegate. It is much more complex for my use case, I am trying to avoid manually handling triggers. It also triggered more frequently then the other one which can cause performance issue.
Hi,
Using type handler approach is an overkill, and it shouldn't be implemented this way. Please share your plugin source code or a minimal reproducible example plugin, so I can investigate the issue.
Hi,I have the same problem, I want to customize some suggestions while writing comments
plugin.xml:
<completion.contributor
language="JAVA" order="first"
implementationClass="com.test.MyCompletionContributor"/>
MyCompletionContributor.java:
public MyCompletionContributor() {
extend(CompletionType.BASIC,
PlatformPatterns.psiElement().inside(PsiComment.class),
new CompletionProvider<CompletionParameters>() {
@Override
public void addCompletions(@NotNull CompletionParameters parameters,
@NotNull ProcessingContext context,
@NotNull CompletionResultSet resultSet) {
resultSet.addElement(LookupElementBuilder.create("Hello"));
resultSet.addElement(LookupElementBuilder.create("World"));
}
}
);
}
but it does not work while in single line comment
it only works in comments block while typing @