Debian Codegraph @

vendor/jfcherng/php-diff/src/Renderer/Html/LineRenderer/Word.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jfcherng\Diff\Renderer\Html\LineRenderer;
6
7 use Jfcherng\Diff\Renderer\RendererConstant;
8 use Jfcherng\Diff\SequenceMatcher;
9 use Jfcherng\Diff\Utility\ReverseIterator;
10 use Jfcherng\Diff\Utility\Str;
11 use Jfcherng\Utility\MbString;
12
13 final class Word extends AbstractLineRenderer
14 {
15 /**
16 * @return static
17 */
18 public function render(MbString $mbOld, MbString $mbNew): LineRendererInterface
19 {
20 static $splitRegex = '/([' . RendererConstant::PUNCTUATIONS_RANGE . '])/uS';
21 static $dummyHtmlClosure = RendererConstant::HTML_CLOSURES[0] . RendererConstant::HTML_CLOSURES[1];
22
23 $pregFlag = \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY;
24 $oldWords = $mbOld->toArraySplit($splitRegex, -1, $pregFlag);
25 $newWords = $mbNew->toArraySplit($splitRegex, -1, $pregFlag);
26
27 $hunk = $this->getChangedExtentSegments($oldWords, $newWords);
28
29 // reversely iterate hunk
30 foreach (ReverseIterator::fromArray($hunk) as [$op, $i1, $i2, $j1, $j2]) {
31 if ($op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_DEL)) {
32 $oldWords[$i1] = RendererConstant::HTML_CLOSURES[0] . $oldWords[$i1];
33 $oldWords[$i2 - 1] .= RendererConstant::HTML_CLOSURES[1];
34
35 // insert dummy HTML closure to ensure there are always
36 // the same amounts of HTML closures in $oldWords and $newWords
37 // thus, this should make that "wordGlues" work correctly
38 // @see https://github.com/jfcherng/php-diff/pull/25
39 if ($op === SequenceMatcher::OP_DEL) {
40 array_splice($newWords, $j1, 0, [$dummyHtmlClosure]);
41 }
42 }
43
44 if ($op & (SequenceMatcher::OP_REP | SequenceMatcher::OP_INS)) {
45 $newWords[$j1] = RendererConstant::HTML_CLOSURES[0] . $newWords[$j1];
46 $newWords[$j2 - 1] .= RendererConstant::HTML_CLOSURES[1];
47
48 if ($op === SequenceMatcher::OP_INS) {
49 array_splice($oldWords, $i1, 0, [$dummyHtmlClosure]);
50 }
51 }
52 }
53
54 if (!empty($hunk) && !empty($this->rendererOptions['wordGlues'])) {
55 $regexGlues = array_map(
56 static fn (string $glue): string => preg_quote($glue, '/'),
57 $this->rendererOptions['wordGlues'],
58 );
59
60 $gluePattern = '/^(?:' . implode('|', $regexGlues) . ')+$/uS';
61
62 $this->glueWordsResult($oldWords, $gluePattern);
63 $this->glueWordsResult($newWords, $gluePattern);
64 }
65
66 $mbOld->set(implode('', $oldWords));
67 $mbNew->set(implode('', $newWords));
68
69 return $this;
70 }
71
72 /**
73 * Beautify diff result by glueing words.
74 *
75 * What this function does is basically making
76 * ["<diff_begin>good<diff_end>", "-", "<diff_begin>looking<diff_end>"]
77 * into
78 * ["<diff_begin>good", "-", "looking<diff_end>"].
79 *
80 * @param array $words the words
81 * @param string $gluePattern the regex to determine a string is purely glue or not
82 */
83 protected function glueWordsResult(array &$words, string $gluePattern): void
84 {
85 /** @var int index of the word which has the trailing closure */
86 $endClosureIdx = -1;
87
88 foreach ($words as $idx => &$word) {
89 if ($word === '') {
90 continue;
91 }
92
93 if ($endClosureIdx < 0) {
94 if (Str::endsWith($word, RendererConstant::HTML_CLOSURES[1])) {
95 $endClosureIdx = $idx;
96 }
97 } elseif (Str::startsWith($word, RendererConstant::HTML_CLOSURES[0])) {
98 $words[$endClosureIdx] = substr($words[$endClosureIdx], 0, -\strlen(RendererConstant::HTML_CLOSURES[1]));
99 $word = substr($word, \strlen(RendererConstant::HTML_CLOSURES[0]));
100 $endClosureIdx = $idx;
101 } elseif (!preg_match($gluePattern, $word)) {
102 $endClosureIdx = -1;
103 }
104 }
105 }
106 }