From 471dc7dfbcc9a529e5b53ee2920bcfa69a14d150 Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 13 Dec 2023 15:39:01 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Re-export=20item-specific=20helper=20types?= =?UTF-8?q?=20from=20`crate::item::=E2=80=A6`,=20instead=20of=20their=20su?= =?UTF-8?q?b-modules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/analyzer.rs | 2 +- src/command/dependencies/printer.rs | 2 +- src/command/structure/printer.rs | 2 +- src/item.rs | 16 ++++++++++------ 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/analyzer.rs b/src/analyzer.rs index 2fbc095d..723688e2 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -22,7 +22,7 @@ use ra_ap_syntax::{ast, AstNode, SourceFile}; use ra_ap_vfs::Vfs; use crate::{ - item::attr::{ItemCfgAttr, ItemTestAttr}, + item::{ItemCfgAttr, ItemTestAttr}, options::{general::Options as GeneralOptions, project::Options as ProjectOptions}, }; diff --git a/src/command/dependencies/printer.rs b/src/command/dependencies/printer.rs index 31f1efd8..e79d8a4b 100644 --- a/src/command/dependencies/printer.rs +++ b/src/command/dependencies/printer.rs @@ -16,7 +16,7 @@ use ra_ap_ide::RootDatabase; use crate::{ analyzer, graph::{Edge, EdgeKind, Graph, Node}, - item::visibility::ItemVisibility, + item::ItemVisibility, }; use super::{ diff --git a/src/command/structure/printer.rs b/src/command/structure/printer.rs index f5fde3f8..3c010dd8 100644 --- a/src/command/structure/printer.rs +++ b/src/command/structure/printer.rs @@ -8,7 +8,7 @@ use std::fmt; use ra_ap_ide::RootDatabase; -use crate::{analyzer, item::visibility::ItemVisibility, tree::Tree}; +use crate::{analyzer, item::ItemVisibility, tree::Tree}; use super::{ options::{Options, SortBy}, diff --git a/src/item.rs b/src/item.rs index 9b47e96b..ea410bee 100644 --- a/src/item.rs +++ b/src/item.rs @@ -7,11 +7,15 @@ use ra_ap_ide_db::RootDatabase; use crate::analyzer; -use self::{attr::ItemAttrs, visibility::ItemVisibility}; +mod attr; +mod kind; +mod visibility; -pub(crate) mod attr; -pub(crate) mod kind; -pub(crate) mod visibility; +pub(crate) use self::{ + attr::{ItemAttrs, ItemCfgAttr, ItemTestAttr}, + kind::ItemKind, + visibility::ItemVisibility, +}; #[derive(Clone, PartialEq, Debug)] pub struct Item { @@ -33,8 +37,8 @@ impl Item { ItemAttrs { cfgs, test } } - pub fn kind(&self, db: &RootDatabase) -> kind::ItemKind { - kind::ItemKind::new(self.hir, db) + pub fn kind(&self, db: &RootDatabase) -> ItemKind { + ItemKind::new(self.hir, db) } pub fn display_name(&self, db: &RootDatabase) -> String { From cf8f26be872db19a8dfdaf634c36569791627994 Mon Sep 17 00:00:00 2001 From: Vincent Esche Date: Wed, 13 Dec 2023 16:50:48 +0100 Subject: [PATCH 2/2] Re-structure `ItemKind` into individual helper types for item ordering, display name and discriminant --- src/command/structure/printer.rs | 4 +- src/item.rs | 28 ++++---- src/item/attr.rs | 9 +++ src/item/kind_display_name.rs | 71 +++++++++++++++++++ src/item/{kind.rs => kind_ordering.rs} | 95 ++++++-------------------- 5 files changed, 118 insertions(+), 89 deletions(-) create mode 100644 src/item/kind_display_name.rs rename src/item/{kind.rs => kind_ordering.rs} (67%) diff --git a/src/command/structure/printer.rs b/src/command/structure/printer.rs index 3c010dd8..77bb31da 100644 --- a/src/command/structure/printer.rs +++ b/src/command/structure/printer.rs @@ -60,10 +60,10 @@ impl<'a> Printer<'a> { subtrees.sort_by_cached_key(|tree| tree.item.display_name(self.db)); } SortBy::Visibility => { - subtrees.sort_by_cached_key(|tree| tree.item.visibility(self.db).clone()); + subtrees.sort_by_cached_key(|tree| tree.item.visibility(self.db)); } SortBy::Kind => { - subtrees.sort_by_cached_key(|tree| tree.item.kind(self.db).clone()); + subtrees.sort_by_cached_key(|tree| tree.item.kind_ordering(self.db)); } } diff --git a/src/item.rs b/src/item.rs index ea410bee..22a1c32e 100644 --- a/src/item.rs +++ b/src/item.rs @@ -7,16 +7,18 @@ use ra_ap_ide_db::RootDatabase; use crate::analyzer; -mod attr; -mod kind; -mod visibility; - pub(crate) use self::{ attr::{ItemAttrs, ItemCfgAttr, ItemTestAttr}, - kind::ItemKind, + kind_display_name::ItemKindDisplayName, + kind_ordering::ItemKindOrdering, visibility::ItemVisibility, }; +mod attr; +mod kind_display_name; +mod kind_ordering; +mod visibility; + #[derive(Clone, PartialEq, Debug)] pub struct Item { pub hir: hir::ModuleDef, @@ -32,13 +34,15 @@ impl Item { } pub fn attrs(&self, db: &RootDatabase) -> ItemAttrs { - let cfgs: Vec<_> = analyzer::cfg_attrs(self.hir, db); - let test = analyzer::test_attr(self.hir, db); - ItemAttrs { cfgs, test } + ItemAttrs::new(self, db) + } + + pub fn kind_ordering(&self, db: &RootDatabase) -> ItemKindOrdering { + ItemKindOrdering::new(self, db) } - pub fn kind(&self, db: &RootDatabase) -> ItemKind { - ItemKind::new(self.hir, db) + pub fn kind_display_name(&self, db: &RootDatabase) -> ItemKindDisplayName { + ItemKindDisplayName::new(self, db) } pub fn display_name(&self, db: &RootDatabase) -> String { @@ -48,8 +52,4 @@ impl Item { pub fn display_path(&self, db: &RootDatabase) -> String { analyzer::display_path(self.hir, db) } - - pub fn kind_display_name(&self, db: &RootDatabase) -> String { - self.kind(db).to_string() - } } diff --git a/src/item/attr.rs b/src/item/attr.rs index 970761f2..d621bca0 100644 --- a/src/item/attr.rs +++ b/src/item/attr.rs @@ -5,6 +5,9 @@ use std::fmt; use ra_ap_cfg::{CfgAtom, CfgExpr}; +use ra_ap_ide_db::RootDatabase; + +use crate::{analyzer, item::Item}; #[derive(Clone, PartialEq, Debug)] pub enum ItemCfgAttr { @@ -88,6 +91,12 @@ pub struct ItemAttrs { } impl ItemAttrs { + pub fn new(item: &Item, db: &RootDatabase) -> ItemAttrs { + let cfgs: Vec<_> = analyzer::cfg_attrs(item.hir, db); + let test = analyzer::test_attr(item.hir, db); + Self { cfgs, test } + } + pub fn is_empty(&self) -> bool { self.test.is_none() && self.cfgs.is_empty() } diff --git a/src/item/kind_display_name.rs b/src/item/kind_display_name.rs new file mode 100644 index 00000000..0351dba2 --- /dev/null +++ b/src/item/kind_display_name.rs @@ -0,0 +1,71 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// 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 core::fmt; + +use ra_ap_hir::{self as hir}; +use ra_ap_ide_db::RootDatabase; + +use super::Item; + +#[derive(Clone, Eq, PartialEq)] +pub struct ItemKindDisplayName(String); + +impl ItemKindDisplayName { + pub fn new(item: &Item, db: &RootDatabase) -> Self { + Self(match item.hir { + hir::ModuleDef::Module(hir) => { + if hir.is_crate_root() { + "crate".to_owned() + } else { + "mod".to_owned() + } + } + hir::ModuleDef::Function(hir) => { + let mut keywords = vec![]; + if hir.is_const(db) { + keywords.push("const"); + } + if hir.is_async(db) { + keywords.push("async"); + } + if hir.is_unsafe_to_call(db) { + keywords.push("unsafe"); + } + keywords.push("fn"); + keywords.join(" ") + } + hir::ModuleDef::Adt(hir::Adt::Struct(_hir)) => "struct".to_owned(), + hir::ModuleDef::Adt(hir::Adt::Union(_hir)) => "union".to_owned(), + hir::ModuleDef::Adt(hir::Adt::Enum(_hir)) => "enum".to_owned(), + hir::ModuleDef::Variant(_hir) => "variant".to_owned(), + hir::ModuleDef::Const(_hir) => "const".to_owned(), + hir::ModuleDef::Static(_hir) => "static".to_owned(), + hir::ModuleDef::Trait(hir) => { + let mut keywords = vec![]; + if hir.is_unsafe(db) { + keywords.push("unsafe"); + } + keywords.push("trait"); + keywords.join(" ") + } + hir::ModuleDef::TraitAlias(_hir) => "trait".to_owned(), + hir::ModuleDef::TypeAlias(_hir) => "type".to_owned(), + hir::ModuleDef::BuiltinType(_hir) => "builtin".to_owned(), + hir::ModuleDef::Macro(_hir) => "macro".to_owned(), + }) + } +} + +impl fmt::Display for ItemKindDisplayName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +impl fmt::Debug for ItemKindDisplayName { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.0) + } +} diff --git a/src/item/kind.rs b/src/item/kind_ordering.rs similarity index 67% rename from src/item/kind.rs rename to src/item/kind_ordering.rs index 4fc3c489..94466c9c 100644 --- a/src/item/kind.rs +++ b/src/item/kind_ordering.rs @@ -2,13 +2,15 @@ // 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 std::{cmp::Ordering, fmt}; +use std::cmp::Ordering; use ra_ap_hir::{self as hir, ModuleDef}; use ra_ap_ide_db::RootDatabase; +use super::Item; + #[derive(Clone, PartialEq, Eq, Debug)] -pub enum ItemKind { +pub(crate) enum ItemKindOrdering { Module { is_crate_root: bool, }, @@ -32,9 +34,9 @@ pub enum ItemKind { Macro, } -impl ItemKind { - pub fn new(hir: hir::ModuleDef, db: &RootDatabase) -> Self { - match hir { +impl ItemKindOrdering { + pub fn new(item: &Item, db: &RootDatabase) -> Self { + match item.hir { ModuleDef::Module(module_def_hir) => Self::Module { is_crate_root: module_def_hir.is_crate_root(), }, @@ -63,30 +65,30 @@ impl ItemKind { fn numerical_order(&self) -> isize { match self { - ItemKind::Module { .. } => 0, - ItemKind::Trait { .. } => 1, - ItemKind::TraitAlias => 1, - ItemKind::TypeAlias => 2, - ItemKind::Struct => 3, - ItemKind::Enum => 4, - ItemKind::Variant => 5, - ItemKind::Union => 6, - ItemKind::BuiltinType => 7, - ItemKind::Function { .. } => 8, - ItemKind::Const => 9, - ItemKind::Static => 10, - ItemKind::Macro => 11, + Self::Module { .. } => 0, + Self::Trait { .. } => 1, + Self::TraitAlias => 1, + Self::TypeAlias => 2, + Self::Struct => 3, + Self::Enum => 4, + Self::Variant => 5, + Self::Union => 6, + Self::BuiltinType => 7, + Self::Function { .. } => 8, + Self::Const => 9, + Self::Static => 10, + Self::Macro => 11, } } } -impl PartialOrd for ItemKind { +impl PartialOrd for ItemKindOrdering { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for ItemKind { +impl Ord for ItemKindOrdering { fn cmp(&self, other: &Self) -> Ordering { let ord = self.numerical_order().cmp(&other.numerical_order()); @@ -174,56 +176,3 @@ impl Ord for ItemKind { } } } - -impl fmt::Display for ItemKind { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Module { is_crate_root } => { - if *is_crate_root { - write!(f, "crate") - } else { - write!(f, "mod") - } - } - Self::Function { - is_const, - is_async, - is_unsafe_to_call, - } => { - let mut keywords = vec![]; - - if *is_const { - keywords.push("const"); - } - if *is_async { - keywords.push("async"); - } - if *is_unsafe_to_call { - keywords.push("unsafe"); - } - - keywords.push("fn"); - - write!(f, "{}", keywords.join(" ")) - } - Self::Struct => write!(f, "struct"), - Self::Union => write!(f, "union"), - Self::Enum => write!(f, "enum"), - Self::Variant => write!(f, "variant"), - Self::Const => write!(f, "const"), - Self::Static => write!(f, "static"), - Self::Trait { is_unsafe } => { - let mut keywords = vec![]; - if *is_unsafe { - keywords.push("unsafe"); - } - keywords.push("trait"); - write!(f, "{}", keywords.join(" ")) - } - Self::TraitAlias => write!(f, "trait"), - Self::TypeAlias => write!(f, "type"), - Self::BuiltinType => write!(f, "builtin"), - Self::Macro => write!(f, "macro"), - } - } -}