Skip to content

Commit

Permalink
chore: project_root_of -> project_root_dir_of
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Feb 1, 2024
1 parent c75dc9c commit 6018d0f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
11 changes: 4 additions & 7 deletions crates/els/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::time::Duration;

use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::pathutil::{project_entry_file_of, project_root_dir_of};
use erg_common::spawn::{safe_yield, spawn_new_thread};
use erg_common::style::*;
use erg_common::traits::Stream;
Expand All @@ -31,7 +32,7 @@ use crate::channels::WorkerMessage;
use crate::diff::{ASTDiff, HIRDiff};
use crate::server::{DefaultFeatures, ELSResult, RedirectableStdout, Server};
use crate::server::{ASK_AUTO_SAVE_ID, HEALTH_CHECKER_ID};
use crate::util::{self, project_root_of, NormalizedUrl};
use crate::util::{self, NormalizedUrl};

#[derive(Debug)]
pub enum BuildASTError {
Expand Down Expand Up @@ -408,19 +409,15 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
"method": "window/workDoneProgress/create",
"params": progress_token,
}));
let Some(project_root) = project_root_of(&current_dir().unwrap()) else {
let Some(project_root) = project_root_dir_of(&current_dir().unwrap()) else {
work_done!(token);
};
let src_dir = if project_root.join("src").is_dir() {
project_root.join("src")
} else {
project_root
};
let main_path = if src_dir.join("main.er").exists() {
src_dir.join("main.er")
} else if src_dir.join("lib.er").exists() {
src_dir.join("lib.er")
} else {
let Some(main_path) = project_entry_file_of(&current_dir().unwrap()) else {
work_done!(token);
};
let Ok(main_uri) = NormalizedUrl::from_file_path(main_path) else {
Expand Down
6 changes: 3 additions & 3 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use erg_common::config::ErgConfig;
use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::env::erg_path;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::pathutil::{project_root_dir_of, NormalizedPathBuf};
use erg_common::shared::{MappedRwLockReadGuard, Shared};
use erg_common::spawn::{safe_yield, spawn_new_thread};
use erg_common::traits::Stream;
Expand Down Expand Up @@ -61,7 +61,7 @@ use crate::file_cache::FileCache;
use crate::hir_visitor::{ExprKind, HIRVisitor};
use crate::message::{ErrorMessage, LSPResult};
use crate::scheduler::Scheduler;
use crate::util::{self, loc_to_pos, project_root_of, NormalizedUrl};
use crate::util::{self, loc_to_pos, NormalizedUrl};

pub const HEALTH_CHECKER_ID: i64 = 10000;
pub const ASK_AUTO_SAVE_ID: i64 = 10001;
Expand Down Expand Up @@ -952,7 +952,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
}

pub(crate) fn get_workspace_ctxs(&self) -> Vec<&Context> {
let project_root = project_root_of(&self.home).unwrap_or(self.home.clone());
let project_root = project_root_dir_of(&self.home).unwrap_or(self.home.clone());
let mut ctxs = vec![];
for (path, ent) in self.shared.raw_path_and_modules() {
if path.starts_with(&project_root) {
Expand Down
14 changes: 0 additions & 14 deletions crates/els/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,3 @@ pub(crate) fn abs_loc_to_lsp_loc(loc: &AbsLocation) -> Option<lsp_types::Locatio
let range = loc_to_range(loc.loc)?;
Some(lsp_types::Location::new(uri, range))
}

pub(crate) fn project_root_of(path: &Path) -> Option<PathBuf> {
if path.is_dir() && path.join("package.er").exists() {
return Some(path.to_path_buf());
}
let mut path = path.to_path_buf();
while let Some(parent) = path.parent() {
if parent.join("package.er").exists() {
return Some(parent.to_path_buf());
}
path = parent.to_path_buf();
}
None
}
29 changes: 28 additions & 1 deletion crates/erg_common/pathutil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{normalize_path, Str};
/// `PathBuf` may give false equivalence decisions in non-case-sensitive file systems.
/// Use this for dictionary keys, etc.
/// See also: `els::util::NormalizedUrl`
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, PartialOrd, Ord)]
pub struct NormalizedPathBuf(PathBuf);

impl fmt::Display for NormalizedPathBuf {
Expand Down Expand Up @@ -254,3 +254,30 @@ pub fn mod_name(path: &Path) -> Str {
}
Str::from(name)
}

pub fn project_root_dir_of(path: &Path) -> Option<PathBuf> {
if path.is_dir() && path.join("package.er").exists() {
return Some(path.to_path_buf());
}
let mut path = path.to_path_buf();
while let Some(parent) = path.parent() {
if parent.join("package.er").exists() {
return Some(parent.to_path_buf());
}
path = parent.to_path_buf();
}
None
}

pub fn project_entry_file_of(path: &Path) -> Option<PathBuf> {
let project_root = project_root_dir_of(path)?;
if project_root.join("src/lib.er").exists() {
Some(project_root.join("src/lib.er"))
} else if project_root.join("src/main.er").exists() {
Some(project_root.join("src/main.er"))
} else if project_root.join("src/lib.d.er").exists() {
Some(project_root.join("src/lib.d.er"))
} else {
None
}
}
4 changes: 4 additions & 0 deletions crates/erg_common/tsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl<T: Eq + Hash, U> Node<T, U> {
self.depends_on.insert(dep);
}

pub fn pop_dep(&mut self, dep: &T) -> bool {
self.depends_on.remove(dep)
}

pub fn depends_on(&self, dep: &T) -> bool {
self.depends_on.contains(dep)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/erg_compiler/module/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ impl ModuleGraph {
depends_on: NormalizedPathBuf,
) -> Result<(), IncRefError> {
self.add_node_if_none(referrer);
if self.ancestors(&depends_on).contains(referrer) && referrer != &depends_on {
if referrer == &depends_on {
return Ok(());
}
if self.ancestors(&depends_on).contains(referrer) {
return Err(IncRefError::CycleDetected);
}
if let Some(node) = self.0.iter_mut().find(|n| &n.id == referrer) {
if referrer == &depends_on {
return Ok(());
}
node.push_dep(depends_on);
} else {
unreachable!("node not found: {}", referrer.display());
Expand Down

0 comments on commit 6018d0f

Please sign in to comment.