mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Slightly reduce code duplication
This commit is contained in:
parent
8e36cb5860
commit
1956d57ed4
3 changed files with 30 additions and 33 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use std::{ops, sync::Arc};
|
use std::{ops, sync::Arc};
|
||||||
|
|
||||||
use hir_expand::{either::Either, hygiene::Hygiene, AstId};
|
use hir_expand::{either::Either, hygiene::Hygiene, AstId, Source};
|
||||||
use mbe::ast_to_token_tree;
|
use mbe::ast_to_token_tree;
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
@ -40,23 +40,19 @@ impl Attrs {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return Attrs::default(),
|
None => return Attrs::default(),
|
||||||
};
|
};
|
||||||
let hygiene = Hygiene::new(db, src.file_id);
|
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
|
||||||
Attr::from_attrs_owner(&src.value, &hygiene)
|
|
||||||
}
|
}
|
||||||
AttrDefId::StructFieldId(it) => {
|
AttrDefId::StructFieldId(it) => {
|
||||||
let src = it.parent.child_source(db);
|
let src = it.parent.child_source(db);
|
||||||
match &src.value[it.local_id] {
|
match &src.value[it.local_id] {
|
||||||
Either::A(_tuple) => Attrs::default(),
|
Either::A(_tuple) => Attrs::default(),
|
||||||
Either::B(record) => {
|
Either::B(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
|
||||||
let hygiene = Hygiene::new(db, src.file_id);
|
|
||||||
Attr::from_attrs_owner(record, &hygiene)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
AttrDefId::EnumVariantId(var_id) => {
|
||||||
AttrDefId::EnumVariantId(it) => {
|
let src = var_id.parent.child_source(db);
|
||||||
let src = it.parent.child_source(db);
|
let src = src.as_ref().map(|it| &it[var_id.local_id]);
|
||||||
let hygiene = Hygiene::new(db, src.file_id);
|
Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner))
|
||||||
Attr::from_attrs_owner(&src.value[it.local_id], &hygiene)
|
|
||||||
}
|
}
|
||||||
AttrDefId::AdtId(it) => match it {
|
AttrDefId::AdtId(it) => match it {
|
||||||
AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
|
AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
|
||||||
|
@ -73,6 +69,22 @@ impl Attrs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn from_attrs_owner(db: &impl DefDatabase, owner: Source<&dyn AttrsOwner>) -> Attrs {
|
||||||
|
let hygiene = Hygiene::new(db, owner.file_id);
|
||||||
|
Attrs::new(owner.value, &hygiene)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn new(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
|
||||||
|
let mut attrs = owner.attrs().peekable();
|
||||||
|
let entries = if attrs.peek().is_none() {
|
||||||
|
// Avoid heap allocation
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
|
||||||
|
};
|
||||||
|
Attrs { entries }
|
||||||
|
}
|
||||||
|
|
||||||
pub fn has_atom(&self, atom: &str) -> bool {
|
pub fn has_atom(&self, atom: &str) -> bool {
|
||||||
self.iter().any(|it| it.is_simple_atom(atom))
|
self.iter().any(|it| it.is_simple_atom(atom))
|
||||||
}
|
}
|
||||||
|
@ -100,7 +112,7 @@ pub enum AttrInput {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Attr {
|
impl Attr {
|
||||||
pub(crate) fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
|
fn from_src(ast: ast::Attr, hygiene: &Hygiene) -> Option<Attr> {
|
||||||
let path = Path::from_src(ast.path()?, hygiene)?;
|
let path = Path::from_src(ast.path()?, hygiene)?;
|
||||||
let input = match ast.input() {
|
let input = match ast.input() {
|
||||||
None => None,
|
None => None,
|
||||||
|
@ -117,17 +129,6 @@ impl Attr {
|
||||||
Some(Attr { path, input })
|
Some(Attr { path, input })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_attrs_owner(owner: &dyn AttrsOwner, hygiene: &Hygiene) -> Attrs {
|
|
||||||
let mut attrs = owner.attrs().peekable();
|
|
||||||
let entries = if attrs.peek().is_none() {
|
|
||||||
// Avoid heap allocation
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(attrs.flat_map(|ast| Attr::from_src(ast, hygiene)).collect())
|
|
||||||
};
|
|
||||||
Attrs { entries }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_simple_atom(&self, name: &str) -> bool {
|
pub fn is_simple_atom(&self, name: &str) -> bool {
|
||||||
// FIXME: Avoid cloning
|
// FIXME: Avoid cloning
|
||||||
self.path.as_ident().map_or(false, |s| s.to_string() == name)
|
self.path.as_ident().map_or(false, |s| s.to_string() == name)
|
||||||
|
@ -154,8 +155,8 @@ where
|
||||||
N: ast::AttrsOwner,
|
N: ast::AttrsOwner,
|
||||||
D: DefDatabase,
|
D: DefDatabase,
|
||||||
{
|
{
|
||||||
let hygiene = Hygiene::new(db, src.file_id());
|
let src = Source::new(src.file_id(), src.to_node(db));
|
||||||
Attr::from_attrs_owner(&src.to_node(db), &hygiene)
|
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attrs_from_loc<T, D>(node: T, db: &D) -> Attrs
|
fn attrs_from_loc<T, D>(node: T, db: &D) -> Attrs
|
||||||
|
@ -165,6 +166,5 @@ where
|
||||||
D: DefDatabase,
|
D: DefDatabase,
|
||||||
{
|
{
|
||||||
let src = node.source(db);
|
let src = node.source(db);
|
||||||
let hygiene = Hygiene::new(db, src.file_id);
|
Attrs::from_attrs_owner(db, src.as_ref().map(|it| it as &dyn AttrsOwner))
|
||||||
Attr::from_attrs_owner(&src.value, &hygiene)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ impl LangItems {
|
||||||
{
|
{
|
||||||
let attrs = db.attrs(item.into());
|
let attrs = db.attrs(item.into());
|
||||||
if let Some(lang_item_name) = attrs.find_string_value("lang") {
|
if let Some(lang_item_name) = attrs.find_string_value("lang") {
|
||||||
self.items.entry(lang_item_name).or_insert_with(|| constructor(item));
|
self.items.entry(lang_item_name.clone()).or_insert_with(|| constructor(item));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,7 @@ use ra_syntax::{
|
||||||
use test_utils::tested_by;
|
use test_utils::tested_by;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
attr::{Attr, Attrs},
|
attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source,
|
||||||
db::DefDatabase,
|
|
||||||
path::Path,
|
|
||||||
FileAstId, HirFileId, LocalImportId, Source,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// `RawItems` is a set of top-level items in a file (except for impls).
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
||||||
|
@ -407,6 +404,6 @@ impl RawItemsCollector {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
|
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
|
||||||
Attr::from_attrs_owner(item, &self.hygiene)
|
Attrs::new(item, &self.hygiene)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue