X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fide_db%2Fsrc%2Fhelpers.rs;h=e589940dae29f58ef7bb0d2d48eb80a4de89cf21;hb=bfc263f1f98aece963a4b103d787005346f0c1c7;hp=18a5f64891e5a9d5c9dade3b4fc1e7c3ce794dd6;hpb=69500e8c6bad63feec7dc94af844dc30f454a592;p=rust.git diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs index 18a5f64891e..e589940dae2 100644 --- a/crates/ide_db/src/helpers.rs +++ b/crates/ide_db/src/helpers.rs @@ -14,7 +14,7 @@ use hir::{ItemInNs, MacroDef, ModuleDef, Name, PathResolution, Semantics}; use itertools::Itertools; use syntax::{ - ast::{self, make, HasLoopBody, Ident}, + ast::{self, make, HasLoopBody}, AstNode, AstToken, Direction, SyntaxElement, SyntaxKind, SyntaxToken, TokenAtOffset, WalkEvent, T, }; @@ -38,7 +38,7 @@ pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option { pub fn get_path_in_derive_attr( sema: &hir::Semantics, attr: &ast::Attr, - cursor: &Ident, + cursor: &ast::Ident, ) -> Option { let path = attr.path()?; let tt = attr.token_tree()?; @@ -55,7 +55,7 @@ pub fn get_path_in_derive_attr( } /// Parses the path the identifier is part of inside a token tree. -pub fn get_path_at_cursor_in_tt(cursor: &Ident) -> Option { +pub fn get_path_at_cursor_in_tt(cursor: &ast::Ident) -> Option { let cursor = cursor.syntax(); let first = cursor .siblings_with_tokens(Direction::Prev) @@ -67,7 +67,11 @@ pub fn get_path_at_cursor_in_tt(cursor: &Ident) -> Option { .filter_map(SyntaxElement::into_token) .take_while(|tok| tok != cursor); - ast::Path::parse(&path_tokens.chain(iter::once(cursor.clone())).join("")).ok() + syntax::hacks::parse_expr_from_str(&path_tokens.chain(iter::once(cursor.clone())).join("")) + .and_then(|expr| match expr { + ast::Expr::PathExpr(it) => it.path(), + _ => None, + }) } /// Parses and resolves the path at the cursor position in the given attribute, if it is a derive. @@ -75,7 +79,7 @@ pub fn get_path_at_cursor_in_tt(cursor: &Ident) -> Option { pub fn try_resolve_derive_input( sema: &hir::Semantics, attr: &ast::Attr, - cursor: &Ident, + cursor: &ast::Ident, ) -> Option { let path = get_path_in_derive_attr(sema, attr, cursor)?; let scope = sema.scope(attr.syntax()); @@ -323,7 +327,12 @@ pub fn parse_tt_as_comma_sep_paths(input: ast::TokenTree) -> Option it.path(), + _ => None, + }) + }) .collect(); Some(paths) }