1use super::lines;
6use super::{Block, Document, Note, Options, Row};
7use crate::protocol::display::{Line, Role};
8use crate::protocol::vocabulary::{Ago, Files, IntentLine, Mark, Plural};
9use crate::{FileChange, ImportSite, Match, Notice, Occurrence, Respelling};
10
11#[derive(Debug, Clone, Default)]
13pub struct Presentation {
14 pub body: Vec<Row>,
16 pub notes: Vec<Row>,
19}
20
21pub trait View {
27 fn rows(&self, block: &Block, options: Options, width: usize) -> Vec<Row>;
31
32 fn present(&self, report: &Document, options: Options, width: usize) -> Presentation {
34 let (body, notes) = report.parts();
35 Presentation {
36 body: body
37 .iter()
38 .flat_map(|block| self.rows(block, options, width))
39 .collect(),
40 notes: notes
41 .iter()
42 .flat_map(|block| self.rows(block, options, width))
43 .collect(),
44 }
45 }
46
47 fn occurrence(
51 &self,
52 occurrence: &Occurrence,
53 ordinal: usize,
54 ticked: bool,
55 width: usize,
56 ) -> Row;
57
58 fn respelling(&self, respelling: &Respelling, width: usize) -> Row;
60
61 fn notice(&self, notice: &Notice, width: usize) -> Row;
63
64 fn rewrite(
67 &self,
68 m: &Match,
69 after: Option<&str>,
70 ordinal: usize,
71 ticked: bool,
72 width: usize,
73 ) -> Row;
74}
75
76#[derive(Debug, Default)]
78pub struct Detailed;
79
80impl View for Detailed {
81 fn rows(&self, block: &Block, options: Options, _width: usize) -> Vec<Row> {
82 match block {
83 Block::Title(text) => vec![
84 Row::new(Line::of(Role::Title, text.clone())),
85 Row::new(Line::new()),
86 ],
87 Block::Heading(text) => vec![Row::new(Line::of(Role::Strong, text.clone()))],
88 Block::Matches(matches) => lines::Sections::new(matches).lines(),
89 Block::Verdicts { occurrences, files } => lines::Verdicts::new(occurrences)
90 .expanded(options.verbose)
91 .planned(files.as_deref())
92 .lines(),
93 Block::Outline(items) => Row::wrap(
94 lines::OutlineTree::new(items)
95 .with_reach(options.verbose)
96 .lines(),
97 ),
98 Block::Sites(sites) => Row::wrap(
99 sites
100 .iter()
101 .flat_map(|site| lines::SiteLine::new(site).lines())
102 .collect(),
103 ),
104 Block::Dead(items) => Row::wrap(
105 items
106 .iter()
107 .map(|item| {
108 let mut line = lines::PlacedLine::new(&item.declaration).line();
109 if item.unsure > 0 {
110 line = line
111 .and(Role::Plain, " ")
112 .and_line(Line::mark(Mark::Unverified))
113 .and(Role::Plain, " ")
114 .and(Role::Dim, item.unsure.to_string());
115 }
116 line
117 })
118 .collect(),
119 ),
120 Block::Exposed(items) => items
121 .iter()
122 .flat_map(|item| {
123 let mut rows = vec![Row::new(
124 lines::PlacedLine::new(&item.declaration)
125 .line()
126 .and(Role::Plain, " ")
127 .and_line(Line::mark(Mark::ImportedBy))
128 .and(Role::Plain, " ")
129 .and(Role::Strong, item.importers.to_string()),
130 )];
131 rows.extend(item.via.iter().map(|alias| {
132 Row::new(
133 Line::of(Role::Plain, " ")
134 .and_line(Line::mark(Mark::ReExport))
135 .and(Role::Plain, " ")
136 .and(Role::Address, alias.to_string()),
137 )
138 }));
139 rows
140 })
141 .collect(),
142 Block::Consumers(consumers) => {
143 let mut rows: Vec<Row> = Vec::new();
144 let mut depth = 0;
145 for consumer in consumers {
146 if consumer.depth != depth {
147 depth = consumer.depth;
148 let at_depth = consumers.iter().filter(|c| c.depth == depth).count();
149 rows.push(Row::new(Line::new()));
150 rows.push(Row::new(
151 Line::mark(Mark::ImportedBy)
152 .and(Role::Plain, " ")
153 .and(Role::Strong, at_depth.to_string())
154 .and(Role::Plain, " ")
155 .and(Role::Dim, format!("depth {depth}")),
156 ));
157 }
158 let mut line = Line::of(Role::Plain, " ")
159 .and(Role::Address, format!("◆ {}", consumer.module))
160 .and(Role::Plain, " ")
161 .and(Role::Path, consumer.path.display().to_string());
162 if consumer.depth > 1 {
163 line = line.and(Role::Dim, format!(" via {}", consumer.through));
164 }
165 rows.push(Row::new(line));
166 }
167 rows
168 }
169 Block::History(entries) => {
170 let now = Ago::now();
171 let last = entries.len().saturating_sub(1);
172 Row::wrap(
173 entries
174 .iter()
175 .enumerate()
176 .map(|(i, item)| {
177 lines::HistoryLine::new(item, now).newest(i == last).line()
178 })
179 .collect(),
180 )
181 }
182 Block::Batch(intents) => Row::wrap(
183 intents
184 .iter()
185 .enumerate()
186 .map(|(i, intent)| {
187 Line::of(Role::Plain, " ")
188 .and(Role::Ordinal, (i + 1).to_string())
189 .and(Role::Plain, " ")
190 .and(Role::Plain, IntentLine(intent).to_string())
191 })
192 .collect(),
193 ),
194 Block::Undo { moves, restored } => {
195 let mut rows = Row::wrap(
196 moves
197 .iter()
198 .map(|(from, to)| {
199 Line::of(Role::Plain, " ")
200 .and(Role::Dim, to.display().to_string())
201 .and(Role::Plain, " ")
202 .and_line(Line::mark(Mark::Import))
203 .and(Role::Plain, " ")
204 .and(Role::Path, from.display().to_string())
205 })
206 .collect(),
207 );
208 rows.extend(restored.iter().map(|path| {
209 Row::new(
210 Line::of(Role::Plain, " ").and(Role::Path, path.display().to_string()),
211 )
212 }));
213 rows
214 }
215 Block::DepGroups { imports, own } => {
216 Row::wrap(lines::DepGroups::new(imports, own.as_deref()).lines())
217 }
218 Block::Importers(importers) => Row::wrap(lines::ImporterRows::new(importers).lines()),
219 Block::Explanation(result) => {
220 let mut rows = vec![Row::new(Line::of(
221 Role::Strong,
222 format!("{}:{}", result.path.display(), result.position.display()),
223 ))];
224 if let Some(dep) = &result.import {
226 let mut line = Line::mark(Mark::Import)
227 .and(Role::Plain, " ")
228 .and(Role::Strong, dep.import.path.to_string());
229 if let Some(address) = &dep.address {
230 line = line
231 .and(Role::Plain, " ")
232 .and(Role::Address, format!("◆ {address}"));
233 }
234 rows.push(Row::new(line));
235 let file = dep
236 .file
237 .as_ref()
238 .map(|f| Line::of(Role::Path, f.display().to_string()));
239 match (&dep.origin, file) {
240 (Some(origin), file) => {
241 let mut line = Line::of(Role::Plain, " ")
242 .and_line(Line::mark(Mark::ReExport))
243 .and(Role::Plain, " ")
244 .and(Role::Address, origin.to_string())
245 .and(Role::Plain, " ");
246 if let Some(file) = file {
247 line = line.and_line(file);
248 }
249 rows.push(Row::new(line));
250 }
251 (None, Some(file)) => {
252 rows.push(Row::new(Line::of(Role::Plain, " ").and_line(file)));
253 }
254 (None, None) => {}
255 }
256 }
257 let Some(symbol) = &result.symbol else {
258 if result.import.is_none() {
259 rows.push(Row::new(
260 Line::mark(Mark::Nothing)
261 .and(Role::Plain, " ")
262 .and(Role::Dim, "not inside a declaration"),
263 ));
264 }
265 if let Some(module) = &result.module {
266 rows.push(Row::new(
267 Line::mark(Mark::Address)
268 .and(Role::Plain, " ")
269 .and(Role::Address, module.to_string()),
270 ));
271 }
272 return rows;
273 };
274 if let (Some(declared), Some(line)) = (&result.declared, &result.line) {
275 rows.extend(
276 lines::Caret::new(
277 declared.line,
278 line,
279 declared.column as usize,
280 symbol.name.chars().count(),
281 )
282 .lines()
283 .into_iter()
284 .map(Row::new),
285 );
286 rows.push(Row::new(Line::new()));
287 }
288 let mut line = Line::mark(Mark::Declaration).and(Role::Plain, " ").and(
289 Role::Declaration,
290 format!("{} {}", symbol.kind, symbol.name),
291 );
292 if let Some(modifier) = symbol.modifier() {
293 line = line.and(Role::Plain, " ").and(Role::Symbol, modifier);
294 }
295 if let Some(address) = result.address.as_ref().or(result.module.as_ref()) {
296 line = line
297 .and(Role::Plain, " ")
298 .and(Role::Address, format!("◆ {address}"));
299 }
300 rows.push(Row::new(line));
301 if let Some(reach) = &result.reach {
302 rows.push(Row::new(
303 Line::of(Role::Plain, " ")
304 .and(Role::Dim, "reaches")
305 .and(Role::Plain, " ")
306 .and(Role::Plain, reach.to_string()),
307 ));
308 }
309 for alias in &result.via {
310 rows.push(Row::new(
311 Line::of(Role::Plain, " ")
312 .and_line(Line::mark(Mark::ReExport))
313 .and(Role::Plain, " ")
314 .and(Role::Address, alias.to_string()),
315 ));
316 }
317 rows.push(Row::new(
318 Line::mark(Mark::ImportedBy)
319 .and(Role::Plain, " ")
320 .and(Role::Strong, result.importers.len().to_string()),
321 ));
322 rows.extend(result.importers.iter().map(|path| {
323 Row::new(
324 Line::of(Role::Plain, " ").and(Role::Path, path.display().to_string()),
325 )
326 }));
327 rows
328 }
329 Block::Imports {
330 unresolved,
331 unused,
332 redundant,
333 } => {
334 let sections: [(Mark, &str, &[ImportSite]); 3] = [
335 (Mark::Nothing, "unresolved", unresolved),
336 (Mark::ByHand, "unused", unused),
337 (Mark::ByHand, "redundant", redundant),
338 ];
339 let mut rows = Vec::new();
340 let mut first = true;
341 for (mark, word, sites) in sections {
342 if sites.is_empty() {
343 continue;
344 }
345 if !first {
346 rows.push(Row::new(Line::new()));
347 }
348 first = false;
349 rows.push(Row::new(
350 Line::mark(mark)
351 .and(Role::Plain, " ")
352 .and(Role::Strong, word)
353 .and(Role::Plain, " ")
354 .and(Role::Dim, sites.len().to_string()),
355 ));
356 rows.extend(
357 sites
358 .iter()
359 .map(|site| Row::new(lines::ImportSiteLine::new(site).line())),
360 );
361 }
362 rows
363 }
364 Block::Line(line) | Block::Summary(line) => vec![Row::new(line.clone())],
365 Block::Note(Note::Hint(text)) => vec![Row::new(
366 Line::of(Role::Hint, "hint: ").and(Role::Plain, text.clone()),
367 )],
368 Block::Note(Note::Warning(line)) => vec![Row::new(
369 Line::of(Role::Warning, "warning: ").and_line(line.clone()),
370 )],
371 Block::Changes(files) => {
372 let mut rows = Vec::new();
373 for (i, file) in files.iter().enumerate() {
374 if i > 0 {
375 rows.push(Row::new(Line::new()));
376 }
377 rows.extend(
378 lines::Diff::new(file, false)
379 .lines()
380 .into_iter()
381 .map(Row::new),
382 );
383 }
384 rows
385 }
386 Block::Moved {
387 files,
388 respellings,
389 notices,
390 } => {
391 let structural: Vec<&FileChange> = files
392 .iter()
393 .filter(|f| lines::Diff::is_structural(f, respellings))
394 .collect();
395 let mut rows: Vec<Row> = Vec::new();
396 if !respellings.is_empty() {
397 rows.push(Row::new(
398 Line::mark(Mark::Import)
399 .and(Role::Plain, " ")
400 .and(Role::Strong, respellings.len().to_string())
401 .and(Role::Plain, " ")
402 .and(
403 Role::Dim,
404 Files::among(respellings.iter().map(|r| r.path.as_path()))
405 .to_string(),
406 ),
407 ));
408 }
409 if !structural.is_empty() {
410 rows.push(Row::new(
411 Line::mark(Mark::Structure)
412 .and(Role::Plain, " ")
413 .and(Role::Strong, structural.len().to_string())
414 .and(Role::Plain, " ")
415 .and(Role::Dim, Plural(structural.len(), "file").to_string()),
416 ));
417 }
418 if !notices.is_empty() {
419 rows.push(Row::new(
420 Line::mark(Mark::ByHand)
421 .and(Role::Plain, " ")
422 .and(Role::Strong, notices.len().to_string()),
423 ));
424 }
425 if !respellings.is_empty() || !notices.is_empty() {
426 rows.push(Row::new(Line::new()));
427 let width = respellings
428 .iter()
429 .map(|r| lines::Respellings::site(&r.path, r.start.line).len())
430 .chain(
431 notices
432 .iter()
433 .map(|n| lines::Respellings::site(&n.path, n.start.line).len()),
434 )
435 .max()
436 .unwrap_or(0);
437 rows.extend(
438 lines::Respellings::new(respellings, width)
439 .lines()
440 .into_iter()
441 .map(Row::new),
442 );
443 rows.extend(
444 notices
445 .iter()
446 .map(|n| Row::new(lines::NoticeRow::new(n, width).line())),
447 );
448 }
449 for file in files {
450 let is_structural = lines::Diff::is_structural(file, respellings);
451 if !(options.diff || is_structural) {
452 continue;
453 }
454 rows.push(Row::new(Line::new()));
455 rows.extend(
456 lines::Diff::new(file, is_structural)
457 .lines()
458 .into_iter()
459 .map(Row::new),
460 );
461 }
462 rows
463 }
464 Block::Blank => vec![Row::new(Line::new())],
465 }
466 }
467
468 fn occurrence(
469 &self,
470 occurrence: &Occurrence,
471 ordinal: usize,
472 _ticked: bool,
473 _width: usize,
474 ) -> Row {
475 Row::at(
476 lines::Verdicts::row(occurrence, ordinal),
477 occurrence.m.path.clone(),
478 occurrence.m.start.line,
479 )
480 }
481
482 fn respelling(&self, respelling: &Respelling, _width: usize) -> Row {
483 let site = lines::Respellings::site(&respelling.path, respelling.start.line).len();
484 Row::at(
485 lines::Respellings::row(respelling, site),
486 respelling.path.clone(),
487 respelling.start.line,
488 )
489 }
490
491 fn notice(&self, notice: &Notice, _width: usize) -> Row {
492 let site = lines::Respellings::site(¬ice.path, notice.start.line).len();
493 Row::at(
494 lines::NoticeRow::new(notice, site).line(),
495 notice.path.clone(),
496 notice.start.line,
497 )
498 }
499
500 fn rewrite(
501 &self,
502 m: &Match,
503 _after: Option<&str>,
504 ordinal: usize,
505 _ticked: bool,
506 _width: usize,
507 ) -> Row {
508 Row::at(
509 lines::MatchRow::numbered(m, ordinal),
510 m.path.clone(),
511 m.start.line,
512 )
513 }
514}