During plug-in development, how to embed prompt text in the editor code? My idea version is 2020. As shown in the figure:

Answered
0
4 comments

Hello! Can you give a specific example?

0

Hey, can you show me the problem with the code below? I want to embed prompts in the code, but it doesn't display properly when I run the code below.

 

 

 

package com.leaniss.smartchat.action;

import com.intellij.codeInsight.hints.*;
import com.intellij.codeInsight.hints.presentation.InlayPresentation;
import com.intellij.codeInsight.hints.presentation.PresentationFactory;
import com.intellij.lang.Language;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.util.ui.UI;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.event.MouseEvent;

/**
 * @Description //TODO
 * @Date 2023/12/3 19:50
 * @Author yongwangat
 **/
public class JavaScriptJSXCodeVisionProvider implements InlayHintsProvider<NoSettings> {
    private static final String CODE_LENS_ID = "Java";
    private static final SettingsKey<NoSettings> KEY = new SettingsKey<>(CODE_LENS_ID);

    @Nullable
    @Override
    public InlayHintsCollector getCollectorFor(@NotNull PsiFile file,
                                               @NotNull Editor editor,
                                               @NotNull NoSettings settings,
                                               @NotNull InlayHintsSink __) {
        return new FactoryInlayHintsCollector(editor) {
            @Override
            public boolean collect(@NotNull PsiElement element, @NotNull Editor editor, @NotNull InlayHintsSink sink) {
                // 在方法名后添加提示
                PresentationFactory factory = getFactory();
                int lineEndOffset = editor.getDocument().getLineEndOffset(element.getTextOffset());

                sink.addInlineElement(
                        lineEndOffset,
                        true,
                        factory.smallText("My Custom Java Hint")
                );

                return false;
            }
        };
    }

    @NotNull
    @Override
    public NoSettings createSettings() {
        return new NoSettings();
    }

    @NotNull
    @Override
    public @Nls String getName() {
        return "Custom Java Hints";
    }

    @NotNull
    @Override
    public SettingsKey<NoSettings> getKey() {
        return KEY;
    }

    @Nullable
    @Override
    public String getPreviewText() {
        return "Test Inlay for Java";
    }

    @NotNull
    @Override
    public ImmediateConfigurable createConfigurable(@NotNull NoSettings settings) {
        return new ImmediateConfigurable() {
            @NotNull
            @Override
            public JComponent createComponent(@NotNull ChangeListener listener) {
                // 可以添加自定义配置界面的组件
                return new JPanel();
            }
        };
    }

    @Override
    public boolean isLanguageSupported(@NotNull Language language) {
        // 指定该插件支持的语言,这里是 Java
        boolean flag = language.isKindOf(Language.findLanguageByID("JAVA"));
        System.out.println("插件支持的语言===="+flag);
        return language.isKindOf(Language.findLanguageByID("JAVA"));
    }

    @Override
    public boolean isVisibleInSettings() {
        return true;
    }
}

0

I don't see what the issue is. Is it registered in plugin.xml? Is it enabled in settings? Always provide the full context.

I also suggest checking existing implementations. See Exploring API on the tips about how to find them, especially:

 

0

Please sign in to leave a comment.