@@ -61,6 +61,8 @@ use crate::docs::render_docs_as_code;
61
61
use crate :: docs:: Doc ;
62
62
use crate :: docs:: DocFunction ;
63
63
use crate :: docs:: DocItem ;
64
+ use crate :: docs:: DocMember ;
65
+ use crate :: docs:: DocModule ;
64
66
use crate :: docs:: Identifier ;
65
67
use crate :: docs:: Location ;
66
68
use crate :: errors:: EvalMessage ;
@@ -95,6 +97,14 @@ enum ResolveLoadError {
95
97
WrongScheme ( String , LspUrl ) ,
96
98
}
97
99
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
+
98
108
#[ derive( thiserror:: Error , Debug ) ]
99
109
pub ( crate ) enum TestServerError {
100
110
#[ error( "Attempted to set the contents of a file with a non-absolute path `{}`" , . 0 . display( ) ) ]
@@ -170,6 +180,51 @@ impl LspContext for TestServerContext {
170
180
}
171
181
}
172
182
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
+
173
228
fn resolve_string_literal (
174
229
& self ,
175
230
literal : & str ,
@@ -228,6 +283,17 @@ impl LspContext for TestServerContext {
228
283
) -> anyhow:: Result < Option < LspUrl > > {
229
284
Ok ( self . builtin_symbols . get ( symbol) . cloned ( ) )
230
285
}
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
+ }
231
297
}
232
298
233
299
/// A server for use in testing that provides helpers for sending requests, correlating
0 commit comments