1- #[ macro_use]
21mod types;
32mod error;
43mod definitions;
@@ -12,12 +11,12 @@ use solc_wrapper::{get_ast_for_file, SolcAstFile};
1211pub use solc_wrapper:: SolcFile ;
1312pub use solc_ast_rs_types:: types:: * ;
1413pub use types:: { Location , Position } ;
15- use types:: { get_id , get_range , get_reference_id , InteractableNode } ;
14+ use types:: InteractableNode ;
1615use usages:: UsagesFinder ;
1716use definitions:: DefinitionFinder ;
18- use utils:: { index_to_position , join_path} ;
17+ use utils:: join_path;
1918
20- use crate :: utils:: is_node_a_type ;
19+ use crate :: utils:: get_location ;
2120
2221#[ derive( Debug ) ]
2322pub struct ReferencesProvider {
@@ -29,111 +28,80 @@ impl ReferencesProvider {
2928 pub fn set_base_path ( & mut self , base_path : String ) {
3029 self . base_path = base_path;
3130 }
32-
31+
3332 pub fn update_file_content ( & mut self ) -> Result < ( ) , ReferencesError > {
3433 self . files = get_ast_for_file ( self . base_path . clone ( ) ) ?;
3534 Ok ( ( ) )
3635 }
3736
38- pub fn get_definition ( & self , uri : & str , position : Position ) -> Option < Location > {
37+ fn get_node ( & self , uri : & str , position : Position ) -> Option < ( SolcAstFile , InteractableNode ) > {
3938 let found_node: Option < InteractableNode > ;
40- let mut node_finder = NodeVisitor :: new ( position. clone ( ) , String :: from ( "" ) ) ;
4139 let source_file;
4240 if let Some ( file) = self . files . iter ( ) . find ( |file| file. file . path == uri) {
41+ let mut node_finder = NodeVisitor :: new ( position. clone ( ) , & file. file . content ) ;
4342 source_file = file;
44- found_node = node_finder. find ( & file. ast , & file . file . content ) ;
43+ found_node = node_finder. find ( & file. ast ) ;
4544 }
4645 else {
4746 eprintln ! ( "No file found at uri: {}" , uri) ;
4847 return None ;
4948 }
5049 if found_node. is_none ( ) {
51- eprintln ! ( "No node found at position: {:?}" , & position) ;
50+ eprintln ! ( "[NODE FINDER] No node found at position: {:?}" , & position) ;
5251 return None ;
5352 }
54- let found_node = found_node. unwrap ( ) ;
53+ Some ( ( source_file. clone ( ) , found_node. unwrap ( ) ) )
54+ }
55+
56+ pub fn get_definition ( & self , uri : & str , position : Position ) -> Option < Location > {
57+ let ( source_file, found_node) = match self . get_node ( uri, position) {
58+ Some ( ( file, node) ) => ( file, node) ,
59+ None => return None
60+ } ;
5561
56- let ref_id = match get_reference_id ( & found_node. clone ( ) )
57- {
62+ let ref_id = match found_node. get_reference_id ( ) {
5863 Some ( id) => id,
5964 None => {
6065 match found_node {
6166 InteractableNode :: ImportDirective ( import) => {
62- let uri = join_path ( & self . base_path , & import. absolute_path ) ;
6367 return Some ( Location {
64- start : Position {
65- line : 1 ,
66- column : 1
67- } ,
68- end : Position {
69- line : 1 ,
70- column : 1
71- } ,
72- uri : uri
68+ start : Position :: default ( ) ,
69+ end : Position :: default ( ) ,
70+ uri : join_path ( & self . base_path , & import. absolute_path )
7371 } ) ;
7472 } ,
75- _ => { }
73+ _ => {
74+ return Some ( get_location ( & found_node, & source_file) ) ;
75+ }
7676 }
77- let range = get_range ( & found_node) ;
78- return Some ( Location {
79- start : index_to_position ( range. index , & source_file. file . content ) ,
80- end : index_to_position ( range. index + range. length , & source_file. file . content ) ,
81- uri : uri. to_string ( )
82- } ) ;
8377 }
8478 } ;
8579 let mut def_finder = DefinitionFinder :: new ( ref_id) ;
8680 for file in & self . files {
8781 if let Some ( node) = def_finder. find ( & file. ast ) {
88- let range = get_range ( & node) ;
89- let start = index_to_position ( range. index , & file. file . content ) ;
90- let end = index_to_position ( range. index + range. length , & file. file . content ) ;
91- return Some ( Location {
92- start,
93- end,
94- uri : file. file . path . clone ( )
95- } ) ;
82+ return Some ( get_location ( & node, file) ) ;
9683 }
9784 }
9885 None
9986 }
10087
10188 pub fn get_references ( & self , uri : & str , position : Position ) -> Vec < Location > {
10289 let mut references: Vec < Location > = Vec :: new ( ) ;
103- let mut found_node: Option < InteractableNode > = None ;
104- let mut node_finder = NodeVisitor :: new ( position. clone ( ) , String :: from ( "" ) ) ;
105- if let Some ( file) = self . files . iter ( ) . find ( |file| file. file . path == uri) {
106- found_node = node_finder. find ( & file. ast , & file. file . content ) ;
107- eprintln ! ( "Found node: {:?}" , found_node) ;
108- }
109- else {
110- eprintln ! ( "No file found at uri: {}" , uri) ;
111- return references;
112- }
113- if found_node. is_none ( ) {
114- eprintln ! ( "No node found at position: {:?}" , & position) ;
115- return references;
116- }
117- let found_node = found_node. unwrap ( ) ;
118- let ref_id = get_id ( & found_node. clone ( ) ) ;
119- eprintln ! ( "Id: {:?}" , ref_id) ;
90+ let ( _, found_node) = match self . get_node ( uri, position) {
91+ Some ( ( file, node) ) => ( file, node) ,
92+ None => return vec ! [ ]
93+ } ;
12094
121- let mut usages_finder = UsagesFinder :: new ( ref_id, is_node_a_type ( & found_node) ) ;
95+ let id = found_node. get_id ( ) ;
96+ eprintln ! ( "Id: {:?}" , id) ;
97+
98+ let mut usages_finder = UsagesFinder :: new ( id) ;
12299 for file in & self . files {
123100 let nodes = usages_finder. find ( & file. ast ) ;
124101 for node in nodes {
125- let range = get_range ( & node) ;
126- let start = index_to_position ( range. index , & file. file . content ) ;
127- let end = index_to_position ( range. index + range. length , & file. file . content ) ;
128- let location = Location {
129- start,
130- end,
131- uri : file. file . path . clone ( )
132- } ;
133- references. push ( location) ;
102+ references. push ( get_location ( & node, & file) ) ;
134103 }
135104 }
136-
137105 references
138106 }
139107}
0 commit comments