mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Simplify CompletionContext by introducing a path CallKind enum
This commit is contained in:
parent
ad9234fef2
commit
e475bcdcc6
6 changed files with 49 additions and 32 deletions
|
@ -4,7 +4,7 @@ use either::Either;
|
||||||
use hir::{HasVisibility, ScopeDef};
|
use hir::{HasVisibility, ScopeDef};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use crate::{context::CompletionContext, Completions};
|
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
|
||||||
|
|
||||||
/// Complete dot accesses, i.e. fields or methods.
|
/// Complete dot accesses, i.e. fields or methods.
|
||||||
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
|
@ -18,7 +18,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
_ => return,
|
_ => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
if ctx.is_call {
|
if matches!(ctx.completion_location, Some(ImmediateLocation::MethodCall { .. })) {
|
||||||
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
||||||
} else {
|
} else {
|
||||||
complete_fields(ctx, &receiver_ty, |field, ty| match field {
|
complete_fields(ctx, &receiver_ty, |field, ty| match field {
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location {
|
let (dot_receiver, receiver_is_ambiguous_float_literal) = match &ctx.completion_location {
|
||||||
Some(ImmediateLocation::MethodCall { receiver: Some(it) }) => (it, false),
|
Some(ImmediateLocation::MethodCall { receiver: Some(it), .. }) => (it, false),
|
||||||
Some(ImmediateLocation::FieldAccess {
|
Some(ImmediateLocation::FieldAccess {
|
||||||
receiver: Some(it),
|
receiver: Some(it),
|
||||||
receiver_is_ambiguous_float_literal,
|
receiver_is_ambiguous_float_literal,
|
||||||
|
|
|
@ -29,6 +29,18 @@ pub(crate) enum PatternRefutability {
|
||||||
Irrefutable,
|
Irrefutable,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(crate) struct PathCompletionContext {
|
||||||
|
/// If this is a call with () already there
|
||||||
|
call_kind: Option<CallKind>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub(crate) enum CallKind {
|
||||||
|
Pat,
|
||||||
|
Mac,
|
||||||
|
Expr,
|
||||||
|
}
|
||||||
/// `CompletionContext` is created early during completion to figure out, where
|
/// `CompletionContext` is created early during completion to figure out, where
|
||||||
/// exactly is the cursor, syntax-wise.
|
/// exactly is the cursor, syntax-wise.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -68,6 +80,7 @@ pub(crate) struct CompletionContext<'a> {
|
||||||
pub(super) prev_sibling: Option<ImmediatePrevSibling>,
|
pub(super) prev_sibling: Option<ImmediatePrevSibling>,
|
||||||
pub(super) attribute_under_caret: Option<ast::Attr>,
|
pub(super) attribute_under_caret: Option<ast::Attr>,
|
||||||
|
|
||||||
|
pub(super) path_context: Option<PathCompletionContext>,
|
||||||
/// FIXME: `ActiveParameter` is string-based, which is very very wrong
|
/// FIXME: `ActiveParameter` is string-based, which is very very wrong
|
||||||
pub(super) active_parameter: Option<ActiveParameter>,
|
pub(super) active_parameter: Option<ActiveParameter>,
|
||||||
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
||||||
|
@ -78,12 +91,6 @@ pub(crate) struct CompletionContext<'a> {
|
||||||
pub(super) can_be_stmt: bool,
|
pub(super) can_be_stmt: bool,
|
||||||
/// `true` if we expect an expression at the cursor position.
|
/// `true` if we expect an expression at the cursor position.
|
||||||
pub(super) is_expr: bool,
|
pub(super) is_expr: bool,
|
||||||
/// If this is a call (method or function) in particular, i.e. the () are already there.
|
|
||||||
pub(super) is_call: bool,
|
|
||||||
/// Like `is_call`, but for tuple patterns.
|
|
||||||
pub(super) is_pattern_call: bool,
|
|
||||||
/// If this is a macro call, i.e. the () are already there.
|
|
||||||
pub(super) is_macro_call: bool,
|
|
||||||
pub(super) is_path_type: bool,
|
pub(super) is_path_type: bool,
|
||||||
pub(super) has_type_args: bool,
|
pub(super) has_type_args: bool,
|
||||||
pub(super) locals: Vec<(String, Local)>,
|
pub(super) locals: Vec<(String, Local)>,
|
||||||
|
@ -153,9 +160,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
path_qual: None,
|
path_qual: None,
|
||||||
can_be_stmt: false,
|
can_be_stmt: false,
|
||||||
is_expr: false,
|
is_expr: false,
|
||||||
is_call: false,
|
path_context: None,
|
||||||
is_pattern_call: false,
|
|
||||||
is_macro_call: false,
|
|
||||||
is_path_type: false,
|
is_path_type: false,
|
||||||
has_type_args: false,
|
has_type_args: false,
|
||||||
previous_token: None,
|
previous_token: None,
|
||||||
|
@ -250,14 +255,14 @@ impl<'a> CompletionContext<'a> {
|
||||||
pub(crate) fn has_dot_receiver(&self) -> bool {
|
pub(crate) fn has_dot_receiver(&self) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
&self.completion_location,
|
&self.completion_location,
|
||||||
Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver })
|
Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver,.. })
|
||||||
if receiver.is_some()
|
if receiver.is_some()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
|
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
|
||||||
match &self.completion_location {
|
match &self.completion_location {
|
||||||
Some(ImmediateLocation::MethodCall { receiver })
|
Some(ImmediateLocation::MethodCall { receiver, .. })
|
||||||
| Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(),
|
| Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -316,6 +321,10 @@ impl<'a> CompletionContext<'a> {
|
||||||
) || self.attribute_under_caret.is_some()
|
) || self.attribute_under_caret.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
|
||||||
|
self.path_context.as_ref().and_then(|it| it.call_kind)
|
||||||
|
}
|
||||||
|
|
||||||
fn fill_impl_def(&mut self) {
|
fn fill_impl_def(&mut self) {
|
||||||
self.impl_def = self
|
self.impl_def = self
|
||||||
.sema
|
.sema
|
||||||
|
@ -568,17 +577,21 @@ impl<'a> CompletionContext<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(segment) = ast::PathSegment::cast(parent) {
|
if let Some(segment) = ast::PathSegment::cast(parent) {
|
||||||
|
let mut path_ctx = PathCompletionContext { call_kind: None };
|
||||||
let path = segment.parent_path();
|
let path = segment.parent_path();
|
||||||
self.is_call = path
|
|
||||||
.syntax()
|
|
||||||
.parent()
|
|
||||||
.and_then(ast::PathExpr::cast)
|
|
||||||
.and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
|
|
||||||
.is_some();
|
|
||||||
self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
|
|
||||||
self.is_pattern_call =
|
|
||||||
path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some();
|
|
||||||
|
|
||||||
|
if let Some(p) = path.syntax().parent() {
|
||||||
|
path_ctx.call_kind = match_ast! {
|
||||||
|
match p {
|
||||||
|
ast::PathExpr(it) => it.syntax().parent().and_then(ast::CallExpr::cast).map(|_| CallKind::Expr),
|
||||||
|
ast::MacroCall(_it) => Some(CallKind::Mac),
|
||||||
|
ast::TupleStructPat(_it) => Some(CallKind::Pat),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
self.path_context = Some(path_ctx);
|
||||||
|
dbg!(&self.path_context);
|
||||||
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
|
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
|
||||||
self.has_type_args = segment.generic_arg_list().is_some();
|
self.has_type_args = segment.generic_arg_list().is_some();
|
||||||
|
|
||||||
|
@ -623,8 +636,6 @@ impl<'a> CompletionContext<'a> {
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
|
self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
|
||||||
}
|
}
|
||||||
self.is_call |=
|
|
||||||
matches!(self.completion_location, Some(ImmediateLocation::MethodCall { .. }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ use hir::Semantics;
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::non_trivia_sibling,
|
algo::non_trivia_sibling,
|
||||||
ast::{self, LoopBodyOwner},
|
ast::{self, ArgListOwner, LoopBodyOwner},
|
||||||
match_ast, AstNode, Direction, SyntaxElement,
|
match_ast, AstNode, Direction, SyntaxElement,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
|
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
|
||||||
|
@ -39,6 +39,7 @@ pub(crate) enum ImmediateLocation {
|
||||||
// Original file ast node
|
// Original file ast node
|
||||||
MethodCall {
|
MethodCall {
|
||||||
receiver: Option<ast::Expr>,
|
receiver: Option<ast::Expr>,
|
||||||
|
has_parens: bool,
|
||||||
},
|
},
|
||||||
// Original file ast node
|
// Original file ast node
|
||||||
FieldAccess {
|
FieldAccess {
|
||||||
|
@ -204,6 +205,7 @@ pub(crate) fn determine_location(
|
||||||
.receiver()
|
.receiver()
|
||||||
.map(|e| e.syntax().text_range())
|
.map(|e| e.syntax().text_range())
|
||||||
.and_then(|r| find_node_with_range(original_file, r)),
|
.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())
|
||||||
},
|
},
|
||||||
_ => return None,
|
_ => return None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use crate::{item::Builder, CompletionContext};
|
use crate::{context::CallKind, item::Builder, patterns::ImmediateLocation, CompletionContext};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) enum Params {
|
pub(super) enum Params {
|
||||||
|
@ -32,10 +32,12 @@ impl Builder {
|
||||||
cov_mark::hit!(no_parens_in_use_item);
|
cov_mark::hit!(no_parens_in_use_item);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ctx.is_pattern_call {
|
if matches!(ctx.path_call_kind(), Some(CallKind::Expr) | Some(CallKind::Pat))
|
||||||
return false;
|
| matches!(
|
||||||
}
|
ctx.completion_location,
|
||||||
if ctx.is_call {
|
Some(ImmediateLocation::MethodCall { has_parens: true, .. })
|
||||||
|
)
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ use ide_db::SymbolKind;
|
||||||
use syntax::display::macro_label;
|
use syntax::display::macro_label;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::CallKind,
|
||||||
item::{CompletionItem, CompletionKind, ImportEdit},
|
item::{CompletionItem, CompletionKind, ImportEdit},
|
||||||
render::RenderContext,
|
render::RenderContext,
|
||||||
};
|
};
|
||||||
|
@ -68,7 +69,8 @@ impl<'a> MacroRender<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn needs_bang(&self) -> bool {
|
fn needs_bang(&self) -> bool {
|
||||||
self.ctx.completion.use_item_syntax.is_none() && !self.ctx.completion.is_macro_call
|
self.ctx.completion.use_item_syntax.is_none()
|
||||||
|
&& !matches!(self.ctx.completion.path_call_kind(), Some(CallKind::Mac))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn label(&self) -> String {
|
fn label(&self) -> String {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue