mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
fix: Fix snippets triggering where they shouldn't
This commit is contained in:
parent
582f99d293
commit
0ce620686c
10 changed files with 133 additions and 101 deletions
|
@ -1370,11 +1370,12 @@ impl Function {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
|
pub fn has_self_param(self, db: &dyn HirDatabase) -> bool {
|
||||||
if !db.function_data(self.id).has_self_param() {
|
db.function_data(self.id).has_self_param()
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
Some(SelfParam { func: self.id })
|
|
||||||
|
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
|
||||||
|
self.has_self_param(db).then(|| SelfParam { func: self.id })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
|
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
|
||||||
|
|
|
@ -42,7 +42,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
Some(PathCompletionCtx {
|
Some(PathCompletionCtx {
|
||||||
is_absolute_path: false,
|
is_absolute_path: false,
|
||||||
qualifier: None,
|
qualifier: None,
|
||||||
kind: PathKind::Expr,
|
kind: PathKind::Expr { .. },
|
||||||
..
|
..
|
||||||
}) if !ctx.is_path_disallowed() => {}
|
}) if !ctx.is_path_disallowed() => {}
|
||||||
_ => return,
|
_ => return,
|
||||||
|
|
|
@ -15,9 +15,12 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
let (&is_absolute_path, qualifier) = match &ctx.path_context {
|
let (&is_absolute_path, qualifier) = match &ctx.path_context {
|
||||||
Some(PathCompletionCtx { kind: PathKind::Expr, is_absolute_path, qualifier, .. }) => {
|
Some(PathCompletionCtx {
|
||||||
(is_absolute_path, qualifier)
|
kind: PathKind::Expr { .. },
|
||||||
}
|
is_absolute_path,
|
||||||
|
qualifier,
|
||||||
|
..
|
||||||
|
}) => (is_absolute_path, qualifier),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -161,12 +161,12 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
(_, ItemInNs::Types(hir::ModuleDef::Module(_))) => true,
|
(_, ItemInNs::Types(hir::ModuleDef::Module(_))) => true,
|
||||||
// and so are macros(except for attributes)
|
// and so are macros(except for attributes)
|
||||||
(
|
(
|
||||||
PathKind::Expr | PathKind::Type | PathKind::Item | PathKind::Pat,
|
PathKind::Expr { .. } | PathKind::Type | PathKind::Item { .. } | PathKind::Pat,
|
||||||
ItemInNs::Macros(mac),
|
ItemInNs::Macros(mac),
|
||||||
) => mac.is_fn_like(ctx.db),
|
) => mac.is_fn_like(ctx.db),
|
||||||
(PathKind::Item, _) => true,
|
(PathKind::Item { .. }, _) => true,
|
||||||
|
|
||||||
(PathKind::Expr, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
|
(PathKind::Expr { .. }, ItemInNs::Types(_) | ItemInNs::Values(_)) => true,
|
||||||
|
|
||||||
(PathKind::Pat, ItemInNs::Types(_)) => true,
|
(PathKind::Pat, ItemInNs::Types(_)) => true,
|
||||||
(PathKind::Pat, ItemInNs::Values(def)) => matches!(def, hir::ModuleDef::Const(_)),
|
(PathKind::Pat, ItemInNs::Values(def)) => matches!(def, hir::ModuleDef::Const(_)),
|
||||||
|
|
|
@ -13,9 +13,12 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
let (&is_absolute_path, qualifier) = match &ctx.path_context {
|
let (&is_absolute_path, qualifier) = match &ctx.path_context {
|
||||||
Some(PathCompletionCtx { kind: PathKind::Item, is_absolute_path, qualifier, .. }) => {
|
Some(PathCompletionCtx {
|
||||||
(is_absolute_path, qualifier)
|
kind: PathKind::Item { .. },
|
||||||
}
|
is_absolute_path,
|
||||||
|
qualifier,
|
||||||
|
..
|
||||||
|
}) => (is_absolute_path, qualifier),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -125,9 +125,11 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
||||||
}
|
}
|
||||||
|
|
||||||
let (can_be_stmt, in_loop_body) = match ctx.path_context {
|
let (can_be_stmt, in_loop_body) = match ctx.path_context {
|
||||||
Some(PathCompletionCtx { is_absolute_path: false, can_be_stmt, in_loop_body, .. }) => {
|
Some(PathCompletionCtx {
|
||||||
(can_be_stmt, in_loop_body)
|
is_absolute_path: false,
|
||||||
}
|
kind: PathKind::Expr { in_block_expr, in_loop_body, .. },
|
||||||
|
..
|
||||||
|
}) => (in_block_expr, in_loop_body),
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,9 @@ use ide_db::{imports::insert_use::ImportScope, SnippetCap};
|
||||||
use syntax::T;
|
use syntax::T;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
context::PathCompletionCtx, item::Builder, CompletionContext, CompletionItem,
|
context::{ItemListKind, PathCompletionCtx, PathKind},
|
||||||
CompletionItemKind, Completions, SnippetScope,
|
item::Builder,
|
||||||
|
CompletionContext, CompletionItem, CompletionItemKind, Completions, SnippetScope,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
|
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
|
||||||
|
@ -16,14 +17,13 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
if ctx.function_def.is_none() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let can_be_stmt = match ctx.path_context {
|
let can_be_stmt = match ctx.path_context {
|
||||||
Some(PathCompletionCtx {
|
Some(PathCompletionCtx {
|
||||||
is_absolute_path: false, qualifier: None, can_be_stmt, ..
|
is_absolute_path: false,
|
||||||
}) => can_be_stmt,
|
qualifier: None,
|
||||||
|
kind: PathKind::Expr { in_block_expr, .. },
|
||||||
|
..
|
||||||
|
}) => in_block_expr,
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -43,11 +43,16 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
if !(ctx.expects_item() || ctx.has_block_expr_parent())
|
let path_kind = match ctx.path_context {
|
||||||
|| ctx.previous_token_is(T![unsafe])
|
Some(PathCompletionCtx {
|
||||||
|| ctx.path_qual().is_some()
|
is_absolute_path: false,
|
||||||
|| ctx.has_unfinished_impl_or_trait_prev_sibling()
|
qualifier: None,
|
||||||
{
|
kind: kind @ (PathKind::Item { .. } | PathKind::Expr { in_block_expr: true, .. }),
|
||||||
|
..
|
||||||
|
}) => kind,
|
||||||
|
_ => return,
|
||||||
|
};
|
||||||
|
if ctx.previous_token_is(T![unsafe]) || ctx.has_unfinished_impl_or_trait_prev_sibling() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ctx.has_visibility_prev_sibling() {
|
if ctx.has_visibility_prev_sibling() {
|
||||||
|
@ -64,7 +69,8 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test-related snippets shouldn't be shown in blocks.
|
// Test-related snippets shouldn't be shown in blocks.
|
||||||
if !ctx.has_block_expr_parent() {
|
if let PathKind::Item { kind: ItemListKind::SourceFile | ItemListKind::Module, .. } = path_kind
|
||||||
|
{
|
||||||
let mut item = snippet(
|
let mut item = snippet(
|
||||||
ctx,
|
ctx,
|
||||||
cap,
|
cap,
|
||||||
|
@ -96,7 +102,9 @@ fn ${1:feature}() {
|
||||||
item.lookup_by("tfn");
|
item.lookup_by("tfn");
|
||||||
item.add_to(acc);
|
item.add_to(acc);
|
||||||
}
|
}
|
||||||
|
if let PathKind::Item { kind: ItemListKind::SourceFile | ItemListKind::Module, .. }
|
||||||
|
| PathKind::Expr { .. } = path_kind
|
||||||
|
{
|
||||||
let item = snippet(
|
let item = snippet(
|
||||||
ctx,
|
ctx,
|
||||||
cap,
|
cap,
|
||||||
|
@ -110,6 +118,7 @@ macro_rules! $1 {
|
||||||
);
|
);
|
||||||
item.add_to(acc);
|
item.add_to(acc);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn add_custom_completions(
|
fn add_custom_completions(
|
||||||
acc: &mut Completions,
|
acc: &mut Completions,
|
||||||
|
|
|
@ -56,19 +56,17 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
|
||||||
if let Some((kind, replacement_range, impl_def)) = completion_match(ctx) {
|
if let Some((kind, replacement_range, impl_def)) = completion_match(ctx) {
|
||||||
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
|
if let Some(hir_impl) = ctx.sema.to_def(&impl_def) {
|
||||||
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| {
|
get_missing_assoc_items(&ctx.sema, &impl_def).into_iter().for_each(|item| {
|
||||||
|
use self::ImplCompletionKind::*;
|
||||||
match (item, kind) {
|
match (item, kind) {
|
||||||
(
|
(hir::AssocItem::Function(func), All | Fn) => {
|
||||||
hir::AssocItem::Function(fn_item),
|
add_function_impl(acc, ctx, replacement_range, func, hir_impl)
|
||||||
ImplCompletionKind::All | ImplCompletionKind::Fn,
|
}
|
||||||
) => add_function_impl(acc, ctx, replacement_range, fn_item, hir_impl),
|
(hir::AssocItem::TypeAlias(type_alias), All | TypeAlias) => {
|
||||||
(
|
add_type_alias_impl(acc, ctx, replacement_range, type_alias)
|
||||||
hir::AssocItem::TypeAlias(type_item),
|
}
|
||||||
ImplCompletionKind::All | ImplCompletionKind::TypeAlias,
|
(hir::AssocItem::Const(const_), All | Const) => {
|
||||||
) => add_type_alias_impl(acc, ctx, replacement_range, type_item),
|
add_const_impl(acc, ctx, replacement_range, const_, hir_impl)
|
||||||
(
|
}
|
||||||
hir::AssocItem::Const(const_item),
|
|
||||||
ImplCompletionKind::All | ImplCompletionKind::Const,
|
|
||||||
) => add_const_impl(acc, ctx, replacement_range, const_item, hir_impl),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -76,6 +74,7 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: This should be lifted out so that we can do proper smart item keyword completions
|
||||||
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, TextRange, ast::Impl)> {
|
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, TextRange, ast::Impl)> {
|
||||||
let token = ctx.token.clone();
|
let token = ctx.token.clone();
|
||||||
|
|
||||||
|
@ -152,15 +151,15 @@ fn add_function_impl(
|
||||||
func: hir::Function,
|
func: hir::Function,
|
||||||
impl_def: hir::Impl,
|
impl_def: hir::Impl,
|
||||||
) {
|
) {
|
||||||
let fn_name = func.name(ctx.db).to_smol_str();
|
let fn_name = func.name(ctx.db);
|
||||||
|
|
||||||
let label = if func.assoc_fn_params(ctx.db).is_empty() {
|
let label = format!(
|
||||||
format!("fn {}()", fn_name)
|
"fn {}({})",
|
||||||
} else {
|
fn_name,
|
||||||
format!("fn {}(..)", fn_name)
|
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
|
||||||
};
|
);
|
||||||
|
|
||||||
let completion_kind = if func.self_param(ctx.db).is_some() {
|
let completion_kind = if func.has_self_param(ctx.db) {
|
||||||
CompletionItemKind::Method
|
CompletionItemKind::Method
|
||||||
} else {
|
} else {
|
||||||
CompletionItemKind::SymbolKind(SymbolKind::Function)
|
CompletionItemKind::SymbolKind(SymbolKind::Function)
|
||||||
|
|
|
@ -45,7 +45,10 @@ pub(crate) enum Visible {
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
pub(super) enum PathKind {
|
pub(super) enum PathKind {
|
||||||
Expr,
|
Expr {
|
||||||
|
in_block_expr: bool,
|
||||||
|
in_loop_body: bool,
|
||||||
|
},
|
||||||
Type,
|
Type,
|
||||||
Attr {
|
Attr {
|
||||||
kind: AttrKind,
|
kind: AttrKind,
|
||||||
|
@ -53,7 +56,9 @@ pub(super) enum PathKind {
|
||||||
},
|
},
|
||||||
Derive,
|
Derive,
|
||||||
/// Path in item position, that is inside an (Assoc)ItemList
|
/// Path in item position, that is inside an (Assoc)ItemList
|
||||||
Item,
|
Item {
|
||||||
|
kind: ItemListKind,
|
||||||
|
},
|
||||||
Pat,
|
Pat,
|
||||||
Vis {
|
Vis {
|
||||||
has_in_token: bool,
|
has_in_token: bool,
|
||||||
|
@ -61,6 +66,15 @@ pub(super) enum PathKind {
|
||||||
Use,
|
Use,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub(super) enum ItemListKind {
|
||||||
|
SourceFile,
|
||||||
|
Module,
|
||||||
|
Impl,
|
||||||
|
Trait,
|
||||||
|
ExternBlock,
|
||||||
|
}
|
||||||
|
|
||||||
#[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)
|
||||||
|
@ -78,9 +92,6 @@ pub(crate) struct PathCompletionCtx {
|
||||||
pub(super) kind: PathKind,
|
pub(super) 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(super) has_type_args: bool,
|
||||||
/// `true` if we are a statement or a last expr in the block.
|
|
||||||
pub(super) can_be_stmt: bool,
|
|
||||||
pub(super) in_loop_body: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -314,7 +325,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expects_expression(&self) -> bool {
|
pub(crate) fn expects_expression(&self) -> bool {
|
||||||
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Expr, .. }))
|
matches!(self.path_context, Some(PathCompletionCtx { kind: PathKind::Expr { .. }, .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expects_type(&self) -> bool {
|
pub(crate) fn expects_type(&self) -> bool {
|
||||||
|
@ -968,13 +979,18 @@ impl<'a> CompletionContext<'a> {
|
||||||
is_absolute_path: false,
|
is_absolute_path: false,
|
||||||
qualifier: None,
|
qualifier: None,
|
||||||
parent: path.parent_path(),
|
parent: path.parent_path(),
|
||||||
kind: PathKind::Item,
|
kind: PathKind::Item { kind: ItemListKind::SourceFile },
|
||||||
has_type_args: false,
|
has_type_args: false,
|
||||||
can_be_stmt: false,
|
|
||||||
in_loop_body: false,
|
|
||||||
};
|
};
|
||||||
let mut pat_ctx = None;
|
let mut pat_ctx = None;
|
||||||
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
|
|
||||||
|
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)
|
||||||
|
};
|
||||||
|
|
||||||
path_ctx.kind = path.syntax().ancestors().find_map(|it| {
|
path_ctx.kind = path.syntax().ancestors().find_map(|it| {
|
||||||
// using Option<Option<PathKind>> as extra controlflow
|
// using Option<Option<PathKind>> as extra controlflow
|
||||||
|
@ -983,7 +999,10 @@ impl<'a> CompletionContext<'a> {
|
||||||
ast::PathType(_) => Some(PathKind::Type),
|
ast::PathType(_) => Some(PathKind::Type),
|
||||||
ast::PathExpr(it) => {
|
ast::PathExpr(it) => {
|
||||||
path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind()));
|
path_ctx.has_call_parens = it.syntax().parent().map_or(false, |it| ast::CallExpr::can_cast(it.kind()));
|
||||||
Some(PathKind::Expr)
|
let in_block_expr = is_in_block(it.syntax());
|
||||||
|
let in_loop_body = is_in_loop_body(it.syntax());
|
||||||
|
|
||||||
|
Some(PathKind::Expr { in_block_expr, in_loop_body })
|
||||||
},
|
},
|
||||||
ast::TupleStructPat(it) => {
|
ast::TupleStructPat(it) => {
|
||||||
path_ctx.has_call_parens = true;
|
path_ctx.has_call_parens = true;
|
||||||
|
@ -1001,17 +1020,25 @@ impl<'a> CompletionContext<'a> {
|
||||||
},
|
},
|
||||||
ast::MacroCall(it) => {
|
ast::MacroCall(it) => {
|
||||||
path_ctx.has_macro_bang = it.excl_token().is_some();
|
path_ctx.has_macro_bang = it.excl_token().is_some();
|
||||||
match it.syntax().parent().map(|it| it.kind()) {
|
let parent = it.syntax().parent();
|
||||||
|
match parent.as_ref().map(|it| it.kind()) {
|
||||||
Some(SyntaxKind::MACRO_PAT) => Some(PathKind::Pat),
|
Some(SyntaxKind::MACRO_PAT) => Some(PathKind::Pat),
|
||||||
Some(SyntaxKind::MACRO_TYPE) => Some(PathKind::Type),
|
Some(SyntaxKind::MACRO_TYPE) => Some(PathKind::Type),
|
||||||
Some(SyntaxKind::MACRO_EXPR) => Some(PathKind::Expr),
|
Some(SyntaxKind::ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::Module }),
|
||||||
Some(
|
Some(SyntaxKind::ASSOC_ITEM_LIST) => Some(PathKind::Item { kind: match parent.and_then(|it| it.parent()).map(|it| it.kind()) {
|
||||||
SyntaxKind::ITEM_LIST
|
Some(SyntaxKind::TRAIT) => ItemListKind::Trait,
|
||||||
| SyntaxKind::ASSOC_ITEM_LIST
|
Some(SyntaxKind::IMPL) => ItemListKind::Impl,
|
||||||
| SyntaxKind::EXTERN_ITEM_LIST
|
|
||||||
| SyntaxKind::SOURCE_FILE
|
|
||||||
) => Some(PathKind::Item),
|
|
||||||
_ => return Some(None),
|
_ => return Some(None),
|
||||||
|
} }),
|
||||||
|
Some(SyntaxKind::EXTERN_ITEM_LIST) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
|
||||||
|
Some(SyntaxKind::SOURCE_FILE) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
|
||||||
|
_ => {
|
||||||
|
return Some(parent.and_then(ast::MacroExpr::cast).map(|it| {
|
||||||
|
let in_loop_body = is_in_loop_body(it.syntax());
|
||||||
|
let in_block_expr = is_in_block(it.syntax());
|
||||||
|
PathKind::Expr { in_block_expr, in_loop_body }
|
||||||
|
}));
|
||||||
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ast::Meta(meta) => (|| {
|
ast::Meta(meta) => (|| {
|
||||||
|
@ -1032,10 +1059,16 @@ impl<'a> CompletionContext<'a> {
|
||||||
})(),
|
})(),
|
||||||
ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
|
ast::Visibility(it) => Some(PathKind::Vis { has_in_token: it.in_token().is_some() }),
|
||||||
ast::UseTree(_) => Some(PathKind::Use),
|
ast::UseTree(_) => Some(PathKind::Use),
|
||||||
ast::ItemList(_) => Some(PathKind::Item),
|
ast::ItemList(_) => Some(PathKind::Item { kind: ItemListKind::Module }),
|
||||||
ast::AssocItemList(_) => Some(PathKind::Item),
|
ast::AssocItemList(it) => Some(PathKind::Item { kind: {
|
||||||
ast::ExternItemList(_) => Some(PathKind::Item),
|
match it.syntax().parent()?.kind() {
|
||||||
ast::SourceFile(_) => Some(PathKind::Item),
|
SyntaxKind::TRAIT => ItemListKind::Trait,
|
||||||
|
SyntaxKind::IMPL => ItemListKind::Impl,
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
}}),
|
||||||
|
ast::ExternItemList(_) => Some(PathKind::Item { kind: ItemListKind::ExternBlock }),
|
||||||
|
ast::SourceFile(_) => Some(PathKind::Item { kind: ItemListKind::SourceFile }),
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1086,24 +1119,6 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find either enclosing expr statement (thing with `;`) or a
|
|
||||||
// block. If block, check that we are the last expr.
|
|
||||||
path_ctx.can_be_stmt = name_ref
|
|
||||||
.syntax()
|
|
||||||
.ancestors()
|
|
||||||
.find_map(|node| {
|
|
||||||
if let Some(stmt) = ast::ExprStmt::cast(node.clone()) {
|
|
||||||
return Some(stmt.syntax().text_range() == name_ref.syntax().text_range());
|
|
||||||
}
|
|
||||||
if let Some(stmt_list) = ast::StmtList::cast(node) {
|
|
||||||
return Some(
|
|
||||||
stmt_list.tail_expr().map(|e| e.syntax().text_range())
|
|
||||||
== Some(name_ref.syntax().text_range()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
None
|
|
||||||
})
|
|
||||||
.unwrap_or(false);
|
|
||||||
Some((path_ctx, pat_ctx))
|
Some((path_ctx, pat_ctx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,7 +197,7 @@ fn should_add_parens(ctx: &CompletionContext) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
match ctx.path_context {
|
match ctx.path_context {
|
||||||
Some(PathCompletionCtx { kind: PathKind::Expr, has_call_parens: true, .. }) => {
|
Some(PathCompletionCtx { kind: PathKind::Expr { .. }, has_call_parens: true, .. }) => {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type, .. }) => {
|
Some(PathCompletionCtx { kind: PathKind::Use | PathKind::Type, .. }) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue