Run method when cursor is in specific function argument

Hi,

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?

0
2 comments

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

EditorFactory.getInstance().getEventMulticaster();

eventMulticaster.addCaretListener(new CaretListener() {
@Override
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);

 

1

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, StringLiteralExpression::class.java, false) }
}

override fun caretRemoved(e: CaretEvent?) {}
}
0

Please sign in to leave a comment.