11use std:: fmt;
22
33use cairo_lang_debug:: DebugWithDb ;
4- use cairo_lang_defs:: db:: DefsGroup ;
54use cairo_lang_defs:: ids:: { GenericTypeId , LookupItemId , ModuleId , ModuleItemId , TraitItemId } ;
65use cairo_lang_diagnostics:: DiagnosticsBuilder ;
76use cairo_lang_filesystem:: db:: FilesGroup ;
@@ -10,11 +9,9 @@ use cairo_lang_parser::parser::Parser;
109use cairo_lang_semantic:: diagnostic:: { NotFoundItemType , SemanticDiagnostics } ;
1110use cairo_lang_semantic:: expr:: inference:: InferenceId ;
1211use cairo_lang_semantic:: items:: functions:: GenericFunctionId ;
13- use cairo_lang_semantic:: items :: module :: ModuleSemantic ;
12+ use cairo_lang_semantic:: lsp_helpers :: LspHelpers ;
1413use cairo_lang_semantic:: resolve:: { AsSegments , ResolutionContext , ResolvedGenericItem , Resolver } ;
15- use cairo_lang_syntax:: node:: ast:: { Expr , ExprPath , ItemModule } ;
16- use cairo_lang_syntax:: node:: helpers:: GetIdentifier ;
17- use cairo_lang_syntax:: node:: { SyntaxNode , TypedSyntaxNode } ;
14+ use cairo_lang_syntax:: node:: ast:: { Expr , ExprPath } ;
1815use cairo_lang_utils:: Intern ;
1916use itertools:: Itertools ;
2017use pulldown_cmark:: {
@@ -336,7 +333,7 @@ impl<'db> DocumentationCommentParser<'db> {
336333 path : String ,
337334 ) -> Option < DocumentableItemId < ' db > > {
338335 let syntax_node = item_id. stable_location ( self . db ) ?. syntax_node ( self . db ) ;
339- let containing_module = self . find_module_containing_node ( & syntax_node) ?;
336+ let containing_module = self . db . find_module_containing_node ( syntax_node) ?;
340337 let mut resolver = Resolver :: new ( self . db , containing_module, InferenceId :: NoContext ) ;
341338 let mut diagnostics = SemanticDiagnostics :: default ( ) ;
342339 let segments = self . parse_comment_link_path ( path) ?;
@@ -373,55 +370,6 @@ impl<'db> DocumentationCommentParser<'db> {
373370
374371 if let Expr :: Path ( expr_path) = expr { Some ( expr_path) } else { None }
375372 }
376-
377- /// Finds a [`ModuleId`] containing the node.
378- ///
379- /// If the node is located in a virtual file generated by a compiler plugin, this method will
380- /// return the (sub)module of the main, user-written file that leads to the node.
381- fn find_module_containing_node ( & self , node : & SyntaxNode < ' db > ) -> Option < ModuleId < ' db > > {
382- let db = self . db ;
383-
384- // Get the main module of the main file that leads to the node.
385- // The node may be located in a virtual file of a submodule.
386- // This code attempts to get the absolute "parent" of both "module" and "file" parts.
387- let main_module = {
388- // Get the file where the node is located.
389- // This might be a virtual file generated by a compiler plugin.
390- let node_file_id = node. stable_ptr ( db) . file_id ( db) ;
391-
392- // Get the root module of a file containing the node.
393- let node_main_module = db. file_modules ( node_file_id) . ok ( ) ?. iter ( ) . copied ( ) . next ( ) ?;
394-
395- // Get the main module of the file.
396- let main_file = db. module_main_file ( node_main_module) . ok ( ) ?;
397-
398- // Get the main module of that file.
399- db. file_modules ( main_file) . ok ( ) ?. iter ( ) . copied ( ) . next ( ) ?
400- } ;
401-
402- // Get the stack (bottom-up) of submodule names in the file containing the node, in the main
403- // module, that lead to the node.
404- node. ancestors ( db)
405- . filter_map ( |node| ItemModule :: cast ( db, node) )
406- . map ( |item_module| {
407- item_module
408- . stable_ptr ( db)
409- . name_green ( db)
410- . identifier ( db)
411- } )
412- // Buffer the stack to get DoubleEndedIterator.
413- . collect :: < Vec < _ > > ( )
414- . into_iter ( )
415- // And get id of the (sub)module containing the node by traversing this stack top-down.
416- . try_rfold ( main_module, |module, name| {
417- let ModuleItemId :: Submodule ( submodule) =
418- db. module_item_by_name ( module, name) . ok ( ) ??
419- else {
420- return None ;
421- } ;
422- Some ( ModuleId :: Submodule ( submodule) )
423- } )
424- }
425373}
426374
427375trait ToDocumentableItemId < ' db , T > {
0 commit comments