9437: fix: Don't classify paths inside attribute TokenTrees r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-06-30 19:52:03 +00:00 committed by GitHub
commit c8c4d73648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -1,5 +1,3 @@
use std::iter::successors;
use syntax::{ast, AstNode, T}; use syntax::{ast, AstNode, T};
use crate::{AssistContext, AssistId, AssistKind, Assists}; use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -18,9 +16,8 @@
pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn split_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let colon_colon = ctx.find_token_syntax_at_offset(T![::])?; let colon_colon = ctx.find_token_syntax_at_offset(T![::])?;
let path = ast::Path::cast(colon_colon.parent()?)?.qualifier()?; let path = ast::Path::cast(colon_colon.parent()?)?.qualifier()?;
let top_path = successors(Some(path.clone()), |it| it.parent_path()).last()?;
let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?; let use_tree = path.top_path().syntax().ancestors().find_map(ast::UseTree::cast)?;
let new_tree = use_tree.split_prefix(&path); let new_tree = use_tree.split_prefix(&path);
if new_tree == use_tree { if new_tree == use_tree {

View File

@ -383,14 +383,17 @@ pub fn classify(
} }
} }
} }
let top_path = path.top_path();
if let Some(resolved) = sema.resolve_path(&path) { let is_attribute_path = top_path
return if path.syntax().ancestors().find_map(ast::Attr::cast).is_some() { .syntax()
.ancestors()
.find_map(ast::Attr::cast)
.map(|attr| attr.path().as_ref() == Some(&top_path));
return match is_attribute_path {
Some(true) => sema.resolve_path(&path).and_then(|resolved| {
match resolved { match resolved {
// Don't wanna collide with builtin attributes here like `test` hence guard // Don't wanna collide with builtin attributes here like `test` hence guard
PathResolution::Def(module @ ModuleDef::Module(_)) PathResolution::Def(module @ ModuleDef::Module(_)) if path == top_path => {
if path.parent_path().is_some() =>
{
Some(NameRefClass::Definition(Definition::ModuleDef(module))) Some(NameRefClass::Definition(Definition::ModuleDef(module)))
} }
PathResolution::Macro(mac) if mac.kind() == hir::MacroKind::Attr => { PathResolution::Macro(mac) if mac.kind() == hir::MacroKind::Attr => {
@ -398,10 +401,10 @@ pub fn classify(
} }
_ => None, _ => None,
} }
} else { }),
Some(NameRefClass::Definition(resolved.into())) Some(false) => None,
}; None => sema.resolve_path(&path).map(Into::into).map(NameRefClass::Definition),
} };
} }
let extern_crate = ast::ExternCrate::cast(parent)?; let extern_crate = ast::ExternCrate::cast(parent)?;

View File

@ -336,6 +336,14 @@ pub fn segments(&self) -> impl Iterator<Item = ast::PathSegment> + Clone {
pub fn qualifiers(&self) -> impl Iterator<Item = ast::Path> + Clone { pub fn qualifiers(&self) -> impl Iterator<Item = ast::Path> + Clone {
successors(self.qualifier(), |p| p.qualifier()) successors(self.qualifier(), |p| p.qualifier())
} }
pub fn top_path(&self) -> ast::Path {
let mut this = self.clone();
while let Some(path) = this.parent_path() {
this = path;
}
this
}
} }
impl ast::Use { impl ast::Use {