@@ -149,14 +149,15 @@ fn find_following_commit_or_rollback(
149149
150150#[ cfg( test) ]
151151mod test {
152- use crate :: builtins:: { BUILTINS_SQL , builtins_file} ;
152+ use crate :: builtins:: builtins_file;
153153 use crate :: db:: { Database , File } ;
154154 use crate :: goto_definition:: goto_definition;
155155 use crate :: test_utils:: fixture;
156156 use annotate_snippets:: { AnnotationKind , Level , Renderer , Snippet , renderer:: DecorStyle } ;
157157 use insta:: assert_snapshot;
158158 use log:: info;
159159 use rowan:: TextRange ;
160+ use rustc_hash:: FxHashMap ;
160161
161162 #[ track_caller]
162163 fn goto ( sql : & str ) -> String {
@@ -171,60 +172,64 @@ mod test {
171172 // marker after the item we're trying to go to def on.
172173 offset = offset. checked_sub ( 1 . into ( ) ) . unwrap_or_default ( ) ;
173174 let db = Database :: default ( ) ;
174- let file = File :: new ( & db, sql. clone ( ) . into ( ) ) ;
175- assert_eq ! ( crate :: db:: parse( & db, file) . errors( ) , vec![ ] ) ;
176- let results = goto_definition ( & db, file, offset) ;
177- if !results. is_empty ( ) {
178- let offset: usize = offset. into ( ) ;
179- let mut current_dests = vec ! [ ] ;
180- let mut builtin_dests = vec ! [ ] ;
181- let builtins_file = builtins_file ( & db) ;
182- for ( i, location) in results. iter ( ) . enumerate ( ) {
183- let label_index = i + 2 ;
184- if location. file == file {
185- current_dests. push ( ( label_index, location. range ) ) ;
186- } else if location. file == builtins_file {
187- builtin_dests. push ( ( label_index, location. range ) ) ;
188- }
189- }
190-
191- let has_builtins = !builtin_dests. is_empty ( ) ;
175+ let current_file = File :: new ( & db, sql. into ( ) ) ;
176+ assert_eq ! ( crate :: db:: parse( & db, current_file) . errors( ) , vec![ ] ) ;
177+ let results = goto_definition ( & db, current_file, offset) ;
178+ if results. is_empty ( ) {
179+ return None ;
180+ }
192181
193- let mut snippet = Snippet :: source ( & sql ) . fold ( true ) ;
194- if has_builtins {
195- // only show the current file when we have two file types, aka current and builtins
196- snippet = snippet . path ( "current.sql" ) ;
197- } else {
198- snippet = annotate_destinations ( snippet , current_dests ) ;
199- }
200- snippet = snippet . annotation (
201- AnnotationKind :: Context
202- . span ( offset..offset + 1 )
203- . label ( "1. source" ) ,
204- ) ;
182+ let mut file_paths = FxHashMap :: default ( ) ;
183+ file_paths . insert ( current_file , "current.sql" ) ;
184+ file_paths . insert ( builtins_file ( & db ) , " builtins.sql" ) ;
185+
186+ let offset : usize = offset . into ( ) ;
187+ let mut dests_by_file : FxHashMap < File , Vec < ( usize , TextRange ) > > = FxHashMap :: default ( ) ;
188+ for ( i , location ) in results . iter ( ) . enumerate ( ) {
189+ dests_by_file
190+ . entry ( location . file )
191+ . or_default ( )
192+ . push ( ( i + 2 , location . range ) ) ;
193+ }
205194
206- let mut groups = vec ! [ Level :: INFO . primary_title ( "definition" ) . element ( snippet ) ] ;
195+ let multi_file = dests_by_file . len ( ) > 1 || !dests_by_file . contains_key ( & current_file ) ;
207196
208- if has_builtins {
209- let builtins_snippet = Snippet :: source ( BUILTINS_SQL ) . path ( "builtin.sql" ) . fold ( true ) ;
210- let builtins_snippet = annotate_destinations ( builtins_snippet, builtin_dests) ;
211- groups. push (
212- Level :: INFO
213- . primary_title ( "definition" )
214- . element ( builtins_snippet) ,
215- ) ;
216- }
197+ let mut snippet = Snippet :: source ( current_file. content ( & db) . as_ref ( ) ) . fold ( true ) ;
198+ if multi_file {
199+ snippet = snippet. path ( * file_paths. get ( & current_file) . unwrap ( ) ) ;
200+ }
201+ if let Some ( current_dests) = dests_by_file. remove ( & current_file) {
202+ snippet = annotate_destinations ( snippet, current_dests) ;
203+ }
204+ snippet = snippet. annotation (
205+ AnnotationKind :: Context
206+ . span ( offset..offset + 1 )
207+ . label ( "1. source" ) ,
208+ ) ;
217209
218- let renderer = Renderer :: plain ( ) . decor_style ( DecorStyle :: Unicode ) ;
219- return Some (
220- renderer
221- . render ( & groups)
222- . to_string ( )
223- // hacky cleanup to make the text shorter
224- . replace ( "info: definition" , "" ) ,
210+ let mut groups = vec ! [ Level :: INFO . primary_title( "definition" ) . element( snippet) ] ;
211+
212+ for ( dest_file, dests) in dests_by_file {
213+ let path = file_paths. get ( & dest_file) . unwrap ( ) ;
214+ let other_snippet = Snippet :: source ( dest_file. content ( & db) . as_ref ( ) )
215+ . path ( * path)
216+ . fold ( true ) ;
217+ let other_snippet = annotate_destinations ( other_snippet, dests) ;
218+ groups. push (
219+ Level :: INFO
220+ . primary_title ( "definition" )
221+ . element ( other_snippet) ,
225222 ) ;
226223 }
227- None
224+
225+ let renderer = Renderer :: plain ( ) . decor_style ( DecorStyle :: Unicode ) ;
226+ Some (
227+ renderer
228+ . render ( & groups)
229+ . to_string ( )
230+ // hacky cleanup to make the text shorter
231+ . replace ( "info: definition" , "" ) ,
232+ )
228233 }
229234
230235 fn goto_not_found ( sql : & str ) {
@@ -870,7 +875,7 @@ select now$0();
870875 β β 1. source
871876 β°β΄
872877
873- ββΈ builtin .sql:11089:28
878+ ββΈ builtins .sql:11089:28
874879 β
875880 11089 β create function pg_catalog.now() returns timestamp with time zone
876881 β°β΄ βββ 2. destination
@@ -888,7 +893,7 @@ select current_timestamp$0;
888893 β β 1. source
889894 β°β΄
890895
891- ββΈ builtin .sql:11089:28
896+ ββΈ builtins .sql:11089:28
892897 β
893898 11089 β create function pg_catalog.now() returns timestamp with time zone
894899 β°β΄ βββ 2. destination
@@ -989,7 +994,7 @@ select * from t where current_timestamp$0 > t.created_at;
989994 β β 1. source
990995 β°β΄
991996
992- ββΈ builtin .sql:11089:28
997+ ββΈ builtins .sql:11089:28
993998 β
994999 11089 β create function pg_catalog.now() returns timestamp with time zone
9951000 β°β΄ βββ 2. destination
0 commit comments