use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
const ALLOWED: &[(&str, &str)] = &[
("src/archive.rs", "editor::Editor"),
("src/git/tests.rs", "diff::DiffData"),
("src/highlight.rs", "editor::SELECTABLE_LANGUAGES"),
("src/highlight.rs", "editor::language_scope_id"),
("src/import_vscode.rs", "editor::WhitespaceMode"),
("src/lsp/manager/tests.rs", "editor::Editor"),
("src/pane_host.rs", "terminal::VoidListener"),
("src/pane_host.rs", "terminal::ansi_rows_from"),
("src/pane_host.rs", "terminal::resize_term"),
("src/scrubber.rs", "editor::Editor"),
("src/scrubber.rs", "editor::split_into_lines"),
("src/testing/worker.rs", "testing::TestingPanel"),
("src/view_ipc.rs", "editor::Editor"),
];
fn is_ui(rel: &str) -> bool {
rel.starts_with("src/app/") || rel.starts_with("src/widgets/") || rel == "src/main.rs"
}
fn uses(root: &Path) -> BTreeSet<(String, String)> {
let mut found = BTreeSet::new();
for file in rust_files(&root.join("src")) {
let rel = file
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
if is_ui(&rel) || rel == "src/view_boundary.rs" {
continue;
}
let text = std::fs::read_to_string(&file).unwrap();
let mut rest = text.as_str();
while let Some(at) = rest.find("crate::") {
let named_crate = !rest[..at].ends_with(|c: char| c.is_alphanumeric() || c == '_');
rest = &rest[at + "crate::".len()..];
if !named_crate {
continue;
}
for path in widget_paths(rest) {
found.insert((rel.clone(), cut_after_type(&path)));
}
}
}
found
}
fn rust_files(dir: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
let mut dirs = vec![dir.to_path_buf()];
while let Some(dir) = dirs.pop() {
for entry in std::fs::read_dir(&dir).unwrap().flatten() {
let path = entry.path();
if path.is_dir() {
dirs.push(path);
} else if path.extension().is_some_and(|e| e == "rs") {
files.push(path);
}
}
}
files
}
fn widget_paths(after_crate: &str) -> Vec<String> {
use_tree(after_crate)
.0
.into_iter()
.filter_map(|p| match p.strip_prefix("widgets") {
Some("") => Some(String::new()),
Some(sub) => sub.strip_prefix("::").map(str::to_string),
None => None,
})
.collect()
}
fn use_tree(s: &str) -> (Vec<String>, usize) {
let trimmed = s.trim_start();
let lead = s.len() - trimmed.len();
if let Some(group) = trimmed.strip_prefix('{') {
let mut paths = Vec::new();
let mut at = 0;
loop {
let (mut sub, used) = use_tree(&group[at..]);
paths.append(&mut sub);
at += used;
let next = group[at..].trim_start();
at = group.len() - next.len();
match next.chars().next() {
Some(',') => at += 1,
Some('}') => return (paths, lead + 1 + at + 1),
_ => return (paths, lead + 1 + at),
}
}
}
let name_len = trimmed
.find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '*'))
.unwrap_or(trimmed.len());
if name_len == 0 {
return (Vec::new(), lead);
}
let name = &trimmed[..name_len];
match trimmed[name_len..].strip_prefix("::") {
Some(tail) => {
let (subs, used) = use_tree(tail);
let paths = match subs.is_empty() {
true => vec![name.to_string()],
false => subs
.into_iter()
.map(|p| match p.as_str() {
"self" => name.to_string(),
_ => format!("{name}::{p}"),
})
.collect(),
};
(paths, lead + name_len + 2 + used)
}
None => (
vec![name.to_string()],
lead + name_len + alias_len(&trimmed[name_len..]),
),
}
}
fn alias_len(s: &str) -> usize {
let trimmed = s.trim_start();
let Some(after) = trimmed.strip_prefix("as") else {
return 0;
};
let name = after.trim_start();
if name.len() == after.len() {
return 0;
}
let name_len = name
.find(|c: char| !(c.is_alphanumeric() || c == '_'))
.unwrap_or(name.len());
s.len() - name.len() + name_len
}
fn cut_after_type(path: &str) -> String {
let segments: Vec<&str> = path.split("::").collect();
let end = segments
.iter()
.position(|s| s.starts_with(|c: char| c.is_uppercase()))
.map_or(segments.len(), |i| i + 1);
segments[..end].join("::")
}
#[test]
fn outside_the_ui_only_the_listed_parts_come_from_the_views() {
let found = uses(Path::new(env!("CARGO_MANIFEST_DIR")));
let allowed: BTreeSet<(String, String)> = ALLOWED
.iter()
.map(|(file, path)| (file.to_string(), path.to_string()))
.collect();
let new: Vec<_> = found.difference(&allowed).collect();
let gone: Vec<_> = allowed.difference(&found).collect();
assert!(
new.is_empty(),
"code outside the UI names a view; move what it needs out of `src/widgets/` \
next to its users (an empty path is `crate::widgets` itself): {new:?}"
);
assert!(
gone.is_empty(),
"these listed uses of a view are gone; strike them from ALLOWED, which \
may only shrink: {gone:?}"
);
}
#[test]
fn a_grouped_nested_or_aliased_use_is_read_in_full() {
assert_eq!(
widget_paths("widgets::editor::Editor::new(x)"),
["editor::Editor::new"]
);
assert_eq!(
widget_paths("widgets::{editor::{self, Editor}, terminal::VoidListener};"),
["editor", "editor::Editor", "terminal::VoidListener"]
);
assert_eq!(
widget_paths("widgets::{\n diff::DiffData,\n testing,\n};"),
["diff::DiffData", "testing"]
);
assert_eq!(widget_paths("widgets;"), [""]);
assert_eq!(widget_paths("widgets as views;"), [""]);
assert_eq!(
widget_paths("{git, widgets::search::SearchPanel};"),
["search::SearchPanel"]
);
assert_eq!(
widget_paths("widgets::{editor::Editor as E, search::SearchPanel};"),
["editor::Editor", "search::SearchPanel"]
);
assert_eq!(widget_paths("{self, widgets as w, git::query};"), [""]);
assert!(widget_paths("git::query(p)").is_empty());
assert!(widget_paths("widgets_extra::X;").is_empty());
assert_eq!(cut_after_type("editor::Editor::new"), "editor::Editor");
assert_eq!(
cut_after_type("terminal::resize_term"),
"terminal::resize_term"
);
assert_eq!(
cut_after_type("editor::SELECTABLE_LANGUAGES"),
"editor::SELECTABLE_LANGUAGES"
);
}
#[test]
fn a_new_use_outside_the_ui_is_caught_and_the_ui_is_not_read() {
let root = tempfile::tempdir().unwrap();
let src = root.path().join("src");
std::fs::create_dir_all(src.join("widgets")).unwrap();
std::fs::create_dir_all(src.join("app")).unwrap();
std::fs::write(
src.join("engine.rs"),
"use crate::widgets::search::SearchPanel;\n/// See [`crate::widgets::diff`].\n\
use crate::{git, widgets::{editor::Editor as E, ports::PortsPanel}};\n",
)
.unwrap();
std::fs::write(
src.join("app/mod.rs"),
"use crate::widgets::editor::Editor;\n",
)
.unwrap();
std::fs::write(src.join("widgets/a.rs"), "use crate::widgets::b::B;\n").unwrap();
std::fs::write(src.join("main.rs"), "use crate::widgets::editor::Editor;\n").unwrap();
std::fs::write(src.join("other.rs"), "use crate::widgets_extra::X;\n").unwrap();
let found: Vec<_> = uses(root.path()).into_iter().collect();
assert_eq!(
found,
[
("src/engine.rs".to_string(), "diff".to_string()),
("src/engine.rs".to_string(), "editor::Editor".to_string()),
("src/engine.rs".to_string(), "ports::PortsPanel".to_string()),
(
"src/engine.rs".to_string(),
"search::SearchPanel".to_string()
),
]
);
}