pub(crate) mod lines;
mod view;
pub use view::{Detailed, Presentation, View};
use vvv_core::RelPath;
use crate::SymbolKind;
use crate::protocol::display::{Line, Role};
use crate::protocol::vocabulary::{Files, IntentLine, Mark, Plural};
use crate::protocol::{
Answer, Batch, BatchIntent, Consumer, Dead, Dep, Deps, Explanation, Exposed, Failure, History,
Impact, ImportSite, Importer, ImportsReport, Intent, Locations, Move, MoveIntent, MoveSymbol,
Outline, OutlineItem, References, Rename, Rewrite, Site, Skipped, Surface, Undo, Unreferenced,
};
use crate::{Address, Confidence, FileChange, HistoryEntry, Match, Notice, Occurrence, Respelling};
use lines as l;
#[derive(Debug, Clone, Copy, Default)]
pub struct Options {
pub verbose: bool,
pub diff: bool,
}
#[derive(Debug, Default, Clone)]
pub struct Document {
body: Vec<Block>,
notes: Vec<Block>,
}
impl Document {
pub fn new() -> Self {
Self::default()
}
pub fn body(&mut self, lines: impl IntoIterator<Item = Line>) {
self.body.extend(lines.into_iter().map(Block::Line));
}
pub fn notes(&mut self, lines: impl IntoIterator<Item = Line>) {
self.notes.extend(lines.into_iter().map(Block::Line));
}
pub fn block_body(&mut self, block: Block) {
self.body.push(block);
}
pub fn block_note(&mut self, block: Block) {
self.notes.push(block);
}
pub fn parts(&self) -> (&[Block], &[Block]) {
(&self.body, &self.notes)
}
pub fn of(answer: &Answer, options: Options) -> Self {
match answer {
Answer::Search(r) => Self::search(&r.matches, &r.skipped),
Answer::Outline(r) => Self::outline(r),
Answer::References(r) => Self::references(r),
Answer::Where(r) => Self::locations(r),
Answer::Deps(r) => Self::deps(r),
Answer::Explain(r) => Self::explain(r),
Answer::Surface(r) => Self::surface(r),
Answer::Impact(r) => Self::impact(r),
Answer::Dead(r) => Self::dead(r),
Answer::Imports(r) => Self::imports(r),
Answer::File(_) => Self::new(),
Answer::Rewrite(r) => Self::rewrite(r),
Answer::Rename(r) => Self::rename(r, options),
Answer::Move(r) => Self::move_file(r, options),
Answer::MoveSymbol(r) => Self::move_symbol(r, options),
Answer::Batch(r) => Self::batch(r, options),
Answer::Undo(r) => Self::undo(r),
Answer::History(r) => Self::history(r),
}
}
pub fn error(failure: &Failure) -> Self {
let mut report = Self::new();
report.notes([Line::of(Role::Error, "✗ ").and(Role::Plain, failure.message.clone())]);
for line in failure.hint.iter().flat_map(|h| h.lines()) {
report.hint(line);
}
report
}
fn title(&mut self, text: impl std::fmt::Display) {
self.block_body(Block::Title(text.to_string()));
}
fn hint(&mut self, text: impl std::fmt::Display) {
self.block_note(Block::Note(Note::Hint(text.to_string())));
}
fn warning(&mut self, line: Line) {
self.block_note(Block::Note(Note::Warning(line)));
}
fn module_header(&mut self, module: Option<&Address>, path: &std::path::Path) {
match module.filter(|m| !m.package().as_str().is_empty()) {
Some(module) => self.body([Line::mark(Mark::Address)
.and(Role::Plain, " ")
.and(Role::Address, module.to_string())
.and(Role::Plain, " ")
.and(Role::Path, path.display().to_string())]),
None => self.body([Line::of(Role::Path, path.display().to_string())]),
}
}
fn moved(
&mut self,
files: &[FileChange],
respellings: &[Respelling],
notices: &[Notice],
) -> usize {
let structural = files
.iter()
.filter(|f| l::Diff::is_structural(f, respellings))
.count();
self.block_body(Block::Moved {
files: files.to_vec(),
respellings: respellings.to_vec(),
notices: notices.to_vec(),
});
structural
}
fn moved_summary(
&mut self,
applied: bool,
history_id: Option<u64>,
counts: MoveCounts,
diff: bool,
) {
let MoveCounts {
respellings,
structural,
notices,
files,
} = counts;
let mut counts: Vec<Line> = Vec::new();
if respellings > 0 {
counts.push(
Line::mark(Mark::Import)
.and(Role::Plain, " ")
.and(Role::Plain, respellings.to_string()),
);
}
if structural > 0 {
counts.push(
Line::mark(Mark::Structure)
.and(Role::Plain, " ")
.and(Role::Plain, structural.to_string()),
);
}
if notices > 0 {
counts.push(
Line::mark(Mark::ByHand)
.and(Role::Plain, " ")
.and(Role::Plain, notices.to_string()),
);
}
let counts = Self::join(counts, " ")
.and(Role::Plain, " ")
.and(Role::Dim, Plural(files, "file").to_string());
if applied {
self.receipt(true, history_id, counts);
return;
}
self.notes([counts]);
let mut flags = vec!["--apply to write"];
if !diff {
flags.push("--diff for the full patch");
}
self.hint(flags.join(" · "));
}
fn receipt(&mut self, applied: bool, history_id: Option<u64>, counts: Line) {
match (applied, history_id) {
(true, Some(id)) => self.notes([Line::mark(Mark::Safe)
.and(Role::Plain, " ")
.and(Role::Strong, format!("#{id}"))
.and(Role::Plain, " ")
.and_line(counts)]),
(true, None) => self.notes([Line::mark(Mark::Safe)
.and(Role::Plain, " ")
.and_line(counts)]),
(false, _) => {
self.notes([counts]);
self.hint("--apply to write");
}
}
}
fn addresses(&mut self, from: &Address, to: &Address) {
if from.package().as_str().is_empty() {
return;
}
self.body([Line::mark(Mark::Address)
.and(Role::Plain, " ")
.and(Role::Address, from.to_string())
.and(Role::Plain, " ")
.and_line(Line::mark(Mark::Import))
.and(Role::Plain, " ")
.and(Role::Address, to.to_string())]);
}
fn declarations(&mut self, declarations: &[Match]) {
self.body(declarations.iter().map(|m| l::Declaration::new(m).line()));
self.block_body(Block::Blank);
}
fn verdict_counts(occurrences: &[Occurrence]) -> Line {
let count = |c: Confidence| occurrences.iter().filter(|o| o.confidence == c).count();
Line::counts(&[
(
Mark::from(Confidence::Resolved),
count(Confidence::Resolved),
),
(
Mark::from(Confidence::Unresolved),
count(Confidence::Unresolved),
),
(Mark::from(Confidence::Other), count(Confidence::Other)),
])
.and(
Role::Dim,
format!(
" {}",
Files::among(occurrences.iter().map(|o| o.m.path.as_path()))
),
)
}
fn edits_in(files: &[FileChange]) -> usize {
files.iter().map(|f| f.edits.len()).sum()
}
fn join(parts: Vec<Line>, separator: &str) -> Line {
let mut line = Line::new();
for (i, part) in parts.into_iter().enumerate() {
if i > 0 {
line = line.and(Role::Plain, separator.to_owned());
}
line = line.and_line(part);
}
line
}
pub fn search(matches: &[Match], skipped: &[Skipped]) -> Self {
let mut report = Self::new();
report.block_body(Block::Matches(matches.to_vec()));
if matches.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no matches"),
));
} else {
report.block_note(Block::Summary(Self::search_summary(matches)));
}
for skipped in skipped {
report.warning(l::SkippedLine::new(skipped).line());
}
if !skipped.is_empty() {
report.hint(
"a pattern is written in one language; pass --lang to search that one only (the matches above are complete for the others)",
);
}
report
}
fn search_summary(matches: &[Match]) -> Line {
let (declarations, uses) = l::Sections::split(matches);
let mut counts: Vec<Line> = Vec::new();
if !declarations.is_empty() {
counts.push(
Line::mark(Mark::Declaration)
.and(Role::Plain, " ")
.and(Role::Plain, declarations.len().to_string()),
);
}
if !uses.is_empty() {
counts.push(
Line::of(Role::Dim, "○")
.and(Role::Plain, " ")
.and(Role::Plain, uses.len().to_string()),
);
}
Self::join(counts, " ").and(Role::Plain, " ").and(
Role::Dim,
Files::among(matches.iter().map(|m| m.path.as_path())).to_string(),
)
}
fn outline(result: &Outline) -> Self {
let mut report = Self::new();
report.module_header(result.module.as_ref(), &result.path);
if result.items.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no declarations"),
));
return report;
}
report.block_body(Block::Blank);
report.block_body(Block::Outline(result.items.clone()));
report.block_note(Block::Summary(Self::outline_summary(result)));
report
}
fn outline_summary(result: &Outline) -> Line {
let mut counts: Vec<(String, usize)> = Vec::new();
for item in &result.items {
if item.symbol.kind == SymbolKind::Impl {
continue;
}
let key = item.symbol.modifier().unwrap_or("·").to_owned();
match counts.iter_mut().find(|(k, _)| *k == key) {
Some((_, n)) => *n += 1,
None => counts.push((key, 1)),
}
}
counts.sort_by_key(|(k, _)| k == "·");
let counts: Vec<Line> = counts
.iter()
.map(|(k, n)| {
Line::of(Role::Dim, k.clone())
.and(Role::Plain, " ")
.and(Role::Plain, n.to_string())
})
.collect();
Line::mark(Mark::Declaration)
.and(Role::Plain, " ")
.and(Role::Plain, result.items.len().to_string())
.and(Role::Plain, " ")
.and_line(Self::join(counts, " "))
}
fn references(result: &References) -> Self {
let mut report = Self::new();
report.declarations(&result.declarations);
report.block_body(Block::Verdicts {
occurrences: result.occurrences.clone(),
files: None,
});
report.block_note(Block::Summary(Self::verdict_counts(&result.occurrences)));
report
}
fn locations(result: &Locations) -> Self {
let mut report = Self::new();
report.block_body(Block::Sites(result.sites.clone()));
report.block_note(Block::Summary(
Line::mark(Mark::Declaration)
.and(Role::Plain, " ")
.and(Role::Plain, result.sites.len().to_string()),
));
if result.sites.iter().all(|s| s.import.is_none())
&& result.sites.iter().any(|s| s.address.is_some())
{
report.hint("--from <file> for the import to write there");
}
report
}
fn deps(result: &Deps) -> Self {
let mut report = Self::new();
report.module_header(result.module.as_ref(), &result.path);
report.block_body(Block::Blank);
let outgoing = l::DepGroups::statements(&result.imports);
report.body([Line::mark(Mark::Import)
.and(Role::Plain, " ")
.and(Role::Strong, outgoing.to_string())]);
if result.imports.is_empty() {
report.body([Line::of(Role::Plain, " ").and_line(Line::mark(Mark::Nothing))]);
}
let own = result
.module
.as_ref()
.map(|m| m.package().as_str().to_owned());
report.block_body(Block::DepGroups {
imports: result.imports.clone(),
own,
});
report.block_body(Block::Blank);
let incoming = l::ImporterRows::sites(&result.importers).len();
report.body([Line::mark(Mark::ImportedBy)
.and(Role::Plain, " ")
.and(Role::Strong, incoming.to_string())
.and(Role::Plain, " ")
.and(
Role::Dim,
Files::among(result.importers.iter().map(|i| i.path.as_path())).to_string(),
)]);
if result.importers.is_empty() {
report.body([Line::of(Role::Plain, " ").and_line(Line::mark(Mark::Nothing))]);
}
report.block_body(Block::Importers(result.importers.clone()));
report.block_note(Block::Summary(Self::deps_summary(outgoing, incoming)));
for skipped in &result.skipped {
report.warning(l::SkippedLine::new(skipped).line());
}
report
}
fn deps_summary(outgoing: usize, incoming: usize) -> Line {
Line::mark(Mark::Import)
.and(Role::Plain, format!(" {outgoing} "))
.and_line(Line::mark(Mark::ImportedBy))
.and(Role::Plain, format!(" {incoming}"))
}
fn explain(result: &Explanation) -> Self {
let mut report = Self::new();
report.block_body(Block::Explanation(Box::new(result.clone())));
report
}
fn surface(result: &Surface) -> Self {
let mut report = Self::new();
let scope = result
.package
.as_ref()
.map_or_else(|| "every package".to_owned(), ToString::to_string);
report.title(format!("surface {scope}"));
if result.items.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " nothing exposed"),
));
return report;
}
report.block_body(Block::Exposed(result.items.clone()));
report.block_note(Block::Summary(Self::surface_summary(result)));
report
}
fn surface_summary(result: &Surface) -> Line {
let importers: usize = result.items.iter().map(|i| i.importers).sum();
Line::mark(Mark::Declaration)
.and(Role::Plain, " ")
.and(Role::Strong, result.items.len().to_string())
.and(Role::Plain, " ")
.and_line(Line::mark(Mark::ImportedBy))
.and(Role::Plain, " ")
.and(Role::Dim, format!("{importers} imports"))
}
fn impact(result: &Impact) -> Self {
let mut report = Self::new();
report.body([Line::of(Role::Title, format!("impact {}", result.name))
.and(Role::Plain, " ")
.and(Role::Address, format!("◆ {}", result.address))]);
if result.consumers.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no module imports it"),
));
return report;
}
report.block_body(Block::Consumers(result.consumers.clone()));
report.block_note(Block::Summary(
Line::mark(Mark::ImportedBy)
.and(Role::Plain, " ")
.and(Role::Strong, result.consumers.len().to_string())
.and(Role::Plain, " ")
.and(
Role::Dim,
Files::among(result.consumers.iter().map(|c| c.path.as_path())).to_string(),
),
));
report
}
fn dead(result: &Dead) -> Self {
let mut report = Self::new();
report.title("dead");
if result.items.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " everything is referred to"),
));
return report;
}
report.block_body(Block::Dead(result.items.clone()));
report.block_note(Block::Summary(Self::dead_summary(result)));
report
}
fn dead_summary(result: &Dead) -> Line {
let unsure = result.items.iter().filter(|i| i.unsure > 0).count();
Line::mark(Mark::Declaration)
.and(Role::Plain, " ")
.and(Role::Strong, result.items.len().to_string())
.and(Role::Plain, " ")
.and_line(Line::mark(Mark::Unverified))
.and(Role::Plain, " ")
.and(
Role::Dim,
format!("{unsure} with tokens that might be uses"),
)
}
fn imports(result: &ImportsReport) -> Self {
let mut report = Self::new();
let scope = result.path.as_ref().map_or_else(
|| "every file".to_owned(),
|path| path.display().to_string(),
);
report.title(format!("imports {scope}"));
let sections: [(Mark, &str, &[ImportSite]); 3] = [
(Mark::Nothing, "unresolved", &result.unresolved),
(Mark::ByHand, "unused", &result.unused),
(Mark::ByHand, "redundant", &result.redundant),
];
let total: usize = sections.iter().map(|(_, _, s)| s.len()).sum();
if total == 0 {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " nothing to look at"),
));
return report;
}
report.block_body(Block::Imports {
unresolved: result.unresolved.clone(),
unused: result.unused.clone(),
redundant: result.redundant.clone(),
});
report.block_note(Block::Summary(Self::imports_summary(
§ions,
result.unplaced.len(),
)));
report
}
fn imports_summary(sections: &[(Mark, &str, &[ImportSite])], unplaced: usize) -> Line {
let mut counts: Vec<Line> = sections
.iter()
.filter(|(_, _, s)| !s.is_empty())
.map(|(mark, word, s)| {
Line::mark(*mark)
.and(Role::Plain, format!(" {word} "))
.and(Role::Plain, s.len().to_string())
})
.collect();
if unplaced > 0 {
counts.push(
Line::mark(Mark::Nothing)
.and(Role::Plain, " ")
.and(Role::Dim, "not placed")
.and(Role::Plain, " ")
.and(Role::Dim, unplaced.to_string()),
);
}
Self::join(counts, " ")
}
fn rewrite(result: &Rewrite) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::Rewrite(result.intent.clone())));
report.block_body(Block::Changes(result.files.clone()));
let edits = Self::edits_in(&result.files);
if edits == 0 {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no matches"),
));
return report;
}
report.receipt(
result.applied,
result.history_id,
Line::mark(Mark::Rewrite)
.and(Role::Plain, format!(" {edits} "))
.and(Role::Dim, Plural(result.files.len(), "file").to_string()),
);
report
}
fn rename(result: &Rename, options: Options) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::Rename(result.intent.clone())));
report.declarations(&result.declarations);
report.block_body(Block::Verdicts {
occurrences: result.occurrences.clone(),
files: Some(result.files.clone()),
});
let strip = Self::verdict_counts(&result.occurrences);
let edits = Self::edits_in(&result.files);
let plan = Line::of(
Role::Dim,
format!(
"→ {} in {}",
Plural(edits, "occurrence"),
Plural(result.files.len(), "file")
),
);
if result.applied {
report.block_note(Block::Summary(strip));
report.receipt(true, result.history_id, plan);
return report;
}
report.block_note(Block::Summary(strip.and(Role::Plain, " ").and_line(plan)));
let unsure = l::Verdicts::selection(&result.occurrences, Confidence::Unresolved);
let mut flags = vec!["--apply to write".to_owned()];
if !unsure.is_empty() {
flags.push(format!("--select {unsure} for the ? rows alone"));
}
if !options.verbose
&& result
.occurrences
.iter()
.any(|o| o.confidence == Confidence::Resolved)
{
flags.push("-v to list the ✓ rows".to_owned());
}
report.hint(flags.join(" · "));
report
}
fn move_file(result: &Move, options: Options) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::Move(MoveIntent::new(
&result.from,
&result.to,
))));
if let (Some(from), Some(to)) = (&result.from_address, &result.to_address) {
report.addresses(from, to);
}
report.block_body(Block::Blank);
let structural = report.moved(&result.files, &result.respellings, &result.notices);
report.moved_summary(
result.applied,
result.history_id,
MoveCounts {
respellings: result.respellings.len(),
structural,
notices: result.notices.len(),
files: result.files.len(),
},
options.diff,
);
report
}
fn move_symbol(result: &MoveSymbol, options: Options) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::MoveSymbol(result.intent.clone())));
report.addresses(&result.from, &result.to);
report.block_body(Block::Blank);
let structural = report.moved(&result.files, &result.respellings, &result.notices);
report.moved_summary(
result.applied,
result.history_id,
MoveCounts {
respellings: result.respellings.len(),
structural,
notices: result.notices.len(),
files: result.files.len(),
},
options.diff,
);
report
}
fn batch(result: &Batch, options: Options) -> Self {
let mut report = Self::new();
report.title(IntentLine(&Intent::Batch(BatchIntent {
intents: result.intents.clone(),
})));
report.block_body(Block::Batch(result.intents.clone()));
report.block_body(Block::Blank);
let structural = report.moved(&result.files, &[], &result.notices);
report.moved_summary(
result.applied,
result.history_id,
MoveCounts {
respellings: 0,
structural,
notices: result.notices.len(),
files: result.files.len(),
},
options.diff,
);
report
}
fn undo(result: &Undo) -> Self {
let mut report = Self::new();
report.title(format!(
"{} #{} {}",
Mark::Undo.glyph(),
result.undone.id,
IntentLine(&result.undone.intent)
));
report.block_body(Block::Undo {
moves: result.moves_reverted.clone(),
restored: result.restored.clone(),
});
report.block_note(Block::Summary(
Line::mark(Mark::Undo)
.and(Role::Plain, " ")
.and(Role::Plain, format!("#{} ", result.undone.id))
.and(Role::Dim, Plural(result.restored.len(), "file").to_string()),
));
report
}
fn history(result: &History) -> Self {
let mut report = Self::new();
if result.entries.is_empty() {
report.block_note(Block::Summary(
Line::mark(Mark::Nothing).and(Role::Plain, " no history"),
));
report
.hint("--apply writes a plan and records it here; `vvv undo` reverses the newest");
return report;
}
let last = result.entries.len() - 1;
report.block_body(Block::History(result.entries.clone()));
report.block_note(Block::Summary(
Line::of(
Role::Plain,
Plural(result.entries.len(), "entry").to_string(),
)
.and(Role::Plain, " ")
.and_line(Line::mark(Mark::Undo))
.and(Role::Plain, format!(" #{}", result.entries[last].id)),
));
report
}
}
#[derive(Debug, Clone)]
pub enum Block {
Title(String),
Heading(String),
Matches(Vec<Match>),
Verdicts {
occurrences: Vec<Occurrence>,
files: Option<Vec<FileChange>>,
},
Imports {
unresolved: Vec<ImportSite>,
unused: Vec<ImportSite>,
redundant: Vec<ImportSite>,
},
DepGroups {
imports: Vec<Dep>,
own: Option<String>,
},
Importers(Vec<Importer>),
Explanation(Box<Explanation>),
Outline(Vec<OutlineItem>),
Sites(Vec<Site>),
Dead(Vec<Unreferenced>),
Exposed(Vec<Exposed>),
Consumers(Vec<Consumer>),
History(Vec<HistoryEntry>),
Batch(Vec<Intent>),
Undo {
moves: Vec<(RelPath, RelPath)>,
restored: Vec<RelPath>,
},
Line(Line),
Summary(Line),
Note(Note),
Changes(Vec<FileChange>),
Moved {
files: Vec<FileChange>,
respellings: Vec<Respelling>,
notices: Vec<Notice>,
},
Blank,
}
#[derive(Debug, Clone)]
pub struct Row {
pub line: Line,
pub source: Option<Source>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Source {
pub path: RelPath,
pub line: u32,
}
impl Row {
pub fn new(line: Line) -> Self {
Self { line, source: None }
}
pub fn at(line: Line, path: RelPath, at: u32) -> Self {
Self {
line,
source: Some(Source { path, line: at }),
}
}
pub fn wrap(lines: Vec<Line>) -> Vec<Self> {
lines.into_iter().map(Self::new).collect()
}
}
#[derive(Debug, Clone)]
pub enum Note {
Hint(String),
Warning(Line),
}
struct MoveCounts {
respellings: usize,
structural: usize,
notices: usize,
files: usize,
}