//! Patterns telling us certain facts about current syntax element, they are used in completion context
+//!
+//! Most logic in this module first expands the token below the cursor to a maximum node that acts similar to the token itself.
+//! This means we for example expand a NameRef token to its outermost Path node, as semantically these act in the same location
+//! and the completions usually query for path specific things on the Path context instead. This simplifies some location handling.
use hir::Semantics;
use ide_db::RootDatabase;
use syntax::{
algo::non_trivia_sibling,
- ast::{self, LoopBodyOwner},
+ ast::{self, HasArgList, HasLoopBody},
match_ast, AstNode, Direction, SyntaxElement,
SyntaxKind::*,
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
};
#[cfg(test)]
-use crate::test_utils::{check_pattern_is_applicable, check_pattern_is_not_applicable};
+use crate::tests::{check_pattern_is_applicable, check_pattern_is_not_applicable};
/// Immediate previous node to what we are completing.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
IfExpr,
TraitDefName,
ImplDefType,
+ Visibility,
+ Attribute,
}
/// Direct parent "thing" of what we are currently completing.
+///
+/// This may contain nodes of the fake file as well as the original, comments on the variants specify
+/// from which file the nodes are.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ImmediateLocation {
Use,
+ UseTree,
+ Rename,
Impl,
Trait,
RecordField,
+ TupleField,
RefExpr,
IdentPat,
- BlockExpr,
+ StmtList,
ItemList,
- // Fake file ast node
+ TypeBound,
+ Variant,
+ /// Fake file ast node
Attribute(ast::Attr),
- // Fake file ast node
+ /// Fake file ast node
ModDeclaration(ast::Module),
- // Original file ast node
+ Visibility(ast::Visibility),
+ /// Original file ast node
MethodCall {
receiver: Option<ast::Expr>,
+ has_parens: bool,
},
- // Original file ast node
+ /// Original file ast node
FieldAccess {
receiver: Option<ast::Expr>,
receiver_is_ambiguous_float_literal: bool,
},
- // Original file ast node
+ // Only set from a type arg
+ /// Original file ast node
+ GenericArgList(ast::GenericArgList),
/// The record expr of the field name we are completing
+ ///
+ /// Original file ast node
RecordExpr(ast::RecordExpr),
- // Original file ast node
+ /// The record expr of the functional update syntax we are completing
+ ///
+ /// Original file ast node
+ RecordExprUpdate(ast::RecordExpr),
/// The record pat of the field name we are completing
+ ///
+ /// Original file ast node
RecordPat(ast::RecordPat),
}
_ => node,
};
let prev_sibling = non_trivia_sibling(node.into(), Direction::Prev)?.into_node()?;
+ if prev_sibling.kind() == ERROR {
+ let prev_sibling = prev_sibling.first_child()?;
+ let res = match_ast! {
+ match prev_sibling {
+ // vis followed by random ident will always error the parser
+ ast::Visibility(_it) => ImmediatePrevSibling::Visibility,
+ _ => return None,
+ }
+ };
+ return Some(res);
+ }
let res = match_ast! {
match prev_sibling {
ast::ExprStmt(it) => {
} else {
return None
},
+ ast::Attr(_it) => ImmediatePrevSibling::Attribute,
_ => return None,
}
};
) -> Option<ImmediateLocation> {
let node = match name_like {
ast::NameLike::NameRef(name_ref) => {
- if ast::RecordExprField::for_field_name(&name_ref).is_some() {
+ 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() {
+ 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() {
+ if ast::RecordPatField::for_field_name(name).is_some() {
return sema
.find_node_at_offset_with_macros(original_file, offset)
.map(ImmediateLocation::RecordPat);
ast::NameLike::Lifetime(lt) => lt.syntax().clone(),
};
+ match_ast! {
+ match node {
+ ast::TypeBoundList(_it) => return Some(ImmediateLocation::TypeBound),
+ _ => (),
+ }
+ };
+
let parent = match node.parent() {
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.
match parent {
ast::IdentPat(_it) => ImmediateLocation::IdentPat,
ast::Use(_it) => ImmediateLocation::Use,
- ast::BlockExpr(_it) => ImmediateLocation::BlockExpr,
+ ast::UseTree(_it) => ImmediateLocation::UseTree,
+ ast::UseTreeList(_it) => ImmediateLocation::UseTree,
+ ast::Rename(_it) => ImmediateLocation::Rename,
+ ast::StmtList(_it) => ImmediateLocation::StmtList,
ast::SourceFile(_it) => ImmediateLocation::ItemList,
ast::ItemList(_it) => ImmediateLocation::ItemList,
ast::RefExpr(_it) => ImmediateLocation::RefExpr,
- ast::RecordField(_it) => ImmediateLocation::RecordField,
+ ast::Variant(_it) => ImmediateLocation::Variant,
+ ast::RecordField(it) => if it.ty().map_or(false, |it| it.syntax().text_range().contains(offset)) {
+ return None;
+ } else {
+ ImmediateLocation::RecordField
+ },
+ ast::RecordExprFieldList(_it) => sema
+ .find_node_at_offset_with_macros(original_file, offset)
+ .map(ImmediateLocation::RecordExprUpdate)?,
+ ast::TupleField(_it) => ImmediateLocation::TupleField,
+ ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
+ ast::TypeBound(_it) => ImmediateLocation::TypeBound,
+ ast::TypeBoundList(_it) => ImmediateLocation::TypeBound,
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
Some(IMPL) => ImmediateLocation::Impl,
Some(TRAIT) => ImmediateLocation::Trait,
_ => return None,
},
+ ast::GenericArgList(_it) => sema
+ .find_node_at_offset_with_macros(original_file, offset)
+ .map(ImmediateLocation::GenericArgList)?,
ast::Module(it) => {
if it.item_list().is_none() {
ImmediateLocation::ModDeclaration(it)
.receiver()
.map(|e| e.syntax().text_range())
.and_then(|r| find_node_with_range(original_file, r)),
+ has_parens: it.arg_list().map_or(false, |it| it.l_paren_token().is_some())
},
+ ast::Visibility(it) => it.pub_token()
+ .and_then(|t| (t.text_range().end() < offset).then(|| ImmediateLocation::Visibility(it)))?,
_ => return None,
}
};
Some(res)
}
+/// Maximize a nameref to its enclosing path if its the last segment of said path.
+/// That is, when completing a [`NameRef`] we actually handle it as the path it is part of when determining
+/// its location.
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
+ // Get rid of PathExpr, PathType, etc...
+ let path = p
.syntax()
.ancestors()
.take_while(|it| it.text_range() == p.syntax().text_range())
- .last()
- {
+ .last();
+ if let Some(it) = path {
return it;
}
}
}
fn find_node_with_range<N: AstNode>(syntax: &SyntaxNode, range: TextRange) -> Option<N> {
+ let range = syntax.text_range().intersect(range)?;
syntax.covering_element(range).ancestors().find_map(N::cast)
}
}
pub(crate) fn previous_token(element: SyntaxElement) -> Option<SyntaxToken> {
- element.into_token().and_then(|it| previous_non_trivia_token(it))
+ element.into_token().and_then(previous_non_trivia_token)
}
/// Check if the token previous to the previous one is `for`.
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))
+ .and_then(previous_non_trivia_token)
+ .and_then(previous_non_trivia_token)
.filter(|it| it.kind() == T![for])
.is_some()
}
check_pattern_is_applicable(r"for i i$0", for_is_prev2);
}
-pub(crate) fn is_in_loop_body(element: SyntaxElement) -> bool {
- element
- .ancestors()
+pub(crate) fn is_in_loop_body(node: &SyntaxNode) -> bool {
+ node.ancestors()
.take_while(|it| it.kind() != FN && it.kind() != CLOSURE_EXPR)
.find_map(|it| {
let loop_body = match_ast! {
_ => None,
}
};
- loop_body.filter(|it| it.syntax().text_range().contains_range(element.text_range()))
+ loop_body.filter(|it| it.syntax().text_range().contains_range(node.text_range()))
})
.is_some()
}
mod tests {
use syntax::algo::find_node_at_offset;
- use crate::test_utils::position;
+ use crate::tests::position;
use super::*;
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);
+ check_location(r"use f::{f$0}", ImmediateLocation::UseTree);
+ check_location(r"use {f$0}", ImmediateLocation::UseTree);
}
#[test]
#[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);
+ check_location(r"fn my_fn() { let a = 2; f$0 }", ImmediateLocation::StmtList);
+ check_location(r"fn my_fn() { f$0 f }", ImmediateLocation::StmtList);
}
#[test]
check_prev_sibling(r"fn foo() { if true {} w$0", ImmediatePrevSibling::IfExpr);
check_prev_sibling(r"fn foo() { if true {}; w$0", None);
}
+
+ #[test]
+ fn test_vis_prev_sibling() {
+ check_prev_sibling(r"pub w$0", ImmediatePrevSibling::Visibility);
+ }
+
+ #[test]
+ fn test_attr_prev_sibling() {
+ check_prev_sibling(r"#[attr] w$0", ImmediatePrevSibling::Attribute);
+ }
}