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

Split ItemKind into individual task-specific types #242

Merged
merged 2 commits into from
Dec 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
2 changes: 1 addition & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down
2 changes: 1 addition & 1 deletion src/command/dependencies/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ra_ap_ide::RootDatabase;
use crate::{
analyzer,
graph::{Edge, EdgeKind, Graph, Node},
item::visibility::ItemVisibility,
item::ItemVisibility,
};

use super::{
Expand Down
6 changes: 3 additions & 3 deletions src/command/structure/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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));
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ use ra_ap_ide_db::RootDatabase;

use crate::analyzer;

use self::{attr::ItemAttrs, visibility::ItemVisibility};

pub(crate) mod attr;
pub(crate) mod kind;
pub(crate) mod visibility;
pub(crate) use self::{
attr::{ItemAttrs, ItemCfgAttr, ItemTestAttr},
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 {
Expand All @@ -28,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) -> kind::ItemKind {
kind::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 {
Expand All @@ -44,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()
}
}
9 changes: 9 additions & 0 deletions src/item/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
71 changes: 71 additions & 0 deletions src/item/kind_display_name.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
95 changes: 22 additions & 73 deletions src/item/kind.rs → src/item/kind_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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(),
},
Expand Down Expand Up @@ -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<Ordering> {
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());

Expand Down Expand Up @@ -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"),
}
}
}
Loading