Run method when cursor is in specific function argument


I am trying to extend my plugin, but I require some help yet again! I want to show the user a small pop-up, or some other visual indication with text when the cursor is within the parameters of the Translate function (in PHP), shown as following:

The plugin is supposed to use the number underneath the cursor, find the coresponding index in other php file, and show the coresponding translation for the user somewhere in the IDE. So first things first, how do I trigger any method in the plugin, when the cursor is within the parameters? I thought of inspections, but those wont work in this case. Actions cant be triggered like this, can they?

Comment actions Permalink

I'd try CaretListener, just as is done in SubstitutionShortInfoHandler.  It can be installed in some StartupActivity:


eventMulticaster.addCaretListener(new CaretListener() {
public void caretPositionChanged(CaretEvent e) {
LogicalPosition position = e.getNewPosition();
final int offset = editor.logicalPositionToOffset(position);
Document document = editor.getDocument();
PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
PsiTreeUtil.findElementOfClassAtOffset(file, offset, StringLiteralExpression.class, false);
}, project);


Comment actions Permalink

Thanks, the following code worked:

class CaretListenerStartup : StartupActivity {
override fun runActivity(project: Project) {
val multicaster = EditorFactory.getInstance().eventMulticaster;
multicaster.addCaretListener(Listener(), project)

class Listener : CaretListener {
override fun caretAdded(e: CaretEvent?) {}

override fun caretPositionChanged(e: CaretEvent) {
val position = e.newPosition
val editor = e.editor
val document = editor.document
val offset = editor.logicalPositionToOffset(position)
val file = editor.project?.let { PsiDocumentManager.getInstance(it).getPsiFile(document) }
val element = file?.let { PsiTreeUtil.findElementOfClassAtOffset(it, offset,, false) }

override fun caretRemoved(e: CaretEvent?) {}

Please sign in to leave a comment.