mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Don't complete values in type position
This commit is contained in:
parent
590472607c
commit
1d74ef1d98
8 changed files with 76 additions and 55 deletions
|
@ -2496,6 +2496,18 @@ impl ScopeDef {
|
||||||
|
|
||||||
items
|
items
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_value_def(&self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self,
|
||||||
|
ScopeDef::ModuleDef(ModuleDef::Function(_))
|
||||||
|
| ScopeDef::ModuleDef(ModuleDef::Variant(_))
|
||||||
|
| ScopeDef::ModuleDef(ModuleDef::Const(_))
|
||||||
|
| ScopeDef::ModuleDef(ModuleDef::Static(_))
|
||||||
|
| ScopeDef::GenericParam(GenericParam::ConstParam(_))
|
||||||
|
| ScopeDef::Local(_)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ItemInNs> for ScopeDef {
|
impl From<ItemInNs> for ScopeDef {
|
||||||
|
|
|
@ -35,8 +35,9 @@ pub enum PathResolution {
|
||||||
Def(ModuleDef),
|
Def(ModuleDef),
|
||||||
/// A local binding (only value namespace)
|
/// A local binding (only value namespace)
|
||||||
Local(Local),
|
Local(Local),
|
||||||
/// A generic parameter
|
/// A type parameter
|
||||||
TypeParam(TypeParam),
|
TypeParam(TypeParam),
|
||||||
|
/// A const parameter
|
||||||
ConstParam(ConstParam),
|
ConstParam(ConstParam),
|
||||||
SelfType(Impl),
|
SelfType(Impl),
|
||||||
Macro(MacroDef),
|
Macro(MacroDef),
|
||||||
|
|
|
@ -56,10 +56,16 @@ impl Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Completions {
|
impl Completions {
|
||||||
pub(crate) fn add(&mut self, item: CompletionItem) {
|
fn add(&mut self, item: CompletionItem) {
|
||||||
self.buf.push(item)
|
self.buf.push(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_opt(&mut self, item: Option<CompletionItem>) {
|
||||||
|
if let Some(item) = item {
|
||||||
|
self.buf.push(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn add_all<I>(&mut self, items: I)
|
pub(crate) fn add_all<I>(&mut self, items: I)
|
||||||
where
|
where
|
||||||
I: IntoIterator,
|
I: IntoIterator,
|
||||||
|
@ -103,9 +109,10 @@ impl Completions {
|
||||||
local_name: hir::Name,
|
local_name: hir::Name,
|
||||||
resolution: &hir::ScopeDef,
|
resolution: &hir::ScopeDef,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
|
if ctx.expects_type() && resolution.is_value_def() {
|
||||||
self.add(item);
|
return;
|
||||||
}
|
}
|
||||||
|
self.add_opt(render_resolution(RenderContext::new(ctx), local_name, resolution));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_macro(
|
pub(crate) fn add_macro(
|
||||||
|
@ -118,9 +125,7 @@ impl Completions {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
|
self.add_opt(render_macro(RenderContext::new(ctx), None, name, macro_));
|
||||||
self.add(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_function(
|
pub(crate) fn add_function(
|
||||||
|
@ -129,9 +134,10 @@ impl Completions {
|
||||||
func: hir::Function,
|
func: hir::Function,
|
||||||
local_name: Option<hir::Name>,
|
local_name: Option<hir::Name>,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
|
if ctx.expects_type() {
|
||||||
self.add(item)
|
return;
|
||||||
}
|
}
|
||||||
|
self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_method(
|
pub(crate) fn add_method(
|
||||||
|
@ -141,10 +147,7 @@ impl Completions {
|
||||||
receiver: Option<hir::Name>,
|
receiver: Option<hir::Name>,
|
||||||
local_name: Option<hir::Name>,
|
local_name: Option<hir::Name>,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_method(RenderContext::new(ctx), None, receiver, local_name, func)
|
self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
|
||||||
{
|
|
||||||
self.add(item)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_variant_pat(
|
pub(crate) fn add_variant_pat(
|
||||||
|
@ -153,9 +156,7 @@ impl Completions {
|
||||||
variant: hir::Variant,
|
variant: hir::Variant,
|
||||||
local_name: Option<hir::Name>,
|
local_name: Option<hir::Name>,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) {
|
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
|
||||||
self.add(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_qualified_variant_pat(
|
pub(crate) fn add_qualified_variant_pat(
|
||||||
|
@ -164,9 +165,7 @@ impl Completions {
|
||||||
variant: hir::Variant,
|
variant: hir::Variant,
|
||||||
path: hir::ModPath,
|
path: hir::ModPath,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
|
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
|
||||||
self.add(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_struct_pat(
|
pub(crate) fn add_struct_pat(
|
||||||
|
@ -175,21 +174,18 @@ impl Completions {
|
||||||
strukt: hir::Struct,
|
strukt: hir::Struct,
|
||||||
local_name: Option<hir::Name>,
|
local_name: Option<hir::Name>,
|
||||||
) {
|
) {
|
||||||
if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) {
|
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
|
||||||
self.add(item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
||||||
if let Some(item) = render_const(RenderContext::new(ctx), constant) {
|
if ctx.expects_type() {
|
||||||
self.add(item);
|
return;
|
||||||
}
|
}
|
||||||
|
self.add_opt(render_const(RenderContext::new(ctx), constant));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
|
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
|
||||||
if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
|
self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
|
||||||
self.add(item)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_qualified_enum_variant(
|
pub(crate) fn add_qualified_enum_variant(
|
||||||
|
|
|
@ -69,7 +69,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_inner || !attr_completion.prefer_inner {
|
if is_inner || !attr_completion.prefer_inner {
|
||||||
acc.add(item.build());
|
item.add_to(acc);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attrib
|
||||||
if let Some(docs) = mac.docs(ctx.sema.db) {
|
if let Some(docs) = mac.docs(ctx.sema.db) {
|
||||||
item.documentation(docs);
|
item.documentation(docs);
|
||||||
}
|
}
|
||||||
acc.add(item.build());
|
item.add_to(acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -90,7 +90,6 @@
|
||||||
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
|
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
|
||||||
//! capability enabled.
|
//! capability enabled.
|
||||||
|
|
||||||
use hir::ModPath;
|
|
||||||
use ide_db::helpers::{
|
use ide_db::helpers::{
|
||||||
import_assets::{ImportAssets, ImportCandidate},
|
import_assets::{ImportAssets, ImportCandidate},
|
||||||
insert_use::ImportScope,
|
insert_use::ImportScope,
|
||||||
|
@ -208,7 +207,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compute_fuzzy_completion_order_key(
|
fn compute_fuzzy_completion_order_key(
|
||||||
proposed_mod_path: &ModPath,
|
proposed_mod_path: &hir::ModPath,
|
||||||
user_input_lowercased: &str,
|
user_input_lowercased: &str,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
cov_mark::hit!(certain_fuzzy_order_test);
|
cov_mark::hit!(certain_fuzzy_order_test);
|
||||||
|
|
|
@ -339,7 +339,6 @@ fn x() -> $0
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
st Foo
|
st Foo
|
||||||
fn x() fn()
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -391,7 +390,6 @@ pub mod prelude {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn foo() fn()
|
|
||||||
md std
|
md std
|
||||||
st Option
|
st Option
|
||||||
"#]],
|
"#]],
|
||||||
|
@ -448,7 +446,6 @@ pub mod prelude {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn foo() fn()
|
|
||||||
md std
|
md std
|
||||||
md core
|
md core
|
||||||
st String
|
st String
|
||||||
|
@ -509,7 +506,6 @@ macro_rules! foo { () => {} }
|
||||||
fn main() { let x: $0 }
|
fn main() { let x: $0 }
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
fn main() fn()
|
|
||||||
ma foo!(…) macro_rules! foo
|
ma foo!(…) macro_rules! foo
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
|
|
@ -29,6 +29,12 @@ pub(crate) enum PatternRefutability {
|
||||||
Irrefutable,
|
Irrefutable,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub(super) enum PathKind {
|
||||||
|
Expr,
|
||||||
|
Type,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct PathCompletionContext {
|
pub(crate) struct PathCompletionContext {
|
||||||
/// If this is a call with () already there
|
/// If this is a call with () already there
|
||||||
|
@ -36,13 +42,12 @@ pub(crate) struct PathCompletionContext {
|
||||||
/// 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.
|
||||||
pub(super) is_trivial_path: bool,
|
pub(super) is_trivial_path: bool,
|
||||||
/// If not a trivial path, the prefix (qualifier).
|
/// If not a trivial path, the prefix (qualifier).
|
||||||
pub(super) path_qual: Option<ast::Path>,
|
pub(super) qualifier: Option<ast::Path>,
|
||||||
pub(super) is_path_type: bool,
|
pub(super) kind: Option<PathKind>,
|
||||||
|
/// 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.
|
/// `true` if we are a statement or a last expr in the block.
|
||||||
pub(super) can_be_stmt: bool,
|
pub(super) can_be_stmt: bool,
|
||||||
/// `true` if we expect an expression at the cursor position.
|
|
||||||
pub(super) is_expr: bool,
|
|
||||||
pub(super) in_loop_body: bool,
|
pub(super) in_loop_body: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,7 +313,11 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn expects_expression(&self) -> bool {
|
pub(crate) fn expects_expression(&self) -> bool {
|
||||||
self.path_context.as_ref().map_or(false, |it| it.is_expr)
|
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Expr), .. }))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn expects_type(&self) -> bool {
|
||||||
|
matches!(self.path_context, Some(PathCompletionContext { kind: Some(PathKind::Type), .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
|
pub(crate) fn path_call_kind(&self) -> Option<CallKind> {
|
||||||
|
@ -316,11 +325,11 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn is_trivial_path(&self) -> bool {
|
pub(crate) fn is_trivial_path(&self) -> bool {
|
||||||
self.path_context.as_ref().map_or(false, |it| it.is_trivial_path)
|
matches!(self.path_context, Some(PathCompletionContext { is_trivial_path: true, .. }))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
|
pub(crate) fn path_qual(&self) -> Option<&ast::Path> {
|
||||||
self.path_context.as_ref().and_then(|it| it.path_qual.as_ref())
|
self.path_context.as_ref().and_then(|it| it.qualifier.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_impl_def(&mut self) {
|
fn fill_impl_def(&mut self) {
|
||||||
|
@ -573,12 +582,11 @@ impl<'a> CompletionContext<'a> {
|
||||||
let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
|
let path_ctx = self.path_context.get_or_insert(PathCompletionContext {
|
||||||
call_kind: None,
|
call_kind: None,
|
||||||
is_trivial_path: false,
|
is_trivial_path: false,
|
||||||
path_qual: None,
|
qualifier: None,
|
||||||
has_type_args: false,
|
has_type_args: false,
|
||||||
is_path_type: false,
|
|
||||||
can_be_stmt: false,
|
can_be_stmt: false,
|
||||||
is_expr: false,
|
|
||||||
in_loop_body: false,
|
in_loop_body: false,
|
||||||
|
kind: None,
|
||||||
});
|
});
|
||||||
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
|
path_ctx.in_loop_body = is_in_loop_body(name_ref.syntax());
|
||||||
let path = segment.parent_path();
|
let path = segment.parent_path();
|
||||||
|
@ -593,11 +601,20 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
path_ctx.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
|
|
||||||
|
if let Some(parent) = path.syntax().parent() {
|
||||||
|
path_ctx.kind = match_ast! {
|
||||||
|
match parent {
|
||||||
|
ast::PathType(_it) => Some(PathKind::Type),
|
||||||
|
ast::PathExpr(_it) => Some(PathKind::Expr),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
path_ctx.has_type_args = segment.generic_arg_list().is_some();
|
path_ctx.has_type_args = segment.generic_arg_list().is_some();
|
||||||
|
|
||||||
if let Some(path) = path_or_use_tree_qualifier(&path) {
|
if let Some(path) = path_or_use_tree_qualifier(&path) {
|
||||||
path_ctx.path_qual = path
|
path_ctx.qualifier = path
|
||||||
.segment()
|
.segment()
|
||||||
.and_then(|it| {
|
.and_then(|it| {
|
||||||
find_node_with_range::<ast::PathSegment>(
|
find_node_with_range::<ast::PathSegment>(
|
||||||
|
@ -635,7 +652,6 @@ impl<'a> CompletionContext<'a> {
|
||||||
None
|
None
|
||||||
})
|
})
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
path_ctx.is_expr = path.syntax().parent().and_then(ast::PathExpr::cast).is_some();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ use ide_db::{
|
||||||
use syntax::TextRange;
|
use syntax::TextRange;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
context::{PathCompletionContext, PathKind},
|
||||||
item::{CompletionRelevanceTypeMatch, ImportEdit},
|
item::{CompletionRelevanceTypeMatch, ImportEdit},
|
||||||
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
|
render::{enum_variant::render_variant, function::render_fn, macro_::render_macro},
|
||||||
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance,
|
||||||
|
@ -54,6 +55,9 @@ pub(crate) fn render_resolution_with_import<'a>(
|
||||||
import_edit: ImportEdit,
|
import_edit: ImportEdit,
|
||||||
) -> Option<CompletionItem> {
|
) -> Option<CompletionItem> {
|
||||||
let resolution = hir::ScopeDef::from(import_edit.import.original_item);
|
let resolution = hir::ScopeDef::from(import_edit.import.original_item);
|
||||||
|
if ctx.completion.expects_type() && resolution.is_value_def() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
let local_name = match resolution {
|
let local_name = match resolution {
|
||||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db),
|
||||||
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
|
hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?,
|
||||||
|
@ -275,13 +279,10 @@ impl<'a> Render<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add `<>` for generic types
|
// Add `<>` for generic types
|
||||||
if self
|
if matches!(
|
||||||
.ctx
|
self.ctx.completion.path_context,
|
||||||
.completion
|
Some(PathCompletionContext { kind: Some(PathKind::Type), has_type_args: false, .. })
|
||||||
.path_context
|
) && self.ctx.completion.config.add_call_parenthesis
|
||||||
.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() {
|
if let Some(cap) = self.ctx.snippet_cap() {
|
||||||
let has_non_default_type_params = match resolution {
|
let has_non_default_type_params = match resolution {
|
||||||
|
|
Loading…
Reference in a new issue