7342: Show deprecated completions for deprecated traits r=kjeremy a=SomeoneToIgnore

TIL that there are two kinds of deprecation tags and a few details about String methods.

<img width="472" alt="Screenshot 2021-01-19 at 01 11 38" src="https://user-images.githubusercontent.com/2690773/104970200-569d3380-59f3-11eb-9ee3-627b3c8a5c9d.png">


Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
bors[bot] 2021-01-19 01:33:49 +00:00 committed by GitHub
commit 00680a5d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 36 deletions

View File

@ -1,6 +1,6 @@
use std::iter; use std::iter;
use hir::AsName; use hir::{AsAssocItem, AsName};
use ide_db::helpers::{import_assets::ImportCandidate, mod_path_to_ast}; use ide_db::helpers::{import_assets::ImportCandidate, mod_path_to_ast};
use ide_db::RootDatabase; use ide_db::RootDatabase;
use syntax::{ use syntax::{

View File

@ -48,7 +48,7 @@
//! 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, ScopeDef}; use hir::{AsAssocItem, ModPath, ScopeDef};
use ide_db::helpers::{ use ide_db::helpers::{
import_assets::{ImportAssets, ImportCandidate}, import_assets::{ImportAssets, ImportCandidate},
insert_use::ImportScope, insert_use::ImportScope,
@ -601,11 +601,12 @@ fn main() {
} }
#[test] #[test]
fn zero_input_assoc_item_completion() { fn zero_input_deprecated_assoc_item_completion() {
check( check(
r#" r#"
//- /lib.rs crate:dep //- /lib.rs crate:dep
pub mod test_mod { pub mod test_mod {
#[deprecated]
pub trait TestTrait { pub trait TestTrait {
const SPECIAL_CONST: u8; const SPECIAL_CONST: u8;
type HumbleType; type HumbleType;
@ -628,7 +629,7 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
me random_method() (dep::test_mod::TestTrait) fn random_method(&self) me random_method() (dep::test_mod::TestTrait) fn random_method(&self) DEPRECATED
"#]], "#]],
); );
@ -636,6 +637,7 @@ fn main() {
r#" r#"
//- /lib.rs crate:dep //- /lib.rs crate:dep
pub mod test_mod { pub mod test_mod {
#[deprecated]
pub trait TestTrait { pub trait TestTrait {
const SPECIAL_CONST: u8; const SPECIAL_CONST: u8;
type HumbleType; type HumbleType;
@ -657,8 +659,8 @@ fn main() {
} }
"#, "#,
expect![[r#" expect![[r#"
ct SPECIAL_CONST (dep::test_mod::TestTrait) ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED
fn weird_function() (dep::test_mod::TestTrait) fn weird_function() fn weird_function() (dep::test_mod::TestTrait) fn weird_function() DEPRECATED
"#]], "#]],
); );
} }

View File

@ -10,7 +10,9 @@
mod builder_ext; mod builder_ext;
use hir::{Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type}; use hir::{
AsAssocItem, Documentation, HasAttrs, HirDisplay, ModuleDef, Mutability, ScopeDef, Type,
};
use ide_db::{helpers::SnippetCap, RootDatabase}; use ide_db::{helpers::SnippetCap, RootDatabase};
use syntax::TextRange; use syntax::TextRange;
use test_utils::mark; use test_utils::mark;
@ -87,7 +89,24 @@ fn source_range(&self) -> TextRange {
} }
fn is_deprecated(&self, node: impl HasAttrs) -> bool { fn is_deprecated(&self, node: impl HasAttrs) -> bool {
node.attrs(self.db()).by_key("deprecated").exists() let attrs = node.attrs(self.db());
attrs.by_key("deprecated").exists() || attrs.by_key("rustc_deprecated").exists()
}
fn is_deprecated_assoc_item(&self, as_assoc_item: impl AsAssocItem) -> bool {
let db = self.db();
let assoc = match as_assoc_item.as_assoc_item(db) {
Some(assoc) => assoc,
None => return false,
};
let is_assoc_deprecated = match assoc {
hir::AssocItem::Function(it) => self.is_deprecated(it),
hir::AssocItem::Const(it) => self.is_deprecated(it),
hir::AssocItem::TypeAlias(it) => self.is_deprecated(it),
};
is_assoc_deprecated
|| assoc.containing_trait(db).map(|trait_| self.is_deprecated(trait_)).unwrap_or(false)
} }
fn docs(&self, node: impl HasAttrs) -> Option<Documentation> { fn docs(&self, node: impl HasAttrs) -> Option<Documentation> {
@ -206,8 +225,6 @@ fn render_resolution(
} }
}; };
let docs = self.docs(resolution);
let mut item = let mut item =
CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone()); CompletionItem::new(completion_kind, self.ctx.source_range(), local_name.clone());
if let ScopeDef::Local(local) = resolution { if let ScopeDef::Local(local) = resolution {
@ -253,13 +270,14 @@ fn render_resolution(
} }
} }
let item = item Some(
.kind(kind) item.kind(kind)
.add_import(import_to_add) .add_import(import_to_add)
.set_documentation(docs) .set_ref_match(ref_match)
.set_ref_match(ref_match) .set_documentation(self.docs(resolution))
.build(); .set_deprecated(self.is_deprecated(resolution))
Some(item) .build(),
)
} }
fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> { fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
@ -275,6 +293,16 @@ fn docs(&self, resolution: &ScopeDef) -> Option<Documentation> {
_ => None, _ => None,
} }
} }
fn is_deprecated(&self, resolution: &ScopeDef) -> bool {
match resolution {
ScopeDef::ModuleDef(it) => self.ctx.is_deprecated_assoc_item(*it),
ScopeDef::MacroDef(it) => self.ctx.is_deprecated(*it),
ScopeDef::GenericParam(it) => self.ctx.is_deprecated(*it),
ScopeDef::AdtSelfType(it) => self.ctx.is_deprecated(*it),
_ => false,
}
}
} }
fn compute_score_from_active( fn compute_score_from_active(
@ -485,7 +513,7 @@ fn sets_deprecated_flag_in_items() {
r#" r#"
#[deprecated] #[deprecated]
fn something_deprecated() {} fn something_deprecated() {}
#[deprecated(since = "1.0.0")] #[rustc_deprecated(since = "1.0.0")]
fn something_else_deprecated() {} fn something_else_deprecated() {}
fn main() { som$0 } fn main() { som$0 }
@ -494,8 +522,8 @@ fn main() { som$0 }
[ [
CompletionItem { CompletionItem {
label: "main()", label: "main()",
source_range: 121..124, source_range: 127..130,
delete: 121..124, delete: 127..130,
insert: "main()$0", insert: "main()$0",
kind: Function, kind: Function,
lookup: "main", lookup: "main",
@ -503,8 +531,8 @@ fn main() { som$0 }
}, },
CompletionItem { CompletionItem {
label: "something_deprecated()", label: "something_deprecated()",
source_range: 121..124, source_range: 127..130,
delete: 121..124, delete: 127..130,
insert: "something_deprecated()$0", insert: "something_deprecated()$0",
kind: Function, kind: Function,
lookup: "something_deprecated", lookup: "something_deprecated",
@ -513,8 +541,8 @@ fn main() { som$0 }
}, },
CompletionItem { CompletionItem {
label: "something_else_deprecated()", label: "something_else_deprecated()",
source_range: 121..124, source_range: 127..130,
delete: 121..124, delete: 127..130,
insert: "something_else_deprecated()$0", insert: "something_else_deprecated()$0",
kind: Function, kind: Function,
lookup: "something_else_deprecated", lookup: "something_else_deprecated",

View File

@ -38,7 +38,10 @@ fn render(self) -> Option<CompletionItem> {
let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
.kind(CompletionItemKind::Const) .kind(CompletionItemKind::Const)
.set_documentation(self.ctx.docs(self.const_)) .set_documentation(self.ctx.docs(self.const_))
.set_deprecated(self.ctx.is_deprecated(self.const_)) .set_deprecated(
self.ctx.is_deprecated(self.const_)
|| self.ctx.is_deprecated_assoc_item(self.const_),
)
.detail(detail) .detail(detail)
.build(); .build();

View File

@ -44,7 +44,9 @@ fn render(self, import_to_add: Option<ImportEdit>) -> CompletionItem {
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
.kind(self.kind()) .kind(self.kind())
.set_documentation(self.ctx.docs(self.func)) .set_documentation(self.ctx.docs(self.func))
.set_deprecated(self.ctx.is_deprecated(self.func)) .set_deprecated(
self.ctx.is_deprecated(self.func) || self.ctx.is_deprecated_assoc_item(self.func),
)
.detail(self.detail()) .detail(self.detail())
.add_call_parens(self.ctx.completion, self.name, params) .add_call_parens(self.ctx.completion, self.name, params)
.add_import(import_to_add) .add_import(import_to_add)

View File

@ -38,7 +38,10 @@ fn render(self) -> Option<CompletionItem> {
let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name) let item = CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), name)
.kind(CompletionItemKind::TypeAlias) .kind(CompletionItemKind::TypeAlias)
.set_documentation(self.ctx.docs(self.type_alias)) .set_documentation(self.ctx.docs(self.type_alias))
.set_deprecated(self.ctx.is_deprecated(self.type_alias)) .set_deprecated(
self.ctx.is_deprecated(self.type_alias)
|| self.ctx.is_deprecated_assoc_item(self.type_alias),
)
.detail(detail) .detail(detail)
.build(); .build();

View File

@ -83,6 +83,9 @@ pub(crate) fn completion_list_with_config(
let width = label_width.saturating_sub(monospace_width(it.label())); let width = label_width.saturating_sub(monospace_width(it.label()));
format_to!(buf, "{:width$} {}", "", detail, width = width); format_to!(buf, "{:width$} {}", "", detail, width = width);
} }
if it.deprecated() {
format_to!(buf, " DEPRECATED");
}
format_to!(buf, "\n"); format_to!(buf, "\n");
buf buf
}) })

View File

@ -272,15 +272,6 @@ pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) {
hir_ty::diagnostics::validate_module_item(db, module.id.krate, id, sink) hir_ty::diagnostics::validate_module_item(db, module.id.krate, id, sink)
} }
pub fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
match self {
ModuleDef::Function(f) => f.as_assoc_item(db),
ModuleDef::Const(c) => c.as_assoc_item(db),
ModuleDef::TypeAlias(t) => t.as_assoc_item(db),
_ => None,
}
}
} }
impl Module { impl Module {
@ -1060,6 +1051,16 @@ fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
as_assoc_item(db, AssocItem::TypeAlias, self.id) as_assoc_item(db, AssocItem::TypeAlias, self.id)
} }
} }
impl AsAssocItem for ModuleDef {
fn as_assoc_item(self, db: &dyn HirDatabase) -> Option<AssocItem> {
match self {
ModuleDef::Function(it) => it.as_assoc_item(db),
ModuleDef::Const(it) => it.as_assoc_item(db),
ModuleDef::TypeAlias(it) => it.as_assoc_item(db),
_ => None,
}
}
}
fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem> fn as_assoc_item<ID, DEF, CTOR, AST>(db: &dyn HirDatabase, ctor: CTOR, id: ID) -> Option<AssocItem>
where where
ID: Lookup<Data = AssocItemLoc<AST>>, ID: Lookup<Data = AssocItemLoc<AST>>,