I developed a function to embed prompts, but the name is not displayed in the configuration interface of embedded prompts. Configuration interface address Settings | Editor | Inlay Hints|Code Vision
as the picture shows:

Below is the code I used to implement this function
class CodeVisionInlay : CodeVisionProvider<Unit>{
companion object {
internal const val id: String = "java.inheritors"
}
override fun isAvailableFor(project: Project): Boolean {
return true
}
override fun precomputeOnUiThread(editor: Editor) {
}
override fun preparePreview(editor: Editor, file: PsiFile) {
}
override fun computeCodeVision(editor: Editor, uiData: Unit): CodeVisionState {
return runReadAction {
val project = editor.project ?: return@runReadAction CodeVisionState.READY_EMPTY
val document = editor.document
val file = PsiDocumentManager.getInstance(project).getPsiFile(document)
?: return@runReadAction CodeVisionState.READY_EMPTY
val lenses = ArrayList<Pair<TextRange, CodeVisionEntry>>()
val traverser = SyntaxTraverser.psiTraverser(file)
for (element in traverser.preOrderDfsTraversal()) {
if (element is PsiMethod) {
val textRange = InlayHintsUtils.getTextRangeWithoutLeadingCommentsAndWhitespaces(element)
val length = editor.document.textLength
val adjustedRange =
TextRange(Integer.min(textRange.startOffset, length), Integer.min(textRange.endOffset, length))
val icon = ToolUtils.getImages("/images/pluginIcon.svg")//AllIcons.Vcs.Author
val clickHandler = CodeAuthorClickHandler(element)
val entry = ClickableTextCodeVisionEntry("SmartChat", id, onClick = clickHandler, icon, "SmartChat", "SmartChat", emptyList())
entry.showInMorePopup = false
lenses.add(adjustedRange to entry)
}
}
return@runReadAction CodeVisionState.Ready(lenses)
}
}
private class CodeAuthorClickHandler(val element: PsiElement) : (MouseEvent?, Editor) -> Unit {
override fun invoke(event: MouseEvent?, editor: Editor) {
if ((event != null) ) {
JBPopupFactory.getInstance().createListPopup(
PluginDartIconActioinMenuList(
element = element
)
).show(RelativePoint(event.locationOnScreen))
}
}
}
override val name: String
get() = JavaBundle.message("settings.inlay.java.inheritors")
override val relativeOrderings: List<CodeVisionRelativeOrdering>
get() = emptyList()
override val defaultAnchor: CodeVisionAnchorKind
get() = CodeVisionAnchorKind.Default
override val id: String
get() = Companion.id
data class PluginDartIconActionMenuItem(val key:String,val title: String, val type: String, val icon: Icon)
class PluginDartIconActioinMenuList(val element: PsiElement) : BaseListPopupStep<PluginDartIconActionMenuItem>() {
//按钮组列表
private val menus = getMenus()
init {
super.init(element.text, menus, menus.map { it.icon })
}
override fun getTextFor(value: PluginDartIconActionMenuItem?): String {
return value?.title ?: "未知选项"
}
override fun onChosen(selectedValue: PluginDartIconActionMenuItem?, finalChoice: Boolean): PopupStep<*>? {
val key = selectedValue?.key;
val title = selectedValue?.title
val project = PsiManager.getInstance(element.getProject()).project
val service = project.service<MyProjectService>()
if(service.jbCefBrowser == null) {
val notificationGroup = NotificationGroup("AISE.Notification", NotificationDisplayType.BALLOON, true)
val notify = notificationGroup.createNotification("Please open SmartChat chat window before using SmartChat context menu(Menu->View->Tool Window->SmartChat). ", NotificationType.WARNING)
Notifications.Bus.notify(notify)
println("jbCefBrowser is null !")
}
val selectedText = element.text;
val msgJSON = JSONObject()
msgJSON.put("type","sendCodeContextMessage.jetBrains")
msgJSON.put("content","")
msgJSON.put("id",System.currentTimeMillis())
msgJSON.put("key",key) //"Dev_Code_Explain"
msgJSON.put("visibleText","") // "Dev_Code_Explain"
msgJSON.put("selectedContent", selectedText)
msgJSON.put("triggerSource","ext.contextMenu")
val data = JSONArray()
msgJSON["selection"] = data
if (selectedText !== null ) {
service.jbCefBrowser!!.cefBrowser.mainFrame.executeJavaScript("window.pluginCallBrowser(${msgJSON.toJSONString()})",
service.jbCefBrowser!!.cefBrowser.mainFrame.url, 0) //JSONStyle.NO_COMPRESS
}
else {
val notificationGroup = NotificationGroup("AISE.Notification", NotificationDisplayType.BALLOON, true)
val notify = notificationGroup.createNotification("No code block selected for "+title, NotificationType.INFORMATION)
Notifications.Bus.notify(notify)
println("selectedText is null !!selectedText:"+selectedText)
}
return super.onChosen(selectedValue, finalChoice)
}
fun getMenus(): List<PluginDartIconActionMenuItem> {
val listOfValues = mutableListOf<PluginDartIconActionMenuItem>()
val jsonArrayString = """
[
{
"key": "Dev_Code_Explain",
"text": "explain",
"description": "New York",
"actionClassName":"ToolbarDecorator",
"icon":"Export"
},
{
"key": "Dev_Code_Review",
"text": "review",
"description": "review",
"actionClassName":"Nodes",
"icon":"Field"
},
{
"key": "Dev_Code_GenUT",
"text": "generate unit tests",
"description": "generate unit tests",
"actionClassName":"Nodes",
"icon":"TestSourceFolder"
}
]
"""
val gson = Gson()
// 使用TypeToken创建一个Type,以便Gson知道如何解析List<Map>
val listType = object : TypeToken<List<Map<String, String>>>() {}.type
// 将JSON字符串转换为List<Map>
val list: List<Map<String, String>> = gson.fromJson(jsonArrayString, listType)
for (element in list) {
val firstMap: Map<String, String> = element
val key = firstMap["key"].toString()
val text = firstMap["text"].toString()
val actionClassName = firstMap["actionClassName"].toString()
val icon = firstMap["icon"].toString()
val iconImage = ToolUtils.getIconName(actionClassName,icon)
listOfValues.add(
PluginDartIconActionMenuItem(
key = key,
title = text,
type = "navToPub",
icon = iconImage
))
}
return listOfValues
}
}
}
Please sign in to leave a comment.
Brothers, do you know how to deal with this problem?
Hi,
What part of the code implements the menu? What is the expected message? As far as I can see, similar entries under Code Vision are implemented via
CodeVisionGroupSettingProvider
.Also, please use Code Block formatting widget for the code listings.
Thanks a lot, CodeVisionGroupSettingProvider works great for me