feat: Add break and return postfix keyword completions

This commit is contained in:
Lukas Wirth 2024-01-30 21:04:37 +01:00
parent 6655960186
commit 15bffe25bd
15 changed files with 256 additions and 116 deletions

View file

@ -4,7 +4,10 @@ use ide_db::FxHashSet;
use syntax::SmolStr; use syntax::SmolStr;
use crate::{ use crate::{
context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified}, context::{
CompletionContext, DotAccess, DotAccessExprCtx, DotAccessKind, PathCompletionCtx,
PathExprCtx, Qualified,
},
CompletionItem, CompletionItemKind, Completions, CompletionItem, CompletionItemKind, Completions,
}; };
@ -51,7 +54,7 @@ pub(crate) fn complete_undotted_self(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
path_ctx: &PathCompletionCtx, path_ctx: &PathCompletionCtx,
expr_ctx: &ExprCtx, expr_ctx: &PathExprCtx,
) { ) {
if !ctx.config.enable_self_on_the_fly { if !ctx.config.enable_self_on_the_fly {
return; return;
@ -66,7 +69,7 @@ pub(crate) fn complete_undotted_self(
return; return;
} }
let self_param = match expr_ctx { let self_param = match expr_ctx {
ExprCtx { self_param: Some(self_param), .. } => self_param, PathExprCtx { self_param: Some(self_param), .. } => self_param,
_ => return, _ => return,
}; };
@ -82,6 +85,10 @@ pub(crate) fn complete_undotted_self(
receiver: None, receiver: None,
receiver_ty: None, receiver_ty: None,
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false }, kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
ctx: DotAccessExprCtx {
in_block_expr: expr_ctx.in_block_expr,
in_breakable: expr_ctx.in_breakable,
},
}, },
Some(hir::known::SELF_PARAM), Some(hir::known::SELF_PARAM),
field, field,
@ -99,6 +106,10 @@ pub(crate) fn complete_undotted_self(
receiver: None, receiver: None,
receiver_ty: None, receiver_ty: None,
kind: DotAccessKind::Method { has_parens: false }, kind: DotAccessKind::Method { has_parens: false },
ctx: DotAccessExprCtx {
in_block_expr: expr_ctx.in_block_expr,
in_breakable: expr_ctx.in_breakable,
},
}, },
func, func,
Some(hir::known::SELF_PARAM), Some(hir::known::SELF_PARAM),

View file

