mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Merge #9164
9164: internal: Reduce the number of direct fields in `CompletionContext` some more r=Veykril a=Veykril Doesn't make the code much simpler yet. bors r+ Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
8b6c3eaaeb
12 changed files with 108 additions and 75 deletions
|
@ -4,7 +4,7 @@ use either::Either;
|
|||
use hir::{HasVisibility, ScopeDef};
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
use crate::{context::CompletionContext, Completions};
|
||||
use crate::{context::CompletionContext, patterns::ImmediateLocation, Completions};
|
||||
|
||||
/// Complete dot accesses, i.e. fields or methods.
|
||||
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,
|
||||
};
|
||||
|
||||
if ctx.is_call {
|
||||
if matches!(ctx.completion_location, Some(ImmediateLocation::MethodCall { .. })) {
|
||||
cov_mark::hit!(test_no_struct_field_completion_for_method_call);
|
||||
} else {
|
||||
complete_fields(ctx, &receiver_ty, |field, ty| match field {
|
||||
|
@ -33,7 +33,7 @@ fn complete_undotted_self(acc: &mut Completions, ctx: &CompletionContext) {
|
|||
if !ctx.config.enable_self_on_the_fly {
|
||||
return;
|
||||
}
|
||||
if !ctx.is_trivial_path || ctx.is_path_disallowed() {
|
||||
if !ctx.is_trivial_path() || ctx.is_path_disallowed() {
|
||||
return;
|
||||
}
|
||||
ctx.scope.process_all_names(&mut |name, def| {
|
||||
|
|
|
@ -161,13 +161,13 @@ pub(crate) fn position_for_import<'a>(
|
|||
) -> Option<&'a SyntaxNode> {
|
||||
Some(match import_candidate {
|
||||
Some(ImportCandidate::Path(_)) => ctx.name_ref_syntax.as_ref()?.syntax(),
|
||||
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual.as_ref()?.syntax(),
|
||||
Some(ImportCandidate::TraitAssocItem(_)) => ctx.path_qual()?.syntax(),
|
||||
Some(ImportCandidate::TraitMethod(_)) => ctx.dot_receiver()?.syntax(),
|
||||
None => ctx
|
||||
.name_ref_syntax
|
||||
.as_ref()
|
||||
.map(|name_ref| name_ref.syntax())
|
||||
.or_else(|| ctx.path_qual.as_ref().map(|path| path.syntax()))
|
||||
.or_else(|| ctx.path_qual().map(|path| path.syntax()))
|
||||
.or_else(|| ctx.dot_receiver().map(|expr| expr.syntax()))?,
|
||||
})
|
||||
}
|
||||
|
@ -190,7 +190,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
|
|||
};
|
||||
let assets_for_path = ImportAssets::for_fuzzy_path(
|
||||
current_module,
|
||||
ctx.path_qual.clone(),
|
||||
ctx.path_qual().cloned(),
|
||||
fuzzy_name,
|
||||
&ctx.sema,
|
||||
approximate_node,
|
||||
|
|
|
@ -19,11 +19,12 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
|
|||
};
|
||||
|
||||
if ctx.use_item_syntax.is_some() {
|
||||
if ctx.path_qual.is_none() {
|
||||
let qual = ctx.path_qual();
|
||||
if qual.is_none() {
|
||||
kw_completion("crate::").add_to(acc);
|
||||
}
|
||||
kw_completion("self").add_to(acc);
|
||||
if iter::successors(ctx.path_qual.clone(), |p| p.qualifier())
|
||||
if iter::successors(qual.cloned(), |p| p.qualifier())
|
||||
.all(|p| p.segment().and_then(|s| s.super_token()).is_some())
|
||||
{
|
||||
kw_completion("super::").add_to(acc);
|
||||
|
@ -128,7 +129,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||
}
|
||||
|
||||
if ctx.in_loop_body {
|
||||
if ctx.can_be_stmt {
|
||||
if ctx.can_be_stmt() {
|
||||
add_keyword("continue", "continue;");
|
||||
add_keyword("break", "break;");
|
||||
} else {
|
||||
|
@ -137,7 +138,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||
}
|
||||
}
|
||||
|
||||
if !ctx.is_trivial_path {
|
||||
if !ctx.is_trivial_path() {
|
||||
return;
|
||||
}
|
||||
let fn_def = match &ctx.function_def {
|
||||
|
@ -147,7 +148,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
|
|||
|
||||
add_keyword(
|
||||
"return",
|
||||
match (ctx.can_be_stmt, fn_def.ret_type().is_some()) {
|
||||
match (ctx.can_be_stmt(), fn_def.ret_type().is_some()) {
|
||||
(true, true) => "return $0;",
|
||||
(true, false) => "return;",
|
||||
(false, true) => "return $0",
|
||||
|
|
|
@ -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 {
|
||||
Some(ImmediateLocation::MethodCall { receiver: Some(it) }) => (it, false),
|
||||
Some(ImmediateLocation::MethodCall { receiver: Some(it), .. }) => (it, false),
|
||||
Some(ImmediateLocation::FieldAccess {
|
||||
receiver: Some(it),
|
||||
receiver_is_ambiguous_float_literal,
|
||||
|
|
|
@ -10,8 +10,8 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon
|
|||
if ctx.is_path_disallowed() || ctx.expects_item() {
|
||||
return;
|
||||
}
|
||||
let path = match &ctx.path_qual {
|
||||
Some(path) => path.clone(),
|
||||
let path = match ctx.path_qual() {
|
||||
Some(path) => path,
|
||||
None => return,
|
||||
};
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
|
|||
}
|
||||
|
||||
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
if !(ctx.is_trivial_path && ctx.function_def.is_some()) {
|
||||
if !(ctx.is_trivial_path() && ctx.function_def.is_some()) {
|
||||
return;
|
||||
}
|
||||
let cap = match ctx.config.snippet_cap {
|
||||
|
@ -22,7 +22,7 @@ pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
|
|||
None => return,
|
||||
};
|
||||
|
||||
if ctx.can_be_stmt {
|
||||
if ctx.can_be_stmt() {
|
||||
snippet(ctx, cap, "pd", "eprintln!(\"$0 = {:?}\", $0);").add_to(acc);
|
||||
snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ use hir::ScopeDef;
|
|||
use crate::{CompletionContext, Completions};
|
||||
|
||||
pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
|
||||
if !ctx.is_trivial_path {
|
||||
if !ctx.is_trivial_path() {
|
||||
return;
|
||||
}
|
||||
if ctx.is_path_disallowed() || ctx.expects_item() {
|
||||
|
|
|
@ -29,6 +29,28 @@ pub(crate) enum PatternRefutability {
|
|||
Irrefutable,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PathCompletionContext {
|
||||
/// If this is a call with () already there
|
||||
call_kind: Option<CallKind>,
|
||||
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
||||
pub(super) is_trivial_path: bool,
|
||||
/// If not a trivial path, the prefix (qualifier).
|
||||
pub(super) path_qual: Option<ast::Path>,
|
||||
pub(super) is_path_type: 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,
|
||||
/// `true` if we expect an expression at the cursor position.
|
||||
pub(super) is_expr: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum CallKind {
|
||||
Pat,
|
||||
Mac,
|
||||
Expr,
|
||||
}
|
||||
/// `CompletionContext` is created early during completion to figure out, where
|
||||
/// exactly is the cursor, syntax-wise.
|
||||
#[derive(Debug)]
|
||||
|
@ -68,24 +90,9 @@ pub(crate) struct CompletionContext<'a> {
|
|||
pub(super) prev_sibling: Option<ImmediatePrevSibling>,
|
||||
pub(super) attribute_under_caret: Option<ast::Attr>,
|
||||
|
||||
pub(super) path_context: Option<PathCompletionContext>,
|
||||
/// FIXME: `ActiveParameter` is string-based, which is very very wrong
|
||||
pub(super) active_parameter: Option<ActiveParameter>,
|
||||
/// A single-indent path, like `foo`. `::foo` should not be considered a trivial path.
|
||||
pub(super) is_trivial_path: bool,
|
||||
/// If not a trivial path, the prefix (qualifier).
|
||||
pub(super) path_qual: Option<ast::Path>,
|
||||
/// `true` if we are a statement or a last expr in the block.
|
||||
pub(super) can_be_stmt: bool,
|
||||
/// `true` if we expect an expression at the cursor position.
|
||||
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) has_type_args: bool,
|
||||
pub(super) locals: Vec<(String, Local)>,
|
||||
|
||||
pub(super) previous_token: Option<SyntaxToken>,
|
||||
|
@ -149,15 +156,7 @@ impl<'a> CompletionContext<'a> {
|
|||
is_label_ref: false,
|
||||
is_param: false,
|
||||
is_pat_or_const: None,
|
||||
is_trivial_path: false,
|
||||
path_qual: None,
|
||||
can_be_stmt: false,
|
||||
is_expr: false,
|
||||
is_call: false,
|
||||
is_pattern_call: false,
|
||||
is_macro_call: false,
|
||||
is_path_type: false,
|
||||
has_type_args: false,
|
||||
path_context: None,
|
||||
previous_token: None,
|
||||
in_loop_body: false,
|
||||
completion_location: None,
|
||||
|
@ -250,14 +249,14 @@ impl<'a> CompletionContext<'a> {
|
|||
pub(crate) fn has_dot_receiver(&self) -> bool {
|
||||
matches!(
|
||||
&self.completion_location,
|
||||
Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver })
|
||||
Some(ImmediateLocation::FieldAccess { receiver, .. }) | Some(ImmediateLocation::MethodCall { receiver,.. })
|
||||
if receiver.is_some()
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn dot_receiver(&self) -> Option<&ast::Expr> {
|
||||
match &self.completion_location {
|
||||
Some(ImmediateLocation::MethodCall { receiver })
|
||||
Some(ImmediateLocation::MethodCall { receiver, .. })
|
||||
| Some(ImmediateLocation::FieldAccess { receiver, .. }) => receiver.as_ref(),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -275,11 +274,6 @@ impl<'a> CompletionContext<'a> {
|
|||
matches!(self.completion_location, Some(ImmediateLocation::ItemList))
|
||||
}
|
||||
|
||||
// fn expects_value(&self) -> bool {
|
||||
pub(crate) fn expects_expression(&self) -> bool {
|
||||
self.is_expr
|
||||
}
|
||||
|
||||
pub(crate) fn has_block_expr_parent(&self) -> bool {
|
||||
matches!(self.completion_location, Some(ImmediateLocation::BlockExpr))
|
||||
}
|
||||
|
@ -316,6 +310,26 @@ impl<'a> CompletionContext<'a> {
|
|||
) || self.attribute_under_caret.is_some()
|
||||
}
|
||||
|
||||
pub(crate) fn expects_expression(&self) -> bool {
|
||||
self.path_context.as_ref().map_or(false, |it| it.is_expr)
|
||||
}
|
||||
|
||||
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
|
||||
self.path_context.as_ref().and_then(|it| it.call_kind)
|
||||
}
|
||||
|
||||
pub(crate) fn is_trivial_path(&self) -> bool {
|
||||
self.path_context.as_ref().map_or(false, |it| it.is_trivial_path)
|
||||
}
|
||||
|
||||
pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
|
||||
self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
|
||||
}
|
||||
|
||||
pub(crate) fn can_be_stmt(&self) -> bool {
|
||||
self.path_context.as_ref().map_or(false, |it| it.can_be_stmt)
|
||||
}
|
||||
|
||||
fn fill_impl_def(&mut self) {
|
||||
self.impl_def = self
|
||||
.sema
|
||||
|
@ -568,22 +582,32 @@ impl<'a> CompletionContext<'a> {
|
|||
};
|
||||
|
||||
if let Some(segment) = ast::PathSegment::cast(parent) {
|
||||
let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
|
||||
call_kind: None,
|
||||
is_trivial_path: false,
|
||||
path_qual: None,
|
||||
has_type_args: false,
|
||||
is_path_type: false,
|
||||
can_be_stmt: false,
|
||||
is_expr: false,
|
||||
});
|
||||
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();
|
||||
|
||||
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
|
||||
self.has_type_args = segment.generic_arg_list().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
|
||||
}
|
||||
};
|
||||
}
|
||||
path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
|
||||
path_ctx.has_type_args = segment.generic_arg_list().is_some();
|
||||
|
||||
if let Some(path) = path_or_use_tree_qualifier(&path) {
|
||||
self.path_qual = path
|
||||
path_ctx.path_qual = path
|
||||
.segment()
|
||||
.and_then(|it| {
|
||||
find_node_with_range::<ast::PathSegment>(
|
||||
|
@ -601,11 +625,11 @@ impl<'a> CompletionContext<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
self.is_trivial_path = true;
|
||||
path_ctx.is_trivial_path = true;
|
||||
|
||||
// Find either enclosing expr statement (thing with `;`) or a
|
||||
// block. If block, check that we are the last expr.
|
||||
self.can_be_stmt = name_ref
|
||||
path_ctx.can_be_stmt = name_ref
|
||||
.syntax()
|
||||
.ancestors()
|
||||
.find_map(|node| {
|
||||
|
@ -621,10 +645,8 @@ impl<'a> CompletionContext<'a> {
|
|||
None
|
||||
})
|
||||
.unwrap_or(false);
|
||||
self.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
|
||||
path_ctx.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 syntax::{
|
||||
algo::non_trivia_sibling,
|
||||
ast::{self, LoopBodyOwner},
|
||||
ast::{self, ArgListOwner, LoopBodyOwner},
|
||||
match_ast, AstNode, Direction, SyntaxElement,
|
||||
SyntaxKind::*,
|
||||
SyntaxNode, SyntaxToken, TextRange, TextSize, T,
|
||||
|
@ -39,6 +39,7 @@ pub(crate) enum ImmediateLocation {
|
|||
// Original file ast node
|
||||
MethodCall {
|
||||
receiver: Option<ast::Expr>,
|
||||
has_parens: bool,
|
||||
},
|
||||
// Original file ast node
|
||||
FieldAccess {
|
||||
|
@ -204,6 +205,7 @@ pub(crate) fn determine_location(
|
|||
.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())
|
||||
},
|
||||
_ => return None,
|
||||
}
|
||||
|
|
|
@ -275,8 +275,12 @@ impl<'a> Render<'a> {
|
|||
};
|
||||
|
||||
// Add `<>` for generic types
|
||||
if self.ctx.completion.is_path_type
|
||||
&& !self.ctx.completion.has_type_args
|
||||
if self
|
||||
.ctx
|
||||
.completion
|
||||
.path_context
|
||||
.as_ref()
|
||||
.map_or(false, |it| it.is_path_type && !it.has_type_args)
|
||||
&& self.ctx.completion.config.add_call_parenthesis
|
||||
{
|
||||
if let Some(cap) = self.ctx.snippet_cap() {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{item::Builder, CompletionContext};
|
||||
use crate::{context::CallKind, item::Builder, patterns::ImmediateLocation, CompletionContext};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) enum Params {
|
||||
|
@ -32,10 +32,12 @@ impl Builder {
|
|||
cov_mark::hit!(no_parens_in_use_item);
|
||||
return false;
|
||||
}
|
||||
if ctx.is_pattern_call {
|
||||
return false;
|
||||
}
|
||||
if ctx.is_call {
|
||||
if matches!(ctx.path_call_kind(), Some(CallKind::Expr) | Some(CallKind::Pat))
|
||||
| matches!(
|
||||
ctx.completion_location,
|
||||
Some(ImmediateLocation::MethodCall { has_parens: true, .. })
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ use ide_db::SymbolKind;
|
|||
use syntax::display::macro_label;
|
||||
|
||||
use crate::{
|
||||
context::CallKind,
|
||||
item::{CompletionItem, CompletionKind, ImportEdit},
|
||||
render::RenderContext,
|
||||
};
|
||||
|
@ -68,7 +69,8 @@ impl<'a> MacroRender<'a> {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue