11//! A module with ide helpers for high-level ide features.
2+ pub mod famous_defs;
3+ pub mod generated_lints;
24pub mod import_assets;
35pub mod insert_use;
46pub mod merge_imports;
7+ pub mod node_ext;
58pub mod rust_doc;
6- pub mod generated_lints;
79
810use std:: collections:: VecDeque ;
911
1012use base_db:: FileId ;
1113use either:: Either ;
12- use hir:: { Crate , Enum , ItemInNs , MacroDef , Module , ModuleDef , Name , ScopeDef , Semantics , Trait } ;
14+ use hir:: { ItemInNs , MacroDef , ModuleDef , Name , Semantics } ;
1315use syntax:: {
1416 ast:: { self , make, LoopBodyOwner } ,
1517 AstNode , Direction , SyntaxElement , SyntaxKind , SyntaxToken , TokenAtOffset , WalkEvent , T ,
1618} ;
1719
1820use crate :: RootDatabase ;
1921
22+ pub use self :: famous_defs:: FamousDefs ;
23+
2024pub fn item_name ( db : & RootDatabase , item : ItemInNs ) -> Option < Name > {
2125 match item {
2226 ItemInNs :: Types ( module_def_id) => ModuleDef :: from ( module_def_id) . name ( db) ,
@@ -27,7 +31,7 @@ pub fn item_name(db: &RootDatabase, item: ItemInNs) -> Option<Name> {
2731
2832/// Resolves the path at the cursor token as a derive macro if it inside a token tree of a derive attribute.
2933pub fn try_resolve_derive_input_at (
30- sema : & Semantics < RootDatabase > ,
34+ sema : & hir :: Semantics < RootDatabase > ,
3135 derive_attr : & ast:: Attr ,
3236 cursor : & SyntaxToken ,
3337) -> Option < MacroDef > {
@@ -113,123 +117,6 @@ pub fn visit_file_defs(
113117 module. impl_defs ( db) . into_iter ( ) . for_each ( |impl_| cb ( Either :: Right ( impl_) ) ) ;
114118}
115119
116- /// Helps with finding well-know things inside the standard library. This is
117- /// somewhat similar to the known paths infra inside hir, but it different; We
118- /// want to make sure that IDE specific paths don't become interesting inside
119- /// the compiler itself as well.
120- ///
121- /// Note that, by default, rust-analyzer tests **do not** include core or std
122- /// libraries. If you are writing tests for functionality using [`FamousDefs`],
123- /// you'd want to include minicore (see `test_utils::MiniCore`) declaration at
124- /// the start of your tests:
125- ///
126- /// ```
127- /// //- minicore: iterator, ord, derive
128- /// ```
129- pub struct FamousDefs < ' a , ' b > ( pub & ' a Semantics < ' b , RootDatabase > , pub Option < Crate > ) ;
130-
131- #[ allow( non_snake_case) ]
132- impl FamousDefs < ' _ , ' _ > {
133- pub fn std ( & self ) -> Option < Crate > {
134- self . find_crate ( "std" )
135- }
136-
137- pub fn core ( & self ) -> Option < Crate > {
138- self . find_crate ( "core" )
139- }
140-
141- pub fn core_cmp_Ord ( & self ) -> Option < Trait > {
142- self . find_trait ( "core:cmp:Ord" )
143- }
144-
145- pub fn core_convert_From ( & self ) -> Option < Trait > {
146- self . find_trait ( "core:convert:From" )
147- }
148-
149- pub fn core_convert_Into ( & self ) -> Option < Trait > {
150- self . find_trait ( "core:convert:Into" )
151- }
152-
153- pub fn core_option_Option ( & self ) -> Option < Enum > {
154- self . find_enum ( "core:option:Option" )
155- }
156-
157- pub fn core_result_Result ( & self ) -> Option < Enum > {
158- self . find_enum ( "core:result:Result" )
159- }
160-
161- pub fn core_default_Default ( & self ) -> Option < Trait > {
162- self . find_trait ( "core:default:Default" )
163- }
164-
165- pub fn core_iter_Iterator ( & self ) -> Option < Trait > {
166- self . find_trait ( "core:iter:traits:iterator:Iterator" )
167- }
168-
169- pub fn core_iter_IntoIterator ( & self ) -> Option < Trait > {
170- self . find_trait ( "core:iter:traits:collect:IntoIterator" )
171- }
172-
173- pub fn core_iter ( & self ) -> Option < Module > {
174- self . find_module ( "core:iter" )
175- }
176-
177- pub fn core_ops_Deref ( & self ) -> Option < Trait > {
178- self . find_trait ( "core:ops:Deref" )
179- }
180-
181- fn find_trait ( & self , path : & str ) -> Option < Trait > {
182- match self . find_def ( path) ? {
183- hir:: ScopeDef :: ModuleDef ( hir:: ModuleDef :: Trait ( it) ) => Some ( it) ,
184- _ => None ,
185- }
186- }
187-
188- fn find_enum ( & self , path : & str ) -> Option < Enum > {
189- match self . find_def ( path) ? {
190- hir:: ScopeDef :: ModuleDef ( hir:: ModuleDef :: Adt ( hir:: Adt :: Enum ( it) ) ) => Some ( it) ,
191- _ => None ,
192- }
193- }
194-
195- fn find_module ( & self , path : & str ) -> Option < Module > {
196- match self . find_def ( path) ? {
197- hir:: ScopeDef :: ModuleDef ( hir:: ModuleDef :: Module ( it) ) => Some ( it) ,
198- _ => None ,
199- }
200- }
201-
202- fn find_crate ( & self , name : & str ) -> Option < Crate > {
203- let krate = self . 1 ?;
204- let db = self . 0 . db ;
205- let res =
206- krate. dependencies ( db) . into_iter ( ) . find ( |dep| dep. name . to_string ( ) == name) ?. krate ;
207- Some ( res)
208- }
209-
210- fn find_def ( & self , path : & str ) -> Option < ScopeDef > {
211- let db = self . 0 . db ;
212- let mut path = path. split ( ':' ) ;
213- let trait_ = path. next_back ( ) ?;
214- let std_crate = path. next ( ) ?;
215- let std_crate = self . find_crate ( std_crate) ?;
216- let mut module = std_crate. root_module ( db) ;
217- for segment in path {
218- module = module. children ( db) . find_map ( |child| {
219- let name = child. name ( db) ?;
220- if name. to_string ( ) == segment {
221- Some ( child)
222- } else {
223- None
224- }
225- } ) ?;
226- }
227- let def =
228- module. scope ( db, None ) . into_iter ( ) . find ( |( name, _def) | name. to_string ( ) == trait_) ?. 1 ;
229- Some ( def)
230- }
231- }
232-
233120#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
234121pub struct SnippetCap {
235122 _private : ( ) ,
0 commit comments