diff --git a/Cargo.lock b/Cargo.lock index 92d56b0..0bc00e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -540,7 +540,7 @@ dependencies = [ [[package]] name = "protols" -version = "0.11.1" +version = "0.11.2" dependencies = [ "async-lsp", "basic-toml", diff --git a/Cargo.toml b/Cargo.toml index bebc838..536da3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "protols" description = "Language server for proto3 files" -version = "0.11.1" +version = "0.11.2" edition = "2024" license = "MIT" homepage = "https://github.com/coder3101/protols" diff --git a/src/config/workspace.rs b/src/config/workspace.rs index a6a293a..266791f 100644 --- a/src/config/workspace.rs +++ b/src/config/workspace.rs @@ -1,5 +1,6 @@ use std::{ collections::{HashMap, HashSet}, + env, path::{Path, PathBuf}, }; @@ -62,7 +63,7 @@ impl WorkspaceProtoConfigs { let wr: ProtolsConfig = basic_toml::from_str(&content).unwrap_or_default(); let fmt = ClangFormatter::new( &wr.config.path.clang_format, - wpath.to_str().expect("non-utf8 path"), + Some(wpath.to_str().expect("non-utf8 path")), ); self.workspaces.insert(w.uri.clone()); @@ -103,6 +104,34 @@ impl WorkspaceProtoConfigs { ipath.extend_from_slice(&self.protoc_include_prefix); Some(ipath) } + + pub fn no_workspace_mode(&mut self) { + let wr = ProtolsConfig::default(); + let rp = if cfg!(target_os = "windows") { + let mut d = String::from("C"); + if let Ok(cdir) = env::current_dir() { + if let Some(drive) = cdir.components().next() { + d = drive.as_os_str().to_string_lossy().to_string() + } + } + format!("{d}://") + } else { + String::from("/") + }; + let uri = match Url::from_file_path(&rp) { + Err(err) => { + tracing::error!(?err, "failed to convert path: {rp} to Url"); + return; + } + Ok(uri) => uri, + }; + + let fmt = ClangFormatter::new(&wr.config.path.clang_format, None); + + self.workspaces.insert(uri.clone()); + self.configs.insert(uri.clone(), wr); + self.formatters.insert(uri.clone(), fmt); + } } #[cfg(test)] diff --git a/src/formatter/clang.rs b/src/formatter/clang.rs index a051175..b9b56a1 100644 --- a/src/formatter/clang.rs +++ b/src/formatter/clang.rs @@ -16,7 +16,7 @@ use super::ProtoFormatter; pub struct ClangFormatter { pub path: String, - working_dir: String, + working_dir: Option, temp_dir: TempDir, } @@ -66,11 +66,11 @@ impl Replacement<'_> { } impl ClangFormatter { - pub fn new(cmd: &str, wdir: &str) -> Self { + pub fn new(cmd: &str, wdir: Option<&str>) -> Self { Self { temp_dir: tempdir().expect("faile to creat temp dir"), path: cmd.to_owned(), - working_dir: wdir.to_owned(), + working_dir: wdir.map(ToOwned::to_owned), } } @@ -83,7 +83,9 @@ impl ClangFormatter { fn get_command(&self, f: &str, u: &Path) -> Option { let mut c = Command::new(self.path.as_str()); - c.current_dir(self.working_dir.as_str()); + if let Some(wd) = &self.working_dir { + c.current_dir(wd.as_str()); + } c.stdin(File::open(u).ok()?); c.args([ "--output-replacements-xml", diff --git a/src/lsp.rs b/src/lsp.rs index fdb91a1..6d665be 100644 --- a/src/lsp.rs +++ b/src/lsp.rs @@ -74,6 +74,9 @@ impl LanguageServer for ProtoLanguageServer { ..Default::default() }), }) + } else { + tracing::info!("running in no workspace mode"); + self.configs.no_workspace_mode() } let mut rename_provider: OneOf = OneOf::Left(true);