How to indicate a problem to the user?

If during a ContextAction the plug-in experiences a problem and needs to display a message to the user. How should this be handled?

For example; A ContextAction that opens a file with a simular name to the current class. What if the target file doesn't exist?

Is it recommended to use MessageBox or is there a more pro-SDK approach?

2 comments
Comment actions Permalink

The unwritten standard is to display a tooltip if the action fails. The underlying bulb action of a quick fix/context action is usually built on BulbActionBase (although it only needs to implement IBulbAction), and this class provides an ExecutePsiTransaction method, which returns an Action<ITextControl>. In other words, you return an action that will be run after the PSI transaction has committed or been rolled back.

Under success conditions, it can be used to move the text caret to the correct location in the text control, but it also allows you to display a tooltip, anchored to the text caret. BulbActionBase provides the ShowTooltip method which can do this for you. The action you return would look like this:

control => ShowTooltip(control, queueName, message)


where queueName is a consistent name used for queueing (the tooltip invocation happens on the UI thread, and is queued to happen once the bulb action completes. The name is used to ensure it's queued correctly, and also for diagnostics). The message is obviously the message you want to display.

Interestingly, I can't see many usages of this in the source code. I'm not sure if this is because most of the built in quick fixes/context actions assume that everything will work until it fails and an exception is thrown, in which case, the transaction is aborted and a standard error tooltip is shown. Or it might be that the tooltip is shown manually, as I haven't trawled through all of the bulb actions.

0
Comment actions Permalink

Thanks that will work great.

0

Please sign in to leave a comment.