11use std:: {
22 collections:: { HashMap , HashSet } ,
33 path:: { Path , PathBuf } ,
4+ process:: Command ,
45} ;
56
67use async_lsp:: lsp_types:: { Url , WorkspaceFolder } ;
@@ -17,10 +18,22 @@ pub struct WorkspaceProtoConfigs {
1718 configs : HashMap < Url , ProtolsConfig > ,
1819 formatters : HashMap < Url , ClangFormatter > ,
1920 protoc_include_prefix : Vec < PathBuf > ,
21+ git_root : Option < PathBuf > ,
2022}
2123
2224impl WorkspaceProtoConfigs {
2325 pub fn new ( ) -> Self {
26+ let mut git_root = None ;
27+ if let Ok ( output) = Command :: new ( "git" )
28+ . args ( [ "rev-parse" , "--show-toplevel" ] )
29+ . output ( )
30+ {
31+ if output. status . success ( ) {
32+ let p = String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ;
33+ git_root = Some ( PathBuf :: from ( p) )
34+ }
35+ }
36+
2437 Self {
2538 workspaces : Default :: default ( ) ,
2639 formatters : Default :: default ( ) ,
@@ -30,6 +43,7 @@ impl WorkspaceProtoConfigs {
3043 . map ( |l| l. include_paths )
3144 . unwrap_or_default ( ) ,
3245 configs : Default :: default ( ) ,
46+ git_root,
3347 }
3448 }
3549
@@ -81,9 +95,18 @@ impl WorkspaceProtoConfigs {
8195 }
8296
8397 pub fn get_include_paths ( & self , uri : & Url ) -> Option < Vec < PathBuf > > {
84- let c = self . get_config_for_uri ( uri) ?;
85- let w = self . get_workspace_for_uri ( uri) ?. to_file_path ( ) . ok ( ) ?;
86- let mut ipath: Vec < PathBuf > = c
98+ let cfg = self . get_config_for_uri ( uri) ?;
99+
100+ // How to replace relative paths in include_paths?
101+ // If there's a git root use that else the root will be LSP workspace root
102+ let w = if let Some ( root) = & self . git_root {
103+ tracing:: info!( ?root, "using git root as relative path replacer" ) ;
104+ root. clone ( )
105+ } else {
106+ self . get_workspace_for_uri ( uri) ?. to_file_path ( ) . ok ( ) ?
107+ } ;
108+
109+ let mut ipath: Vec < PathBuf > = cfg
87110 . config
88111 . include_paths
89112 . iter ( )
0 commit comments