2020-06-12 23:21:48 +00:00
|
|
|
//! Patterns telling us certain facts about current syntax element, they are used in completion context
|
|
|
|
|
2021-05-30 19:23:42 +00:00
|
|
|
use hir::Semantics;
|
|
|
|
use ide_db::RootDatabase;
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::{
|
2020-06-11 12:16:35 +00:00
|
|
|
algo::non_trivia_sibling,
|
|
|
|
ast::{self, LoopBodyOwner},
|
2021-05-30 19:23:42 +00:00
|
|
|
match_ast, AstNode, Direction, SyntaxElement,
|
2021-05-28 20:03:31 +00:00
|
|
|
SyntaxKind::*,
|
2021-05-30 19:23:42 +00:00
|
|
|
SyntaxNode, SyntaxToken, TextSize, T,
|
2020-06-11 12:16:35 +00:00
|
|
|
};
|
|
|
|
|
2020-06-13 11:47:30 +00:00
|
|
|
#[cfg(test)]
|
2020-10-18 10:09:00 +00:00
|
|
|
use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable};
|
2021-05-30 19:23:42 +00:00
|
|
|
|
2021-05-28 20:03:31 +00:00
|
|
|
/// Direct parent container of the cursor position
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) enum ImmediatePrevSibling {
|
|
|
|
IfExpr,
|
|
|
|
TraitDefName,
|
|
|
|
ImplDefType,
|
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
|
2021-05-27 16:15:18 +00:00
|
|
|
/// Direct parent container of the cursor position
|
2021-05-30 19:23:42 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2021-05-27 16:15:18 +00:00
|
|
|
pub(crate) enum ImmediateLocation {
|
2021-05-28 00:40:40 +00:00
|
|
|
Use,
|
2021-05-27 16:15:18 +00:00
|
|
|
Impl,
|
|
|
|
Trait,
|
|
|
|
RecordField,
|
|
|
|
RefExpr,
|
|
|
|
IdentPat,
|
|
|
|
BlockExpr,
|
|
|
|
ItemList,
|
2021-05-30 19:23:42 +00:00
|
|
|
// Fake file ast node
|
|
|
|
Attribute(ast::Attr),
|
|
|
|
// Fake file ast node
|
|
|
|
ModDeclaration(ast::Module),
|
|
|
|
// Original file ast node
|
|
|
|
/// The record expr of the field name we are completing
|
|
|
|
RecordExpr(ast::RecordExpr),
|
|
|
|
// Original file ast node
|
|
|
|
/// The record pat of the field name we are completing
|
|
|
|
RecordPat(ast::RecordPat),
|
2021-05-27 16:15:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 20:03:31 +00:00
|
|
|
pub(crate) fn determine_prev_sibling(name_like: &ast::NameLike) -> Option<ImmediatePrevSibling> {
|
2021-05-30 19:23:42 +00:00
|
|
|
let node = match name_like {
|
|
|
|
ast::NameLike::NameRef(name_ref) => maximize_name_ref(name_ref),
|
|
|
|
ast::NameLike::Name(n) => n.syntax().clone(),
|
|
|
|
ast::NameLike::Lifetime(lt) => lt.syntax().clone(),
|
|
|
|
};
|
2021-05-28 20:03:31 +00:00
|
|
|
let node = match node.parent().and_then(ast::MacroCall::cast) {
|
|
|
|
// When a path is being typed after the name of a trait/type of an impl it is being
|
|
|
|
// parsed as a macro, so when the trait/impl has a block following it an we are between the
|
|
|
|
// name and block the macro will attach the block to itself so maximizing fails to take
|
|
|
|
// that into account
|
|
|
|
// FIXME path expr and statement have a similar problem with attrs
|
|
|
|
Some(call)
|
|
|
|
if call.excl_token().is_none()
|
|
|
|
&& call.token_tree().map_or(false, |t| t.l_curly_token().is_some())
|
|
|
|
&& call.semicolon_token().is_none() =>
|
|
|
|
{
|
|
|
|
call.syntax().clone()
|
|
|
|
}
|
|
|
|
_ => node,
|
|
|
|
};
|
|
|
|
let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
|
|
|
|
let res = match_ast! {
|
|
|
|
match prev_sibling {
|
|
|
|
ast::ExprStmt(it) => {
|
2021-05-28 20:18:52 +00:00
|
|
|
let node = it.expr().filter(|_| it.semicolon_token().is_none())?.syntax().clone();
|
2021-05-28 20:03:31 +00:00
|
|
|
match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::IfExpr(_it) => ImmediatePrevSibling::IfExpr,
|
|
|
|
_ => return None,
|
|
|
|
}
|
2021-05-27 16:15:18 +00:00
|
|
|
}
|
2021-05-28 20:03:31 +00:00
|
|
|
},
|
|
|
|
ast::Trait(it) => if it.assoc_item_list().is_none() {
|
|
|
|
ImmediatePrevSibling::TraitDefName
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
},
|
|
|
|
ast::Impl(it) => if it.assoc_item_list().is_none()
|
|
|
|
&& (it.for_token().is_none() || it.self_ty().is_some()) {
|
|
|
|
ImmediatePrevSibling::ImplDefType
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
},
|
|
|
|
_ => return None,
|
2021-05-27 16:15:18 +00:00
|
|
|
}
|
|
|
|
};
|
2021-05-28 20:03:31 +00:00
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
|
2021-05-30 19:23:42 +00:00
|
|
|
pub(crate) fn determine_location(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
original_file: &SyntaxNode,
|
|
|
|
offset: TextSize,
|
|
|
|
name_like: &ast::NameLike,
|
|
|
|
) -> Option<ImmediateLocation> {
|
|
|
|
let node = match name_like {
|
|
|
|
ast::NameLike::NameRef(name_ref) => {
|
|
|
|
if ast::RecordExprField::for_field_name(&name_ref).is_some() {
|
|
|
|
return sema
|
|
|
|
.find_node_at_offset_with_macros(original_file, offset)
|
|
|
|
.map(ImmediateLocation::RecordExpr);
|
|
|
|
}
|
|
|
|
if ast::RecordPatField::for_field_name_ref(&name_ref).is_some() {
|
|
|
|
return sema
|
|
|
|
.find_node_at_offset_with_macros(original_file, offset)
|
|
|
|
.map(ImmediateLocation::RecordPat);
|
|
|
|
}
|
|
|
|
maximize_name_ref(name_ref)
|
|
|
|
}
|
|
|
|
ast::NameLike::Name(name) => {
|
|
|
|
if ast::RecordPatField::for_field_name(&name).is_some() {
|
|
|
|
return sema
|
|
|
|
.find_node_at_offset_with_macros(original_file, offset)
|
|
|
|
.map(ImmediateLocation::RecordPat);
|
|
|
|
}
|
|
|
|
name.syntax().clone()
|
|
|
|
}
|
|
|
|
ast::NameLike::Lifetime(lt) => lt.syntax().clone(),
|
|
|
|
};
|
|
|
|
|
2021-05-27 16:15:18 +00:00
|
|
|
let parent = match node.parent() {
|
2021-05-28 01:20:55 +00:00
|
|
|
Some(parent) => match ast::MacroCall::cast(parent.clone()) {
|
|
|
|
// When a path is being typed in an (Assoc)ItemList the parser will always emit a macro_call.
|
|
|
|
// This is usually fine as the node expansion code above already accounts for that with
|
|
|
|
// the ancestors call, but there is one exception to this which is that when an attribute
|
|
|
|
// precedes it the code above will not walk the Path to the parent MacroCall as their ranges differ.
|
2021-05-28 20:03:31 +00:00
|
|
|
// FIXME path expr and statement have a similar problem
|
2021-05-28 01:20:55 +00:00
|
|
|
Some(call)
|
|
|
|
if call.excl_token().is_none()
|
|
|
|
&& call.token_tree().is_none()
|
|
|
|
&& call.semicolon_token().is_none() =>
|
|
|
|
{
|
|
|
|
call.syntax().parent()?
|
|
|
|
}
|
|
|
|
_ => parent,
|
|
|
|
},
|
2021-05-27 16:15:18 +00:00
|
|
|
// SourceFile
|
|
|
|
None => {
|
|
|
|
return match node.kind() {
|
|
|
|
MACRO_ITEMS | SOURCE_FILE => Some(ImmediateLocation::ItemList),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-05-30 19:23:42 +00:00
|
|
|
|
2021-05-27 16:15:18 +00:00
|
|
|
let res = match_ast! {
|
|
|
|
match parent {
|
|
|
|
ast::IdentPat(_it) => ImmediateLocation::IdentPat,
|
2021-05-28 00:40:40 +00:00
|
|
|
ast::Use(_it) => ImmediateLocation::Use,
|
2021-05-27 16:15:18 +00:00
|
|
|
ast::BlockExpr(_it) => ImmediateLocation::BlockExpr,
|
|
|
|
ast::SourceFile(_it) => ImmediateLocation::ItemList,
|
|
|
|
ast::ItemList(_it) => ImmediateLocation::ItemList,
|
|
|
|
ast::RefExpr(_it) => ImmediateLocation::RefExpr,
|
|
|
|
ast::RecordField(_it) => ImmediateLocation::RecordField,
|
|
|
|
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
|
|
|
|
Some(IMPL) => ImmediateLocation::Impl,
|
|
|
|
Some(TRAIT) => ImmediateLocation::Trait,
|
|
|
|
_ => return None,
|
|
|
|
},
|
2021-05-30 19:23:42 +00:00
|
|
|
ast::Module(it) => if it.item_list().is_none() {
|
|
|
|
ImmediateLocation::ModDeclaration(it)
|
|
|
|
} else {
|
|
|
|
return None
|
|
|
|
},
|
|
|
|
ast::Attr(it) => ImmediateLocation::Attribute(it),
|
2021-05-27 16:15:18 +00:00
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(res)
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-12 22:55:21 +00:00
|
|
|
|
2021-05-30 19:23:42 +00:00
|
|
|
fn maximize_name_ref(name_ref: &ast::NameRef) -> SyntaxNode {
|
|
|
|
// Maximize a nameref to its enclosing path if its the last segment of said path
|
|
|
|
if let Some(segment) = name_ref.syntax().parent().and_then(ast::PathSegment::cast) {
|
|
|
|
let p = segment.parent_path();
|
|
|
|
if p.parent_path().is_none() {
|
|
|
|
if let Some(it) = p
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.text_range() == p.syntax().text_range())
|
|
|
|
.last()
|
|
|
|
{
|
|
|
|
return it;
|
2021-05-28 20:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-30 19:23:42 +00:00
|
|
|
}
|
|
|
|
name_ref.syntax().clone()
|
2021-05-28 20:03:31 +00:00
|
|
|
}
|
|
|
|
|
2021-05-27 16:15:18 +00:00
|
|
|
pub(crate) fn inside_impl_trait_block(element: SyntaxElement) -> bool {
|
|
|
|
// Here we search `impl` keyword up through the all ancestors, unlike in `has_impl_parent`,
|
|
|
|
// where we only check the first parent with different text range.
|
|
|
|
element
|
2021-05-27 02:34:21 +00:00
|
|
|
.ancestors()
|
2021-05-27 16:15:18 +00:00
|
|
|
.find(|it| it.kind() == IMPL)
|
|
|
|
.map(|it| ast::Impl::cast(it).unwrap())
|
|
|
|
.map(|it| it.trait_().is_some())
|
|
|
|
.unwrap_or(false)
|
2020-06-13 08:43:39 +00:00
|
|
|
}
|
2020-06-13 11:47:30 +00:00
|
|
|
#[test]
|
2021-05-27 16:15:18 +00:00
|
|
|
fn test_inside_impl_trait_block() {
|
|
|
|
check_pattern_is_applicable(r"impl Foo for Bar { f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_applicable(r"impl Foo for Bar { fn f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_not_applicable(r"impl A { f$0 }", inside_impl_trait_block);
|
|
|
|
check_pattern_is_not_applicable(r"impl A { fn f$0 }", inside_impl_trait_block);
|
2020-06-13 11:47:30 +00:00
|
|
|
}
|
2020-06-13 08:43:39 +00:00
|
|
|
|
2021-05-26 19:09:27 +00:00
|
|
|
pub(crate) fn previous_token(element: SyntaxElement) -> Option<SyntaxToken> {
|
|
|
|
element.into_token().and_then(|it| previous_non_trivia_token(it))
|
2020-10-17 07:56:00 +00:00
|
|
|
}
|
2020-10-12 07:59:15 +00:00
|
|
|
|
|
|
|
/// Check if the token previous to the previous one is `for`.
|
2021-01-06 20:15:48 +00:00
|
|
|
/// For example, `for _ i$0` => true.
|
2020-10-12 07:59:15 +00:00
|
|
|
pub(crate) fn for_is_prev2(element: SyntaxElement) -> bool {
|
|
|
|
element
|
|
|
|
.into_token()
|
|
|
|
.and_then(|it| previous_non_trivia_token(it))
|
|
|
|
.and_then(|it| previous_non_trivia_token(it))
|
2021-01-10 15:40:52 +00:00
|
|
|
.filter(|it| it.kind() == T![for])
|
2020-10-12 07:59:15 +00:00
|
|
|
.is_some()
|
|
|
|
}
|
2020-10-17 07:56:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_for_is_prev2() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check_pattern_is_applicable(r"for i i$0", for_is_prev2);
|
2020-10-17 07:56:00 +00:00
|
|
|
}
|
2020-10-12 07:59:15 +00:00
|
|
|
|
2020-06-11 12:16:35 +00:00
|
|
|
pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
|
2021-05-27 00:54:49 +00:00
|
|
|
element
|
|
|
|
.ancestors()
|
|
|
|
.take_while(|it| it.kind() != FN && it.kind() != CLOSURE_EXPR)
|
|
|
|
.find_map(|it| {
|
|
|
|
let loop_body = match_ast! {
|
|
|
|
match it {
|
|
|
|
ast::ForExpr(it) => it.loop_body(),
|
|
|
|
ast::WhileExpr(it) => it.loop_body(),
|
|
|
|
ast::LoopExpr(it) => it.loop_body(),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
loop_body.filter(|it| it.syntax().text_range().contains_range(element.text_range()))
|
|
|
|
})
|
|
|
|
.is_some()
|
2020-06-11 12:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 21:25:58 +00:00
|
|
|
fn previous_non_trivia_token(token: SyntaxToken) -> Option<SyntaxToken> {
|
|
|
|
let mut token = token.prev_token();
|
|
|
|
while let Some(inner) = token.clone() {
|
|
|
|
if !inner.kind().is_trivia() {
|
|
|
|
return Some(inner);
|
|
|
|
} else {
|
|
|
|
token = inner.prev_token();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-05-28 20:18:52 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-05-30 19:23:42 +00:00
|
|
|
use syntax::algo::find_node_at_offset;
|
|
|
|
|
|
|
|
use crate::test_utils::position;
|
|
|
|
|
2021-05-28 20:18:52 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn check_location(code: &str, loc: impl Into<Option<ImmediateLocation>>) {
|
2021-05-30 19:23:42 +00:00
|
|
|
let (db, pos) = position(code);
|
|
|
|
|
|
|
|
let sema = Semantics::new(&db);
|
|
|
|
let original_file = sema.parse(pos.file_id);
|
|
|
|
|
|
|
|
let name_like = find_node_at_offset(original_file.syntax(), pos.offset).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
determine_location(&sema, original_file.syntax(), pos.offset, &name_like),
|
|
|
|
loc.into()
|
|
|
|
);
|
2021-05-28 20:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_prev_sibling(code: &str, sibling: impl Into<Option<ImmediatePrevSibling>>) {
|
|
|
|
check_pattern_is_applicable(code, |e| {
|
|
|
|
let name = &e.parent().and_then(ast::NameLike::cast).expect("Expected a namelike");
|
|
|
|
assert_eq!(determine_prev_sibling(name), sibling.into());
|
|
|
|
true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trait_loc() {
|
|
|
|
check_location(r"trait A { f$0 }", ImmediateLocation::Trait);
|
|
|
|
check_location(r"trait A { #[attr] f$0 }", ImmediateLocation::Trait);
|
|
|
|
check_location(r"trait A { f$0 fn f() {} }", ImmediateLocation::Trait);
|
|
|
|
check_location(r"trait A { fn f() {} f$0 }", ImmediateLocation::Trait);
|
|
|
|
check_location(r"trait A$0 {}", None);
|
|
|
|
check_location(r"trait A { fn f$0 }", None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_loc() {
|
|
|
|
check_location(r"impl A { f$0 }", ImmediateLocation::Impl);
|
|
|
|
check_location(r"impl A { #[attr] f$0 }", ImmediateLocation::Impl);
|
|
|
|
check_location(r"impl A { f$0 fn f() {} }", ImmediateLocation::Impl);
|
|
|
|
check_location(r"impl A { fn f() {} f$0 }", ImmediateLocation::Impl);
|
|
|
|
check_location(r"impl A$0 {}", None);
|
|
|
|
check_location(r"impl A { fn f$0 }", None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_use_loc() {
|
|
|
|
check_location(r"use f$0", ImmediateLocation::Use);
|
|
|
|
check_location(r"use f$0;", ImmediateLocation::Use);
|
|
|
|
check_location(r"use f::{f$0}", None);
|
|
|
|
check_location(r"use {f$0}", None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_record_field_loc() {
|
|
|
|
check_location(r"struct Foo { f$0 }", ImmediateLocation::RecordField);
|
|
|
|
check_location(r"struct Foo { f$0 pub f: i32}", ImmediateLocation::RecordField);
|
|
|
|
check_location(r"struct Foo { pub f: i32, f$0 }", ImmediateLocation::RecordField);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_block_expr_loc() {
|
|
|
|
check_location(r"fn my_fn() { let a = 2; f$0 }", ImmediateLocation::BlockExpr);
|
|
|
|
check_location(r"fn my_fn() { f$0 f }", ImmediateLocation::BlockExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ident_pat_loc() {
|
|
|
|
check_location(r"fn my_fn(m$0) {}", ImmediateLocation::IdentPat);
|
|
|
|
check_location(r"fn my_fn() { let m$0 }", ImmediateLocation::IdentPat);
|
|
|
|
check_location(r"fn my_fn(&m$0) {}", ImmediateLocation::IdentPat);
|
|
|
|
check_location(r"fn my_fn() { let &m$0 }", ImmediateLocation::IdentPat);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ref_expr_loc() {
|
|
|
|
check_location(r"fn my_fn() { let x = &m$0 foo; }", ImmediateLocation::RefExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_item_list_loc() {
|
|
|
|
check_location(r"i$0", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"#[attr] i$0", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"fn f() {} i$0", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"mod foo { f$0 }", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"mod foo { #[attr] f$0 }", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"mod foo { fn f() {} f$0 }", ImmediateLocation::ItemList);
|
|
|
|
check_location(r"mod foo$0 {}", None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_impl_prev_sibling() {
|
|
|
|
check_prev_sibling(r"impl A w$0 ", ImmediatePrevSibling::ImplDefType);
|
|
|
|
check_prev_sibling(r"impl A w$0 {}", ImmediatePrevSibling::ImplDefType);
|
|
|
|
check_prev_sibling(r"impl A for A w$0 ", ImmediatePrevSibling::ImplDefType);
|
|
|
|
check_prev_sibling(r"impl A for A w$0 {}", ImmediatePrevSibling::ImplDefType);
|
|
|
|
check_prev_sibling(r"impl A for w$0 {}", None);
|
|
|
|
check_prev_sibling(r"impl A for w$0", None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trait_prev_sibling() {
|
|
|
|
check_prev_sibling(r"trait A w$0 ", ImmediatePrevSibling::TraitDefName);
|
|
|
|
check_prev_sibling(r"trait A w$0 {}", ImmediatePrevSibling::TraitDefName);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_if_expr_prev_sibling() {
|
|
|
|
check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr);
|
|
|
|
check_prev_sibling(r"fn foo() { if true {}; w$0", None);
|
|
|
|
}
|
|
|
|
}
|