mod address;
mod project;
use std::path::{Path, PathBuf};
pub use address::{Address, Dependency, Package, PackageId, Packages};
pub use project::{FileSet, Parsed, Project};
use crate::edit::Edit;
use crate::import::ImportRef;
use crate::paths::{ModulePath, RelPath};
use crate::semantics::ReachKind;
use crate::symbol::Symbol;
use crate::text::SourceText;
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum ResolveError {
#[error("{} is not addressable in this language (outside the module tree?)", .0.display())]
NotAddressable(RelPath),
#[error("{} does not exist", .0.display())]
Missing(RelPath),
#[error("{} is a crate or directory root; moving it is not supported", .0.display())]
Root(RelPath),
#[error("{} would end up inside itself at {}", from.display(), to.display())]
IntoItself { from: RelPath, to: RelPath },
#[error("moving between crates is not supported ({} → {})", from.display(), to.display())]
CrossProject { from: RelPath, to: RelPath },
#[error("cannot find the file declaring `{name}`")]
NoDeclaringFile { name: String },
#[error(
"no file declares module `{module}`; create {} first so `{declaration}` has a home",
Candidates(candidates)
)]
NoParentFile {
module: String,
candidates: Vec<RelPath>,
declaration: String,
},
#[error("no `{declaration}` in {}; inline or #[path] modules are not supported", parent.display())]
NoDeclaration {
declaration: String,
parent: RelPath,
},
#[error("cannot parse {}: {reason}", path.display())]
Syntax { path: RelPath, reason: String },
}
struct Candidates<'a>(&'a [RelPath]);
impl std::fmt::Display for Candidates<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
[] => write!(f, "the module file"),
[only] => write!(f, "{}", only.display()),
[first, rest @ ..] => {
write!(f, "{}", first.display())?;
write!(f, " (or ")?;
for (i, p) in rest.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", p.display())?;
}
write!(f, ")")
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SideEdit {
pub path: PathBuf,
pub edit: Edit,
}
pub trait Layout: Send + Sync {
fn manifests(&self) -> &'static [&'static str];
fn package(&self, manifest: &Path, text: &str) -> Option<Package>;
fn address(&self, project: &Project, path: &Path) -> Result<Address, ResolveError>;
fn candidates(&self, project: &Project, address: &Address) -> Vec<PathBuf>;
fn resolve(&self, project: &Project, from: &Path, import: &ModulePath) -> Option<Address>;
fn companions(&self, project: &Project, from: &Path, to: &Path) -> Vec<(PathBuf, PathBuf)> {
let _ = (project, from, to);
Vec::new()
}
fn touched_by_move(
&self,
project: &Project,
from: &Path,
to: &Path,
) -> Result<Vec<PathBuf>, ResolveError> {
let _ = (project, from, to);
Ok(Vec::new())
}
}
pub trait Surgery: Send + Sync {
fn render(
&self,
project: &Project,
from: &Path,
target: &Address,
original: &ModulePath,
) -> ModulePath;
fn import_statement(
&self,
project: &Project,
from: &Path,
target: &Address,
name: &str,
) -> Option<String> {
let _ = (project, from, target, name);
None
}
fn regroup(
&self,
project: &Project,
from: &Path,
source: &SourceText,
entries: &[(ImportRef, Address)],
) -> Regrouped {
let _ = (project, from, source);
Regrouped {
edits: Vec::new(),
skipped: entries.iter().map(|(r, _)| r.clone()).collect(),
}
}
fn relocate(
&self,
project: &Project,
from: &Path,
to: &Path,
touched: &[Parsed<'_>],
widen_to: Option<ReachKind>,
) -> Result<Vec<SideEdit>, ResolveError> {
let _ = (project, from, to, touched, widen_to);
Ok(Vec::new())
}
fn import_of_path(&self, path: &ModulePath, name: &str) -> Option<String> {
let _ = (path, name);
None
}
fn import_insertion(&self, file: &Parsed<'_>) -> usize {
file.facts
.imports
.iter()
.filter(|i| i.declares)
.map(|i| i.span.end)
.max()
.map_or(0, |end| {
file.source.as_str()[end..]
.find('\n')
.map_or(file.source.len(), |nl| end + nl + 1)
})
}
fn item_insertion(&self, file: &Parsed<'_>) -> usize {
file.source.len()
}
fn widen(&self, symbol: &Symbol, source: &SourceText, to: ReachKind) -> Option<Edit> {
let _ = (symbol, source, to);
None
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Regrouped {
pub edits: Vec<Edit>,
pub skipped: Vec<ImportRef>,
}