How to find out if a PsiElement is in selected text?

已回答

I am writing a plugin that should offer a run configuration when the user right-clicks on selected text in an editor.
So I created a RunconfigurationProducer, implemented setupConfigurationFromContext and isConfigurationFromContext. 
This all works fine, however I don't know how to find out if the PsiEelement in context.psiLocation is part of any selected text, and what that selected text is.
Right now I'm using

val editors: Array<FileEditor> = FileEditorManager.getInstance(context.project).getSelectedEditors()
val textEditor: TextEditor = editors.get(0) as TextEditor
val caretModel: CaretModel = textEditor.editor.caretModel
val selectedText = caretModel.currentCaret.selectedText


to retrieve the selected text but that doesn't check at all if the user actually right-clicked on it.
How should I find out if the PsiElement that context.psiLocation refers to is in selected text?

0

Please obtain Editor via

dataContext = com.intellij.execution.actions.ConfigurationContext#getDataContext

final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);

 

See Editor Basics trail on how to work with Caret, Offset and map to PsiElement#getTextRange() https://plugins.jetbrains.com/docs/intellij/editor-basics.html

 

0

请先登录再写评论。