Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

red-knot: improve internal documentation in module.rs #11638

Merged
merged 3 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions crates/red_knot/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,40 @@ use crate::FxDashMap;
pub struct Module(u32);

impl Module {
/// Return the absolute name of the module (e.g. `foo.bar`)
pub fn name(&self, db: &dyn SemanticDb) -> QueryResult<ModuleName> {
let jar: &SemanticJar = db.jar()?;
let modules = &jar.module_resolver;

Ok(modules.modules.get(self).unwrap().name.clone())
}

/// Return the path to the source code that defines this module
pub fn path(&self, db: &dyn SemanticDb) -> QueryResult<ModulePath> {
let jar: &SemanticJar = db.jar()?;
let modules = &jar.module_resolver;

Ok(modules.modules.get(self).unwrap().path.clone())
}

/// Determine whether this module is a single-file module or a package
pub fn kind(&self, db: &dyn SemanticDb) -> QueryResult<ModuleKind> {
let jar: &SemanticJar = db.jar()?;
let modules = &jar.module_resolver;

Ok(modules.modules.get(self).unwrap().kind)
}

/// Attempt to resolve a dependency of this module to an absolute [`ModuleName`].
///
/// A dependency could be either absolute (e.g. the `foo` dependency implied by `from foo import bar`)
/// or relative to this module (e.g. the `.foo` dependency implied by `from .foo import bar`)
///
/// - Returns an error if the query failed.
/// - Returns `Ok(None)` if the query succeeded,
/// but the dependency refers to a module that does not exist.
/// - Returns `Ok(Some(ModuleName))` if the query succeeded,
/// and the dependency refers to a module that exists.
pub fn resolve_dependency(
&self,
db: &dyn SemanticDb,
Expand Down Expand Up @@ -124,10 +137,13 @@ impl ModuleName {
Some(Self(name))
}

/// An iterator over the components of the module name:
/// `foo.bar.baz` -> `foo`, `bar`, `baz`
pub fn components(&self) -> impl DoubleEndedIterator<Item = &str> {
self.0.split('.')
}

/// The name of this module's immediate parent, if it has a parent
pub fn parent(&self) -> Option<ModuleName> {
let (_, parent) = self.0.rsplit_once('.')?;

Expand Down Expand Up @@ -159,9 +175,10 @@ impl std::fmt::Display for ModuleName {

#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ModuleKind {
/// A single-file module (e.g. `foo.py` or `foo.pyi`)
Module,

/// A python package (a `__init__.py` or `__init__.pyi` file)
/// A python package (`foo/__init__.py` or `foo/__init__.pyi`)
Package,
}

Expand All @@ -181,10 +198,12 @@ impl ModuleSearchPath {
}
}

/// Determine whether this is a first-party, third-party or standard-library search path
pub fn kind(&self) -> ModuleSearchPathKind {
self.inner.kind
}

/// Return the location of the search path on the file system
pub fn path(&self) -> &Path {
&self.inner.path
}
Expand Down Expand Up @@ -459,6 +478,7 @@ impl ModuleResolver {
}
}

/// Remove a module from the inner cache
pub(crate) fn remove_module(&mut self, file_id: FileId) {
// No locking is required because we're holding a mutable reference to `self`.
let Some((_, id)) = self.by_file.remove(&file_id) else {
Expand Down Expand Up @@ -505,15 +525,19 @@ impl ModulePath {
Self { root, file_id }
}

/// The search path that was used to locate the module
pub fn root(&self) -> &ModuleSearchPath {
&self.root
}

/// The file containing the source code for the module
pub fn file(&self) -> FileId {
self.file_id
}
}

/// Given a module name and a list of search paths in which to lookup modules,
/// attempt to resolve the module name
fn resolve_name(
name: &ModuleName,
search_paths: &[ModuleSearchPath],
Expand Down Expand Up @@ -635,7 +659,9 @@ enum PackageKind {
/// A root package or module. E.g. `foo` in `foo.bar.baz` or just `foo`.
Root,

/// A regular sub-package where the parent contains an `__init__.py`. For example `bar` in `foo.bar` when the `foo` directory contains an `__init__.py`.
/// A regular sub-package where the parent contains an `__init__.py`.
///
/// For example, `bar` in `foo.bar` when the `foo` directory contains an `__init__.py`.
Regular,

/// A sub-package in a namespace package. A namespace package is a package without an `__init__.py`.
Expand Down
Loading