@ -5,7 +5,7 @@ use syntax::ast;
use crate::{ use crate::{
completions::record::add_default_update, completions::record::add_default_update,
context::{ExprCtx, PathCompletionCtx, Qualified}, context::{BreakableKind, PathCompletionCtx, PathExprCtx, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -13,16 +13,16 @@ pub(crate) fn complete_expr_path(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx, path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
expr_ctx: &ExprCtx, expr_ctx: &PathExprCtx,
) { ) {
let _p = tracing::span!(tracing::Level::INFO, "complete_expr_path").entered(); let _p = tracing::span!(tracing::Level::INFO, "complete_expr_path").entered();
if !ctx.qualifier_ctx.none() { if !ctx.qualifier_ctx.none() {
return; return;
} }
let &ExprCtx { let &PathExprCtx {
in_block_expr, in_block_expr,
in_loop_body, in_breakable,
after_if_expr, after_if_expr,
in_condition, in_condition,
incomplete_let, incomplete_let,
@ -290,7 +290,7 @@ pub(crate) fn complete_expr_path(
add_keyword("mut", "mut "); add_keyword("mut", "mut ");
} }
if in_loop_body { if in_breakable != BreakableKind::None {
if in_block_expr { if in_block_expr {
add_keyword("continue", "continue;"); add_keyword("continue", "continue;");
add_keyword("break", "break;"); add_keyword("break", "break;");

View file

@ -1,7 +1,7 @@
//! Completion of paths and keywords at item list position. //! Completion of paths and keywords at item list position.
use crate::{ use crate::{
context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified}, context::{ItemListKind, PathCompletionCtx, PathExprCtx, Qualified},
CompletionContext, Completions, CompletionContext, Completions,
}; };
@ -11,7 +11,7 @@ pub(crate) fn complete_item_list_in_expr(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
path_ctx: &PathCompletionCtx, path_ctx: &PathCompletionCtx,
expr_ctx: &ExprCtx, expr_ctx: &PathExprCtx,
) { ) {
if !expr_ctx.in_block_expr { if !expr_ctx.in_block_expr {
return; return;

View file

@ -81,11 +81,13 @@ fn foo(a: A) { a.$0 }
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -106,11 +108,13 @@ fn foo() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -133,11 +137,13 @@ fn foo(a: A) { a.$0 }
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );

View file

@ -2,6 +2,7 @@
mod format_like; mod format_like;
use hir::ItemInNs;
use ide_db::{ use ide_db::{
documentation::{Documentation, HasDocs}, documentation::{Documentation, HasDocs},
imports::insert_use::ImportScope, imports::insert_use::ImportScope,
@ -17,7 +18,7 @@ use text_edit::TextEdit;
use crate::{ use crate::{
completions::postfix::format_like::add_format_like_completions, completions::postfix::format_like::add_format_like_completions,
context::{CompletionContext, DotAccess, DotAccessKind}, context::{BreakableKind, CompletionContext, DotAccess, DotAccessKind},
item::{Builder, CompletionRelevancePostfixMatch}, item::{Builder, CompletionRelevancePostfixMatch},
CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope, CompletionItem, CompletionItemKind, CompletionRelevance, Completions, SnippetScope,
}; };
@ -44,6 +45,7 @@ pub(crate) fn complete_postfix(
), ),
_ => return, _ => return,
}; };
let expr_ctx = &dot_access.ctx;
let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal); let receiver_text = get_receiver_text(dot_receiver, receiver_is_ambiguous_float_literal);
@ -59,16 +61,22 @@ pub(crate) fn complete_postfix(
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() { if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) { if receiver_ty.impls_trait(ctx.db, drop_trait, &[]) {
if let &[hir::AssocItem::Function(drop_fn)] = &*drop_trait.items(ctx.db) { if let Some(drop_fn) = ctx.famous_defs().core_mem_drop() {
cov_mark::hit!(postfix_drop_completion); if let Some(path) = ctx.module.find_use_path(
// FIXME: check that `drop` is in scope, use fully qualified path if it isn't/if shadowed ctx.db,
let mut item = postfix_snippet( ItemInNs::Values(drop_fn.into()),
"drop", ctx.config.prefer_no_std,
"fn drop(&mut self)", ctx.config.prefer_prelude,
&format!("drop($0{receiver_text})"), ) {
); cov_mark::hit!(postfix_drop_completion);
item.set_documentation(drop_fn.docs(ctx.db)); let mut item = postfix_snippet(
item.add_to(acc, ctx.db); "drop",
"fn drop(&mut self)",
&format!("{path}($0{receiver_text})", path = path.display(ctx.db)),
);
item.set_documentation(drop_fn.docs(ctx.db));
item.add_to(acc, ctx.db);
}
} }
} }
} }
@ -140,6 +148,7 @@ pub(crate) fn complete_postfix(
postfix_snippet("ref", "&expr", &format!("&{receiver_text}")).add_to(acc, ctx.db); postfix_snippet("ref", "&expr", &format!("&{receiver_text}")).add_to(acc, ctx.db);
postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc, ctx.db); postfix_snippet("refm", "&mut expr", &format!("&mut {receiver_text}")).add_to(acc, ctx.db);
postfix_snippet("deref", "*expr", &format!("*{receiver_text}")).add_to(acc, ctx.db);
let mut unsafe_should_be_wrapped = true; let mut unsafe_should_be_wrapped = true;
if dot_receiver.syntax().kind() == BLOCK_EXPR { if dot_receiver.syntax().kind() == BLOCK_EXPR {
@ -224,6 +233,28 @@ pub(crate) fn complete_postfix(
add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text); add_format_like_completions(acc, ctx, &dot_receiver, cap, &literal_text);
} }
} }
postfix_snippet(
"return",
"return expr",
&format!(
"return {receiver_text}{semi}",
semi = if expr_ctx.in_block_expr { ";" } else { "" }
),
)
.add_to(acc, ctx.db);
if let BreakableKind::Block | BreakableKind::Loop = expr_ctx.in_breakable {
postfix_snippet(
"break",
"break expr",
&format!(
"break {receiver_text}{semi}",
semi = if expr_ctx.in_block_expr { ";" } else { "" }
),
)
.add_to(acc, ctx.db);
}
} }
fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String { fn get_receiver_text(receiver: &ast::Expr, receiver_is_ambiguous_float_literal: bool) -> String {
@ -368,6 +399,7 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn if if expr {} sn if if expr {}
sn let let sn let let
sn letm let mut sn letm let mut
@ -375,6 +407,7 @@ fn main() {
sn not !expr sn not !expr
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
sn while while expr {} sn while while expr {}
"#]], "#]],
@ -399,11 +432,13 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn if if expr {} sn if if expr {}
sn match match expr {} sn match match expr {}
sn not !expr sn not !expr
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
sn while while expr {} sn while while expr {}
"#]], "#]],
@ -424,11 +459,13 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
) )
@ -448,6 +485,7 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn if if expr {} sn if if expr {}
sn let let sn let let
sn letm let mut sn letm let mut
@ -455,6 +493,7 @@ fn main() {
sn not !expr sn not !expr
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
sn while while expr {} sn while while expr {}
"#]], "#]],

View file

@ -6,7 +6,7 @@ use syntax::{
}; };
use crate::{ use crate::{
context::{DotAccess, DotAccessKind, PatternContext}, context::{DotAccess, DotAccessExprCtx, DotAccessKind, PatternContext},
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance, CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance,
CompletionRelevancePostfixMatch, Completions, CompletionRelevancePostfixMatch, Completions,
}; };
@ -118,12 +118,17 @@ fn complete_fields(
missing_fields: Vec<(hir::Field, hir::Type)>, missing_fields: Vec<(hir::Field, hir::Type)>,
) { ) {
for (field, ty) in missing_fields { for (field, ty) in missing_fields {
// This should call something else, we shouldn't be synthesizing a DotAccess here
acc.add_field( acc.add_field(
ctx, ctx,
&DotAccess { &DotAccess {
receiver: None, receiver: None,
receiver_ty: None, receiver_ty: None,
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false }, kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal: false },
ctx: DotAccessExprCtx {
in_block_expr: false,
in_breakable: crate::context::BreakableKind::None,
},
}, },
None, None,
field, field,

View file

@ -3,7 +3,7 @@
use ide_db::{documentation::Documentation, imports::insert_use::ImportScope, SnippetCap}; use ide_db::{documentation::Documentation, imports::insert_use::ImportScope, SnippetCap};
use crate::{ use crate::{
context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified}, context::{ItemListKind, PathCompletionCtx, PathExprCtx, Qualified},
item::Builder, item::Builder,
CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope, CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope,
}; };
@ -12,7 +12,7 @@ pub(crate) fn complete_expr_snippet(
acc: &mut Completions, acc: &mut Completions,
ctx: &CompletionContext<'_>, ctx: &CompletionContext<'_>,
path_ctx: &PathCompletionCtx, path_ctx: &PathCompletionCtx,
&ExprCtx { in_block_expr, .. }: &ExprCtx, &PathExprCtx { in_block_expr, .. }: &PathExprCtx,
) { ) {
if !matches!(path_ctx.qualified, Qualified::No) { if !matches!(path_ctx.qualified, Qualified::No) {
return; return;

View file

@ -45,13 +45,13 @@ pub(crate) enum Visible {
/// Existing qualifiers for the thing we are currently completing. /// Existing qualifiers for the thing we are currently completing.
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(super) struct QualifierCtx { pub(crate) struct QualifierCtx {
pub(super) unsafe_tok: Option<SyntaxToken>, pub(crate) unsafe_tok: Option<SyntaxToken>,
pub(super) vis_node: Option<ast::Visibility>, pub(crate) vis_node: Option<ast::Visibility>,
} }
impl QualifierCtx { impl QualifierCtx {
pub(super) fn none(&self) -> bool { pub(crate) fn none(&self) -> bool {
self.unsafe_tok.is_none() && self.vis_node.is_none() self.unsafe_tok.is_none() && self.vis_node.is_none()
} }
} }
@ -60,27 +60,27 @@ impl QualifierCtx {
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct PathCompletionCtx { pub(crate) struct PathCompletionCtx {
/// If this is a call with () already there (or {} in case of record patterns) /// If this is a call with () already there (or {} in case of record patterns)
pub(super) has_call_parens: bool, pub(crate) has_call_parens: bool,
/// If this has a macro call bang ! /// If this has a macro call bang !
pub(super) has_macro_bang: bool, pub(crate) has_macro_bang: bool,
/// The qualifier of the current path. /// The qualifier of the current path.
pub(super) qualified: Qualified, pub(crate) qualified: Qualified,
/// The parent of the path we are completing. /// The parent of the path we are completing.
pub(super) parent: Option<ast::Path>, pub(crate) parent: Option<ast::Path>,
#[allow(dead_code)] #[allow(dead_code)]
/// The path of which we are completing the segment /// The path of which we are completing the segment
pub(super) path: ast::Path, pub(crate) path: ast::Path,
/// The path of which we are completing the segment in the original file /// The path of which we are completing the segment in the original file
pub(crate) original_path: Option<ast::Path>, pub(crate) original_path: Option<ast::Path>,
pub(super) kind: PathKind, pub(crate) kind: PathKind,
/// Whether the path segment has type args or not. /// Whether the path segment has type args or not.
pub(super) has_type_args: bool, pub(crate) has_type_args: bool,
/// Whether the qualifier comes from a use tree parent or not /// Whether the qualifier comes from a use tree parent or not
pub(crate) use_tree_parent: bool, pub(crate) use_tree_parent: bool,
} }
impl PathCompletionCtx { impl PathCompletionCtx {
pub(super) fn is_trivial_path(&self) -> bool { pub(crate) fn is_trivial_path(&self) -> bool {
matches!( matches!(
self, self,
PathCompletionCtx { PathCompletionCtx {
@ -97,9 +97,9 @@ impl PathCompletionCtx {
/// The kind of path we are completing right now. /// The kind of path we are completing right now.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(super) enum PathKind { pub(crate) enum PathKind {
Expr { Expr {
expr_ctx: ExprCtx, expr_ctx: PathExprCtx,
}, },
Type { Type {
location: TypeLocation, location: TypeLocation,
@ -132,9 +132,9 @@ pub(crate) struct AttrCtx {
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(crate) struct ExprCtx { pub(crate) struct PathExprCtx {
pub(crate) in_block_expr: bool, pub(crate) in_block_expr: bool,
pub(crate) in_loop_body: bool, pub(crate) in_breakable: BreakableKind,
pub(crate) after_if_expr: bool, pub(crate) after_if_expr: bool,
/// Whether this expression is the direct condition of an if or while expression /// Whether this expression is the direct condition of an if or while expression
pub(crate) in_condition: bool, pub(crate) in_condition: bool,
@ -221,7 +221,7 @@ pub(crate) enum TypeAscriptionTarget {
/// The kind of item list a [`PathKind::Item`] belongs to. /// The kind of item list a [`PathKind::Item`] belongs to.
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub(super) enum ItemListKind { pub(crate) enum ItemListKind {
SourceFile, SourceFile,
Module, Module,
Impl, Impl,
@ -231,7 +231,7 @@ pub(super) enum ItemListKind {
} }
#[derive(Debug)] #[derive(Debug)]
pub(super) enum Qualified { pub(crate) enum Qualified {
No, No,
With { With {
path: ast::Path, path: ast::Path,
@ -259,37 +259,37 @@ pub(super) enum Qualified {
/// The state of the pattern we are completing. /// The state of the pattern we are completing.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct PatternContext { pub(crate) struct PatternContext {
pub(super) refutability: PatternRefutability, pub(crate) refutability: PatternRefutability,
pub(super) param_ctx: Option<ParamContext>, pub(crate) param_ctx: Option<ParamContext>,
pub(super) has_type_ascription: bool, pub(crate) has_type_ascription: bool,
pub(super) parent_pat: Option<ast::Pat>, pub(crate) parent_pat: Option<ast::Pat>,
pub(super) ref_token: Option<SyntaxToken>, pub(crate) ref_token: Option<SyntaxToken>,
pub(super) mut_token: Option<SyntaxToken>, pub(crate) mut_token: Option<SyntaxToken>,
/// The record pattern this name or ref is a field of /// The record pattern this name or ref is a field of
pub(super) record_pat: Option<ast::RecordPat>, pub(crate) record_pat: Option<ast::RecordPat>,
pub(super) impl_: Option<ast::Impl>, pub(crate) impl_: Option<ast::Impl>,
/// List of missing variants in a match expr /// List of missing variants in a match expr
pub(super) missing_variants: Vec<hir::Variant>, pub(crate) missing_variants: Vec<hir::Variant>,
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ParamContext { pub(crate) struct ParamContext {
pub(super) param_list: ast::ParamList, pub(crate) param_list: ast::ParamList,
pub(super) param: ast::Param, pub(crate) param: ast::Param,
pub(super) kind: ParamKind, pub(crate) kind: ParamKind,
} }
/// The state of the lifetime we are completing. /// The state of the lifetime we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct LifetimeContext { pub(crate) struct LifetimeContext {
pub(super) lifetime: Option<ast::Lifetime>, pub(crate) lifetime: Option<ast::Lifetime>,
pub(super) kind: LifetimeKind, pub(crate) kind: LifetimeKind,
} }
/// The kind of lifetime we are completing. /// The kind of lifetime we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) enum LifetimeKind { pub(crate) enum LifetimeKind {
LifetimeParam { is_decl: bool, param: ast::LifetimeParam }, LifetimeParam { is_decl: bool, param: ast::LifetimeParam },
Lifetime, Lifetime,
LabelRef, LabelRef,
@ -298,16 +298,16 @@ pub(super) enum LifetimeKind {
/// The state of the name we are completing. /// The state of the name we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct NameContext { pub(crate) struct NameContext {
#[allow(dead_code)] #[allow(dead_code)]
pub(super) name: Option<ast::Name>, pub(crate) name: Option<ast::Name>,
pub(super) kind: NameKind, pub(crate) kind: NameKind,
} }
/// The kind of the name we are completing. /// The kind of the name we are completing.
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] #[allow(dead_code)]
pub(super) enum NameKind { pub(crate) enum NameKind {
Const, Const,
ConstParam, ConstParam,
Enum, Enum,
@ -331,15 +331,15 @@ pub(super) enum NameKind {
/// The state of the NameRef we are completing. /// The state of the NameRef we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct NameRefContext { pub(crate) struct NameRefContext {
/// NameRef syntax in the original file /// NameRef syntax in the original file
pub(super) nameref: Option<ast::NameRef>, pub(crate) nameref: Option<ast::NameRef>,
pub(super) kind: NameRefKind, pub(crate) kind: NameRefKind,
} }
/// The kind of the NameRef we are completing. /// The kind of the NameRef we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) enum NameRefKind { pub(crate) enum NameRefKind {
Path(PathCompletionCtx), Path(PathCompletionCtx),
DotAccess(DotAccess), DotAccess(DotAccess),
/// Position where we are only interested in keyword completions /// Position where we are only interested in keyword completions
@ -355,7 +355,7 @@ pub(super) enum NameRefKind {
/// The identifier we are currently completing. /// The identifier we are currently completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) enum CompletionAnalysis { pub(crate) enum CompletionAnalysis {
Name(NameContext), Name(NameContext),
NameRef(NameRefContext), NameRef(NameRefContext),
Lifetime(LifetimeContext), Lifetime(LifetimeContext),
@ -376,14 +376,15 @@ pub(super) enum CompletionAnalysis {
/// Information about the field or method access we are completing. /// Information about the field or method access we are completing.
#[derive(Debug)] #[derive(Debug)]
pub(super) struct DotAccess { pub(crate) struct DotAccess {
pub(super) receiver: Option<ast::Expr>, pub(crate) receiver: Option<ast::Expr>,
pub(super) receiver_ty: Option<TypeInfo>, pub(crate) receiver_ty: Option<TypeInfo>,
pub(super) kind: DotAccessKind, pub(crate) kind: DotAccessKind,
pub(crate) ctx: DotAccessExprCtx,
} }
#[derive(Debug)] #[derive(Debug)]
pub(super) enum DotAccessKind { pub(crate) enum DotAccessKind {
Field { Field {
/// True if the receiver is an integer and there is no ident in the original file after it yet /// True if the receiver is an integer and there is no ident in the original file after it yet
/// like `0.$0` /// like `0.$0`
@ -394,6 +395,21 @@ pub(super) enum DotAccessKind {
}, },
} }
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct DotAccessExprCtx {
pub(crate) in_block_expr: bool,
pub(crate) in_breakable: BreakableKind,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum BreakableKind {
None,
Loop,
For,
While,
Block,
}
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum ParamKind { pub(crate) enum ParamKind {
Function(ast::Fn), Function(ast::Fn),
@ -404,39 +420,39 @@ pub(crate) enum ParamKind {
/// exactly is the cursor, syntax-wise. /// exactly is the cursor, syntax-wise.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct CompletionContext<'a> { pub(crate) struct CompletionContext<'a> {
pub(super) sema: Semantics<'a, RootDatabase>, pub(crate) sema: Semantics<'a, RootDatabase>,
pub(super) scope: SemanticsScope<'a>, pub(crate) scope: SemanticsScope<'a>,
pub(super) db: &'a RootDatabase, pub(crate) db: &'a RootDatabase,
pub(super) config: &'a CompletionConfig, pub(crate) config: &'a CompletionConfig,
pub(super) position: FilePosition, pub(crate) position: FilePosition,
/// The token before the cursor, in the original file. /// The token before the cursor, in the original file.
pub(super) original_token: SyntaxToken, pub(crate) original_token: SyntaxToken,
/// The token before the cursor, in the macro-expanded file. /// The token before the cursor, in the macro-expanded file.
pub(super) token: SyntaxToken, pub(crate) token: SyntaxToken,
/// The crate of the current file. /// The crate of the current file.
pub(super) krate: hir::Crate, pub(crate) krate: hir::Crate,
/// The module of the `scope`. /// The module of the `scope`.
pub(super) module: hir::Module, pub(crate) module: hir::Module,
/// Whether nightly toolchain is used. Cached since this is looked up a lot. /// Whether nightly toolchain is used. Cached since this is looked up a lot.
is_nightly: bool, is_nightly: bool,
/// The expected name of what we are completing. /// The expected name of what we are completing.
/// This is usually the parameter name of the function argument we are completing. /// This is usually the parameter name of the function argument we are completing.
pub(super) expected_name: Option<NameOrNameRef>, pub(crate) expected_name: Option<NameOrNameRef>,
/// The expected type of what we are completing. /// The expected type of what we are completing.
pub(super) expected_type: Option<Type>, pub(crate) expected_type: Option<Type>,
pub(super) qualifier_ctx: QualifierCtx, pub(crate) qualifier_ctx: QualifierCtx,
pub(super) locals: FxHashMap<Name, Local>, pub(crate) locals: FxHashMap<Name, Local>,
/// The module depth of the current module of the cursor position. /// The module depth of the current module of the cursor position.
/// - crate-root /// - crate-root
/// - mod foo /// - mod foo
/// - mod bar /// - mod bar
/// Here depth will be 2 /// Here depth will be 2
pub(super) depth_from_crate_root: usize, pub(crate) depth_from_crate_root: usize,
} }
impl CompletionContext<'_> { impl CompletionContext<'_> {
@ -634,7 +650,7 @@ impl CompletionContext<'_> {
// CompletionContext construction // CompletionContext construction
impl<'a> CompletionContext<'a> { impl<'a> CompletionContext<'a> {
pub(super) fn new( pub(crate) fn new(
db: &'a RootDatabase, db: &'a RootDatabase,
position @ FilePosition { file_id, offset }: FilePosition, position @ FilePosition { file_id, offset }: FilePosition,
config: &'a CompletionConfig, config: &'a CompletionConfig,

View file

@ -11,10 +11,11 @@ use syntax::{
}; };
use crate::context::{ use crate::context::{
AttrCtx, CompletionAnalysis, DotAccess, DotAccessKind, ExprCtx, ItemListKind, LifetimeContext, AttrCtx, BreakableKind, CompletionAnalysis, DotAccess, DotAccessExprCtx, DotAccessKind,
LifetimeKind, NameContext, NameKind, NameRefContext, NameRefKind, ParamContext, ParamKind, ItemListKind, LifetimeContext, LifetimeKind, NameContext, NameKind, NameRefContext,
PathCompletionCtx, PathKind, PatternContext, PatternRefutability, Qualified, QualifierCtx, NameRefKind, ParamContext, ParamKind, PathCompletionCtx, PathExprCtx, PathKind, PatternContext,
TypeAscriptionTarget, TypeLocation, COMPLETION_MARKER, PatternRefutability, Qualified, QualifierCtx, TypeAscriptionTarget, TypeLocation,
COMPLETION_MARKER,
}; };
struct ExpansionResult { struct ExpansionResult {
@ -623,7 +624,8 @@ fn classify_name_ref(
let kind = NameRefKind::DotAccess(DotAccess { let kind = NameRefKind::DotAccess(DotAccess {
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)), receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal }, kind: DotAccessKind::Field { receiver_is_ambiguous_float_literal },
receiver receiver,
ctx: DotAccessExprCtx { in_block_expr: is_in_block(field.syntax()), in_breakable: is_in_breakable(field.syntax()) }
}); });
return Some(make_res(kind)); return Some(make_res(kind));
}, },
@ -636,7 +638,8 @@ fn classify_name_ref(
let kind = NameRefKind::DotAccess(DotAccess { let kind = NameRefKind::DotAccess(DotAccess {
receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)), receiver_ty: receiver.as_ref().and_then(|it| sema.type_of_expr(it)),
kind: DotAccessKind::Method { has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some()) }, kind: DotAccessKind::Method { has_parens: method.arg_list().map_or(false, |it| it.l_paren_token().is_some()) },
receiver receiver,
ctx: DotAccessExprCtx { in_block_expr: is_in_block(method.syntax()), in_breakable: is_in_breakable(method.syntax()) }
}); });
return Some(make_res(kind)); return Some(make_res(kind));
}, },
@ -659,13 +662,6 @@ fn classify_name_ref(
use_tree_parent: false, use_tree_parent: false,
}; };
let is_in_block = |it: &SyntaxNode| {
it.parent()
.map(|node| {
ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind())
})
.unwrap_or(false)
};
let func_update_record = |syn: &SyntaxNode| { let func_update_record = |syn: &SyntaxNode| {
if let Some(record_expr) = syn.ancestors().nth(2).and_then(ast::RecordExpr::cast) { if let Some(record_expr) = syn.ancestors().nth(2).and_then(ast::RecordExpr::cast) {
find_node_in_file_compensated(sema, original_file, &record_expr) find_node_in_file_compensated(sema, original_file, &record_expr)
@ -932,7 +928,7 @@ fn classify_name_ref(
let make_path_kind_expr = |expr: ast::Expr| { let make_path_kind_expr = |expr: ast::Expr| {
let it = expr.syntax(); let it = expr.syntax();
let in_block_expr = is_in_block(it); let in_block_expr = is_in_block(it);
let in_loop_body = is_in_loop_body(it); let in_loop_body = is_in_breakable(it);
let after_if_expr = after_if_expr(it.clone()); let after_if_expr = after_if_expr(it.clone());
let ref_expr_parent = let ref_expr_parent =
path.as_single_name_ref().and_then(|_| it.parent()).and_then(ast::RefExpr::cast); path.as_single_name_ref().and_then(|_| it.parent()).and_then(ast::RefExpr::cast);
@ -998,9 +994,9 @@ fn classify_name_ref(
}; };
PathKind::Expr { PathKind::Expr {
expr_ctx: ExprCtx { expr_ctx: PathExprCtx {
in_block_expr, in_block_expr,
in_loop_body, in_breakable: in_loop_body,
after_if_expr, after_if_expr,
in_condition, in_condition,
ref_expr_parent, ref_expr_parent,
@ -1202,7 +1198,7 @@ fn classify_name_ref(
if path_ctx.is_trivial_path() { if path_ctx.is_trivial_path() {
// fetch the full expression that may have qualifiers attached to it // fetch the full expression that may have qualifiers attached to it
let top_node = match path_ctx.kind { let top_node = match path_ctx.kind {
PathKind::Expr { expr_ctx: ExprCtx { in_block_expr: true, .. } } => { PathKind::Expr { expr_ctx: PathExprCtx { in_block_expr: true, .. } } => {
parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| { parent.ancestors().find(|it| ast::PathExpr::can_cast(it.kind())).and_then(|p| {
let parent = p.parent()?; let parent = p.parent()?;
if ast::StmtList::can_cast(parent.kind()) { if ast::StmtList::can_cast(parent.kind()) {
@ -1467,21 +1463,30 @@ fn is_in_token_of_for_loop(path: &ast::Path) -> bool {
.unwrap_or(false) .unwrap_or(false)
} }
fn is_in_loop_body(node: &SyntaxNode) -> bool { fn is_in_breakable(node: &SyntaxNode) -> BreakableKind {
node.ancestors() node.ancestors()
.take_while(|it| it.kind() != SyntaxKind::FN && it.kind() != SyntaxKind::CLOSURE_EXPR) .take_while(|it| it.kind() != SyntaxKind::FN && it.kind() != SyntaxKind::CLOSURE_EXPR)
.find_map(|it| { .find_map(|it| {
let loop_body = match_ast! { let (breakable, loop_body) = match_ast! {
match it { match it {
ast::ForExpr(it) => it.loop_body(), ast::ForExpr(it) => (BreakableKind::For, it.loop_body()),
ast::WhileExpr(it) => it.loop_body(), ast::WhileExpr(it) => (BreakableKind::While, it.loop_body()),
ast::LoopExpr(it) => it.loop_body(), ast::LoopExpr(it) => (BreakableKind::Loop, it.loop_body()),
_ => None, ast::BlockExpr(it) => return it.label().map(|_| BreakableKind::Block),
_ => return None,
} }
}; };
loop_body.filter(|it| it.syntax().text_range().contains_range(node.text_range())) loop_body
.filter(|it| it.syntax().text_range().contains_range(node.text_range()))
.map(|_| breakable)
}) })
.is_some() .unwrap_or(BreakableKind::None)
}
fn is_in_block(node: &SyntaxNode) -> bool {
node.parent()
.map(|node| ast::ExprStmt::can_cast(node.kind()) || ast::StmtList::can_cast(node.kind()))
.unwrap_or(false)
} }
fn previous_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> { fn previous_non_trivia_token(e: impl Into<SyntaxElement>) -> Option<SyntaxToken> {

View file

@ -2199,12 +2199,14 @@ fn main() {
sn while [] sn while []
sn ref [] sn ref []
sn refm [] sn refm []
sn deref []
sn unsafe [] sn unsafe []
sn match [] sn match []
sn box [] sn box []
sn dbg [] sn dbg []
sn dbgr [] sn dbgr []
sn call [] sn call []
sn return []
"#]], "#]],
); );
} }
@ -2227,6 +2229,7 @@ fn main() {
me f() [] me f() []
sn ref [] sn ref []
sn refm [] sn refm []
sn deref []
sn unsafe [] sn unsafe []
sn match [] sn match []
sn box [] sn box []
@ -2235,6 +2238,7 @@ fn main() {
sn call [] sn call []
sn let [] sn let []
sn letm [] sn letm []
sn return []
"#]], "#]],
); );
} }

View file

@ -362,6 +362,27 @@ fn completes_in_loop_ctx() {
sn ppd sn ppd
"#]], "#]],
); );
check_empty(
r"fn my() { loop { foo.$0 } }",
expect![[r#"
sn box Box::new(expr)
sn break break expr
sn call function(expr)
sn dbg dbg!(expr)
sn dbgr dbg!(&expr)
sn deref *expr
sn if if expr {}
sn let let
sn letm let mut
sn match match expr {}
sn not !expr
sn ref &expr
sn refm &mut expr
sn return return expr
sn unsafe unsafe {}
sn while while expr {}
"#]],
);
} }
#[test] #[test]
@ -1115,9 +1136,11 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -1139,9 +1162,11 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -1167,9 +1192,11 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -1191,9 +1218,11 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -1215,9 +1244,11 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );
@ -1238,11 +1269,13 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn if if expr {} sn if if expr {}
sn match match expr {} sn match match expr {}
sn not !expr sn not !expr
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
sn while while expr {} sn while while expr {}
"#]], "#]],

View file

@ -29,11 +29,13 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
) )
@ -60,11 +62,13 @@ fn main() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
) )
@ -93,11 +97,13 @@ fn main() {}
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
) )
@ -126,11 +132,13 @@ fn main() {}
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
) )

View file

@ -1157,11 +1157,13 @@ fn here_we_go() {
sn call function(expr) sn call function(expr)
sn dbg dbg!(expr) sn dbg dbg!(expr)
sn dbgr dbg!(&expr) sn dbgr dbg!(&expr)
sn deref *expr
sn let let sn let let
sn letm let mut sn letm let mut
sn match match expr {} sn match match expr {}
sn ref &expr sn ref &expr
sn refm &mut expr sn refm &mut expr
sn return return expr
sn unsafe unsafe {} sn unsafe unsafe {}
"#]], "#]],
); );

View file

@ -1,7 +1,7 @@
//! See [`FamousDefs`]. //! See [`FamousDefs`].
use base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase}; use base_db::{CrateOrigin, LangCrateOrigin, SourceDatabase};
use hir::{Crate, Enum, Macro, Module, ScopeDef, Semantics, Trait}; use hir::{Crate, Enum, Function, Macro, Module, ScopeDef, Semantics, Trait};
use crate::RootDatabase; use crate::RootDatabase;
@ -110,6 +110,10 @@ impl FamousDefs<'_, '_> {
self.find_macro("core:macros:builtin:derive") self.find_macro("core:macros:builtin:derive")
} }
pub fn core_mem_drop(&self) -> Option<Function> {
self.find_function("core:mem:drop")
}
pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> { pub fn builtin_crates(&self) -> impl Iterator<Item = Crate> {
IntoIterator::into_iter([ IntoIterator::into_iter([
self.std(), self.std(),
@ -149,6 +153,13 @@ impl FamousDefs<'_, '_> {
} }
} }
fn find_function(&self, path: &str) -> Option<Function> {
match self.find_def(path)? {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(it)) => Some(it),
_ => None,
}
}
fn find_lang_crate(&self, origin: LangCrateOrigin) -> Option<Crate> { fn find_lang_crate(&self, origin: LangCrateOrigin) -> Option<Crate> {
let krate = self.1; let krate = self.1;
let db = self.0.db; let db = self.0.db;

View file

@ -328,7 +328,6 @@ pub mod convert {
} }
pub mod mem { pub mod mem {
// region:drop
// region:manually_drop // region:manually_drop
#[lang = "manually_drop"] #[lang = "manually_drop"]
#[repr(transparent)] #[repr(transparent)]
@ -353,6 +352,7 @@ pub mod mem {
// endregion:manually_drop // endregion:manually_drop
// region:drop
pub fn drop<T>(_x: T) {} pub fn drop<T>(_x: T) {}
pub const fn replace<T>(dest: &mut T, src: T) -> T { pub const fn replace<T>(dest: &mut T, src: T) -> T {
unsafe { unsafe {