From c6f1de6ac5d3496fc3c30b5e15263db68d057695 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 6 Oct 2020 21:05:57 +0200 Subject: Use FamousDefs for shorten_iterator hint --- crates/assists/src/utils.rs | 61 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 12 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index eb69c49a4..0335969fd 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -274,24 +274,54 @@ impl TryEnum { /// somewhat similar to the known paths infra inside hir, but it different; We /// want to make sure that IDE specific paths don't become interesting inside /// the compiler itself as well. -pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, pub(crate) Crate); +pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Crate); #[allow(non_snake_case)] impl FamousDefs<'_, '_> { - #[cfg(test)] - pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core + pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core pub mod convert { pub trait From { fn from(T) -> Self; } } +pub mod iter { + pub use self::traits::iterator::Iterator; + mod traits { mod iterator { + use crate::option::Option; + pub trait Iterator { + type Item; + fn next(&mut self) -> Option; + } + } } + + pub use self::sources::*; + mod sources { + use super::Iterator; + pub struct Repeat { + element: A, + } + + pub fn repeat(elt: T) -> Repeat { + Repeat { element: elt } + } + + impl Iterator for Repeat { + type Item = A; + + fn next(&mut self) -> Option { + Some(self.element.clone()) + } + } + } +} + pub mod option { pub enum Option { None, Some(T)} } pub mod prelude { - pub use crate::{convert::From, option::Option::{self, *}}; + pub use crate::{convert::From, iter::Iterator, option::Option::{self, *}}; } #[prelude_import] pub use prelude::*; @@ -305,6 +335,10 @@ pub use prelude::*; self.find_enum("core:option:Option") } + pub fn core_iter_Iterator(&self) -> Option { + self.find_trait("core:iter:traits:iterator:Iterator") + } + fn find_trait(&self, path: &str) -> Option { match self.find_def(path)? { hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it), @@ -324,18 +358,21 @@ pub use prelude::*; let mut path = path.split(':'); let trait_ = path.next_back()?; let std_crate = path.next()?; - let std_crate = self + let std_crate = if self .1 - .dependencies(db) - .into_iter() - .find(|dep| &dep.name.to_string() == std_crate)? - .krate; - + .declaration_name(db) + .map(|name| name.to_string() == std_crate) + .unwrap_or(false) + { + self.1 + } else { + self.1.dependencies(db).into_iter().find(|dep| dep.name.to_string() == std_crate)?.krate + }; let mut module = std_crate.root_module(db); for segment in path { module = module.children(db).find_map(|child| { let name = child.name(db)?; - if &name.to_string() == segment { + if name.to_string() == segment { Some(child) } else { None @@ -343,7 +380,7 @@ pub use prelude::*; })?; } let def = - module.scope(db, None).into_iter().find(|(name, _def)| &name.to_string() == trait_)?.1; + module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1; Some(def) } } -- cgit v1.2.3 From c133651e0a613d4833bba1c1f229222d060e2ba8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Oct 2020 10:14:42 +0200 Subject: Move IntoIterator into FamousDefs --- crates/assists/src/utils.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 0335969fd..b341453d4 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -286,14 +286,21 @@ pub mod convert { } pub mod iter { - pub use self::traits::iterator::Iterator; - mod traits { mod iterator { - use crate::option::Option; - pub trait Iterator { - type Item; - fn next(&mut self) -> Option; + pub use self::traits::{collect::IntoIterator, iterator::Iterator}; + mod traits { + mod iterator { + use crate::option::Option; + pub trait Iterator { + type Item; + fn next(&mut self) -> Option; + } + } + mod collect { + pub trait IntoIterator { + type Item; + } } - } } + } pub use self::sources::*; mod sources { @@ -321,7 +328,7 @@ pub mod option { } pub mod prelude { - pub use crate::{convert::From, iter::Iterator, option::Option::{self, *}}; + pub use crate::{convert::From, iter::{IntoIterator, Iterator}, option::Option::{self, *}}; } #[prelude_import] pub use prelude::*; -- cgit v1.2.3 From 209e9b9926d27ac71bc054bfdd48888e5d7d6d1a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Oct 2020 11:30:42 +0200 Subject: Shorten iterator chain hints --- crates/assists/src/utils.rs | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'crates/assists') diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index b341453d4..92b3c3b00 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -281,21 +281,34 @@ impl FamousDefs<'_, '_> { pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core pub mod convert { pub trait From { - fn from(T) -> Self; + fn from(t: T) -> Self; } } pub mod iter { pub use self::traits::{collect::IntoIterator, iterator::Iterator}; mod traits { - mod iterator { + pub(crate) mod iterator { use crate::option::Option; pub trait Iterator { type Item; fn next(&mut self) -> Option; + fn by_ref(&mut self) -> &mut Self { + self + } + fn take(self, n: usize) -> crate::iter::Take { + crate::iter::Take { inner: self } + } + } + + impl Iterator for &mut I { + type Item = I::Item; + fn next(&mut self) -> Option { + (**self).next() + } } } - mod collect { + pub(crate) mod collect { pub trait IntoIterator { type Item; } @@ -303,21 +316,35 @@ pub mod iter { } pub use self::sources::*; - mod sources { + pub(crate) mod sources { use super::Iterator; + use crate::option::Option::{self, *}; pub struct Repeat { element: A, } - pub fn repeat(elt: T) -> Repeat { + pub fn repeat(elt: T) -> Repeat { Repeat { element: elt } } - impl Iterator for Repeat { + impl Iterator for Repeat { type Item = A; fn next(&mut self) -> Option { - Some(self.element.clone()) + None + } + } + } + + pub use self::adapters::*; + pub(crate) mod adapters { + use super::Iterator; + use crate::option::Option::{self, *}; + pub struct Take { pub(crate) inner: I } + impl Iterator for Take where I: Iterator { + type Item = ::Item; + fn next(&mut self) -> Option<::Item> { + None } } } -- cgit v1.2.3 From e106857e80d1ea304b00b5ff8a3294ca9bacd959 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Oct 2020 12:13:32 +0200 Subject: Shorten iterator hints for std::iter iterators behind references --- crates/assists/src/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/assists') diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 92b3c3b00..c074b8d02 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -308,7 +308,7 @@ pub mod iter { } } } - pub(crate) mod collect { + pub(crate) mod collect { pub trait IntoIterator { type Item; } -- cgit v1.2.3 From 783af171f74d95b498662e5168c3ba320cca8553 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Oct 2020 13:14:12 +0200 Subject: Clean up inlay_hints --- crates/assists/src/utils.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'crates/assists') diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index c074b8d02..c1847f601 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -3,7 +3,7 @@ pub(crate) mod insert_use; use std::{iter, ops}; -use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; +use hir::{Adt, Crate, Enum, Module, ScopeDef, Semantics, Trait, Type}; use ide_db::RootDatabase; use itertools::Itertools; use rustc_hash::FxHashSet; @@ -373,6 +373,10 @@ pub use prelude::*; self.find_trait("core:iter:traits:iterator:Iterator") } + pub fn core_iter(&self) -> Option { + self.find_module("core:iter") + } + fn find_trait(&self, path: &str) -> Option { match self.find_def(path)? { hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it), @@ -387,6 +391,13 @@ pub use prelude::*; } } + fn find_module(&self, path: &str) -> Option { + match self.find_def(path)? { + hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(it)) => Some(it), + _ => None, + } + } + fn find_def(&self, path: &str) -> Option { let db = self.0.db; let mut path = path.split(':'); -- cgit v1.2.3