1+ #[ cfg( test) ]
2+ use std:: collections:: HashMap ;
3+
14use crate :: { bundler:: Bundler , command_executor:: RealCommandExecutor , gemset:: Gemset } ;
25use zed_extension_api:: { self as zed} ;
36
@@ -8,35 +11,52 @@ pub struct LanguageServerBinary {
811 pub env : Option < Vec < ( String , String ) > > ,
912}
1013
14+ #[ derive( Clone , Debug , Default ) ]
15+ pub struct LspBinarySettings {
16+ #[ allow( dead_code) ]
17+ pub path : Option < String > ,
18+ pub arguments : Option < Vec < String > > ,
19+ }
20+
1121pub trait WorktreeLike {
1222 #[ allow( dead_code) ]
1323 fn root_path ( & self ) -> String ;
14-
1524 #[ allow( dead_code) ]
1625 fn shell_env ( & self ) -> Vec < ( String , String ) > ;
17-
18- #[ allow( dead_code) ]
1926 fn read_text_file ( & self , path : & str ) -> Result < String , String > ;
27+ fn lsp_binary_settings ( & self , server_id : & str ) -> Result < Option < LspBinarySettings > , String > ;
2028}
2129
2230impl WorktreeLike for zed:: Worktree {
2331 fn root_path ( & self ) -> String {
24- self . root_path ( )
32+ zed :: Worktree :: root_path ( self )
2533 }
2634
2735 fn shell_env ( & self ) -> Vec < ( String , String ) > {
28- self . shell_env ( )
36+ zed :: Worktree :: shell_env ( self )
2937 }
3038
3139 fn read_text_file ( & self , path : & str ) -> Result < String , String > {
32- self . read_text_file ( path)
40+ zed:: Worktree :: read_text_file ( self , path)
41+ }
42+
43+ fn lsp_binary_settings ( & self , server_id : & str ) -> Result < Option < LspBinarySettings > , String > {
44+ match zed:: settings:: LspSettings :: for_worktree ( server_id, self ) {
45+ Ok ( lsp_settings) => Ok ( lsp_settings. binary . map ( |b| LspBinarySettings {
46+ path : b. path ,
47+ arguments : b. arguments ,
48+ } ) ) ,
49+ Err ( e) => Err ( e) ,
50+ }
3351 }
3452}
3553
3654#[ cfg( test) ]
3755pub struct FakeWorktree {
3856 root_path : String ,
3957 shell_env : Vec < ( String , String ) > ,
58+ files : HashMap < String , Result < String , String > > ,
59+ lsp_binary_settings_map : HashMap < String , Result < Option < LspBinarySettings > , String > > ,
4060}
4161
4262#[ cfg( test) ]
@@ -45,11 +65,21 @@ impl FakeWorktree {
4565 FakeWorktree {
4666 root_path,
4767 shell_env : Vec :: new ( ) ,
68+ files : HashMap :: new ( ) ,
69+ lsp_binary_settings_map : HashMap :: new ( ) ,
4870 }
4971 }
5072
51- fn read_text_file ( & self , _path : & str ) -> Result < String , String > {
52- Ok ( String :: new ( ) )
73+ pub fn add_file ( & mut self , path : String , content : Result < String , String > ) {
74+ self . files . insert ( path, content) ;
75+ }
76+
77+ pub fn add_lsp_binary_setting (
78+ & mut self ,
79+ server_id : String ,
80+ settings : Result < Option < LspBinarySettings > , String > ,
81+ ) {
82+ self . lsp_binary_settings_map . insert ( server_id, settings) ;
5383 }
5484}
5585
@@ -64,7 +94,17 @@ impl WorktreeLike for FakeWorktree {
6494 }
6595
6696 fn read_text_file ( & self , path : & str ) -> Result < String , String > {
67- self . read_text_file ( path)
97+ self . files
98+ . get ( path)
99+ . cloned ( )
100+ . unwrap_or_else ( || Err ( format ! ( "File not found in mock: {}" , path) ) )
101+ }
102+
103+ fn lsp_binary_settings ( & self , server_id : & str ) -> Result < Option < LspBinarySettings > , String > {
104+ self . lsp_binary_settings_map
105+ . get ( server_id)
106+ . cloned ( )
107+ . unwrap_or ( Ok ( None ) )
68108 }
69109}
70110
@@ -104,11 +144,11 @@ pub trait LanguageServer {
104144 let lsp_settings =
105145 zed:: settings:: LspSettings :: for_worktree ( language_server_id. as_ref ( ) , worktree) ?;
106146
107- if let Some ( binary_settings) = lsp_settings. binary {
108- if let Some ( path) = binary_settings. path {
147+ if let Some ( binary_settings) = & lsp_settings. binary {
148+ if let Some ( path) = & binary_settings. path {
109149 return Ok ( LanguageServerBinary {
110- path,
111- args : binary_settings. arguments ,
150+ path : path . clone ( ) ,
151+ args : binary_settings. arguments . clone ( ) ,
112152 env : Some ( worktree. shell_env ( ) ) ,
113153 } ) ;
114154 }
@@ -133,7 +173,7 @@ pub trait LanguageServer {
133173 Ok ( _version) => {
134174 let bundle_path = worktree
135175 . which ( "bundle" )
136- . ok_or ( "Unable to find 'bundle' command: e" ) ?;
176+ . ok_or_else ( || "Unable to find 'bundle' command" . to_string ( ) ) ?;
137177
138178 Ok ( LanguageServerBinary {
139179 path : bundle_path,
@@ -236,9 +276,7 @@ pub trait LanguageServer {
236276
237277#[ cfg( test) ]
238278mod tests {
239- use crate :: language_servers:: language_server:: FakeWorktree ;
240-
241- use super :: { LanguageServer , WorktreeLike } ;
279+ use super :: { FakeWorktree , LanguageServer , WorktreeLike } ;
242280
243281 struct TestServer { }
244282
0 commit comments