Skip to content

Commit

Permalink
Add support editor to use when clicking file name. (#1258)
Browse files Browse the repository at this point in the history
Co-authored-by: Viktor Mazur <vmazu@softserveinc.com>
  • Loading branch information
VictoRD11 and Viktor Mazur committed Nov 18, 2021
1 parent 9853edf commit 1ede251
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
41 changes: 41 additions & 0 deletions config/debugbar.php
Expand Up @@ -42,6 +42,47 @@
'port' => 2304, // Port to use with the "socket" driver
],

/*
|--------------------------------------------------------------------------
| Editor
|--------------------------------------------------------------------------
|
| Choose your preferred editor to use when clicking file name.
|
| Supported: "phpstorm", "vscode", "vscode-insiders", "vscodium", "textmate", "emacs",
| "sublime", "atom", "nova", "macvim", "idea", "netbeans",
| "xdebug", "espresso"
|
*/

'editor' => env('DEBUGBAR_EDITOR', 'phpstorm'),

/*
|--------------------------------------------------------------------------
| Remote Path Mapping
|--------------------------------------------------------------------------
|
| If you are using a remote dev server, like Laravel Homestead, Docker, or
| even a remote VPS, it will be necessary to specify your path mapping.
|
| Leaving one, or both of these, empty or null will not trigger the remote
| URL changes and Debugbar will treat your editor links as local files.
|
| "remote_sites_path" is an absolute base path for your sites or projects
| in Homestead, Vagrant, Docker, or another remote development server.
|
| Example value: "/home/vagrant/Code"
|
| "local_sites_path" is an absolute base path for your sites or projects
| on your local computer where your IDE or code editor is running on.
|
| Example values: "/Users/<name>/Code", "C:\Users\<name>\Documents\Code"
|
*/

'remote_sites_path' => env('DEBUGBAR_REMOTE_SITES_PATH', ''),
'local_sites_path' => env('DEBUGBAR_LOCAL_SITES_PATH', ''),

/*
|--------------------------------------------------------------------------
| Vendors
Expand Down
74 changes: 72 additions & 2 deletions src/DataCollector/RouteCollector.php
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Config;
use InvalidArgumentException;

/**
* Based on Illuminate\Foundation\Console\RoutesCommand for Taylor Otwell
Expand All @@ -24,6 +25,28 @@ class RouteCollector extends DataCollector implements Renderable
*/
protected $router;

/**
* A list of known editor strings.
*
* @var array
*/
protected $editors = [
'sublime' => 'subl://open?url=file://%file&line=%line',
'textmate' => 'txmt://open?url=file://%file&line=%line',
'emacs' => 'emacs://open?url=file://%file&line=%line',
'macvim' => 'mvim://open/?url=file://%file&line=%line',
'phpstorm' => 'phpstorm://open?file=%file&line=%line',
'idea' => 'idea://open?file=%file&line=%line',
'vscode' => 'vscode://file/%file:%line',
'vscode-insiders' => 'vscode-insiders://file/%file:%line',
'vscodium' => 'vscodium://file/%file:%line',
'nova' => 'nova://core/open/file?filename=%file&line=%line',
'xdebug' => 'xdebug://%file@%line',
'atom' => 'atom://core/open/file?filename=%file&line=%line',
'espresso' => 'x-espresso://open?filepath=%file&lines=%line',
'netbeans' => 'netbeans://open/?f=%file:%line',
];

public function __construct(Router $router)
{
$this->router = $router;
Expand Down Expand Up @@ -76,7 +99,12 @@ protected function getRouteInformation($route)

if (isset($reflector)) {
$filename = ltrim(str_replace(base_path(), '', $reflector->getFileName()), '/');
$result['file'] = $filename . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine();

if ($href = $this->getEditorHref($reflector->getFileName(), $reflector->getStartLine())) {
$result['file'] = sprintf('<a href="%s">%s:%s-%s</a>', $href, $filename, $reflector->getStartLine(), $reflector->getEndLine());
} else {
$result['file'] = sprintf('%s:%s-%s', $filename, $reflector->getStartLine(), $reflector->getEndLine());
}
}

if ($middleware = $this->getMiddleware($route)) {
Expand Down Expand Up @@ -117,7 +145,7 @@ public function getWidgets()
$widgets = [
"route" => [
"icon" => "share",
"widget" => "PhpDebugBar.Widgets.VariableListWidget",
"widget" => "PhpDebugBar.Widgets.HtmlVariableListWidget",
"map" => "route",
"default" => "{}"
]
Expand Down Expand Up @@ -145,4 +173,46 @@ protected function displayRoutes(array $routes)

$this->table->render($this->getOutput());
}

/**
* Get the editor href for a given file and line, if available.
*
* @param string $filePath
* @param int $line
*
* @throws InvalidArgumentException If editor resolver does not return a string
*
* @return null|string
*/
protected function getEditorHref($filePath, $line)
{
if (empty(config('debugbar.editor'))) {
return null;
}

if (empty($this->editors[config('debugbar.editor')])) {
throw new InvalidArgumentException(
'Unknown editor identifier: ' . config('debugbar.editor') . '. Known editors:' .
implode(', ', array_keys($this->editors))
);
}

$filePath = $this->replaceSitesPath($filePath);

$url = str_replace(['%file', '%line'], [$filePath, $line], $this->editors[config('debugbar.editor')]);

return $url;
}

/**
* Replace remote path
*
* @param string $filePath
*
* @return string
*/
protected function replaceSitesPath($filePath)
{
return str_replace(config('debugbar.remote_sites_path'), config('debugbar.local_sites_path'), $filePath);
}
}

0 comments on commit 1ede251

Please sign in to comment.