Skip to content

Commit

Permalink
[DX] add link to first line of diff (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 26, 2021
1 parent f39c01e commit 25ac38e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/ChangesReporting/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ private function reportFileDiffs(array $fileDiffs): void
$i = 0;
foreach ($fileDiffs as $fileDiff) {
$relativeFilePath = $fileDiff->getRelativeFilePath();

// append line number for faster file jump in diff
$firstLineNumber = $fileDiff->getFirstLineNumber();
if ($firstLineNumber !== null) {
$relativeFilePath .= ':' . $firstLineNumber;
}

$message = sprintf('<options=bold>%d) %s</>', ++$i, $relativeFilePath);

$this->outputStyle->writeln($message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function isScalar(UnionType $unionType): bool
}

foreach ($types as $type) {
if ($type instanceof StringType && !$type instanceof ConstantStringType) {
if ($type instanceof StringType && ! $type instanceof ConstantStringType) {
continue;
}
if ($type instanceof FloatType) {
Expand Down
6 changes: 4 additions & 2 deletions rules/DowngradePhp72/NodeAnalyzer/BuiltInMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

final class BuiltInMethodAnalyzer
{
public function __construct(private NodeNameResolver $nodeNameResolver, private ClassChildAnalyzer $classChildAnalyzer)
{
public function __construct(
private NodeNameResolver $nodeNameResolver,
private ClassChildAnalyzer $classChildAnalyzer
) {
}

public function isImplementsBuiltInInterface(ClassReflection $classReflection, ClassMethod $classMethod): bool
Expand Down
24 changes: 24 additions & 0 deletions src/ValueObject/Reporting/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@

namespace Rector\Core\ValueObject\Reporting;

use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\SmartFileSystem\SmartFileInfo;

final class FileDiff
{
/**
* @var string
* @se https://regex101.com/r/AUPIX4/1
*/
private const FIRST_LINE_REGEX = '#@@(.*?)(?<' . self::FIRST_LINE_KEY . '>\d+)(.*?)@@#';

/**
* @var string
*/
private const FIRST_LINE_KEY = 'first_line';

/**
* @param RectorWithLineChange[] $rectorWithLineChanges
*/
Expand Down Expand Up @@ -62,6 +74,18 @@ public function getRectorClasses(): array
return $this->sortClasses($rectorClasses);
}

public function getFirstLineNumber(): ?int
{
$match = Strings::match($this->diff, self::FIRST_LINE_REGEX);

// probably some error in diff
if (! isset($match[self::FIRST_LINE_KEY])) {
return null;
}

return (int) $match[self::FIRST_LINE_KEY] - 1;
}

/**
* @template TType as object
* @param array<class-string<TType>> $rectorClasses
Expand Down

0 comments on commit 25ac38e

Please sign in to comment.