Skip to content

Commit 4b91da7

Browse files
authored
Merge pull request #214 from andreaswolf/add-line-number
[FEATURE] Add line number to file path
2 parents 45ac4b4 + ec741d0 commit 4b91da7

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

e2e/typo3-typoscript/expected-output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
1 file with changes
33
===================
44

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

77
---------- begin diff ----------
88
@@ @@

e2e/typo3-yaml/expected-output.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
1 file with changes
33
===================
44

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

77
---------- begin diff ----------
88
@@ @@

packages/fractor/src/ChangesReporting/Output/ConsoleOutputFormatter.php

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ private function reportFileDiffs(array $fileDiffs, bool $absoluteFilePath): void
6161
$i = 0;
6262
foreach ($fileDiffs as $fileDiff) {
6363
$filePath = $absoluteFilePath ? $fileDiff->getAbsoluteFilePath() ?? '' : $fileDiff->getRelativeFilePath();
64+
// append line number for faster file jump in diff
65+
$firstLineNumber = $fileDiff->getFirstLineNumber();
66+
if ($firstLineNumber !== null) {
67+
$filePath .= ':' . $firstLineNumber;
68+
}
6469
$message = \sprintf('<options=bold>%d) %s</>', ++$i, $filePath);
6570
$this->symfonyStyle->writeln($message);
6671
$this->symfonyStyle->newLine();

packages/fractor/src/Differ/ValueObject/FileDiff.php

+23
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,21 @@
44

55
namespace a9f\Fractor\Differ\ValueObject;
66

7+
use Nette\Utils\Strings;
8+
79
final readonly class FileDiff
810
{
11+
/**
12+
* @var string
13+
*/
14+
private const FIRST_LINE_KEY = 'first_line';
15+
16+
/**
17+
* @var string
18+
* @se https://regex101.com/r/AUPIX4/1
19+
*/
20+
private const FIRST_LINE_REGEX = '#@@(.*?)(?<' . self::FIRST_LINE_KEY . '>\\d+)(.*?)@@#';
21+
922
public function __construct(
1023
private string $relativeFilePath,
1124
private string $diff,
@@ -42,4 +55,14 @@ public function getAppliedRules(): array
4255
{
4356
return $this->appliedRules;
4457
}
58+
59+
public function getFirstLineNumber(): ?int
60+
{
61+
$match = Strings::match($this->diff, self::FIRST_LINE_REGEX);
62+
// probably some error in diff
63+
if (! isset($match[self::FIRST_LINE_KEY])) {
64+
return null;
65+
}
66+
return (int) $match[self::FIRST_LINE_KEY];
67+
}
4568
}

0 commit comments

Comments
 (0)