Skip to content

Opening files in your IDE

pmartin edited this page May 30, 2012 · 2 revisions

Line numbers in the left margin of Crew are hyperlinks, that look like this:

crewide://projectname@dir1/dir2/dir3/filename.ext@123

The specific crewide:// protocol which is used in those URLs allows you to configure your browser to open those links directly in your IDE.
That way, when you have a comment in Crew, clicking on the line number next to it will open the right file, at the right line, in your IDE -- making fixing your code so much easier.

A bit of setup is required before the magic can happen:

  • You have to create a script which will receive the filename and line number, and pass them to your IDE,
  • And you have to configure your browser to use that script as handler for crewide:// URLs.

Script passing the filename and line number to your IDE

The first step is to create a script which will receive the full crewide:// as a string, made of:

  • that pseudo-protocol,
  • three parameters, separated by @:
  • The project name, as known on the Crew side,
  • The file path (url-encoded),
  • And the line number.

Using these three informations, the script will have to call your IDE, passing it the filename and line number.
Note that your IDE might require the full-path to the file, starting from the root of your filesystem -- which means the script might have to determine it.

This script can be written in any programming language you like, as long as it is executable.

Here is an example of such a script, written in PHP, for PHPStorm:

#!/usr/bin/env php
<?php
define('PROJECTROOT', '/home/.../projects/myproject/');
define('PHPSTORM', '$HOME/bin/PhpStorm/bin/phpstorm.sh');

// We first remove the protocol, which is of no use to us
// (it was used by the browser, to know which script to launch)
$param = str_replace('crewide://', '', $_SERVER['argv'][1]);

// Extract the thee informations from the URL: projectname, file, and line number
list ($projectName, $file, $line) = explode('@', $param);

// Path to the file
//   - It is received url-encoded => we have to decode it
//   - Our IDE might require a full-path (PHPStorm does)
$file = PROJECTROOT . urldecode($file);

// Let's run our IDE:
// (the expected parameters and options depend on which IDE you are using)
$command = PHPSTORM . ' --line ' . intval($line) . ' ' . escapeshellarg($file);

// Just in case, some logging might help debug
//file_put_contents('/tmp/crew-log.txt', "$command\n", FILE_APPEND);

exec($command);

Save a script based on this one, somewhere like ~/bin/fx-crewide.php, and make sure it is executable:

chmod u+x ~/bin/fx-crewide.php

Configuring your browser to handle crewide:// URLs

The next step is to configure your browser: when a crewide:// link is clicked, the script you created in the first step has to be called.
The configuration depends on the browser you are working with.

Firefox

You first have to create a new configuration entry:

  • Open about:config.
  • Create a new boolean entry, called network.protocol-handler.expose.crewide, and set it to false.

Then, the next time you click on a crewide:// link, Firefox will ask you which application should be used to open it; select the script you created earlier (and you might want to check "remember", so Firefox doesn't ask anymore).

Now, everytime you click on a crewide:// link, Firefox will call your script -- which, in turn, will open your IDE.

Chrome