mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 02:39:20 +00:00
chore: use NormalizedPathBuf
This commit is contained in:
parent
23b696ca1e
commit
e6a75e80df
10 changed files with 78 additions and 89 deletions
|
@ -324,8 +324,8 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
continue;
|
||||
};
|
||||
self.shared.rename_path(
|
||||
&old_uri.to_file_path().unwrap(),
|
||||
new_uri.to_file_path().unwrap(),
|
||||
&old_uri.to_file_path().unwrap().into(),
|
||||
new_uri.to_file_path().unwrap().into(),
|
||||
);
|
||||
self.restore_entry(new_uri, entry);
|
||||
}
|
||||
|
|
|
@ -758,7 +758,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
/// the cache is deleted after each analysis.
|
||||
pub(crate) fn get_checker(&self, path: PathBuf) -> Checker {
|
||||
let shared = self.shared.clone();
|
||||
shared.clear(&path);
|
||||
shared.clear(&NormalizedPathBuf::from(&path));
|
||||
Checker::inherit(self.cfg.inherit(path), shared)
|
||||
}
|
||||
|
||||
|
@ -914,7 +914,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
}
|
||||
|
||||
pub(crate) fn clear_cache(&mut self, uri: &NormalizedUrl) {
|
||||
let path = util::uri_to_path(uri);
|
||||
let path = NormalizedPathBuf::from(util::uri_to_path(uri));
|
||||
self.shared.clear(&path);
|
||||
}
|
||||
|
||||
|
|
|
@ -265,7 +265,7 @@ impl PackageBuilder {
|
|||
if self
|
||||
.shared
|
||||
.graph
|
||||
.inc_ref(&from_path, import_path.to_path_buf())
|
||||
.inc_ref(&from_path, import_path.clone())
|
||||
.is_err()
|
||||
{
|
||||
self.submodules.push(from_path.clone());
|
||||
|
@ -299,7 +299,7 @@ impl PackageBuilder {
|
|||
/// Launch the analysis processes in order according to the dependency graph.
|
||||
fn execute(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
|
||||
log!(info "Start to spawn dependencies processes");
|
||||
let path = self.cfg.input.path().to_path_buf();
|
||||
let path = NormalizedPathBuf::from(self.cfg.input.path());
|
||||
let mut graph = self.shared.graph.clone_inner();
|
||||
let mut ancestors = graph.ancestors(&path).into_vec();
|
||||
while let Some(ancestor) = ancestors.pop() {
|
||||
|
|
|
@ -50,7 +50,7 @@ pub enum SubstituteResult {
|
|||
}
|
||||
|
||||
impl Context {
|
||||
pub(crate) fn mod_registered(&self, path: &Path) -> bool {
|
||||
pub(crate) fn mod_registered(&self, path: &NormalizedPathBuf) -> bool {
|
||||
(self.shared.is_some() && self.promises().is_registered(path)) || self.mod_cached(path)
|
||||
}
|
||||
|
||||
|
@ -63,15 +63,18 @@ impl Context {
|
|||
pub(crate) fn get_mod_with_path(&self, path: &Path) -> Option<&Context> {
|
||||
if self.module_path() == path {
|
||||
return self.get_module();
|
||||
} else if let Some(ctx) = self.get_module_from_stack(&NormalizedPathBuf::from(path)) {
|
||||
}
|
||||
let path = NormalizedPathBuf::from(path);
|
||||
if let Some(ctx) = self.get_module_from_stack(&path) {
|
||||
return Some(ctx);
|
||||
}
|
||||
if self.shared.is_some() && self.promises().is_registered(path) && !self.mod_cached(path) {
|
||||
let _result = self.promises().join(path);
|
||||
if self.shared.is_some() && self.promises().is_registered(&path) && !self.mod_cached(&path)
|
||||
{
|
||||
let _result = self.promises().join(&path);
|
||||
}
|
||||
self.opt_mod_cache()?
|
||||
.raw_ref_ctx(path)
|
||||
.or_else(|| self.opt_py_mod_cache()?.raw_ref_ctx(path))
|
||||
.raw_ref_ctx(&path)
|
||||
.or_else(|| self.opt_py_mod_cache()?.raw_ref_ctx(&path))
|
||||
.map(|mod_ctx| &mod_ctx.context)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::path::Path;
|
||||
|
||||
use erg_common::config::ErgConfig;
|
||||
use erg_common::dict::Dict;
|
||||
|
@ -166,7 +165,7 @@ impl ModuleCache {
|
|||
get_similar_name(self.cache.iter().map(|(v, _)| v.to_str().unwrap()), name).map(Str::rc)
|
||||
}
|
||||
|
||||
pub fn rename_path(&mut self, old: &Path, new: NormalizedPathBuf) {
|
||||
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
if let Some(entry) = self.cache.remove(old) {
|
||||
self.cache.insert(new, entry);
|
||||
}
|
||||
|
@ -302,7 +301,7 @@ impl SharedModuleCache {
|
|||
self.register(builtin_path, None, None, builtin.module);
|
||||
}
|
||||
|
||||
pub fn rename_path<P: Into<NormalizedPathBuf>>(&self, path: &Path, new: P) {
|
||||
pub fn rename_path<P: Into<NormalizedPathBuf>>(&self, path: &NormalizedPathBuf, new: P) {
|
||||
self.0.borrow_mut().rename_path(path, new.into());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::path::Path;
|
||||
|
||||
use erg_common::pathutil::NormalizedPathBuf;
|
||||
use erg_common::set::Set;
|
||||
use erg_common::shared::Shared;
|
||||
|
@ -38,11 +36,10 @@ impl SharedCompileErrors {
|
|||
self.0.borrow_mut().clear();
|
||||
}
|
||||
|
||||
pub fn remove(&self, path: &Path) {
|
||||
let path = NormalizedPathBuf::from(path);
|
||||
pub fn remove(&self, path: &NormalizedPathBuf) {
|
||||
self.0
|
||||
.borrow_mut()
|
||||
.retain(|e| NormalizedPathBuf::from(e.input.path()) != path);
|
||||
.retain(|e| &NormalizedPathBuf::from(e.input.path()) != path);
|
||||
}
|
||||
|
||||
pub fn raw_iter(&self) -> impl Iterator<Item = &CompileError> {
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
use erg_common::config::ErgConfig;
|
||||
use erg_common::pathutil::NormalizedPathBuf;
|
||||
|
||||
|
@ -38,10 +36,7 @@ impl SharedCompilerResource {
|
|||
index: SharedModuleIndex::new(),
|
||||
graph: graph.clone(),
|
||||
trait_impls: SharedTraitImpls::new(),
|
||||
promises: SharedPromises::new(
|
||||
graph,
|
||||
cfg.input.path().canonicalize().unwrap_or_default(),
|
||||
),
|
||||
promises: SharedPromises::new(graph, NormalizedPathBuf::from(cfg.input.path())),
|
||||
errors: SharedCompileErrors::new(),
|
||||
warns: SharedCompileWarnings::new(),
|
||||
};
|
||||
|
@ -69,7 +64,7 @@ impl SharedCompilerResource {
|
|||
|
||||
/// Clear all information about the module.
|
||||
/// Graph information is not cleared (due to ELS).
|
||||
pub fn clear(&self, path: &Path) {
|
||||
pub fn clear(&self, path: &NormalizedPathBuf) {
|
||||
for child in self.graph.children(path) {
|
||||
self.clear(&child);
|
||||
}
|
||||
|
@ -82,7 +77,7 @@ impl SharedCompilerResource {
|
|||
self.warns.remove(path);
|
||||
}
|
||||
|
||||
pub fn rename_path(&self, old: &Path, new: PathBuf) {
|
||||
pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
self.mod_cache.rename_path(old, new.clone());
|
||||
self.py_mod_cache.rename_path(old, new.clone());
|
||||
self.index.rename_path(old, new.clone());
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use erg_common::pathutil::NormalizedPathBuf;
|
||||
use erg_common::set;
|
||||
|
@ -49,14 +48,13 @@ impl ModuleGraph {
|
|||
Self(Graph::new())
|
||||
}
|
||||
|
||||
pub fn get_node(&self, path: &Path) -> Option<&Node<NormalizedPathBuf, ()>> {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
self.0.iter().find(|n| n.id == path)
|
||||
pub fn get_node(&self, path: &NormalizedPathBuf) -> Option<&Node<NormalizedPathBuf, ()>> {
|
||||
self.0.iter().find(|n| &n.id == path)
|
||||
}
|
||||
|
||||
/// if `path` depends on `target`, returns `true`, else `false`.
|
||||
/// if `path` not found, returns `false`
|
||||
pub fn depends_on(&self, path: &Path, target: &Path) -> bool {
|
||||
pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
let target = NormalizedPathBuf::new(target.to_path_buf());
|
||||
self.0
|
||||
|
@ -66,18 +64,16 @@ impl ModuleGraph {
|
|||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn children(&self, path: &Path) -> Set<NormalizedPathBuf> {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
pub fn children(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
|
||||
self.0
|
||||
.iter()
|
||||
.filter(|n| n.depends_on.contains(&path))
|
||||
.filter(|n| n.depends_on.contains(path))
|
||||
.map(|n| n.id.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn parents(&self, path: &Path) -> Option<&Set<NormalizedPathBuf>> {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
self.0.iter().find(|n| n.id == path).map(|n| &n.depends_on)
|
||||
fn parents(&self, path: &NormalizedPathBuf) -> Option<&Set<NormalizedPathBuf>> {
|
||||
self.0.iter().find(|n| &n.id == path).map(|n| &n.depends_on)
|
||||
}
|
||||
|
||||
/// ```erg
|
||||
|
@ -85,7 +81,7 @@ impl ModuleGraph {
|
|||
/// b = import "b"
|
||||
/// ```
|
||||
/// -> a: child, b: parent
|
||||
pub fn ancestors(&self, path: &Path) -> Set<NormalizedPathBuf> {
|
||||
pub fn ancestors(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
|
||||
let mut ancestors = set! {};
|
||||
if let Some(parents) = self.parents(path) {
|
||||
for parent in parents.iter() {
|
||||
|
@ -96,24 +92,25 @@ impl ModuleGraph {
|
|||
ancestors
|
||||
}
|
||||
|
||||
pub fn add_node_if_none(&mut self, path: &Path) {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
if self.0.iter().all(|n| n.id != path) {
|
||||
let node = Node::new(path, (), set! {});
|
||||
pub fn add_node_if_none(&mut self, path: &NormalizedPathBuf) {
|
||||
if self.0.iter().all(|n| &n.id != path) {
|
||||
let node = Node::new(path.clone(), (), set! {});
|
||||
self.0.push(node);
|
||||
}
|
||||
}
|
||||
|
||||
/// returns Err (and do nothing) if this operation makes a cycle
|
||||
pub fn inc_ref(&mut self, referrer: &Path, depends_on: PathBuf) -> Result<(), IncRefError> {
|
||||
pub fn inc_ref(
|
||||
&mut self,
|
||||
referrer: &NormalizedPathBuf,
|
||||
depends_on: NormalizedPathBuf,
|
||||
) -> Result<(), IncRefError> {
|
||||
self.add_node_if_none(referrer);
|
||||
let referrer = NormalizedPathBuf::new(referrer.to_path_buf());
|
||||
let depends_on = NormalizedPathBuf::new(depends_on);
|
||||
if self.ancestors(&depends_on).contains(&referrer) && referrer != depends_on {
|
||||
if self.ancestors(&depends_on).contains(referrer) && referrer != &depends_on {
|
||||
return Err(IncRefError::CycleDetected);
|
||||
}
|
||||
if let Some(node) = self.0.iter_mut().find(|n| n.id == referrer) {
|
||||
if referrer == depends_on {
|
||||
if let Some(node) = self.0.iter_mut().find(|n| &n.id == referrer) {
|
||||
if referrer == &depends_on {
|
||||
return Ok(());
|
||||
}
|
||||
node.push_dep(depends_on);
|
||||
|
@ -139,25 +136,22 @@ impl ModuleGraph {
|
|||
}
|
||||
|
||||
/// Do not erase relationships with modules that depend on `path`
|
||||
pub fn remove(&mut self, path: &Path) {
|
||||
let path = NormalizedPathBuf::new(path.to_path_buf());
|
||||
self.0.retain(|n| n.id != path);
|
||||
pub fn remove(&mut self, path: &NormalizedPathBuf) {
|
||||
self.0.retain(|n| &n.id != path);
|
||||
for node in self.0.iter_mut() {
|
||||
node.depends_on.retain(|p| *p != path);
|
||||
node.depends_on.retain(|p| p != path);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rename_path(&mut self, old: &Path, new: PathBuf) {
|
||||
let old = NormalizedPathBuf::new(old.to_path_buf());
|
||||
let new = NormalizedPathBuf::new(new);
|
||||
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
for node in self.0.iter_mut() {
|
||||
if node.id == old {
|
||||
if &node.id == old {
|
||||
node.id = new.clone();
|
||||
}
|
||||
if node.depends_on.contains(&old) {
|
||||
if node.depends_on.contains(old) {
|
||||
node.depends_on.insert(new.clone());
|
||||
}
|
||||
node.depends_on.retain(|p| *p != old);
|
||||
node.depends_on.retain(|p| p != old);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,7 +185,7 @@ impl SharedModuleGraph {
|
|||
|
||||
pub fn get_node(
|
||||
&self,
|
||||
path: &Path,
|
||||
path: &NormalizedPathBuf,
|
||||
) -> Option<MappedRwLockReadGuard<Node<NormalizedPathBuf, ()>>> {
|
||||
if self.0.borrow().get_node(path).is_some() {
|
||||
Some(RwLockReadGuard::map(self.0.borrow(), |graph| {
|
||||
|
@ -202,23 +196,27 @@ impl SharedModuleGraph {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn depends_on(&self, path: &Path, target: &Path) -> bool {
|
||||
pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool {
|
||||
self.0.borrow().depends_on(path, target)
|
||||
}
|
||||
|
||||
pub fn children(&self, path: &Path) -> Set<NormalizedPathBuf> {
|
||||
pub fn children(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
|
||||
self.0.borrow().children(path)
|
||||
}
|
||||
|
||||
pub fn ancestors(&self, path: &Path) -> Set<NormalizedPathBuf> {
|
||||
pub fn ancestors(&self, path: &NormalizedPathBuf) -> Set<NormalizedPathBuf> {
|
||||
self.0.borrow().ancestors(path)
|
||||
}
|
||||
|
||||
pub fn add_node_if_none(&self, path: &Path) {
|
||||
pub fn add_node_if_none(&self, path: &NormalizedPathBuf) {
|
||||
self.0.borrow_mut().add_node_if_none(path);
|
||||
}
|
||||
|
||||
pub fn inc_ref(&self, referrer: &Path, depends_on: PathBuf) -> Result<(), IncRefError> {
|
||||
pub fn inc_ref(
|
||||
&self,
|
||||
referrer: &NormalizedPathBuf,
|
||||
depends_on: NormalizedPathBuf,
|
||||
) -> Result<(), IncRefError> {
|
||||
self.0.borrow_mut().inc_ref(referrer, depends_on)
|
||||
}
|
||||
|
||||
|
@ -226,11 +224,11 @@ impl SharedModuleGraph {
|
|||
self.0.borrow()
|
||||
}
|
||||
|
||||
pub fn remove(&self, path: &Path) {
|
||||
pub fn remove(&self, path: &NormalizedPathBuf) {
|
||||
self.0.borrow_mut().remove(path);
|
||||
}
|
||||
|
||||
pub fn rename_path(&self, old: &Path, new: PathBuf) {
|
||||
pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
self.0.borrow_mut().rename_path(old, new);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::collections::hash_map::{Iter, Keys, Values};
|
||||
use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use erg_common::dict::Dict;
|
||||
use erg_common::pathutil::NormalizedPathBuf;
|
||||
|
@ -103,7 +102,7 @@ impl ModuleIndex {
|
|||
self.members.clear();
|
||||
}
|
||||
|
||||
pub fn remove_path(&mut self, path: &Path) {
|
||||
pub fn remove_path(&mut self, path: &NormalizedPathBuf) {
|
||||
self.members.retain(|loc, value| {
|
||||
value
|
||||
.referrers
|
||||
|
@ -112,8 +111,7 @@ impl ModuleIndex {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn rename_path(&mut self, old: &Path, new: PathBuf) {
|
||||
let new = NormalizedPathBuf::new(new);
|
||||
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
let mut new_members = Dict::new();
|
||||
for (loc, mut value) in std::mem::take(&mut self.members) {
|
||||
if value.vi.def_loc.module.as_deref() == Some(old) {
|
||||
|
@ -190,11 +188,11 @@ impl SharedModuleIndex {
|
|||
self.0.borrow_mut().initialize();
|
||||
}
|
||||
|
||||
pub fn remove_path(&self, path: &Path) {
|
||||
pub fn remove_path(&self, path: &NormalizedPathBuf) {
|
||||
self.0.borrow_mut().remove_path(path);
|
||||
}
|
||||
|
||||
pub fn rename_path(&self, old: &Path, new: PathBuf) {
|
||||
pub fn rename_path(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
self.0.borrow_mut().rename_path(old, new);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use std::fmt;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::thread::{current, JoinHandle, ThreadId};
|
||||
|
||||
use erg_common::consts::DEBUG_MODE;
|
||||
|
@ -92,17 +91,17 @@ impl fmt::Display for SharedPromises {
|
|||
}
|
||||
|
||||
impl SharedPromises {
|
||||
pub fn new(graph: SharedModuleGraph, path: PathBuf) -> Self {
|
||||
pub fn new(graph: SharedModuleGraph, path: NormalizedPathBuf) -> Self {
|
||||
Self {
|
||||
graph,
|
||||
path: NormalizedPathBuf::new(path),
|
||||
path,
|
||||
promises: Shared::new(Dict::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert<P: Into<NormalizedPathBuf>>(&self, path: P, handle: JoinHandle<()>) {
|
||||
pub fn insert(&self, path: impl Into<NormalizedPathBuf>, handle: JoinHandle<()>) {
|
||||
let path = path.into();
|
||||
if self.promises.borrow().get(&path).is_some() {
|
||||
if self.is_registered(&path) {
|
||||
if DEBUG_MODE {
|
||||
panic!("already registered: {}", path.display());
|
||||
}
|
||||
|
@ -113,40 +112,40 @@ impl SharedPromises {
|
|||
.insert(path, Promise::running(handle));
|
||||
}
|
||||
|
||||
pub fn remove(&self, path: &Path) {
|
||||
self.promises.borrow_mut().remove(path);
|
||||
pub fn remove(&self, path: &NormalizedPathBuf) -> Option<Promise> {
|
||||
self.promises.borrow_mut().remove(path)
|
||||
}
|
||||
|
||||
pub fn initialize(&self) {
|
||||
self.promises.borrow_mut().clear();
|
||||
}
|
||||
|
||||
pub fn rename(&self, old: &Path, new: PathBuf) {
|
||||
let Some(promise) = self.promises.borrow_mut().remove(old) else {
|
||||
pub fn rename(&self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
let Some(promise) = self.remove(old) else {
|
||||
return;
|
||||
};
|
||||
self.promises.borrow_mut().insert(new.into(), promise);
|
||||
self.promises.borrow_mut().insert(new, promise);
|
||||
}
|
||||
|
||||
pub fn is_registered(&self, path: &Path) -> bool {
|
||||
pub fn is_registered(&self, path: &NormalizedPathBuf) -> bool {
|
||||
self.promises.borrow().get(path).is_some()
|
||||
}
|
||||
|
||||
pub fn is_joined(&self, path: &Path) -> bool {
|
||||
pub fn is_joined(&self, path: &NormalizedPathBuf) -> bool {
|
||||
self.promises
|
||||
.borrow()
|
||||
.get(path)
|
||||
.is_some_and(|promise| promise.is_joined())
|
||||
}
|
||||
|
||||
pub fn can_be_joined(&self, path: &Path) -> bool {
|
||||
pub fn can_be_joined(&self, path: &NormalizedPathBuf) -> bool {
|
||||
self.promises
|
||||
.borrow()
|
||||
.get(path)
|
||||
.is_some_and(|promise| promise.is_finished())
|
||||
}
|
||||
|
||||
fn join_checked(&self, path: &Path, promise: Promise) -> std::thread::Result<()> {
|
||||
fn join_checked(&self, path: &NormalizedPathBuf, promise: Promise) -> std::thread::Result<()> {
|
||||
let Promise::Running { handle, parent } = promise else {
|
||||
*self.promises.borrow_mut().get_mut(path).unwrap() = promise;
|
||||
return Ok(());
|
||||
|
@ -184,7 +183,7 @@ impl SharedPromises {
|
|||
res
|
||||
}
|
||||
|
||||
pub fn join(&self, path: &Path) -> std::thread::Result<()> {
|
||||
pub fn join(&self, path: &NormalizedPathBuf) -> std::thread::Result<()> {
|
||||
while let Some(Promise::Joining) | None = self.promises.borrow().get(path) {
|
||||
safe_yield();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue