Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* AMP Validation Error Source File IntelliJ IDEA Editor.
*
* @package AMP_Validation_Error_Source_File_Idea_Editor
* @author Weston Ruter, Google
* @link https://gist.github.com/westonruter/1a36a78a79fb1abbdfd1bb5b38a9f3a1
* @license GPL-2.0-or-later
* @copyright 2019 Google Inc.
*
* @wordpress-plugin
* Plugin Name: AMP Validation Error Source File IntelliJ IDEA Editor
* Plugin URI: https://gist.github.com/westonruter/1a36a78a79fb1abbdfd1bb5b38a9f3a1
* Description: Open the source files for AMP validation errors in the IntelliJ IDEA editor. Also supports mapping the file path from the Docker container to the host machine when using Lando. Requires <a href="https://github.com/ampproject/amp-wp">AMP plugin</a> v1.4-beta.
* Version: 0.1
* Author: Weston Ruter, Google
* Author URI: https://weston.ruter.net/
* License: GNU General Public License v2 (or later)
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
namespace AMP_Validation_Error_Source_File_Idea_Editor;
/*
* If wanting to use a non-idea protocol (e.g. phpstorm), simply add another
* `amp_validation_error_source_file_editor_url_template` filter to replace the protocol. For example:
*
* add_filter( 'amp_validation_error_source_file_editor_url_template', function( $template_url ) {
* if ( 'idea:' === substr( $template_url, 0, 5 ) ) {
* $template_url = 'phpstorm:' . substr( $template_url, 5 );
* }
* return $template_url;
* } );
*/
add_filter( 'amp_validation_error_source_file_editor_url_template', __NAMESPACE__ . '\get_idea_editor_url_template', 9 );
/**
* Get IntelliJ file editor template.
*
* @return string URL template.
*/
function get_idea_editor_url_template() {
return 'idea://open?file={{file}}&line={{line}}';
}
/**
* Map the path from Lando container to the host machine.
*
* @param string $source_file_path Source file path.
* @return string Mapped source file path.
*/
function filter_source_file_path( $source_file_path ) {
if ( isset( $_ENV['LANDO_WEBROOT'], $_ENV['LANDO_APP_ROOT'] ) && 0 === strpos( $source_file_path, trailingslashit( $_ENV['LANDO_WEBROOT'] ) ) ) {
$app_webroot_relative_path = '';
if ( preg_match( ':^/app(/.+):', $_ENV['LANDO_WEBROOT'], $matches ) ) {
$app_webroot_relative_path = $matches[1];
}
$source_file_path = $_ENV['LANDO_APP_ROOT'] . $app_webroot_relative_path . substr( $source_file_path, strlen( $_ENV['LANDO_WEBROOT'] ) );
}
return $source_file_path;
}
add_filter( 'amp_validation_error_source_file_path', __NAMESPACE__ . '\filter_source_file_path' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.