Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/diff/query/PhabricatorDiffInlineCommentQuery.php
Show All 12 Lines | abstract class PhabricatorDiffInlineCommentQuery | ||||
abstract protected function buildInlineCommentWhereClauseParts( | abstract protected function buildInlineCommentWhereClauseParts( | ||||
AphrontDatabaseConnection $conn); | AphrontDatabaseConnection $conn); | ||||
abstract public function withObjectPHIDs(array $phids); | abstract public function withObjectPHIDs(array $phids); | ||||
abstract protected function loadHiddenCommentIDs( | abstract protected function loadHiddenCommentIDs( | ||||
$viewer_phid, | $viewer_phid, | ||||
array $comments); | array $comments); | ||||
abstract protected function newInlineContextMap(array $inlines); | |||||
final public function withFixedStates(array $states) { | final public function withFixedStates(array $states) { | ||||
$this->fixedStates = $states; | $this->fixedStates = $states; | ||||
return $this; | return $this; | ||||
} | } | ||||
final public function needReplyToComments($need_reply_to) { | final public function needReplyToComments($need_reply_to) { | ||||
$this->needReplyToComments = $need_reply_to; | $this->needReplyToComments = $need_reply_to; | ||||
return $this; | return $this; | ||||
▲ Show 20 Lines • Show All 231 Lines • ▼ Show 20 Lines | if ($this->needInlineContext) { | ||||
if ($object->getDocumentEngineKey() !== null) { | if ($object->getDocumentEngineKey() !== null) { | ||||
$inline->attachInlineContext(null); | $inline->attachInlineContext(null); | ||||
continue; | continue; | ||||
} | } | ||||
$need_context[] = $inline; | $need_context[] = $inline; | ||||
} | } | ||||
foreach ($need_context as $inline) { | if ($need_context) { | ||||
$changeset = id(new DifferentialChangesetQuery()) | $context_map = $this->newInlineContextMap($need_context); | ||||
->setViewer($viewer) | |||||
->withIDs(array($inline->getChangesetID())) | |||||
->needHunks(true) | |||||
->executeOne(); | |||||
if (!$changeset) { | |||||
$inline->attachInlineContext(null); | |||||
continue; | |||||
} | |||||
$hunks = $changeset->getHunks(); | |||||
$is_simple = | |||||
(count($hunks) === 1) && | |||||
((int)head($hunks)->getOldOffset() <= 1) && | |||||
((int)head($hunks)->getNewOffset() <= 1); | |||||
if (!$is_simple) { | |||||
$inline->attachInlineContext(null); | |||||
continue; | |||||
} | |||||
if ($inline->getIsNewFile()) { | |||||
$vector = $changeset->getNewStatePathVector(); | |||||
$filename = last($vector); | |||||
$corpus = $changeset->makeNewFile(); | |||||
} else { | |||||
$vector = $changeset->getOldStatePathVector(); | |||||
$filename = last($vector); | |||||
$corpus = $changeset->makeOldFile(); | |||||
} | |||||
$corpus = phutil_split_lines($corpus); | |||||
// Adjust the line number into a 0-based offset. | |||||
$offset = $inline->getLineNumber(); | |||||
$offset = $offset - 1; | |||||
// Adjust the inclusive range length into a row count. | |||||
$length = $inline->getLineLength(); | |||||
$length = $length + 1; | |||||
$head_min = max(0, $offset - 3); | |||||
$head_max = $offset; | |||||
$head_len = $head_max - $head_min; | |||||
if ($head_len) { | foreach ($need_context as $key => $inline) { | ||||
$head = array_slice($corpus, $head_min, $head_len, true); | $inline->attachInlineContext(idx($context_map, $key)); | ||||
$head = $this->simplifyContext($head, true); | |||||
} else { | |||||
$head = array(); | |||||
} | } | ||||
$body = array_slice($corpus, $offset, $length, true); | |||||
$tail = array_slice($corpus, $offset + $length, 3, true); | |||||
$tail = $this->simplifyContext($tail, false); | |||||
$context = id(new PhabricatorDiffInlineCommentContext()) | |||||
->setFilename($filename) | |||||
->setHeadLines($head) | |||||
->setBodyLines($body) | |||||
->setTailLines($tail); | |||||
$inline->attachInlineContext($context); | |||||
} | } | ||||
} | } | ||||
return $inlines; | return $inlines; | ||||
} | } | ||||
private function simplifyContext(array $lines, $is_head) { | final protected function simplifyContext(array $lines, $is_head) { | ||||
// We want to provide the smallest amount of context we can while still | // We want to provide the smallest amount of context we can while still | ||||
// being useful, since the actual code is visible nearby and showing a | // being useful, since the actual code is visible nearby and showing a | ||||
// ton of context is silly. | // ton of context is silly. | ||||
// Examine each line until we find one that looks "useful" (not just | // Examine each line until we find one that looks "useful" (not just | ||||
// whitespace or a single bracket). Once we find a useful piece of context | // whitespace or a single bracket). Once we find a useful piece of context | ||||
// to anchor the text, discard the rest of the lines beyond it. | // to anchor the text, discard the rest of the lines beyond it. | ||||
Show All 21 Lines |