@@ -61,6 +61,8 @@ use crate::docs::render_docs_as_code;
6161use crate :: docs:: Doc ;
6262use crate :: docs:: DocFunction ;
6363use crate :: docs:: DocItem ;
64+ use crate :: docs:: DocMember ;
65+ use crate :: docs:: DocModule ;
6466use crate :: docs:: Identifier ;
6567use crate :: docs:: Location ;
6668use crate :: errors:: EvalMessage ;
@@ -95,6 +97,14 @@ enum ResolveLoadError {
9597 WrongScheme ( String , LspUrl ) ,
9698}
9799
100+ #[ derive( thiserror:: Error , Debug ) ]
101+ enum RenderLoadError {
102+ #[ error( "Path `{}` provided, which does not seem to contain a filename" , . 0 . display( ) ) ]
103+ MissingTargetFilename ( PathBuf ) ,
104+ #[ error( "Urls `{}` and `{}` was expected to be of type `{}`" , . 1 , . 2 , . 0 ) ]
105+ WrongScheme ( String , LspUrl , LspUrl ) ,
106+ }
107+
98108#[ derive( thiserror:: Error , Debug ) ]
99109pub ( crate ) enum TestServerError {
100110 #[ error( "Attempted to set the contents of a file with a non-absolute path `{}`" , . 0 . display( ) ) ]
@@ -170,6 +180,51 @@ impl LspContext for TestServerContext {
170180 }
171181 }
172182
183+ fn render_as_load (
184+ & self ,
185+ target : & LspUrl ,
186+ current_file : & LspUrl ,
187+ workspace_root : Option < & Path > ,
188+ ) -> anyhow:: Result < String > {
189+ match ( target, current_file) {
190+ ( LspUrl :: File ( target_path) , LspUrl :: File ( current_file_path) ) => {
191+ let target_package = target_path. parent ( ) ;
192+ let current_file_package = current_file_path. parent ( ) ;
193+ let target_filename = target_path. file_name ( ) ;
194+
195+ // If both are in the same package, return a relative path.
196+ if matches ! ( ( target_package, current_file_package) , ( Some ( a) , Some ( b) ) if a == b) {
197+ return match target_filename {
198+ Some ( filename) => Ok ( format ! ( ":{}" , filename. to_string_lossy( ) ) ) ,
199+ None => {
200+ Err ( RenderLoadError :: MissingTargetFilename ( target_path. clone ( ) ) . into ( ) )
201+ }
202+ } ;
203+ }
204+
205+ let target_path = workspace_root
206+ . and_then ( |root| target_path. strip_prefix ( root) . ok ( ) )
207+ . unwrap_or ( target_path) ;
208+
209+ Ok ( format ! (
210+ "//{}:{}" ,
211+ target_package
212+ . map( |path| path. to_string_lossy( ) )
213+ . unwrap_or_default( ) ,
214+ target_filename
215+ . unwrap_or( target_path. as_os_str( ) )
216+ . to_string_lossy( )
217+ ) )
218+ }
219+ _ => Err ( RenderLoadError :: WrongScheme (
220+ "file://" . to_owned ( ) ,
221+ target. clone ( ) ,
222+ current_file. clone ( ) ,
223+ )
224+ . into ( ) ) ,
225+ }
226+ }
227+
173228 fn resolve_string_literal (
174229 & self ,
175230 literal : & str ,
@@ -228,6 +283,17 @@ impl LspContext for TestServerContext {
228283 ) -> anyhow:: Result < Option < LspUrl > > {
229284 Ok ( self . builtin_symbols . get ( symbol) . cloned ( ) )
230285 }
286+
287+ fn get_environment ( & self , _uri : & LspUrl ) -> DocModule {
288+ DocModule {
289+ docs : None ,
290+ members : self
291+ . builtin_symbols
292+ . keys ( )
293+ . map ( |name| ( name. clone ( ) , DocMember :: Function ( DocFunction :: default ( ) ) ) )
294+ . collect ( ) ,
295+ }
296+ }
231297}
232298
233299/// A server for use in testing that provides helpers for sending requests, correlating
0 commit comments