3 final class PhabricatorSourceCodeView
extends AphrontView
{
7 private $highlights = array();
8 private $canClickHighlight = true;
9 private $truncatedFirstBytes = false;
10 private $truncatedFirstLines = false;
11 private $symbolMetadata;
13 private $coverage = array();
15 public function setLines(array $lines) {
16 $this->lines
= $lines;
20 public function setURI(PhutilURI
$uri) {
25 public function setHighlights(array $array) {
26 $this->highlights
= array_fuse($array);
30 public function disableHighlightOnClick() {
31 $this->canClickHighlight
= false;
35 public function setTruncatedFirstBytes($truncated_first_bytes) {
36 $this->truncatedFirstBytes
= $truncated_first_bytes;
40 public function setTruncatedFirstLines($truncated_first_lines) {
41 $this->truncatedFirstLines
= $truncated_first_lines;
45 public function setSymbolMetadata(array $symbol_metadata) {
46 $this->symbolMetadata
= $symbol_metadata;
50 public function getSymbolMetadata() {
51 return $this->symbolMetadata
;
54 public function setBlameMap(array $map) {
55 $this->blameMap
= $map;
59 public function getBlameMap() {
60 return $this->blameMap
;
63 public function setCoverage(array $coverage) {
64 $this->coverage
= $coverage;
68 public function getCoverage() {
69 return $this->coverage
;
72 public function render() {
73 $blame_map = $this->getBlameMap();
74 $has_blame = ($blame_map !== null);
76 require_celerity_resource('phabricator-source-code-view-css');
77 require_celerity_resource('syntax-highlighting-css');
79 if ($this->canClickHighlight
) {
80 Javelin
::initBehavior('phabricator-line-linker');
87 $lines = $this->lines
;
88 if ($this->truncatedFirstLines
) {
89 $lines[] = phutil_tag(
95 } else if ($this->truncatedFirstBytes
) {
96 $last_key = last_key($lines);
97 $lines[$last_key] = hsprintf(
108 $base_uri = (string)$this->uri
;
109 $wrote_anchor = false;
111 $coverage = $this->getCoverage();
112 $coverage_count = count($coverage);
113 $coverage_data = ipull($coverage, 'data');
115 // TODO: Modularize this properly, see T13125.
116 $coverage_map = array(
117 'C' => 'background: #66bbff;',
118 'U' => 'background: #dd8866;',
119 'N' => 'background: #ddeeff;',
120 'X' => 'background: #aa00aa;',
123 foreach ($lines as $line) {
124 $row_attributes = array();
125 if (isset($this->highlights
[$line_number])) {
126 $row_attributes['class'] = 'phabricator-source-highlight';
127 if (!$wrote_anchor) {
128 $row_attributes['id'] = 'phabricator-line-linker-anchor';
129 $wrote_anchor = true;
133 if ($this->canClickHighlight
) {
135 $line_href = $base_uri.'$'.$line_number;
140 $tag_number = phutil_tag(
143 'href' => $line_href,
144 'data-n' => $line_number,
147 $tag_number = phutil_tag(
154 $lines = idx($blame_map, $line_number);
157 $skip_blame = 'skip';
158 $info_blame = 'info';
164 $blame_cells = array(
168 'class' => 'phabricator-source-blame-skip',
169 'data-blame' => $skip_blame,
174 'class' => 'phabricator-source-blame-info',
175 'data-blame' => $info_blame,
176 'data-blame-lines' => $lines,
183 $coverage_cells = array();
184 foreach ($coverage as $coverage_idx => $coverage_spec) {
185 if (isset($coverage_spec['data'][$line_number - 1])) {
186 $coverage_char = $coverage_spec['data'][$line_number - 1];
188 $coverage_char = null;
191 $coverage_style = idx($coverage_map, $coverage_char, null);
193 $coverage_cells[] = phutil_tag(
196 'class' => 'phabricator-source-coverage',
197 'style' => $coverage_style,
198 'data-coverage' => $coverage_idx.'/'.$coverage_char,
202 $rows[] = phutil_tag(
210 'class' => 'phabricator-source-line',
216 'class' => 'phabricator-source-code',
226 $classes[] = 'phabricator-source-code-view';
227 $classes[] = 'remarkup-code';
228 $classes[] = 'PhabricatorMonospaced';
230 $symbol_metadata = $this->getSymbolMetadata();
233 $sigils[] = 'phabricator-source';
234 $sigils[] = 'has-symbols';
236 Javelin
::initBehavior('repository-crossreference');
238 return phutil_tag_div(
239 'phabricator-source-code-container',
243 'class' => implode(' ', $classes),
244 'sigil' => implode(' ', $sigils),
246 'uri' => (string)$this->uri
,
247 'symbols' => $symbol_metadata,
250 phutil_implode_html('', $rows)));