Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add line number to file path #214

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/typo3-typoscript/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1 file with changes
===================

1) typo3-typoscript/result/cache-hash.typoscript
1) typo3-typoscript/result/cache-hash.typoscript:3

---------- begin diff ----------
@@ @@
Expand Down
2 changes: 1 addition & 1 deletion e2e/typo3-yaml/expected-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
1 file with changes
===================

1) typo3-yaml/result/my_form.form.yaml
1) typo3-yaml/result/my_form.form.yaml:2

---------- begin diff ----------
@@ @@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void
$i = 0;
foreach ($fileDiffs as $fileDiff) {
$filePath = $absoluteFilePath ? $fileDiff->getAbsoluteFilePath() ?? '' : $fileDiff->getRelativeFilePath();
// append line number for faster file jump in diff
$firstLineNumber = $fileDiff->getFirstLineNumber();
if ($firstLineNumber !== null) {
$filePath .= ':' . $firstLineNumber;
}
$message = \sprintf('<options=bold>%d) %s</>', ++$i, $filePath);
$this->symfonyStyle->writeln($message);
$this->symfonyStyle->newLine();
Expand Down
23 changes: 23 additions & 0 deletions packages/fractor/src/Differ/ValueObject/FileDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@

namespace a9f\Fractor\Differ\ValueObject;

use Nette\Utils\Strings;

final readonly class FileDiff
{
/**
* @var string
*/
private const FIRST_LINE_KEY = 'first_line';

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

public function __construct(
private string $relativeFilePath,
private string $diff,
Expand Down Expand Up @@ -42,4 +55,14 @@ public function getAppliedRules(): array
{
return $this->appliedRules;
}

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];
}
}