Posted on

Clickable Stack Traces and Function Names in Query Monitor

Many panels in Query Monitor display function names or stack traces. Wouldn’t it be great if you could click the function name and the file opens up in your text editor or IDE at the correct position? With the clickable file links feature you can, and you’ll wonder how you lived without it:

Screenshot of clickable function names in Query Monitor

There are two steps to enabling this in Query Monitor.

1. Configure Your Editor’s URL Handler

In order for a link on a web page to be able to open your editor, you need to configure your editor so it opens when a certain URL scheme is encountered. Here are the instructions for some common editors:

PhpStorm

You don’t need to do anything. PhpStorm natively supports phpstorm:// URLs.

VS Code

You don’t need to do anything. VS Code natively supports code:// URLs.

Atom

Install the Atom Open package to add support for atom:// URLs.

Sublime Text

Install the subl-handler package to add support for subl:// URLs.

Netbeans

Follow these instructions by Simon Wheatley to add support for nbopen:// URLs.

2. Enable Clickable File Links in Query Monitor

Clickable file links are enabled via a filter attached to the qm/output/file_link_format hook, which is best placed in a mu-plugin.

The filter should return the correct link format according to the editor that you use. Note that there are differences between the URL format for different editors, so pick the correct one.

PhpStorm

add_filter( 'qm/output/file_link_format', function( $format ) {
	return 'phpstorm://open?file=%f&line=%l';
} );

VS Code

add_filter( 'qm/output/file_link_format', function( $format ) {
	return 'vscode://file/%f:%l';
} );

Atom

add_filter( 'qm/output/file_link_format', function( $format ) {
	return 'atom://open/?url=file://%f&line=%l';
} );

Sublime Text

add_filter( 'qm/output/file_link_format', function( $format ) {
	return 'subl://open/?url=file://%f&line=%l';
} );

Netbeans

add_filter( 'qm/output/file_link_format', function( $format ) {
	return 'nbopen://%f:%l';
} );

That’s it! Query Monitor will now show you clickable file links wherever it outputs function names, file names, or stack traces.

Xdebug Setting

Pro tip: Query Monitor respects Xdebug’s xdebug.file_link_format directive in your php.ini file, so you can skip the need to use the qm/output/file_link_format filter completely if you set this directive to the correct format from above. Xdebug doesn’t even need to be enabled for this to work. More info here.

I use VS Code, so the directive looks like this:

xdebug.file_link_format = "vscode://file/%f:%l"

Remote File Path Mapping

If you’re debugging a remote site, or using Docker or a virtual machine, you’ll need to map the path on the server to a path on your local machine so your editor doesn’t try to load a non-existent file. You can do this using a filter on the qm/output/file_path_map hook which accepts an array of remote paths and the local path they map to.

For example, if you use Chassis or another Vagrant-based VM, your path mapping needs to look like this:

add_filter( 'qm/output/file_path_map', function( $map ) {
	$map['/vagrant/'] = '/path/to/local/project/';
	return $map;
} );

If you use VVV your path mapping needs to look like this:

add_filter( 'qm/output/file_path_map', function( $map ) {
	$map['/srv/'] = '/path/to/vvv/';
	return $map;
} );

That’s It!

I hope you enjoy clicking on your newly-clickable file links.

Leave a Reply

Your email address will not be published. Required fields are marked *