Skip to content

Commit

Permalink
Refactor orphan detection and restrict --orphans/--no-orphans to `gen…
Browse files Browse the repository at this point in the history
…erate tree` command
  • Loading branch information
regexident committed Oct 8, 2023
1 parent 478fe29 commit 6fa586f
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 310 deletions.
6 changes: 2 additions & 4 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Command {
#[allow(unused_variables)]
Self::Tree(options) => {
let builder_options = TreeBuilderOptions {
orphans: options.selection.orphans,
orphans: options.orphans,
};
let filter_options = TreeFilterOptions {
focus_on: options.selection.focus_on.clone(),
Expand All @@ -124,9 +124,7 @@ impl Command {
}
#[allow(unused_variables)]
Self::Graph(options) => {
let builder_options = GraphBuilderOptions {
orphans: options.selection.orphans,
};
let builder_options = GraphBuilderOptions {};
let filter_options = GraphFilterOptions {
focus_on: options.selection.focus_on.clone(),
max_depth: options.selection.max_depth,
Expand Down
1 change: 0 additions & 1 deletion src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub(crate) mod edge;
pub(super) mod filter;
pub(crate) mod node;
pub(crate) mod options;
pub(super) mod orphans;
pub(super) mod printer;
pub(crate) mod util;
pub(super) mod walker;
Expand Down
10 changes: 1 addition & 9 deletions src/graph/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ use crate::{
item::Item,
};

use super::orphans::add_orphan_nodes_to;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Options {
pub orphans: bool,
}
pub struct Options {}

#[derive(Debug)]
pub struct Builder<'a> {
Expand Down Expand Up @@ -119,10 +115,6 @@ impl<'a> Builder<'a> {
self.add_edge(module_idx, declaration_idx, edge);
}

if self.options.orphans {
add_orphan_nodes_to(&mut self.graph, module_idx);
}

self.add_dependencies(module_idx, self.dependencies_of_module(module_hir));

Some(module_idx)
Expand Down
155 changes: 0 additions & 155 deletions src/graph/orphans.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,8 @@ impl Item {

module.is_crate_root()
}

pub(crate) fn is_file(&self) -> bool {
self.file_path.is_some()
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod options;

pub(crate) mod graph;
pub(crate) mod item;
pub(crate) mod orphans;
pub(crate) mod target;
pub(crate) mod theme;
pub(crate) mod tree;
8 changes: 0 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ pub mod selection {
/// Exclude tests (e.g. `#[test] fn …`). [default]
#[arg(long = "no-tests", action = ArgAction::SetFalse, overrides_with = "tests")]
pub no_tests: (),

/// Include orphaned modules (i.e. unused files in /src).
#[arg(long = "orphans")]
pub orphans: bool,

/// Exclude orphaned modules (i.e. unused files in /src). [default]
#[arg(long = "no-orphans", action = ArgAction::SetFalse, overrides_with = "orphans")]
pub no_orphans: (),
}
}

Expand Down
80 changes: 0 additions & 80 deletions src/orphans.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub(super) mod command;
pub(super) mod filter;
pub(crate) mod node;
pub(super) mod options;
pub(super) mod orphans;
pub(crate) mod printer;

#[derive(Clone, PartialEq, Debug)]
Expand Down
22 changes: 15 additions & 7 deletions src/tree/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use hir::ModuleDef;
use log::trace;

use ra_ap_hir::{self as hir, Crate};
use ra_ap_ide_db::RootDatabase;
use ra_ap_vfs::Vfs;
Expand All @@ -14,6 +13,8 @@ use crate::{
tree::{node::Node, Tree},
};

use super::orphans::orphan_nodes_for;

// use super::orphans::add_orphan_nodes_to;

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -80,16 +81,23 @@ impl<'a> Builder<'a> {
let item = Item::new(ModuleDef::Module(module_hir), self.db, self.vfs);
let mut node = Node::new(item, vec![]);

for declaration in module_hir.declarations(self.db) {
let Some(subnode) = self.process_moduledef(declaration) else {
continue;
};
// eprintln!("node: {:?}", node.item.path);

let subnodes = module_hir
.declarations(self.db)
.into_iter()
.filter_map(|moduledef_hir| self.process_moduledef(moduledef_hir));

for subnode in subnodes {
// eprintln!("- subnode: {:?}", subnode.item.path);
node.push_subnode(subnode);
}

if self.options.orphans {
// add_orphan_nodes_to(&mut self.graph, module_idx);
if self.options.orphans && node.item.is_file() {
for subnode in orphan_nodes_for(&node) {
// eprintln!("- orphan: {:?}", subnode.item.path);
node.push_subnode(subnode);
}
}

Some(node)
Expand Down
Loading

0 comments on commit 6fa586f

Please sign in to comment.