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

Add support for sorting the output of the generate tree command #218

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ Options:
--traits Include traits (e.g. trait, unsafe trait)
--no-traits Exclude traits (e.g. trait, unsafe trait). [default]
--fns Include functions (e.g. fns, async fns, const fns)
--no-fns Include functions (e.g. fns, async fns, const fns). [default]
--no-fns Exclude functions (e.g. fns, async fns, const fns). [default]
--tests Include tests (e.g. `#[test] fn …`)
--no-tests Exclude tests (e.g. `#[test] fn …`). [default]
--sort-by <SORT_BY> The sorting order to use (e.g. name, visibility, kind) [default: name]
--sort-reversed Reverses the sorting order
--orphans Include orphaned modules (i.e. unused files in /src)
--no-orphans Exclude orphaned modules (i.e. unused files in /src). [default]
-h, --help Print help
Expand Down
5 changes: 4 additions & 1 deletion src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ impl Command {
uses: false,
externs: false,
};
let printer_options = TreePrinterOptions {};
let printer_options = TreePrinterOptions {
sort_by: options.sort_by,
sort_reversed: options.sort_reversed,
};

let command =
GenerateTreeCommand::new(builder_options, filter_options, printer_options);
Expand Down
6 changes: 2 additions & 4 deletions src/graph/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use ra_ap_ide_db::RootDatabase;

use crate::item::Item;

#[derive(Clone, PartialEq, Debug)]
Expand All @@ -20,7 +18,7 @@ impl Node {
self.item.display_path()
}

pub fn kind_display_name(&self, db: &RootDatabase) -> Option<String> {
self.item.kind_display_name(db)
pub fn kind_display_name(&self) -> Option<String> {
self.item.kind_display_name()
}
}
15 changes: 8 additions & 7 deletions src/graph/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ impl FromStr for LayoutAlgorithm {
impl ToString for LayoutAlgorithm {
fn to_string(&self) -> String {
match self {
Self::None => "none".to_owned(),
Self::Dot => "dot".to_owned(),
Self::Neato => "neato".to_owned(),
Self::Twopi => "twopi".to_owned(),
Self::Circo => "circo".to_owned(),
Self::Fdp => "fdp".to_owned(),
Self::Sfdp => "sfdp".to_owned(),
Self::None => "none",
Self::Dot => "dot",
Self::Neato => "neato",
Self::Twopi => "twopi",
Self::Circo => "circo",
Self::Fdp => "fdp",
Self::Sfdp => "sfdp",
}
.to_owned()
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/graph/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> Printer<'a> {

let id = node.item.path.join("::");
let kind = node
.kind_display_name(self.db)
.kind_display_name()
.unwrap_or_else(|| "orphan".to_owned());

let label = self.node_label(node).unwrap();
Expand Down Expand Up @@ -215,9 +215,7 @@ impl<'a> Printer<'a> {
None => Some("orphan".to_owned()),
};

let kind = node
.kind_display_name(self.db)
.unwrap_or_else(|| "mod".to_owned());
let kind = node.kind_display_name().unwrap_or_else(|| "mod".to_owned());

if let Some(visibility) = visibility {
write!(f, "{visibility} ")?;
Expand Down
59 changes: 7 additions & 52 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use std::path::PathBuf;

use hir::ModuleDef;
use ra_ap_hir::{self as hir};
use ra_ap_ide_db::RootDatabase;
use ra_ap_vfs::Vfs;
Expand All @@ -14,6 +13,7 @@ use crate::graph::util;
use self::{attr::ItemAttrs, visibility::ItemVisibility};

pub(crate) mod attr;
pub(crate) mod kind;
pub(crate) mod visibility;

#[derive(Clone, PartialEq, Debug)]
Expand All @@ -24,6 +24,7 @@ pub struct Item {
pub hir: Option<hir::ModuleDef>,
pub visibility: Option<visibility::ItemVisibility>,
pub attrs: attr::ItemAttrs,
pub kind: Option<kind::ItemKind>,
}

impl Item {
Expand Down Expand Up @@ -64,13 +65,16 @@ impl Item {
ItemAttrs { cfgs, test }
};

let kind = hir.map(|hir| kind::ItemKind::new(hir, db));

Self {
crate_name,
path,
file_path,
hir,
visibility,
attrs,
kind,
}
}

Expand All @@ -85,57 +89,8 @@ impl Item {
self.path.join("::")
}

pub fn kind_display_name(&self, db: &RootDatabase) -> Option<String> {
let Some(module_def) = self.hir else {
return None;
};

match module_def {
ModuleDef::Module(module_def) => {
if module_def.is_crate_root() {
Some("crate".to_owned())
} else {
Some("mod".to_owned())
}
}
ModuleDef::Function(function_def) => {
let mut keywords = vec![];

if function_def.is_const(db) {
keywords.push("const");
}
if function_def.is_async(db) {
keywords.push("async");
}
if function_def.is_unsafe_to_call(db) {
keywords.push("unsafe");
}

keywords.push("fn");

Some(keywords.join(" "))
}
ModuleDef::Adt(adt_def) => match adt_def {
hir::Adt::Struct(_) => Some("struct".to_owned()),
hir::Adt::Union(_) => Some("union".to_owned()),
hir::Adt::Enum(_) => Some("enum".to_owned()),
},
ModuleDef::Variant(_) => Some("variant".to_owned()),
ModuleDef::Const(_) => Some("const".to_owned()),
ModuleDef::Static(_) => Some("static".to_owned()),
ModuleDef::Trait(trait_def) => {
let mut keywords = vec![];
if trait_def.is_unsafe(db) {
keywords.push("unsafe");
}
keywords.push("trait");
Some(keywords.join(" "))
}
ModuleDef::TraitAlias(_) => Some("trait".to_owned()),
ModuleDef::TypeAlias(_) => Some("type".to_owned()),
ModuleDef::BuiltinType(_) => Some("builtin".to_owned()),
ModuleDef::Macro(_) => Some("macro".to_owned()),
}
pub fn kind_display_name(&self) -> Option<String> {
self.kind.as_ref().map(|kind| kind.to_string())
}

pub(crate) fn is_crate(&self, _db: &RootDatabase) -> bool {
Expand Down
Loading
Loading