mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 20:35:09 +00:00
Cleanup convert_path
This commit is contained in:
parent
880baa9e56
commit
d8ef6c24cc
4 changed files with 34 additions and 42 deletions
|
@ -131,7 +131,7 @@ impl CfgDiff {
|
||||||
/// of both.
|
/// of both.
|
||||||
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
|
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
|
||||||
let mut occupied = FxHashSet::default();
|
let mut occupied = FxHashSet::default();
|
||||||
if enable.iter().chain(disable.iter()).any(|item| occupied.insert(item)) {
|
if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) {
|
||||||
// was present
|
// was present
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
|
@ -219,7 +219,6 @@ impl Attr {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
|
fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
|
||||||
dbg!(tt);
|
|
||||||
let span = tt.first()?.first_span();
|
let span = tt.first()?.first_span();
|
||||||
let path_end = tt
|
let path_end = tt
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
//! Defines database & queries for macro expansion.
|
//! Defines database & queries for macro expansion.
|
||||||
|
|
||||||
use base_db::{
|
use base_db::{salsa, CrateId, FileId, SourceDatabase};
|
||||||
salsa::{self, debug::DebugQueryTable},
|
|
||||||
CrateId, FileId, SourceDatabase,
|
|
||||||
};
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use limit::Limit;
|
use limit::Limit;
|
||||||
use mbe::{syntax_node_to_token_tree, ValueResult};
|
use mbe::{syntax_node_to_token_tree, ValueResult};
|
||||||
|
|
|
@ -51,7 +51,7 @@ impl ModPath {
|
||||||
path: ast::Path,
|
path: ast::Path,
|
||||||
span_map: SpanMapRef<'_>,
|
span_map: SpanMapRef<'_>,
|
||||||
) -> Option<ModPath> {
|
) -> Option<ModPath> {
|
||||||
convert_path(db, None, path, span_map)
|
convert_path(db, path, span_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> {
|
pub fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> {
|
||||||
|
@ -199,22 +199,15 @@ fn display_fmt_path(
|
||||||
|
|
||||||
fn convert_path(
|
fn convert_path(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
prefix: Option<ModPath>,
|
|
||||||
path: ast::Path,
|
path: ast::Path,
|
||||||
span_map: SpanMapRef<'_>,
|
span_map: SpanMapRef<'_>,
|
||||||
) -> Option<ModPath> {
|
) -> Option<ModPath> {
|
||||||
let prefix = match path.qualifier() {
|
let mut segments = path.segments();
|
||||||
Some(qual) => Some(convert_path(db, prefix, qual, span_map)?),
|
|
||||||
None => prefix,
|
|
||||||
};
|
|
||||||
|
|
||||||
let segment = path.segment()?;
|
let segment = &segments.next()?;
|
||||||
let mut mod_path = match segment.kind()? {
|
let mut mod_path = match segment.kind()? {
|
||||||
ast::PathSegmentKind::Name(name_ref) => {
|
ast::PathSegmentKind::Name(name_ref) => {
|
||||||
if name_ref.text() == "$crate" {
|
if name_ref.text() == "$crate" {
|
||||||
if prefix.is_some() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
ModPath::from_kind(
|
ModPath::from_kind(
|
||||||
resolve_crate_root(
|
resolve_crate_root(
|
||||||
db,
|
db,
|
||||||
|
@ -224,41 +217,36 @@ fn convert_path(
|
||||||
.unwrap_or(PathKind::Crate),
|
.unwrap_or(PathKind::Crate),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let mut res = prefix.unwrap_or_else(|| {
|
let mut res = ModPath::from_kind(
|
||||||
ModPath::from_kind(
|
segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
|
||||||
segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
|
);
|
||||||
)
|
|
||||||
});
|
|
||||||
res.segments.push(name_ref.as_name());
|
res.segments.push(name_ref.as_name());
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::SelfTypeKw => {
|
ast::PathSegmentKind::SelfTypeKw => {
|
||||||
if prefix.is_some() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE))
|
ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE))
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::CrateKw => {
|
ast::PathSegmentKind::CrateKw => ModPath::from_segments(PathKind::Crate, iter::empty()),
|
||||||
if prefix.is_some() {
|
ast::PathSegmentKind::SelfKw => ModPath::from_segments(PathKind::Super(0), iter::empty()),
|
||||||
return None;
|
|
||||||
}
|
|
||||||
ModPath::from_segments(PathKind::Crate, iter::empty())
|
|
||||||
}
|
|
||||||
ast::PathSegmentKind::SelfKw => {
|
|
||||||
if prefix.is_some() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
ModPath::from_segments(PathKind::Super(0), iter::empty())
|
|
||||||
}
|
|
||||||
ast::PathSegmentKind::SuperKw => {
|
ast::PathSegmentKind::SuperKw => {
|
||||||
let nested_super_count = match prefix.map(|p| p.kind) {
|
let mut deg = 1;
|
||||||
Some(PathKind::Super(n)) => n,
|
let mut next_segment = None;
|
||||||
Some(_) => return None,
|
while let Some(segment) = segments.next() {
|
||||||
None => 0,
|
match segment.kind()? {
|
||||||
};
|
ast::PathSegmentKind::SuperKw => deg += 1,
|
||||||
|
ast::PathSegmentKind::Name(name) => {
|
||||||
|
next_segment = Some(name.as_name());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ast::PathSegmentKind::Type { .. }
|
||||||
|
| ast::PathSegmentKind::SelfTypeKw
|
||||||
|
| ast::PathSegmentKind::SelfKw
|
||||||
|
| ast::PathSegmentKind::CrateKw => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
|
ModPath::from_segments(PathKind::Super(deg), next_segment)
|
||||||
}
|
}
|
||||||
ast::PathSegmentKind::Type { .. } => {
|
ast::PathSegmentKind::Type { .. } => {
|
||||||
// not allowed in imports
|
// not allowed in imports
|
||||||
|
@ -266,6 +254,14 @@ fn convert_path(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
for segment in segments {
|
||||||
|
let name = match segment.kind()? {
|
||||||
|
ast::PathSegmentKind::Name(name) => name.as_name(),
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
mod_path.segments.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
// handle local_inner_macros :
|
// handle local_inner_macros :
|
||||||
// Basically, even in rustc it is quite hacky:
|
// Basically, even in rustc it is quite hacky:
|
||||||
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
|
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
|
||||||
|
|
Loading…
Reference in a new issue